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
LowStar.Lib.AssocList.fst
LowStar.Lib.AssocList.remove_all_
val remove_all_ (#t_k: eqtype) (#t_v: Type0) (hd: LL1.t (t_k & t_v)) (l: G.erased (list (t_k & t_v))) (k: t_k): ST (LL1.t (t_k & t_v) & G.erased (list (t_k & t_v))) (requires fun h0 -> LL1.well_formed h0 hd l /\ LL1.invariant h0 hd l) (ensures fun h0 (hd', l') h1 -> LL1.well_formed h1 hd' l' /\ LL1.invariant h1 hd' l' /\ B.(loc_includes (LL1.footprint h0 hd l) (LL1.footprint h1 hd' l')) /\ B.modifies (LL1.footprint h0 hd l) h0 h1 /\ v_ l' == M.upd (v_ l) k None)
val remove_all_ (#t_k: eqtype) (#t_v: Type0) (hd: LL1.t (t_k & t_v)) (l: G.erased (list (t_k & t_v))) (k: t_k): ST (LL1.t (t_k & t_v) & G.erased (list (t_k & t_v))) (requires fun h0 -> LL1.well_formed h0 hd l /\ LL1.invariant h0 hd l) (ensures fun h0 (hd', l') h1 -> LL1.well_formed h1 hd' l' /\ LL1.invariant h1 hd' l' /\ B.(loc_includes (LL1.footprint h0 hd l) (LL1.footprint h1 hd' l')) /\ B.modifies (LL1.footprint h0 hd l) h0 h1 /\ v_ l' == M.upd (v_ l) k None)
let rec remove_all_ #t_k #t_v hd l k = let h0 = ST.get () in if B.is_null hd then begin M.lemma_equal_intro (v_ l) (M.upd (v_ l) k None); hd, l end else begin let cell = !*hd in let { LL1.data; LL1.next } = cell in let k', v = data in if k = k' then begin B.free hd; let h1 = ST.get () in LL1.frame next (List.Tot.tail l) (B.loc_addr_of_buffer hd) h0 h1; let hd', l' = remove_all_ next (List.Tot.tail l) k in M.lemma_equal_intro (v_ l') (M.upd (v_ l) k None); hd', l' end else begin let hd', l' = remove_all_ next (List.Tot.tail l) k in let h1 = ST.get () in hd *= { LL1.data; LL1.next = hd' }; let h2 = ST.get () in LL1.frame hd' l' (B.loc_addr_of_buffer hd) h1 h2; M.lemma_equal_intro (v_ ((k', v) :: l')) (M.upd (v_ l) k None); assert B.(loc_disjoint (loc_buffer hd) (LL1.footprint h2 hd' l')); assert (B.live h2 hd /\ B.length hd == 1); assert (LL1.well_formed h2 hd' l'); assert (LL1.invariant h2 hd (data :: l')); hd, G.hide ((k', v) :: l') end end
{ "file_name": "krmllib/LowStar.Lib.AssocList.fst", "git_rev": "da1e941b2fcb196aa5d1e34941aa00b4c67ac321", "git_url": "https://github.com/FStarLang/karamel.git", "project_name": "karamel" }
{ "end_col": 5, "end_line": 181, "start_col": 0, "start_line": 152 }
module LowStar.Lib.AssocList /// A Low*, stateful associative list that exposes a map-like interface. module B = LowStar.Buffer module HS = FStar.HyperStack module G = FStar.Ghost module L = FStar.List.Tot module U32 = FStar.UInt32 module ST = FStar.HyperStack.ST module M = FStar.Map module LL2 = LowStar.Lib.LinkedList2 module LL1 = LowStar.Lib.LinkedList open FStar.HyperStack.ST open LowStar.BufferOps #set-options "--fuel 0 --ifuel 0" /// Types, invariants, footprint /// ---------------------------- /// We prefer the packed representation that is in general easier to use and will /// look at the underlying predicate-based representation from LL1 only when /// strictly needed. let t k v = LL2.t (k & v) /// Functions suffixed with an underscore operate on either LL1 or raw lists /// while those without operate on LL2. val v_: #t_k:eqtype -> #t_v:Type0 -> l:list (t_k & t_v) -> Tot (map t_k t_v) let v_ #_ #t_v l = List.Tot.fold_right (fun (k, v) m -> M.upd m k (Some v)) l (M.const (None #t_v)) /// Reflecting a stateful, imperative map as a functional one in a given heap. let v #_ #_ h ll = let l = LL2.v h ll in v_ l let invariant #_ #_ h ll = LL2.invariant h ll // No longer needed in the fsti. val footprint: #t_k:eqtype -> #t_v:Type0 -> h:HS.mem -> ll:t t_k t_v -> Ghost B.loc (requires invariant h ll) (ensures fun _ -> True) let footprint #_ #_ h ll = LL2.footprint h ll let region_of #_ #_ ll = LL2.region_of ll let frame #_ #_ ll _ h0 _ = LL2.footprint_in_r h0 ll val footprint_in_r: #t_k:eqtype -> #t_v:Type0 -> h0:HS.mem -> ll:t t_k t_v -> Lemma (requires invariant h0 ll) (ensures B.(loc_includes (region_of ll) (footprint h0 ll))) [ SMTPat (footprint h0 ll) ] let footprint_in_r #_ #_ h0 ll = LL2.footprint_in_r h0 ll #push-options "--fuel 1" let create_in #_ #_ r = LL2.create_in r #pop-options /// Find /// ---- /// Proper recursion can only be done over the LL1.t type (unpacked representation). val find_ (#t_k: eqtype) (#t_v: Type0) (hd: LL1.t (t_k & t_v)) (l: G.erased (list (t_k & t_v))) (k: t_k): Stack (option t_v) (requires fun h0 -> LL1.well_formed h0 hd l /\ LL1.invariant h0 hd l) (ensures fun h0 x h1 -> let m: map t_k t_v = v_ l in h0 == h1 /\ x == M.sel m k) #push-options "--fuel 1 --ifuel 1" let rec find_ #_ #_ hd l k = if B.is_null hd then None else let cell = !* hd in if fst cell.LL1.data = k then Some (snd cell.LL1.data) else find_ cell.LL1.next (List.Tot.tl l) k #pop-options let find #_ #_ ll k = find_ !*ll.LL2.ptr !*ll.LL2.v k /// Adding elements /// --------------- #push-options "--fuel 1" let add #_ #_ ll k x = LL2.push ll (k, x) #pop-options /// Removing elements /// ----------------- /// This one is tricky to state so temporarily disabled. I'm trying to offer a /// shadowing API that allows revealing the previous binding (if any) when the /// most recent one is remoed. However, as it stands, the precondition is not /// strong enough to conclude that just popping the first occurrence of key /// ``k`` in the list yields ``m``. /// /// I'm unsure on how to do that cleanly and abstractly without revealing to the /// client the underlying associative-list nature of the map abstraction, so /// disabling for now. /// (*val remove_one (#t_k: eqtype) (#t_v: Type0) (ll: t t_k t_v) (k: t_k) (x: G.erased t_v) (m: G.erased (map t_k t_v)): ST unit (requires fun h0 -> invariant h0 ll /\ // TODO: file bug to figure why auto-reveal doesn't work (also in post-condition) v h0 ll == M.upd m k (Some (G.reveal x))) (ensures fun h0 _ h1 -> B.modifies (footprint h0 ll) h0 h1 /\ v h1 ll == G.reveal m)*) val remove_all_ (#t_k: eqtype) (#t_v: Type0) (hd: LL1.t (t_k & t_v)) (l: G.erased (list (t_k & t_v))) (k: t_k): ST (LL1.t (t_k & t_v) & G.erased (list (t_k & t_v))) (requires fun h0 -> LL1.well_formed h0 hd l /\ LL1.invariant h0 hd l) (ensures fun h0 (hd', l') h1 -> LL1.well_formed h1 hd' l' /\ LL1.invariant h1 hd' l' /\ B.(loc_includes (LL1.footprint h0 hd l) (LL1.footprint h1 hd' l')) /\ B.modifies (LL1.footprint h0 hd l) h0 h1 /\ v_ l' == M.upd (v_ l) k None)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Lib.LinkedList2.fst.checked", "LowStar.Lib.LinkedList.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Lib.AssocList.fst" }
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Lib.LinkedList", "short_module": "LL1" }, { "abbrev": true, "full_module": "LowStar.Lib.LinkedList2", "short_module": "LL2" }, { "abbrev": true, "full_module": "FStar.Map", "short_module": "M" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Lib.LinkedList", "short_module": "LL1" }, { "abbrev": true, "full_module": "LowStar.Lib.LinkedList2", "short_module": "LL2" }, { "abbrev": true, "full_module": "FStar.Map", "short_module": "M" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "LowStar.Lib", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_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
hd: LowStar.Lib.LinkedList.t (t_k * t_v) -> l: FStar.Ghost.erased (Prims.list (t_k * t_v)) -> k: t_k -> FStar.HyperStack.ST.ST (LowStar.Lib.LinkedList.t (t_k * t_v) * FStar.Ghost.erased (Prims.list (t_k * t_v)))
FStar.HyperStack.ST.ST
[]
[]
[ "Prims.eqtype", "LowStar.Lib.LinkedList.t", "FStar.Pervasives.Native.tuple2", "FStar.Ghost.erased", "Prims.list", "FStar.Pervasives.Native.Mktuple2", "Prims.unit", "FStar.Map.lemma_equal_intro", "FStar.Pervasives.Native.option", "LowStar.Lib.AssocList.v_", "FStar.Ghost.reveal", "FStar.Map.upd", "FStar.Pervasives.Native.None", "Prims.bool", "LowStar.Buffer.pointer_or_null", "LowStar.Lib.LinkedList.cell", "Prims.op_Equality", "LowStar.Lib.AssocList.remove_all_", "FStar.Ghost.hide", "FStar.List.Tot.Base.tail", "LowStar.Lib.LinkedList.frame", "LowStar.Monotonic.Buffer.loc_addr_of_buffer", "LowStar.Buffer.trivial_preorder", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "LowStar.Monotonic.Buffer.free", "Prims.Cons", "Prims._assert", "LowStar.Lib.LinkedList.invariant", "LowStar.Lib.LinkedList.well_formed", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Lib.LinkedList.footprint", "LowStar.BufferOps.op_Star_Equals", "LowStar.Lib.LinkedList.Mkcell", "LowStar.BufferOps.op_Bang_Star", "LowStar.Monotonic.Buffer.is_null" ]
[ "recursion" ]
false
true
false
false
false
let rec remove_all_ #t_k #t_v hd l k =
let h0 = ST.get () in if B.is_null hd then (M.lemma_equal_intro (v_ l) (M.upd (v_ l) k None); hd, l) else let cell = !*hd in let { LL1.data = data ; LL1.next = next } = cell in let k', v = data in if k = k' then (B.free hd; let h1 = ST.get () in LL1.frame next (List.Tot.tail l) (B.loc_addr_of_buffer hd) h0 h1; let hd', l' = remove_all_ next (List.Tot.tail l) k in M.lemma_equal_intro (v_ l') (M.upd (v_ l) k None); hd', l') else let hd', l' = remove_all_ next (List.Tot.tail l) k in let h1 = ST.get () in hd *= { LL1.data = data; LL1.next = hd' }; let h2 = ST.get () in LL1.frame hd' l' (B.loc_addr_of_buffer hd) h1 h2; M.lemma_equal_intro (v_ ((k', v) :: l')) (M.upd (v_ l) k None); assert B.(loc_disjoint (loc_buffer hd) (LL1.footprint h2 hd' l')); assert (B.live h2 hd /\ B.length hd == 1); assert (LL1.well_formed h2 hd' l'); assert (LL1.invariant h2 hd (data :: l')); hd, G.hide ((k', v) :: l')
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_intel_prod_sum_bound
val lemma_intel_prod_sum_bound (w x y z: nat64) : Lemma (w * x + y + z < pow2_128)
val lemma_intel_prod_sum_bound (w x y z: nat64) : Lemma (w * x + y + z < pow2_128)
let lemma_intel_prod_sum_bound (w x y z:nat64) : Lemma (w * x + y + z < pow2_128) = lemma_mul_bound64 w x
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 25, "end_line": 31, "start_col": 0, "start_line": 29 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
w: Vale.Def.Types_s.nat64 -> x: Vale.Def.Types_s.nat64 -> y: Vale.Def.Types_s.nat64 -> z: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (ensures w * x + y + z < Vale.Def.Words_s.pow2_128)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Def.Types_s.nat64", "Vale.Curve25519.FastMul_helpers.lemma_mul_bound64", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Addition", "FStar.Mul.op_Star", "Vale.Def.Words_s.pow2_128", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_intel_prod_sum_bound (w x y z: nat64) : Lemma (w * x + y + z < pow2_128) =
lemma_mul_bound64 w x
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_mul_bound64
val lemma_mul_bound64 (x y: nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2 * pow2_64 + 1)
val lemma_mul_bound64 (x y: nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2 * pow2_64 + 1)
let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 31, "end_line": 26, "start_col": 0, "start_line": 22 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: Vale.Def.Types_s.nat64 -> y: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (ensures x * y < Vale.Def.Words_s.pow2_128 - 1 /\ x * y <= Vale.Def.Words_s.pow2_128 - 2 * Vale.Def.Words_s.pow2_64 + 1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Def.Types_s.nat64", "Vale.Curve25519.FastMul_helpers.lemma_mul_pow2_bound", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2", "Prims.int", "Prims.pow2", "Vale.Def.Words_s.pow2_128", "Vale.Def.Words_s.pow2_64", "Prims.l_True", "Prims.squash", "Prims.l_and", "Prims.b2t", "Prims.op_LessThan", "FStar.Mul.op_Star", "Prims.op_Subtraction", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_mul_bound64 (x y: nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2 * pow2_64 + 1) =
assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_mul_pow2_bound
val lemma_mul_pow2_bound (b: nat{b > 1}) (x y: natN (pow2 b)) : Lemma (x * y < pow2 (2 * b) - 1 /\ x * y <= pow2 (2 * b) - 2 * pow2 (b) + 1)
val lemma_mul_pow2_bound (b: nat{b > 1}) (x y: natN (pow2 b)) : Lemma (x * y < pow2 (2 * b) - 1 /\ x * y <= pow2 (2 * b) - 2 * pow2 (b) + 1)
let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1)
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 69, "end_line": 19, "start_col": 0, "start_line": 14 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'"
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Prims.nat{b > 1} -> x: Vale.Def.Words_s.natN (Prims.pow2 b) -> y: Vale.Def.Words_s.natN (Prims.pow2 b) -> FStar.Pervasives.Lemma (ensures x * y < Prims.pow2 (2 * b) - 1 /\ x * y <= Prims.pow2 (2 * b) - 2 * Prims.pow2 b + 1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "Vale.Def.Words_s.natN", "Prims.pow2", "Prims._assert", "Prims.op_Equality", "Prims.int", "FStar.Mul.op_Star", "Prims.op_Subtraction", "Prims.op_Addition", "Prims.unit", "FStar.Math.Lemmas.pow2_plus", "Vale.Curve25519.Fast_lemmas_internal.lemma_mul_bounds_le", "Prims.l_True", "Prims.squash", "Prims.l_and", "Prims.op_LessThan", "Prims.op_LessThanOrEqual", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_mul_pow2_bound (b: nat{b > 1}) (x y: natN (pow2 b)) : Lemma (x * y < pow2 (2 * b) - 1 /\ x * y <= pow2 (2 * b) - 2 * pow2 (b) + 1) =
lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b - 1); pow2_plus b b; assert ((pow2 b - 1) * (pow2 b - 1) = pow2 (2 * b) - 2 * pow2 (b) + 1)
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_prod_bounds
val lemma_prod_bounds (dst_hi dst_lo x y: nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1))
val lemma_prod_bounds (dst_hi dst_lo x y: nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1))
let lemma_prod_bounds (dst_hi dst_lo x y:nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1)) = let result = x * y in lemma_div_mod result pow2_64; //assert (result = pow2_64 * (result / pow2_64) + result % pow2_64); //assert (result % pow2_64 == dst_lo); //assert (result / pow2_64 == dst_hi); lemma_mul_bound64 x y
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 25, "end_line": 46, "start_col": 0, "start_line": 33 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y (* Intel manual mentions this fact *) let lemma_intel_prod_sum_bound (w x y z:nat64) : Lemma (w * x + y + z < pow2_128) = lemma_mul_bound64 w x
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
dst_hi: Vale.Def.Types_s.nat64 -> dst_lo: Vale.Def.Types_s.nat64 -> x: Vale.Def.Types_s.nat64 -> y: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (requires Vale.Def.Words_s.pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < Vale.Def.Words_s.pow2_64 - 1 /\ (dst_hi < Vale.Def.Words_s.pow2_64 - 2 \/ dst_lo <= 1))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Def.Types_s.nat64", "Vale.Curve25519.FastMul_helpers.lemma_mul_bound64", "Prims.unit", "FStar.Math.Lemmas.lemma_div_mod", "Vale.Def.Words_s.pow2_64", "Prims.int", "FStar.Mul.op_Star", "Prims.eq2", "Prims.op_Addition", "Prims.squash", "Prims.l_and", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Subtraction", "Prims.l_or", "Prims.op_LessThanOrEqual", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_prod_bounds (dst_hi dst_lo x y: nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1)) =
let result = x * y in lemma_div_mod result pow2_64; lemma_mul_bound64 x y
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_offset_sum
val lemma_offset_sum (a_agg: nat) (a0 a1 a2 a3 a4: nat64) (b_agg: nat) (b0 b1 b2 b3 b4: nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4)
val lemma_offset_sum (a_agg: nat) (a0 a1 a2 a3 a4: nat64) (b_agg: nat) (b0 b1 b2 b3 b4: nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4)
let lemma_offset_sum (a_agg:nat) (a0 a1 a2 a3 a4:nat64) (b_agg:nat) (b0 b1 b2 b3 b4:nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4) = let lhs = a_agg + pow2_64 * b_agg in let rhs = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 in assert_by_tactic (lhs == rhs) int_canon
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 43, "end_line": 65, "start_col": 0, "start_line": 54 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y (* Intel manual mentions this fact *) let lemma_intel_prod_sum_bound (w x y z:nat64) : Lemma (w * x + y + z < pow2_128) = lemma_mul_bound64 w x let lemma_prod_bounds (dst_hi dst_lo x y:nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1)) = let result = x * y in lemma_div_mod result pow2_64; //assert (result = pow2_64 * (result / pow2_64) + result % pow2_64); //assert (result % pow2_64 == dst_lo); //assert (result / pow2_64 == dst_hi); lemma_mul_bound64 x y let lemma_double_bound (x:nat64) : Lemma (add_wrap x x < pow2_64 - 1) = () type bit = b:nat { b <= 1 }
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a_agg: Prims.nat -> a0: Vale.Def.Types_s.nat64 -> a1: Vale.Def.Types_s.nat64 -> a2: Vale.Def.Types_s.nat64 -> a3: Vale.Def.Types_s.nat64 -> a4: Vale.Def.Types_s.nat64 -> b_agg: Prims.nat -> b0: Vale.Def.Types_s.nat64 -> b1: Vale.Def.Types_s.nat64 -> b2: Vale.Def.Types_s.nat64 -> b3: Vale.Def.Types_s.nat64 -> b4: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (requires a_agg = Vale.Curve25519.Fast_defs.pow2_five a0 a1 a2 a3 a4 /\ b_agg = Vale.Curve25519.Fast_defs.pow2_five b0 b1 b2 b3 b4) (ensures a_agg + Vale.Def.Words_s.pow2_64 * b_agg = Vale.Curve25519.Fast_defs.pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Vale.Def.Types_s.nat64", "FStar.Tactics.Effect.assert_by_tactic", "Prims.eq2", "Prims.int", "Vale.Curve25519.FastMul_helpers.int_canon", "Prims.unit", "Vale.Curve25519.Fast_defs.pow2_six", "Prims.op_Addition", "FStar.Mul.op_Star", "Vale.Def.Words_s.pow2_64", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Vale.Curve25519.Fast_defs.pow2_five", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_offset_sum (a_agg: nat) (a0 a1 a2 a3 a4: nat64) (b_agg: nat) (b0 b1 b2 b3 b4: nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4) =
let lhs = a_agg + pow2_64 * b_agg in let rhs = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 in assert_by_tactic (lhs == rhs) int_canon
false
Lib.ByteSequence.fst
Lib.ByteSequence.mask_select
val mask_select: #t:inttype{~(S128? t)} -> mask:int_t t SEC -> a:int_t t SEC -> b:int_t t SEC -> int_t t SEC
val mask_select: #t:inttype{~(S128? t)} -> mask:int_t t SEC -> a:int_t t SEC -> b:int_t t SEC -> int_t t SEC
let mask_select #t mask a b = b ^. (mask &. (a ^. b))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 25, "end_line": 79, "start_col": 0, "start_line": 78 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
mask: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> a: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> b: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> Lib.IntTypes.int_t t Lib.IntTypes.SEC
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_not", "Prims.b2t", "Lib.IntTypes.uu___is_S128", "Lib.IntTypes.int_t", "Lib.IntTypes.SEC", "Lib.IntTypes.op_Hat_Dot", "Lib.IntTypes.op_Amp_Dot" ]
[]
false
false
false
false
false
let mask_select #t mask a b =
b ^. (mask &. (a ^. b))
false
Steel.Reference.fsti
Steel.Reference.vptr_not_null
val vptr_not_null (#opened: _) (#a: Type) (r: ref a) : SteelGhost unit opened (vptr r) (fun _ -> vptr r) (fun _ -> True) (fun h0 _ h1 -> sel r h0 == sel r h1 /\ is_null r == false)
val vptr_not_null (#opened: _) (#a: Type) (r: ref a) : SteelGhost unit opened (vptr r) (fun _ -> vptr r) (fun _ -> True) (fun h0 _ h1 -> sel r h0 == sel r h1 /\ is_null r == false)
let vptr_not_null (#opened: _) (#a: Type) (r: ref a) : SteelGhost unit opened (vptr r) (fun _ -> vptr r) (fun _ -> True) (fun h0 _ h1 -> sel r h0 == sel r h1 /\ is_null r == false ) = vptrp_not_null r full_perm
{ "file_name": "lib/steel/Steel.Reference.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 28, "end_line": 361, "start_col": 0, "start_line": 350 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Reference open FStar.Ghost open Steel.FractionalPermission open Steel.Memory open Steel.Effect.Atomic open Steel.Effect module U32 = FStar.UInt32 module Mem = Steel.Memory /// The main user-facing Steel library. /// This library provides functions to operate on references to values in universe 0, such as uints. /// This library provides two versions, which can interoperate with each other. /// The first one uses the standard separation logic pts_to predicate, and has a non-informative selector /// The second one has a selector which returns the contents of the reference in memory, enabling /// to better separate reasoning about memory safety and functional correctness when handling references. /// An abstract datatype for references val ref ([@@@unused] a:Type0) : Type0 /// The null pointer [@@ noextract_to "krml"] val null (#a:Type0) : ref a /// Checking whether a pointer is null can be done in a decidable way [@@ noextract_to "krml"] val is_null (#a:Type0) (r:ref a) : (b:bool{b <==> r == null}) (** First version of references: Non-informative selector and standard pts_to predicate. All functions names here are postfixed with _pt (for points_to)**) /// The standard points to separation logic assertion, expressing that /// reference [r] is valid in memory, stores value [v], and that we have /// permission [p] on it. val pts_to_sl (#a:Type0) (r:ref a) (p:perm) (v:a) : slprop u#1 /// Lifting the standard points to predicate to vprop, with a non-informative selector. /// The permission [p] and the value [v] are annotated with the smt_fallback attribute, /// enabling SMT rewriting on them during frame inference [@@ __steel_reduce__] let pts_to (#a:Type0) (r:ref a) ([@@@smt_fallback] p:perm) ([@@@ smt_fallback] v:a) = to_vprop (pts_to_sl r p v) /// If two pts_to predicates on the same reference [r] are valid in the memory [m], /// then the two values [v0] and [v1] are identical val pts_to_ref_injective (#a: Type u#0) (r: ref a) (p0 p1:perm) (v0 v1:a) (m:mem) : Lemma (requires interp (pts_to_sl r p0 v0 `Mem.star` pts_to_sl r p1 v1) m) (ensures v0 == v1) /// A valid pts_to predicate implies that the pointer is not the null pointer val pts_to_not_null (#a:Type u#0) (x:ref a) (p:perm) (v:a) (m:mem) : Lemma (requires interp (pts_to_sl x p v) m) (ensures x =!= null) /// Exposing the is_witness_invariant from Steel.Memory for references with fractional permissions val pts_to_witinv (#a:Type) (r:ref a) (p:perm) : Lemma (is_witness_invariant (pts_to_sl r p)) /// A stateful version of the pts_to_ref_injective lemma above val pts_to_injective_eq (#a: Type) (#opened:inames) (#p0 #p1:perm) (#v0 #v1: erased a) (r: ref a) : SteelGhost unit opened (pts_to r p0 v0 `star` pts_to r p1 v1) (fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0) (requires fun _ -> True) (ensures fun _ _ _ -> v0 == v1) /// A permission is always no greater than one val pts_to_perm (#a: _) (#u: _) (#p: _) (#v: _) (r: ref a) : SteelGhost unit u (pts_to r p v) (fun _ -> pts_to r p v) (fun _ -> True) (fun _ _ _ -> p `lesser_equal_perm` full_perm) /// Allocates a reference with value [x]. We have full permission on the newly /// allocated reference. val alloc_pt (#a:Type) (x:a) : Steel (ref a) emp (fun r -> pts_to r full_perm x) (requires fun _ -> True) (ensures fun _ r _ -> not (is_null r)) /// Reads the value in reference [r], as long as it initially is valid val read_pt (#a:Type) (#p:perm) (#v:erased a) (r:ref a) : Steel a (pts_to r p v) (fun x -> pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == Ghost.reveal v) /// A variant of read, useful when an existentially quantified predicate /// depends on the value stored in the reference val read_refine_pt (#a:Type0) (#p:perm) (q:a -> vprop) (r:ref a) : SteelT a (h_exists (fun (v:a) -> pts_to r p v `star` q v)) (fun v -> pts_to r p v `star` q v) /// Writes value [x] in the reference [r], as long as we have full ownership of [r] val write_pt (#a:Type0) (#v:erased a) (r:ref a) (x:a) : SteelT unit (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) /// Frees reference [r], as long as we have full ownership of [r] val free_pt (#a:Type0) (#v:erased a) (r:ref a) : SteelT unit (pts_to r full_perm v) (fun _ -> emp) /// Splits the permission on reference [r] into two. /// This function is computationally irrelevant (it has effect SteelGhost) val share_gen_pt (#a:Type0) (#uses:_) (#p:perm) (#v: a) (r:ref a) (p1 p2: perm) : SteelGhost unit uses (pts_to r p v) (fun _ -> pts_to r p1 v `star` pts_to r p2 v) (fun _ -> p == p1 `sum_perm` p2) (fun _ _ _ -> True) val share_pt (#a:Type0) (#uses:_) (#p:perm) (#v:erased a) (r:ref a) : SteelGhostT unit uses (pts_to r p v) (fun _ -> pts_to r (half_perm p) v `star` pts_to r (half_perm p) v) /// Combines permissions on reference [r]. /// This function is computationally irrelevant (it has effect SteelGhost) val gather_pt (#a:Type0) (#uses:_) (#p0:perm) (#p1:perm) (#v0 #v1:erased a) (r:ref a) : SteelGhostT (_:unit{v0 == v1}) uses (pts_to r p0 v0 `star` pts_to r p1 v1) (fun _ -> pts_to r (sum_perm p0 p1) v0) /// Atomic operations, read, write, and cas /// /// These are not polymorphic and are allowed only for small types (e.g. word-sized) /// For now, exporting only for U32 val atomic_read_pt_u32 (#opened:_) (#p:perm) (#v:erased U32.t) (r:ref U32.t) : SteelAtomic U32.t opened (pts_to r p v) (fun x -> pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == Ghost.reveal v) val atomic_write_pt_u32 (#opened:_) (#v:erased U32.t) (r:ref U32.t) (x:U32.t) : SteelAtomicT unit opened (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) val cas_pt_u32 (#uses:inames) (r:ref U32.t) (v:Ghost.erased U32.t) (v_old:U32.t) (v_new:U32.t) : SteelAtomicT (b:bool{b <==> (Ghost.reveal v == v_old)}) uses (pts_to r full_perm v) (fun b -> if b then pts_to r full_perm v_new else pts_to r full_perm v) val cas_pt_bool (#uses:inames) (r:ref bool) (v:Ghost.erased bool) (v_old:bool) (v_new:bool) : SteelAtomicT (b:bool{b <==> (Ghost.reveal v == v_old)}) uses (pts_to r full_perm v) (fun b -> if b then pts_to r full_perm v_new else pts_to r full_perm v) (** Second version of references: The memory contents are available inside the selector, instead of as an index of the predicate **) /// An abstract separation logic predicate stating that reference [r] is valid in memory. val ptrp (#a:Type0) (r:ref a) ([@@@smt_fallback] p: perm) : slprop u#1 [@@ __steel_reduce__; __reduce__] unfold let ptr (#a:Type0) (r:ref a) : slprop u#1 = ptrp r full_perm /// A selector for references, returning the value of type [a] stored in memory val ptrp_sel (#a:Type0) (r:ref a) (p: perm) : selector a (ptrp r p) [@@ __steel_reduce__; __reduce__] unfold let ptr_sel (#a:Type0) (r:ref a) : selector a (ptr r) = ptrp_sel r full_perm /// Some lemmas to interoperate between the two versions of references val ptrp_sel_interp (#a:Type0) (r:ref a) (p: perm) (m:mem) : Lemma (requires interp (ptrp r p) m) (ensures interp (pts_to_sl r p (ptrp_sel r p m)) m) let ptr_sel_interp (#a:Type0) (r:ref a) (m:mem) : Lemma (requires interp (ptr r) m) (ensures interp (pts_to_sl r full_perm (ptr_sel r m)) m) = ptrp_sel_interp r full_perm m val intro_ptrp_interp (#a:Type0) (r:ref a) (p: perm) (v:erased a) (m:mem) : Lemma (requires interp (pts_to_sl r p v) m) (ensures interp (ptrp r p) m) let intro_ptr_interp (#a:Type0) (r:ref a) (v:erased a) (m:mem) : Lemma (requires interp (pts_to_sl r full_perm v) m) (ensures interp (ptr r) m) = intro_ptrp_interp r full_perm v m /// Combining the separation logic predicate and selector into a vprop [@@ __steel_reduce__] let vptr' #a r p : vprop' = {hp = ptrp r p; t = a; sel = ptrp_sel r p} [@@ __steel_reduce__] unfold let vptrp (#a: Type) (r: ref a) ([@@@smt_fallback] p: perm) = VUnit (vptr' r p) [@@ __steel_reduce__; __reduce__] unfold let vptr r = vptrp r full_perm /// A wrapper to access a reference selector more easily. /// Ensuring that the corresponding ptr vprop is in the context is done by /// calling a variant of the framing tactic, as defined in Steel.Effect.Common [@@ __steel_reduce__] let sel (#a:Type) (#p:vprop) (r:ref a) (h:rmem p{FStar.Tactics.with_tactic selector_tactic (can_be_split p (vptr r) /\ True)}) = h (vptr r) /// Moving from indexed pts_to assertions to selector-based vprops and back val intro_vptr (#a:Type) (#opened:inames) (r:ref a) (p: perm) (v:erased a) : SteelGhost unit opened (pts_to r p v) (fun _ -> vptrp r p) (requires fun _ -> True) (ensures fun _ _ h1 -> h1 (vptrp r p) == reveal v) val elim_vptr (#a:Type) (#opened:inames) (r:ref a) (p: perm) : SteelGhost (erased a) opened (vptrp r p) (fun v -> pts_to r p v) (requires fun _ -> True) (ensures fun h0 v _ -> reveal v == h0 (vptrp r p)) /// Allocates a reference with value [x]. val malloc (#a:Type0) (x:a) : Steel (ref a) emp (fun r -> vptr r) (requires fun _ -> True) (ensures fun _ r h1 -> sel r h1 == x /\ not (is_null r)) /// Frees a reference [r] val free (#a:Type0) (r:ref a) : Steel unit (vptr r) (fun _ -> emp) (requires fun _ -> True) (ensures fun _ _ _ -> True) /// Reads the current value of reference [r] val readp (#a:Type0) (r:ref a) (p: perm) : Steel a (vptrp r p) (fun _ -> vptrp r p) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (vptrp r p) == h1 (vptrp r p) /\ x == h1 (vptrp r p)) let read (#a:Type0) (r:ref a) : Steel a (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> sel r h0 == sel r h1 /\ x == sel r h1) = readp r full_perm /// Writes value [x] in reference [r] val write (#a:Type0) (r:ref a) (x:a) : Steel unit (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun _ _ h1 -> x == sel r h1) val share (#a:Type0) (#uses:_) (#p: perm) (r:ref a) : SteelGhost unit uses (vptrp r p) (fun _ -> vptrp r (half_perm p) `star` vptrp r (half_perm p)) (fun _ -> True) (fun h _ h' -> h' (vptrp r (half_perm p)) == h (vptrp r p) ) val gather_gen (#a:Type0) (#uses:_) (r:ref a) (p0:perm) (p1:perm) : SteelGhost perm uses (vptrp r p0 `star` vptrp r p1) (fun res -> vptrp r res) (fun _ -> True) (fun h res h' -> res == sum_perm p0 p1 /\ h' (vptrp r res) == h (vptrp r p0) /\ h' (vptrp r res) == h (vptrp r p1) ) val gather (#a: Type0) (#uses: _) (#p: perm) (r: ref a) : SteelGhost unit uses (vptrp r (half_perm p) `star` vptrp r (half_perm p)) (fun _ -> vptrp r p) (fun _ -> True) (fun h _ h' -> h' (vptrp r p) == h (vptrp r (half_perm p)) ) /// A stateful lemma variant of the pts_to_not_null lemma above. /// This stateful function is computationally irrelevant and does not modify memory val vptrp_not_null (#opened: _) (#a: Type) (r: ref a) (p: perm) : SteelGhost unit opened (vptrp r p) (fun _ -> vptrp r p) (fun _ -> True) (fun h0 _ h1 -> h0 (vptrp r p) == h1 (vptrp r p) /\ is_null r == false )
{ "checked_file": "/", "dependencies": [ "Steel.Memory.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "Steel.Reference.fsti" }
[ { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.Reference.ref a -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Reference.ref", "Steel.Reference.vptrp_not_null", "Steel.FractionalPermission.full_perm", "Prims.unit", "Steel.Reference.vptr", "Steel.Effect.Common.vprop", "Steel.Effect.Common.rmem", "Prims.l_True", "Prims.l_and", "Prims.eq2", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Steel.Reference.sel", "Prims.bool", "Steel.Reference.is_null" ]
[]
false
true
false
false
false
let vptr_not_null (#opened: _) (#a: Type) (r: ref a) : SteelGhost unit opened (vptr r) (fun _ -> vptr r) (fun _ -> True) (fun h0 _ h1 -> sel r h0 == sel r h1 /\ is_null r == false) =
vptrp_not_null r full_perm
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_sum_a2b
val lemma_sum_a2b (a0 a1 a2:nat64) (a0a1b:nat) (a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5:nat64) (a2b:nat) (a2b_0 a2b_1 a2b_2 a2b_3 a2b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1b = pow2_six a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 /\ a2b = pow2_five a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1b = mul_nats (pow2_two a0 a1) b /\ a2b = mul_nats a2 b /\ (let s1', c1 = add_carry a0a1b_2 a2b_0 0 in let s2', c2 = add_carry a0a1b_3 a2b_1 c1 in let s3', c3 = add_carry a0a1b_4 a2b_2 c2 in let s4', c4 = add_carry a0a1b_5 a2b_3 c3 in let s5', c5 = add_carry 0 a2b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_three a0 a1 a2) * b == pow2_seven a0a1b_0 a0a1b_1 s1 s2 s3 s4 s5)
val lemma_sum_a2b (a0 a1 a2:nat64) (a0a1b:nat) (a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5:nat64) (a2b:nat) (a2b_0 a2b_1 a2b_2 a2b_3 a2b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1b = pow2_six a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 /\ a2b = pow2_five a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1b = mul_nats (pow2_two a0 a1) b /\ a2b = mul_nats a2 b /\ (let s1', c1 = add_carry a0a1b_2 a2b_0 0 in let s2', c2 = add_carry a0a1b_3 a2b_1 c1 in let s3', c3 = add_carry a0a1b_4 a2b_2 c2 in let s4', c4 = add_carry a0a1b_5 a2b_3 c3 in let s5', c5 = add_carry 0 a2b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_three a0 a1 a2) * b == pow2_seven a0a1b_0 a0a1b_1 s1 s2 s3 s4 s5)
let lemma_sum_a2b (a0 a1 a2:nat64) (a0a1b:nat) (a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5:nat64) (a2b:nat) (a2b_0 a2b_1 a2b_2 a2b_3 a2b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1b = pow2_six a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 /\ a2b = pow2_five a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1b = mul_nats (pow2_two a0 a1) b /\ a2b = mul_nats a2 b /\ (let s1', c1 = add_carry a0a1b_2 a2b_0 0 in let s2', c2 = add_carry a0a1b_3 a2b_1 c1 in let s3', c3 = add_carry a0a1b_4 a2b_2 c2 in let s4', c4 = add_carry a0a1b_5 a2b_3 c3 in let s5', c5 = add_carry 0 a2b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_three a0 a1 a2) * b == pow2_seven a0a1b_0 a0a1b_1 s1 s2 s3 s4 s5) = assert_by_tactic ( (pow2_three a0 a1 a2) * b == mul_nats (pow2_two a0 a1) b + pow2_128 * (mul_nats a2 b)) int_canon; assert ( mul_nats (pow2_two a0 a1) b + pow2_128 * (mul_nats a2 b) == a0a1b + pow2_128 * a2b); assert_by_tactic ( a0a1b + pow2_128 * a2b == pow2_seven a0a1b_0 a0a1b_1 (a0a1b_2 + a2b_0) (a0a1b_3 + a2b_1) (a0a1b_4 + a2b_2) (a0a1b_5 + a2b_3) a2b_4) int_canon; lemma_partial_sum_a2b a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 s1 s2 s3 s4 s5; ()
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 4, "end_line": 215, "start_col": 0, "start_line": 176 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y (* Intel manual mentions this fact *) let lemma_intel_prod_sum_bound (w x y z:nat64) : Lemma (w * x + y + z < pow2_128) = lemma_mul_bound64 w x let lemma_prod_bounds (dst_hi dst_lo x y:nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1)) = let result = x * y in lemma_div_mod result pow2_64; //assert (result = pow2_64 * (result / pow2_64) + result % pow2_64); //assert (result % pow2_64 == dst_lo); //assert (result / pow2_64 == dst_hi); lemma_mul_bound64 x y let lemma_double_bound (x:nat64) : Lemma (add_wrap x x < pow2_64 - 1) = () type bit = b:nat { b <= 1 } let lemma_offset_sum (a_agg:nat) (a0 a1 a2 a3 a4:nat64) (b_agg:nat) (b0 b1 b2 b3 b4:nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4) = let lhs = a_agg + pow2_64 * b_agg in let rhs = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 in assert_by_tactic (lhs == rhs) int_canon #push-options "--z3rlimit 60" let lemma_partial_sum (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5 c:nat64) : Lemma (requires (let s1', c1 = add_carry a1 b0 0 in let s2', c2 = add_carry a2 b1 c1 in let s3', c3 = add_carry a3 b2 c2 in let s4', c4 = add_carry a4 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c == c5)) (ensures pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 = pow2_seven a0 s1 s2 s3 s4 s5 c) = () let lemma_partial_sum_a2b (a0 a1 a2 a3 a4 a5 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5:nat64) : Lemma (requires (let s1', c1 = add_carry a2 b0 0 in let s2', c2 = add_carry a3 b1 c1 in let s3', c3 = add_carry a4 b2 c2 in let s4', c4 = add_carry a5 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ 0 == c5)) (ensures pow2_seven a0 a1 (a2 + b0) (a3 + b1) (a4 + b2) (a5 + b3) b4 = pow2_seven a0 a1 s1 s2 s3 s4 s5) = () let lemma_partial_sum_a3b ( a0 a1 a2 a3 a4 a5 a6 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5:nat64) : Lemma (requires(let s1', c1 = add_carry a3 b0 0 in let s2', c2 = add_carry a4 b1 c1 in let s3', c3 = add_carry a5 b2 c2 in let s4', c4 = add_carry a6 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ 0 == c5)) (ensures pow2_eight a0 a1 a2 (a3 + b0) (a4 + b1) (a5 + b2) (a6 + b3) b4 = pow2_eight a0 a1 a2 s1 s2 s3 s4 s5) = () #pop-options let lemma_sum_a1b (a0 a1:nat64) (a0b:nat) (a0b_0 a0b_1 a0b_2 a0b_3 a0b_4:nat64) (a1b:nat) (a1b_0 a1b_1 a1b_2 a1b_3 a1b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5 c:nat64) : Lemma (requires a0b = pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0b = mul_nats a0 b /\ a1b = mul_nats a1 b /\ (let s1', c1 = add_carry a0b_1 a1b_0 0 in let s2', c2 = add_carry a0b_2 a1b_1 c1 in let s3', c3 = add_carry a0b_3 a1b_2 c2 in let s4', c4 = add_carry a0b_4 a1b_3 c3 in let s5', c5 = add_carry 0 a1b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c)) (ensures (pow2_two a0 a1) * b == pow2_seven a0b_0 s1 s2 s3 s4 s5 c) = assert_by_tactic ( (pow2_two a0 a1) * b == pow2_two (mul_nats a0 b) (mul_nats a1 b)) int_canon; assert ( pow2_two (mul_nats a0 b) (mul_nats a1 b) == pow2_two a0b a1b); lemma_offset_sum a0b a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b a1b_0 a1b_1 a1b_2 a1b_3 a1b_4; assert ( pow2_two a0b a1b == pow2_six a0b_0 (a0b_1 + a1b_0) (a0b_2 + a1b_1) (a0b_3 + a1b_2) (a0b_4 + a1b_3) a1b_4); lemma_partial_sum a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 s1 s2 s3 s4 s5 c; ()
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 60, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a0: Vale.Def.Types_s.nat64 -> a1: Vale.Def.Types_s.nat64 -> a2: Vale.Def.Types_s.nat64 -> a0a1b: Prims.nat -> a0a1b_0: Vale.Def.Types_s.nat64 -> a0a1b_1: Vale.Def.Types_s.nat64 -> a0a1b_2: Vale.Def.Types_s.nat64 -> a0a1b_3: Vale.Def.Types_s.nat64 -> a0a1b_4: Vale.Def.Types_s.nat64 -> a0a1b_5: Vale.Def.Types_s.nat64 -> a2b: Prims.nat -> a2b_0: Vale.Def.Types_s.nat64 -> a2b_1: Vale.Def.Types_s.nat64 -> a2b_2: Vale.Def.Types_s.nat64 -> a2b_3: Vale.Def.Types_s.nat64 -> a2b_4: Vale.Def.Types_s.nat64 -> b: Prims.nat -> b0: Vale.Def.Types_s.nat64 -> b1: Vale.Def.Types_s.nat64 -> b2: Vale.Def.Types_s.nat64 -> b3: Vale.Def.Types_s.nat64 -> s1: Vale.Def.Types_s.nat64 -> s2: Vale.Def.Types_s.nat64 -> s3: Vale.Def.Types_s.nat64 -> s4: Vale.Def.Types_s.nat64 -> s5: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (requires a0a1b = Vale.Curve25519.Fast_defs.pow2_six a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 /\ a2b = Vale.Curve25519.Fast_defs.pow2_five a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 /\ b = Vale.Curve25519.Fast_defs.pow2_four b0 b1 b2 b3 /\ a0a1b = Vale.Curve25519.Fast_defs.mul_nats (Vale.Curve25519.Fast_defs.pow2_two a0 a1) b /\ a2b = Vale.Curve25519.Fast_defs.mul_nats a2 b /\ (let _ = Vale.Curve25519.Fast_defs.add_carry a0a1b_2 a2b_0 0 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1' c1 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0a1b_3 a2b_1 c1 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s2' c2 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0a1b_4 a2b_2 c2 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s3' c3 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0a1b_5 a2b_3 c3 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s4' c4 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry 0 a2b_4 c4 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s5' c5 = _ in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0) <: Prims.logical) <: Prims.logical) <: Prims.logical) <: Prims.logical) <: Prims.logical)) (ensures Vale.Curve25519.Fast_defs.pow2_three a0 a1 a2 * b == Vale.Curve25519.Fast_defs.pow2_seven a0a1b_0 a0a1b_1 s1 s2 s3 s4 s5)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Def.Types_s.nat64", "Prims.nat", "Prims.unit", "Vale.Curve25519.FastMul_helpers.lemma_partial_sum_a2b", "FStar.Tactics.Effect.assert_by_tactic", "Prims.eq2", "Prims.int", "Prims.op_Addition", "FStar.Mul.op_Star", "Vale.Def.Words_s.pow2_128", "Vale.Curve25519.Fast_defs.pow2_seven", "Vale.Curve25519.FastMul_helpers.int_canon", "Prims._assert", "Vale.Curve25519.Fast_defs.mul_nats", "Vale.Curve25519.Fast_defs.pow2_two", "Vale.Curve25519.Fast_defs.pow2_three", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Vale.Curve25519.Fast_defs.pow2_six", "Vale.Curve25519.Fast_defs.pow2_five", "Vale.Curve25519.Fast_defs.pow2_four", "Prims.op_BarBar", "Prims.logical", "FStar.Pervasives.Native.tuple2", "Vale.Def.Words_s.nat64", "Vale.Curve25519.Fast_defs.add_carry", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_sum_a2b (a0 a1 a2: nat64) (a0a1b: nat) (a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5: nat64) (a2b: nat) (a2b_0 a2b_1 a2b_2 a2b_3 a2b_4: nat64) (b: nat) (b0 b1 b2 b3 s1 s2 s3 s4 s5: nat64) : Lemma (requires a0a1b = pow2_six a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 /\ a2b = pow2_five a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1b = mul_nats (pow2_two a0 a1) b /\ a2b = mul_nats a2 b /\ (let s1', c1 = add_carry a0a1b_2 a2b_0 0 in let s2', c2 = add_carry a0a1b_3 a2b_1 c1 in let s3', c3 = add_carry a0a1b_4 a2b_2 c2 in let s4', c4 = add_carry a0a1b_5 a2b_3 c3 in let s5', c5 = add_carry 0 a2b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_three a0 a1 a2) * b == pow2_seven a0a1b_0 a0a1b_1 s1 s2 s3 s4 s5) =
assert_by_tactic ((pow2_three a0 a1 a2) * b == mul_nats (pow2_two a0 a1) b + pow2_128 * (mul_nats a2 b)) int_canon; assert (mul_nats (pow2_two a0 a1) b + pow2_128 * (mul_nats a2 b) == a0a1b + pow2_128 * a2b); assert_by_tactic (a0a1b + pow2_128 * a2b == pow2_seven a0a1b_0 a0a1b_1 (a0a1b_2 + a2b_0) (a0a1b_3 + a2b_1) (a0a1b_4 + a2b_2) (a0a1b_5 + a2b_3) a2b_4) int_canon; lemma_partial_sum_a2b a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 s1 s2 s3 s4 s5; ()
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_sum_a1b
val lemma_sum_a1b (a0 a1:nat64) (a0b:nat) (a0b_0 a0b_1 a0b_2 a0b_3 a0b_4:nat64) (a1b:nat) (a1b_0 a1b_1 a1b_2 a1b_3 a1b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5 c:nat64) : Lemma (requires a0b = pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0b = mul_nats a0 b /\ a1b = mul_nats a1 b /\ (let s1', c1 = add_carry a0b_1 a1b_0 0 in let s2', c2 = add_carry a0b_2 a1b_1 c1 in let s3', c3 = add_carry a0b_3 a1b_2 c2 in let s4', c4 = add_carry a0b_4 a1b_3 c3 in let s5', c5 = add_carry 0 a1b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c)) (ensures (pow2_two a0 a1) * b == pow2_seven a0b_0 s1 s2 s3 s4 s5 c)
val lemma_sum_a1b (a0 a1:nat64) (a0b:nat) (a0b_0 a0b_1 a0b_2 a0b_3 a0b_4:nat64) (a1b:nat) (a1b_0 a1b_1 a1b_2 a1b_3 a1b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5 c:nat64) : Lemma (requires a0b = pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0b = mul_nats a0 b /\ a1b = mul_nats a1 b /\ (let s1', c1 = add_carry a0b_1 a1b_0 0 in let s2', c2 = add_carry a0b_2 a1b_1 c1 in let s3', c3 = add_carry a0b_3 a1b_2 c2 in let s4', c4 = add_carry a0b_4 a1b_3 c3 in let s5', c5 = add_carry 0 a1b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c)) (ensures (pow2_two a0 a1) * b == pow2_seven a0b_0 s1 s2 s3 s4 s5 c)
let lemma_sum_a1b (a0 a1:nat64) (a0b:nat) (a0b_0 a0b_1 a0b_2 a0b_3 a0b_4:nat64) (a1b:nat) (a1b_0 a1b_1 a1b_2 a1b_3 a1b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5 c:nat64) : Lemma (requires a0b = pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0b = mul_nats a0 b /\ a1b = mul_nats a1 b /\ (let s1', c1 = add_carry a0b_1 a1b_0 0 in let s2', c2 = add_carry a0b_2 a1b_1 c1 in let s3', c3 = add_carry a0b_3 a1b_2 c2 in let s4', c4 = add_carry a0b_4 a1b_3 c3 in let s5', c5 = add_carry 0 a1b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c)) (ensures (pow2_two a0 a1) * b == pow2_seven a0b_0 s1 s2 s3 s4 s5 c) = assert_by_tactic ( (pow2_two a0 a1) * b == pow2_two (mul_nats a0 b) (mul_nats a1 b)) int_canon; assert ( pow2_two (mul_nats a0 b) (mul_nats a1 b) == pow2_two a0b a1b); lemma_offset_sum a0b a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b a1b_0 a1b_1 a1b_2 a1b_3 a1b_4; assert ( pow2_two a0b a1b == pow2_six a0b_0 (a0b_1 + a1b_0) (a0b_2 + a1b_1) (a0b_3 + a1b_2) (a0b_4 + a1b_3) a1b_4); lemma_partial_sum a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 s1 s2 s3 s4 s5 c; ()
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 4, "end_line": 173, "start_col": 0, "start_line": 133 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y (* Intel manual mentions this fact *) let lemma_intel_prod_sum_bound (w x y z:nat64) : Lemma (w * x + y + z < pow2_128) = lemma_mul_bound64 w x let lemma_prod_bounds (dst_hi dst_lo x y:nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1)) = let result = x * y in lemma_div_mod result pow2_64; //assert (result = pow2_64 * (result / pow2_64) + result % pow2_64); //assert (result % pow2_64 == dst_lo); //assert (result / pow2_64 == dst_hi); lemma_mul_bound64 x y let lemma_double_bound (x:nat64) : Lemma (add_wrap x x < pow2_64 - 1) = () type bit = b:nat { b <= 1 } let lemma_offset_sum (a_agg:nat) (a0 a1 a2 a3 a4:nat64) (b_agg:nat) (b0 b1 b2 b3 b4:nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4) = let lhs = a_agg + pow2_64 * b_agg in let rhs = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 in assert_by_tactic (lhs == rhs) int_canon #push-options "--z3rlimit 60" let lemma_partial_sum (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5 c:nat64) : Lemma (requires (let s1', c1 = add_carry a1 b0 0 in let s2', c2 = add_carry a2 b1 c1 in let s3', c3 = add_carry a3 b2 c2 in let s4', c4 = add_carry a4 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c == c5)) (ensures pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 = pow2_seven a0 s1 s2 s3 s4 s5 c) = () let lemma_partial_sum_a2b (a0 a1 a2 a3 a4 a5 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5:nat64) : Lemma (requires (let s1', c1 = add_carry a2 b0 0 in let s2', c2 = add_carry a3 b1 c1 in let s3', c3 = add_carry a4 b2 c2 in let s4', c4 = add_carry a5 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ 0 == c5)) (ensures pow2_seven a0 a1 (a2 + b0) (a3 + b1) (a4 + b2) (a5 + b3) b4 = pow2_seven a0 a1 s1 s2 s3 s4 s5) = () let lemma_partial_sum_a3b ( a0 a1 a2 a3 a4 a5 a6 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5:nat64) : Lemma (requires(let s1', c1 = add_carry a3 b0 0 in let s2', c2 = add_carry a4 b1 c1 in let s3', c3 = add_carry a5 b2 c2 in let s4', c4 = add_carry a6 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ 0 == c5)) (ensures pow2_eight a0 a1 a2 (a3 + b0) (a4 + b1) (a5 + b2) (a6 + b3) b4 = pow2_eight a0 a1 a2 s1 s2 s3 s4 s5) = () #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a0: Vale.Def.Types_s.nat64 -> a1: Vale.Def.Types_s.nat64 -> a0b: Prims.nat -> a0b_0: Vale.Def.Types_s.nat64 -> a0b_1: Vale.Def.Types_s.nat64 -> a0b_2: Vale.Def.Types_s.nat64 -> a0b_3: Vale.Def.Types_s.nat64 -> a0b_4: Vale.Def.Types_s.nat64 -> a1b: Prims.nat -> a1b_0: Vale.Def.Types_s.nat64 -> a1b_1: Vale.Def.Types_s.nat64 -> a1b_2: Vale.Def.Types_s.nat64 -> a1b_3: Vale.Def.Types_s.nat64 -> a1b_4: Vale.Def.Types_s.nat64 -> b: Prims.nat -> b0: Vale.Def.Types_s.nat64 -> b1: Vale.Def.Types_s.nat64 -> b2: Vale.Def.Types_s.nat64 -> b3: Vale.Def.Types_s.nat64 -> s1: Vale.Def.Types_s.nat64 -> s2: Vale.Def.Types_s.nat64 -> s3: Vale.Def.Types_s.nat64 -> s4: Vale.Def.Types_s.nat64 -> s5: Vale.Def.Types_s.nat64 -> c: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (requires a0b = Vale.Curve25519.Fast_defs.pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = Vale.Curve25519.Fast_defs.pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = Vale.Curve25519.Fast_defs.pow2_four b0 b1 b2 b3 /\ a0b = Vale.Curve25519.Fast_defs.mul_nats a0 b /\ a1b = Vale.Curve25519.Fast_defs.mul_nats a1 b /\ (let _ = Vale.Curve25519.Fast_defs.add_carry a0b_1 a1b_0 0 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1' c1 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0b_2 a1b_1 c1 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s2' c2 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0b_3 a1b_2 c2 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s3' c3 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0b_4 a1b_3 c3 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s4' c4 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry 0 a1b_4 c4 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s5' c5 = _ in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c) <: Prims.logical) <: Prims.logical) <: Prims.logical) <: Prims.logical) <: Prims.logical)) (ensures Vale.Curve25519.Fast_defs.pow2_two a0 a1 * b == Vale.Curve25519.Fast_defs.pow2_seven a0b_0 s1 s2 s3 s4 s5 c)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Def.Types_s.nat64", "Prims.nat", "Prims.unit", "Vale.Curve25519.FastMul_helpers.lemma_partial_sum", "Prims._assert", "Prims.eq2", "Vale.Curve25519.Fast_defs.pow2_two", "Vale.Curve25519.Fast_defs.pow2_six", "Prims.op_Addition", "Vale.Curve25519.FastMul_helpers.lemma_offset_sum", "Vale.Curve25519.Fast_defs.mul_nats", "FStar.Tactics.Effect.assert_by_tactic", "Prims.int", "FStar.Mul.op_Star", "Vale.Curve25519.FastMul_helpers.int_canon", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Vale.Curve25519.Fast_defs.pow2_five", "Vale.Curve25519.Fast_defs.pow2_four", "Prims.op_BarBar", "Prims.l_or", "Prims.op_LessThan", "Vale.Def.Words_s.pow2_64", "Prims.logical", "FStar.Pervasives.Native.tuple2", "Vale.Def.Words_s.nat64", "Vale.Curve25519.Fast_defs.add_carry", "Prims.squash", "Vale.Curve25519.Fast_defs.pow2_seven", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_sum_a1b (a0 a1: nat64) (a0b: nat) (a0b_0 a0b_1 a0b_2 a0b_3 a0b_4: nat64) (a1b: nat) (a1b_0 a1b_1 a1b_2 a1b_3 a1b_4: nat64) (b: nat) (b0 b1 b2 b3 s1 s2 s3 s4 s5 c: nat64) : Lemma (requires a0b = pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0b = mul_nats a0 b /\ a1b = mul_nats a1 b /\ (let s1', c1 = add_carry a0b_1 a1b_0 0 in let s2', c2 = add_carry a0b_2 a1b_1 c1 in let s3', c3 = add_carry a0b_3 a1b_2 c2 in let s4', c4 = add_carry a0b_4 a1b_3 c3 in let s5', c5 = add_carry 0 a1b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c)) (ensures (pow2_two a0 a1) * b == pow2_seven a0b_0 s1 s2 s3 s4 s5 c) =
assert_by_tactic ((pow2_two a0 a1) * b == pow2_two (mul_nats a0 b) (mul_nats a1 b)) int_canon; assert (pow2_two (mul_nats a0 b) (mul_nats a1 b) == pow2_two a0b a1b); lemma_offset_sum a0b a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b a1b_0 a1b_1 a1b_2 a1b_3 a1b_4; assert (pow2_two a0b a1b == pow2_six a0b_0 (a0b_1 + a1b_0) (a0b_2 + a1b_1) (a0b_3 + a1b_2) (a0b_4 + a1b_3) a1b_4); lemma_partial_sum a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 s1 s2 s3 s4 s5 c; ()
false
Lib.ByteSequence.fst
Lib.ByteSequence.mask_select_lemma
val mask_select_lemma: #t:inttype{~(S128? t)} -> mask:int_t t SEC -> a:int_t t SEC -> b:int_t t SEC -> Lemma (requires v mask = 0 \/ v mask = v (ones t SEC)) (ensures mask_select mask a b == (if v mask = 0 then b else a))
val mask_select_lemma: #t:inttype{~(S128? t)} -> mask:int_t t SEC -> a:int_t t SEC -> b:int_t t SEC -> Lemma (requires v mask = 0 \/ v mask = v (ones t SEC)) (ensures mask_select mask a b == (if v mask = 0 then b else a))
let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 10, "end_line": 94, "start_col": 0, "start_line": 81 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
mask: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> a: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> b: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> FStar.Pervasives.Lemma (requires Lib.IntTypes.v mask = 0 \/ Lib.IntTypes.v mask = Lib.IntTypes.v (Lib.IntTypes.ones t Lib.IntTypes.SEC)) (ensures Lib.ByteSequence.mask_select mask a b == (match Lib.IntTypes.v mask = 0 with | true -> b | _ -> a))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_not", "Prims.b2t", "Lib.IntTypes.uu___is_S128", "Lib.IntTypes.int_t", "Lib.IntTypes.SEC", "Prims.op_Equality", "Prims.int", "Lib.IntTypes.v", "Prims.unit", "Prims._assert", "Lib.IntTypes.range_t", "Lib.IntTypes.logxor_lemma", "Prims.eq2", "Prims.bool", "Lib.IntTypes.op_Hat_Dot", "Lib.IntTypes.logand_lemma", "Lib.IntTypes.op_Amp_Dot" ]
[]
false
false
true
false
false
let mask_select_lemma #t mask a b =
let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^. b); if v mask = 0 then (assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); ()) else (assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); ())
false
Steel.Reference.fsti
Steel.Reference.read
val read (#a: Type0) (r: ref a) : Steel a (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> sel r h0 == sel r h1 /\ x == sel r h1)
val read (#a: Type0) (r: ref a) : Steel a (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> sel r h0 == sel r h1 /\ x == sel r h1)
let read (#a:Type0) (r:ref a) : Steel a (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> sel r h0 == sel r h1 /\ x == sel r h1) = readp r full_perm
{ "file_name": "lib/steel/Steel.Reference.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 19, "end_line": 298, "start_col": 0, "start_line": 294 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Reference open FStar.Ghost open Steel.FractionalPermission open Steel.Memory open Steel.Effect.Atomic open Steel.Effect module U32 = FStar.UInt32 module Mem = Steel.Memory /// The main user-facing Steel library. /// This library provides functions to operate on references to values in universe 0, such as uints. /// This library provides two versions, which can interoperate with each other. /// The first one uses the standard separation logic pts_to predicate, and has a non-informative selector /// The second one has a selector which returns the contents of the reference in memory, enabling /// to better separate reasoning about memory safety and functional correctness when handling references. /// An abstract datatype for references val ref ([@@@unused] a:Type0) : Type0 /// The null pointer [@@ noextract_to "krml"] val null (#a:Type0) : ref a /// Checking whether a pointer is null can be done in a decidable way [@@ noextract_to "krml"] val is_null (#a:Type0) (r:ref a) : (b:bool{b <==> r == null}) (** First version of references: Non-informative selector and standard pts_to predicate. All functions names here are postfixed with _pt (for points_to)**) /// The standard points to separation logic assertion, expressing that /// reference [r] is valid in memory, stores value [v], and that we have /// permission [p] on it. val pts_to_sl (#a:Type0) (r:ref a) (p:perm) (v:a) : slprop u#1 /// Lifting the standard points to predicate to vprop, with a non-informative selector. /// The permission [p] and the value [v] are annotated with the smt_fallback attribute, /// enabling SMT rewriting on them during frame inference [@@ __steel_reduce__] let pts_to (#a:Type0) (r:ref a) ([@@@smt_fallback] p:perm) ([@@@ smt_fallback] v:a) = to_vprop (pts_to_sl r p v) /// If two pts_to predicates on the same reference [r] are valid in the memory [m], /// then the two values [v0] and [v1] are identical val pts_to_ref_injective (#a: Type u#0) (r: ref a) (p0 p1:perm) (v0 v1:a) (m:mem) : Lemma (requires interp (pts_to_sl r p0 v0 `Mem.star` pts_to_sl r p1 v1) m) (ensures v0 == v1) /// A valid pts_to predicate implies that the pointer is not the null pointer val pts_to_not_null (#a:Type u#0) (x:ref a) (p:perm) (v:a) (m:mem) : Lemma (requires interp (pts_to_sl x p v) m) (ensures x =!= null) /// Exposing the is_witness_invariant from Steel.Memory for references with fractional permissions val pts_to_witinv (#a:Type) (r:ref a) (p:perm) : Lemma (is_witness_invariant (pts_to_sl r p)) /// A stateful version of the pts_to_ref_injective lemma above val pts_to_injective_eq (#a: Type) (#opened:inames) (#p0 #p1:perm) (#v0 #v1: erased a) (r: ref a) : SteelGhost unit opened (pts_to r p0 v0 `star` pts_to r p1 v1) (fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0) (requires fun _ -> True) (ensures fun _ _ _ -> v0 == v1) /// A permission is always no greater than one val pts_to_perm (#a: _) (#u: _) (#p: _) (#v: _) (r: ref a) : SteelGhost unit u (pts_to r p v) (fun _ -> pts_to r p v) (fun _ -> True) (fun _ _ _ -> p `lesser_equal_perm` full_perm) /// Allocates a reference with value [x]. We have full permission on the newly /// allocated reference. val alloc_pt (#a:Type) (x:a) : Steel (ref a) emp (fun r -> pts_to r full_perm x) (requires fun _ -> True) (ensures fun _ r _ -> not (is_null r)) /// Reads the value in reference [r], as long as it initially is valid val read_pt (#a:Type) (#p:perm) (#v:erased a) (r:ref a) : Steel a (pts_to r p v) (fun x -> pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == Ghost.reveal v) /// A variant of read, useful when an existentially quantified predicate /// depends on the value stored in the reference val read_refine_pt (#a:Type0) (#p:perm) (q:a -> vprop) (r:ref a) : SteelT a (h_exists (fun (v:a) -> pts_to r p v `star` q v)) (fun v -> pts_to r p v `star` q v) /// Writes value [x] in the reference [r], as long as we have full ownership of [r] val write_pt (#a:Type0) (#v:erased a) (r:ref a) (x:a) : SteelT unit (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) /// Frees reference [r], as long as we have full ownership of [r] val free_pt (#a:Type0) (#v:erased a) (r:ref a) : SteelT unit (pts_to r full_perm v) (fun _ -> emp) /// Splits the permission on reference [r] into two. /// This function is computationally irrelevant (it has effect SteelGhost) val share_gen_pt (#a:Type0) (#uses:_) (#p:perm) (#v: a) (r:ref a) (p1 p2: perm) : SteelGhost unit uses (pts_to r p v) (fun _ -> pts_to r p1 v `star` pts_to r p2 v) (fun _ -> p == p1 `sum_perm` p2) (fun _ _ _ -> True) val share_pt (#a:Type0) (#uses:_) (#p:perm) (#v:erased a) (r:ref a) : SteelGhostT unit uses (pts_to r p v) (fun _ -> pts_to r (half_perm p) v `star` pts_to r (half_perm p) v) /// Combines permissions on reference [r]. /// This function is computationally irrelevant (it has effect SteelGhost) val gather_pt (#a:Type0) (#uses:_) (#p0:perm) (#p1:perm) (#v0 #v1:erased a) (r:ref a) : SteelGhostT (_:unit{v0 == v1}) uses (pts_to r p0 v0 `star` pts_to r p1 v1) (fun _ -> pts_to r (sum_perm p0 p1) v0) /// Atomic operations, read, write, and cas /// /// These are not polymorphic and are allowed only for small types (e.g. word-sized) /// For now, exporting only for U32 val atomic_read_pt_u32 (#opened:_) (#p:perm) (#v:erased U32.t) (r:ref U32.t) : SteelAtomic U32.t opened (pts_to r p v) (fun x -> pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == Ghost.reveal v) val atomic_write_pt_u32 (#opened:_) (#v:erased U32.t) (r:ref U32.t) (x:U32.t) : SteelAtomicT unit opened (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) val cas_pt_u32 (#uses:inames) (r:ref U32.t) (v:Ghost.erased U32.t) (v_old:U32.t) (v_new:U32.t) : SteelAtomicT (b:bool{b <==> (Ghost.reveal v == v_old)}) uses (pts_to r full_perm v) (fun b -> if b then pts_to r full_perm v_new else pts_to r full_perm v) val cas_pt_bool (#uses:inames) (r:ref bool) (v:Ghost.erased bool) (v_old:bool) (v_new:bool) : SteelAtomicT (b:bool{b <==> (Ghost.reveal v == v_old)}) uses (pts_to r full_perm v) (fun b -> if b then pts_to r full_perm v_new else pts_to r full_perm v) (** Second version of references: The memory contents are available inside the selector, instead of as an index of the predicate **) /// An abstract separation logic predicate stating that reference [r] is valid in memory. val ptrp (#a:Type0) (r:ref a) ([@@@smt_fallback] p: perm) : slprop u#1 [@@ __steel_reduce__; __reduce__] unfold let ptr (#a:Type0) (r:ref a) : slprop u#1 = ptrp r full_perm /// A selector for references, returning the value of type [a] stored in memory val ptrp_sel (#a:Type0) (r:ref a) (p: perm) : selector a (ptrp r p) [@@ __steel_reduce__; __reduce__] unfold let ptr_sel (#a:Type0) (r:ref a) : selector a (ptr r) = ptrp_sel r full_perm /// Some lemmas to interoperate between the two versions of references val ptrp_sel_interp (#a:Type0) (r:ref a) (p: perm) (m:mem) : Lemma (requires interp (ptrp r p) m) (ensures interp (pts_to_sl r p (ptrp_sel r p m)) m) let ptr_sel_interp (#a:Type0) (r:ref a) (m:mem) : Lemma (requires interp (ptr r) m) (ensures interp (pts_to_sl r full_perm (ptr_sel r m)) m) = ptrp_sel_interp r full_perm m val intro_ptrp_interp (#a:Type0) (r:ref a) (p: perm) (v:erased a) (m:mem) : Lemma (requires interp (pts_to_sl r p v) m) (ensures interp (ptrp r p) m) let intro_ptr_interp (#a:Type0) (r:ref a) (v:erased a) (m:mem) : Lemma (requires interp (pts_to_sl r full_perm v) m) (ensures interp (ptr r) m) = intro_ptrp_interp r full_perm v m /// Combining the separation logic predicate and selector into a vprop [@@ __steel_reduce__] let vptr' #a r p : vprop' = {hp = ptrp r p; t = a; sel = ptrp_sel r p} [@@ __steel_reduce__] unfold let vptrp (#a: Type) (r: ref a) ([@@@smt_fallback] p: perm) = VUnit (vptr' r p) [@@ __steel_reduce__; __reduce__] unfold let vptr r = vptrp r full_perm /// A wrapper to access a reference selector more easily. /// Ensuring that the corresponding ptr vprop is in the context is done by /// calling a variant of the framing tactic, as defined in Steel.Effect.Common [@@ __steel_reduce__] let sel (#a:Type) (#p:vprop) (r:ref a) (h:rmem p{FStar.Tactics.with_tactic selector_tactic (can_be_split p (vptr r) /\ True)}) = h (vptr r) /// Moving from indexed pts_to assertions to selector-based vprops and back val intro_vptr (#a:Type) (#opened:inames) (r:ref a) (p: perm) (v:erased a) : SteelGhost unit opened (pts_to r p v) (fun _ -> vptrp r p) (requires fun _ -> True) (ensures fun _ _ h1 -> h1 (vptrp r p) == reveal v) val elim_vptr (#a:Type) (#opened:inames) (r:ref a) (p: perm) : SteelGhost (erased a) opened (vptrp r p) (fun v -> pts_to r p v) (requires fun _ -> True) (ensures fun h0 v _ -> reveal v == h0 (vptrp r p)) /// Allocates a reference with value [x]. val malloc (#a:Type0) (x:a) : Steel (ref a) emp (fun r -> vptr r) (requires fun _ -> True) (ensures fun _ r h1 -> sel r h1 == x /\ not (is_null r)) /// Frees a reference [r] val free (#a:Type0) (r:ref a) : Steel unit (vptr r) (fun _ -> emp) (requires fun _ -> True) (ensures fun _ _ _ -> True) /// Reads the current value of reference [r] val readp (#a:Type0) (r:ref a) (p: perm) : Steel a (vptrp r p) (fun _ -> vptrp r p) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (vptrp r p) == h1 (vptrp r p) /\ x == h1 (vptrp r p))
{ "checked_file": "/", "dependencies": [ "Steel.Memory.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "Steel.Reference.fsti" }
[ { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.Reference.ref a -> Steel.Effect.Steel a
Steel.Effect.Steel
[]
[]
[ "Steel.Reference.ref", "Steel.Reference.readp", "Steel.FractionalPermission.full_perm", "Steel.Reference.vptr", "Steel.Effect.Common.vprop", "Steel.Effect.Common.rmem", "Prims.l_True", "Prims.l_and", "Prims.eq2", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Steel.Reference.sel" ]
[]
false
true
false
false
false
let read (#a: Type0) (r: ref a) : Steel a (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> sel r h0 == sel r h1 /\ x == sel r h1) =
readp r full_perm
false
Lib.ByteSequence.fst
Lib.ByteSequence.seq_mask_select
val seq_mask_select: #t:inttype{~(S128? t)} -> #len:size_nat -> a:lseq (int_t t SEC) len -> b:lseq (int_t t SEC) len -> mask:int_t t SEC -> Pure (lseq (int_t t SEC) len) (requires v mask = 0 \/ v mask = v (ones t SEC)) (ensures fun res -> res == (if v mask = 0 then b else a))
val seq_mask_select: #t:inttype{~(S128? t)} -> #len:size_nat -> a:lseq (int_t t SEC) len -> b:lseq (int_t t SEC) len -> mask:int_t t SEC -> Pure (lseq (int_t t SEC) len) (requires v mask = 0 \/ v mask = v (ones t SEC)) (ensures fun res -> res == (if v mask = 0 then b else a))
let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 104, "start_col": 0, "start_line": 96 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len -> b: Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len -> mask: Lib.IntTypes.int_t t Lib.IntTypes.SEC -> Prims.Pure (Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len)
Prims.Pure
[]
[]
[ "Lib.IntTypes.inttype", "Prims.l_not", "Prims.b2t", "Lib.IntTypes.uu___is_S128", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.SEC", "Prims.unit", "Prims.op_Equality", "Prims.int", "Lib.IntTypes.v", "Lib.Sequence.eq_intro", "Prims.bool", "FStar.Classical.forall_intro", "Prims.nat", "Prims.op_LessThan", "Prims.eq2", "Lib.IntTypes.range_t", "Lib.Sequence.op_String_Access", "Prims.l_True", "Prims.squash", "Lib.IntTypes.range", "Lib.Sequence.index", "Prims.Nil", "FStar.Pervasives.pattern", "Lib.ByteSequence.mask_select_lemma", "Prims.l_Forall", "Prims.l_imp", "Lib.ByteSequence.mask_select", "Lib.Sequence.map2" ]
[]
false
false
false
false
false
let seq_mask_select #t #len a b mask =
let res = map2 (mask_select mask) a b in let lemma_aux (i: nat{i < len}) : Lemma (v res.[ i ] == (if v mask = 0 then v b.[ i ] else v a.[ i ])) = mask_select_lemma mask a.[ i ] b.[ i ] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_not_equal_slice
val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k)))
val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k)))
let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 92, "end_line": 20, "start_col": 0, "start_line": 19 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: FStar.Seq.Base.seq a -> b2: FStar.Seq.Base.seq a -> i: Prims.nat -> j: Prims.nat -> k: Prims.nat { i <= j /\ i <= k /\ j <= k /\ k <= FStar.Seq.Base.length b1 /\ k <= FStar.Seq.Base.length b2 } -> FStar.Pervasives.Lemma (requires ~(FStar.Seq.Base.equal (FStar.Seq.Base.slice b1 i j) (FStar.Seq.Base.slice b2 i j))) (ensures ~(FStar.Seq.Base.equal (FStar.Seq.Base.slice b1 i k) (FStar.Seq.Base.slice b2 i k)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "Prims._assert", "Prims.l_Forall", "Prims.op_LessThan", "Prims.op_Subtraction", "Prims.eq2", "FStar.Seq.Base.index", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.unit" ]
[]
false
false
true
false
false
let lemma_not_equal_slice #a b1 b2 i j k =
assert (forall (n: nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i))
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_not_equal_last
val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j)))
val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j)))
let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 29, "start_col": 0, "start_line": 27 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: FStar.Seq.Base.seq a -> b2: FStar.Seq.Base.seq a -> i: Prims.nat -> j: Prims.nat{i < j /\ j <= FStar.Seq.Base.length b1 /\ j <= FStar.Seq.Base.length b2} -> FStar.Pervasives.Lemma (requires ~(FStar.Seq.Base.index b1 (j - 1) == FStar.Seq.Base.index b2 (j - 1))) (ensures ~(FStar.Seq.Base.equal (FStar.Seq.Base.slice b1 i j) (FStar.Seq.Base.slice b2 i j)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims.l_and", "Prims.b2t", "Prims.op_LessThan", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "FStar.Seq.Base.lemma_index_slice", "Prims.op_Subtraction", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_not_equal_last #a b1 b2 i j =
Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1)
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_be
val nat_from_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> n:nat{n < pow2 (length b * bits t)}
val nat_from_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> n:nat{n < pow2 (length b * bits t)}
let nat_from_intseq_be = nat_from_intseq_be_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 122, "start_col": 0, "start_line": 122 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> n: Prims.nat{n < Prims.pow2 (Lib.Sequence.length b * Lib.IntTypes.bits t)}
Prims.Tot
[ "total" ]
[]
[ "Lib.ByteSequence.nat_from_intseq_be_" ]
[]
false
false
false
false
false
let nat_from_intseq_be =
nat_from_intseq_be_
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_le
val nat_from_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> n:nat{n < pow2 (length b * bits t)}
val nat_from_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> n:nat{n < pow2 (length b * bits t)}
let nat_from_intseq_le = nat_from_intseq_le_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 141, "start_col": 0, "start_line": 141 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> n: Prims.nat{n < Prims.pow2 (Lib.Sequence.length b * Lib.IntTypes.bits t)}
Prims.Tot
[ "total" ]
[]
[ "Lib.ByteSequence.nat_from_intseq_le_" ]
[]
false
false
false
false
false
let nat_from_intseq_le =
nat_from_intseq_le_
false
Vale.Curve25519.FastMul_helpers.fst
Vale.Curve25519.FastMul_helpers.lemma_sum_a3b
val lemma_sum_a3b (a0 a1 a2 a3:nat64) (a0a1a2b:nat) (a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6:nat64) (a3b:nat) (a3b_0 a3b_1 a3b_2 a3b_3 a3b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1a2b = pow2_seven a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 /\ a3b = pow2_five a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1a2b = mul_nats (pow2_three a0 a1 a2) b /\ a3b = mul_nats a3 b /\ (let s1', c1 = add_carry a0a1a2b_3 a3b_0 0 in let s2', c2 = add_carry a0a1a2b_4 a3b_1 c1 in let s3', c3 = add_carry a0a1a2b_5 a3b_2 c2 in let s4', c4 = add_carry a0a1a2b_6 a3b_3 c3 in let s5', c5 = add_carry 0 a3b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_four a0 a1 a2 a3) * b == pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 s1 s2 s3 s4 s5)
val lemma_sum_a3b (a0 a1 a2 a3:nat64) (a0a1a2b:nat) (a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6:nat64) (a3b:nat) (a3b_0 a3b_1 a3b_2 a3b_3 a3b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1a2b = pow2_seven a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 /\ a3b = pow2_five a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1a2b = mul_nats (pow2_three a0 a1 a2) b /\ a3b = mul_nats a3 b /\ (let s1', c1 = add_carry a0a1a2b_3 a3b_0 0 in let s2', c2 = add_carry a0a1a2b_4 a3b_1 c1 in let s3', c3 = add_carry a0a1a2b_5 a3b_2 c2 in let s4', c4 = add_carry a0a1a2b_6 a3b_3 c3 in let s5', c5 = add_carry 0 a3b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_four a0 a1 a2 a3) * b == pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 s1 s2 s3 s4 s5)
let lemma_sum_a3b (a0 a1 a2 a3:nat64) (a0a1a2b:nat) (a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6:nat64) (a3b:nat) (a3b_0 a3b_1 a3b_2 a3b_3 a3b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1a2b = pow2_seven a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 /\ a3b = pow2_five a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1a2b = mul_nats (pow2_three a0 a1 a2) b /\ a3b = mul_nats a3 b /\ (let s1', c1 = add_carry a0a1a2b_3 a3b_0 0 in let s2', c2 = add_carry a0a1a2b_4 a3b_1 c1 in let s3', c3 = add_carry a0a1a2b_5 a3b_2 c2 in let s4', c4 = add_carry a0a1a2b_6 a3b_3 c3 in let s5', c5 = add_carry 0 a3b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_four a0 a1 a2 a3) * b == pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 s1 s2 s3 s4 s5) = assert_by_tactic ( (pow2_four a0 a1 a2 a3) * b == mul_nats (pow2_three a0 a1 a2) b + pow2_192 * (mul_nats a3 b)) int_canon; assert ( mul_nats (pow2_three a0 a1 a2) b + pow2_192 * (mul_nats a3 b) == a0a1a2b + pow2_192 * a3b); assert_by_tactic ( a0a1a2b + pow2_192 * a3b == pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 (a0a1a2b_3 + a3b_0) (a0a1a2b_4 + a3b_1) (a0a1a2b_5 + a3b_2) (a0a1a2b_6 + a3b_3) a3b_4) int_canon; lemma_partial_sum_a3b a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 s1 s2 s3 s4 s5; ()
{ "file_name": "vale/code/crypto/ecc/curve25519/Vale.Curve25519.FastMul_helpers.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 4, "end_line": 257, "start_col": 0, "start_line": 218 }
module Vale.Curve25519.FastMul_helpers open Vale.Def.Words_s open Vale.Def.Types_s open FStar.Mul open FStar.Tactics open FStar.Tactics.CanonCommSemiring open Vale.Curve25519.Fast_defs open Vale.Curve25519.Fast_lemmas_internal open FStar.Math.Lemmas #reset-options "--using_facts_from '* -FStar.Tactics -FStar.Reflection'" #push-options "--z3rlimit 10" let lemma_mul_pow2_bound (b:nat{b > 1}) (x y:natN (pow2 b)) : Lemma (x * y < pow2 (2*b) - 1 /\ x * y <= pow2 (2*b) - 2*pow2(b) + 1) = lemma_mul_bounds_le x (pow2 b - 1) y (pow2 b -1); pow2_plus b b; assert ( (pow2 b - 1) * (pow2 b -1) = pow2 (2*b) - 2*pow2(b) + 1) #pop-options let lemma_mul_bound64 (x y:nat64) : Lemma (x * y < pow2_128 - 1 /\ x * y <= pow2_128 - 2*pow2_64 + 1) = assert_norm (pow2 64 == pow2_64); assert_norm (pow2 128 == pow2_128); lemma_mul_pow2_bound 64 x y (* Intel manual mentions this fact *) let lemma_intel_prod_sum_bound (w x y z:nat64) : Lemma (w * x + y + z < pow2_128) = lemma_mul_bound64 w x let lemma_prod_bounds (dst_hi dst_lo x y:nat64) : Lemma (requires pow2_64 * dst_hi + dst_lo == x * y) (ensures dst_hi < pow2_64 - 1 /\ (dst_hi < pow2_64 - 2 \/ dst_lo <= 1)) = let result = x * y in lemma_div_mod result pow2_64; //assert (result = pow2_64 * (result / pow2_64) + result % pow2_64); //assert (result % pow2_64 == dst_lo); //assert (result / pow2_64 == dst_hi); lemma_mul_bound64 x y let lemma_double_bound (x:nat64) : Lemma (add_wrap x x < pow2_64 - 1) = () type bit = b:nat { b <= 1 } let lemma_offset_sum (a_agg:nat) (a0 a1 a2 a3 a4:nat64) (b_agg:nat) (b0 b1 b2 b3 b4:nat64) : Lemma (requires a_agg = pow2_five a0 a1 a2 a3 a4 /\ b_agg = pow2_five b0 b1 b2 b3 b4) (ensures a_agg + pow2_64 * b_agg = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4) = let lhs = a_agg + pow2_64 * b_agg in let rhs = pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 in assert_by_tactic (lhs == rhs) int_canon #push-options "--z3rlimit 60" let lemma_partial_sum (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5 c:nat64) : Lemma (requires (let s1', c1 = add_carry a1 b0 0 in let s2', c2 = add_carry a2 b1 c1 in let s3', c3 = add_carry a3 b2 c2 in let s4', c4 = add_carry a4 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c == c5)) (ensures pow2_six a0 (a1 + b0) (a2 + b1) (a3 + b2) (a4 + b3) b4 = pow2_seven a0 s1 s2 s3 s4 s5 c) = () let lemma_partial_sum_a2b (a0 a1 a2 a3 a4 a5 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5:nat64) : Lemma (requires (let s1', c1 = add_carry a2 b0 0 in let s2', c2 = add_carry a3 b1 c1 in let s3', c3 = add_carry a4 b2 c2 in let s4', c4 = add_carry a5 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ 0 == c5)) (ensures pow2_seven a0 a1 (a2 + b0) (a3 + b1) (a4 + b2) (a5 + b3) b4 = pow2_seven a0 a1 s1 s2 s3 s4 s5) = () let lemma_partial_sum_a3b ( a0 a1 a2 a3 a4 a5 a6 b0 b1 b2 b3 b4 s1 s2 s3 s4 s5:nat64) : Lemma (requires(let s1', c1 = add_carry a3 b0 0 in let s2', c2 = add_carry a4 b1 c1 in let s3', c3 = add_carry a5 b2 c2 in let s4', c4 = add_carry a6 b3 c3 in let s5', c5 = add_carry 0 b4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ 0 == c5)) (ensures pow2_eight a0 a1 a2 (a3 + b0) (a4 + b1) (a5 + b2) (a6 + b3) b4 = pow2_eight a0 a1 a2 s1 s2 s3 s4 s5) = () #pop-options let lemma_sum_a1b (a0 a1:nat64) (a0b:nat) (a0b_0 a0b_1 a0b_2 a0b_3 a0b_4:nat64) (a1b:nat) (a1b_0 a1b_1 a1b_2 a1b_3 a1b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5 c:nat64) : Lemma (requires a0b = pow2_five a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 /\ a1b = pow2_five a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0b = mul_nats a0 b /\ a1b = mul_nats a1 b /\ (let s1', c1 = add_carry a0b_1 a1b_0 0 in let s2', c2 = add_carry a0b_2 a1b_1 c1 in let s3', c3 = add_carry a0b_3 a1b_2 c2 in let s4', c4 = add_carry a0b_4 a1b_3 c3 in let s5', c5 = add_carry 0 a1b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == c)) (ensures (pow2_two a0 a1) * b == pow2_seven a0b_0 s1 s2 s3 s4 s5 c) = assert_by_tactic ( (pow2_two a0 a1) * b == pow2_two (mul_nats a0 b) (mul_nats a1 b)) int_canon; assert ( pow2_two (mul_nats a0 b) (mul_nats a1 b) == pow2_two a0b a1b); lemma_offset_sum a0b a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b a1b_0 a1b_1 a1b_2 a1b_3 a1b_4; assert ( pow2_two a0b a1b == pow2_six a0b_0 (a0b_1 + a1b_0) (a0b_2 + a1b_1) (a0b_3 + a1b_2) (a0b_4 + a1b_3) a1b_4); lemma_partial_sum a0b_0 a0b_1 a0b_2 a0b_3 a0b_4 a1b_0 a1b_1 a1b_2 a1b_3 a1b_4 s1 s2 s3 s4 s5 c; () #push-options "--z3rlimit 60" let lemma_sum_a2b (a0 a1 a2:nat64) (a0a1b:nat) (a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5:nat64) (a2b:nat) (a2b_0 a2b_1 a2b_2 a2b_3 a2b_4:nat64) (b:nat) (b0 b1 b2 b3:nat64) (s1 s2 s3 s4 s5:nat64) : Lemma (requires a0a1b = pow2_six a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 /\ a2b = pow2_five a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1b = mul_nats (pow2_two a0 a1) b /\ a2b = mul_nats a2 b /\ (let s1', c1 = add_carry a0a1b_2 a2b_0 0 in let s2', c2 = add_carry a0a1b_3 a2b_1 c1 in let s3', c3 = add_carry a0a1b_4 a2b_2 c2 in let s4', c4 = add_carry a0a1b_5 a2b_3 c3 in let s5', c5 = add_carry 0 a2b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_three a0 a1 a2) * b == pow2_seven a0a1b_0 a0a1b_1 s1 s2 s3 s4 s5) = assert_by_tactic ( (pow2_three a0 a1 a2) * b == mul_nats (pow2_two a0 a1) b + pow2_128 * (mul_nats a2 b)) int_canon; assert ( mul_nats (pow2_two a0 a1) b + pow2_128 * (mul_nats a2 b) == a0a1b + pow2_128 * a2b); assert_by_tactic ( a0a1b + pow2_128 * a2b == pow2_seven a0a1b_0 a0a1b_1 (a0a1b_2 + a2b_0) (a0a1b_3 + a2b_1) (a0a1b_4 + a2b_2) (a0a1b_5 + a2b_3) a2b_4) int_canon; lemma_partial_sum_a2b a0a1b_0 a0a1b_1 a0a1b_2 a0a1b_3 a0a1b_4 a0a1b_5 a2b_0 a2b_1 a2b_2 a2b_3 a2b_4 s1 s2 s3 s4 s5; ()
{ "checked_file": "/", "dependencies": [ "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.Fast_lemmas_internal.fsti.checked", "Vale.Curve25519.Fast_defs.fst.checked", "prims.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.CanonCommSemiring.fst.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Vale.Curve25519.FastMul_helpers.fst" }
[ { "abbrev": false, "full_module": "FStar.Math.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_lemmas_internal", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519.Fast_defs", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.CanonCommSemiring", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "Vale.Curve25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 60, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a0: Vale.Def.Types_s.nat64 -> a1: Vale.Def.Types_s.nat64 -> a2: Vale.Def.Types_s.nat64 -> a3: Vale.Def.Types_s.nat64 -> a0a1a2b: Prims.nat -> a0a1a2b_0: Vale.Def.Types_s.nat64 -> a0a1a2b_1: Vale.Def.Types_s.nat64 -> a0a1a2b_2: Vale.Def.Types_s.nat64 -> a0a1a2b_3: Vale.Def.Types_s.nat64 -> a0a1a2b_4: Vale.Def.Types_s.nat64 -> a0a1a2b_5: Vale.Def.Types_s.nat64 -> a0a1a2b_6: Vale.Def.Types_s.nat64 -> a3b: Prims.nat -> a3b_0: Vale.Def.Types_s.nat64 -> a3b_1: Vale.Def.Types_s.nat64 -> a3b_2: Vale.Def.Types_s.nat64 -> a3b_3: Vale.Def.Types_s.nat64 -> a3b_4: Vale.Def.Types_s.nat64 -> b: Prims.nat -> b0: Vale.Def.Types_s.nat64 -> b1: Vale.Def.Types_s.nat64 -> b2: Vale.Def.Types_s.nat64 -> b3: Vale.Def.Types_s.nat64 -> s1: Vale.Def.Types_s.nat64 -> s2: Vale.Def.Types_s.nat64 -> s3: Vale.Def.Types_s.nat64 -> s4: Vale.Def.Types_s.nat64 -> s5: Vale.Def.Types_s.nat64 -> FStar.Pervasives.Lemma (requires a0a1a2b = Vale.Curve25519.Fast_defs.pow2_seven a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 /\ a3b = Vale.Curve25519.Fast_defs.pow2_five a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 /\ b = Vale.Curve25519.Fast_defs.pow2_four b0 b1 b2 b3 /\ a0a1a2b = Vale.Curve25519.Fast_defs.mul_nats (Vale.Curve25519.Fast_defs.pow2_three a0 a1 a2) b /\ a3b = Vale.Curve25519.Fast_defs.mul_nats a3 b /\ (let _ = Vale.Curve25519.Fast_defs.add_carry a0a1a2b_3 a3b_0 0 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1' c1 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0a1a2b_4 a3b_1 c1 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s2' c2 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0a1a2b_5 a3b_2 c2 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s3' c3 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry a0a1a2b_6 a3b_3 c3 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s4' c4 = _ in let _ = Vale.Curve25519.Fast_defs.add_carry 0 a3b_4 c4 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s5' c5 = _ in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0) <: Prims.logical) <: Prims.logical) <: Prims.logical) <: Prims.logical) <: Prims.logical)) (ensures Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 * b == Vale.Curve25519.Fast_defs.pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 s1 s2 s3 s4 s5)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Def.Types_s.nat64", "Prims.nat", "Prims.unit", "Vale.Curve25519.FastMul_helpers.lemma_partial_sum_a3b", "FStar.Tactics.Effect.assert_by_tactic", "Prims.eq2", "Prims.int", "Prims.op_Addition", "FStar.Mul.op_Star", "Vale.Curve25519.Fast_defs.pow2_192", "Vale.Curve25519.Fast_defs.pow2_eight", "Vale.Curve25519.FastMul_helpers.int_canon", "Prims._assert", "Vale.Curve25519.Fast_defs.mul_nats", "Vale.Curve25519.Fast_defs.pow2_three", "Vale.Curve25519.Fast_defs.pow2_four", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Vale.Curve25519.Fast_defs.pow2_seven", "Vale.Curve25519.Fast_defs.pow2_five", "Prims.op_BarBar", "Prims.logical", "FStar.Pervasives.Native.tuple2", "Vale.Def.Words_s.nat64", "Vale.Curve25519.Fast_defs.add_carry", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_sum_a3b (a0 a1 a2 a3: nat64) (a0a1a2b: nat) (a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6: nat64) (a3b: nat) (a3b_0 a3b_1 a3b_2 a3b_3 a3b_4: nat64) (b: nat) (b0 b1 b2 b3 s1 s2 s3 s4 s5: nat64) : Lemma (requires a0a1a2b = pow2_seven a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 /\ a3b = pow2_five a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 /\ b = pow2_four b0 b1 b2 b3 /\ a0a1a2b = mul_nats (pow2_three a0 a1 a2) b /\ a3b = mul_nats a3 b /\ (let s1', c1 = add_carry a0a1a2b_3 a3b_0 0 in let s2', c2 = add_carry a0a1a2b_4 a3b_1 c1 in let s3', c3 = add_carry a0a1a2b_5 a3b_2 c2 in let s4', c4 = add_carry a0a1a2b_6 a3b_3 c3 in let s5', c5 = add_carry 0 a3b_4 c4 in s1 == s1' /\ s2 == s2' /\ s3 == s3' /\ s4 == s4' /\ s5 == s5' /\ c5 == 0)) (ensures (pow2_four a0 a1 a2 a3) * b == pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 s1 s2 s3 s4 s5) =
assert_by_tactic ((pow2_four a0 a1 a2 a3) * b == mul_nats (pow2_three a0 a1 a2) b + pow2_192 * (mul_nats a3 b)) int_canon; assert (mul_nats (pow2_three a0 a1 a2) b + pow2_192 * (mul_nats a3 b) == a0a1a2b + pow2_192 * a3b); assert_by_tactic (a0a1a2b + pow2_192 * a3b == pow2_eight a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 (a0a1a2b_3 + a3b_0) (a0a1a2b_4 + a3b_1) (a0a1a2b_5 + a3b_2) (a0a1a2b_6 + a3b_3) a3b_4) int_canon; lemma_partial_sum_a3b a0a1a2b_0 a0a1a2b_1 a0a1a2b_2 a0a1a2b_3 a0a1a2b_4 a0a1a2b_5 a0a1a2b_6 a3b_0 a3b_1 a3b_2 a3b_3 a3b_4 s1 s2 s3 s4 s5; ()
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_to_intseq_be
val nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_be b}
val nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_be b}
let nat_to_intseq_be = nat_to_intseq_be_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 40, "end_line": 163, "start_col": 0, "start_line": 163 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) {Lib.Sequence.length b == len /\ n == Lib.ByteSequence.nat_from_intseq_be b}
Prims.Tot
[ "total" ]
[]
[ "Lib.ByteSequence.nat_to_intseq_be_" ]
[]
false
false
false
false
false
let nat_to_intseq_be =
nat_to_intseq_be_
false
Steel.Reference.fsti
Steel.Reference.ghost_read
val ghost_read (#a: Type0) (#opened: inames) (r: ghost_ref a) : SteelGhost (Ghost.erased a) opened (ghost_vptr r) (fun _ -> ghost_vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (ghost_vptr r) == h1 (ghost_vptr r) /\ Ghost.reveal x == h1 (ghost_vptr r) )
val ghost_read (#a: Type0) (#opened: inames) (r: ghost_ref a) : SteelGhost (Ghost.erased a) opened (ghost_vptr r) (fun _ -> ghost_vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (ghost_vptr r) == h1 (ghost_vptr r) /\ Ghost.reveal x == h1 (ghost_vptr r) )
let ghost_read (#a:Type0) (#opened:inames) (r:ghost_ref a) : SteelGhost (Ghost.erased a) opened (ghost_vptr r) (fun _ -> ghost_vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (ghost_vptr r) == h1 (ghost_vptr r) /\ Ghost.reveal x == h1 (ghost_vptr r)) = ghost_readp r full_perm
{ "file_name": "lib/steel/Steel.Reference.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 25, "end_line": 522, "start_col": 0, "start_line": 517 }
(* Copyright 2020 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.Reference open FStar.Ghost open Steel.FractionalPermission open Steel.Memory open Steel.Effect.Atomic open Steel.Effect module U32 = FStar.UInt32 module Mem = Steel.Memory /// The main user-facing Steel library. /// This library provides functions to operate on references to values in universe 0, such as uints. /// This library provides two versions, which can interoperate with each other. /// The first one uses the standard separation logic pts_to predicate, and has a non-informative selector /// The second one has a selector which returns the contents of the reference in memory, enabling /// to better separate reasoning about memory safety and functional correctness when handling references. /// An abstract datatype for references val ref ([@@@unused] a:Type0) : Type0 /// The null pointer [@@ noextract_to "krml"] val null (#a:Type0) : ref a /// Checking whether a pointer is null can be done in a decidable way [@@ noextract_to "krml"] val is_null (#a:Type0) (r:ref a) : (b:bool{b <==> r == null}) (** First version of references: Non-informative selector and standard pts_to predicate. All functions names here are postfixed with _pt (for points_to)**) /// The standard points to separation logic assertion, expressing that /// reference [r] is valid in memory, stores value [v], and that we have /// permission [p] on it. val pts_to_sl (#a:Type0) (r:ref a) (p:perm) (v:a) : slprop u#1 /// Lifting the standard points to predicate to vprop, with a non-informative selector. /// The permission [p] and the value [v] are annotated with the smt_fallback attribute, /// enabling SMT rewriting on them during frame inference [@@ __steel_reduce__] let pts_to (#a:Type0) (r:ref a) ([@@@smt_fallback] p:perm) ([@@@ smt_fallback] v:a) = to_vprop (pts_to_sl r p v) /// If two pts_to predicates on the same reference [r] are valid in the memory [m], /// then the two values [v0] and [v1] are identical val pts_to_ref_injective (#a: Type u#0) (r: ref a) (p0 p1:perm) (v0 v1:a) (m:mem) : Lemma (requires interp (pts_to_sl r p0 v0 `Mem.star` pts_to_sl r p1 v1) m) (ensures v0 == v1) /// A valid pts_to predicate implies that the pointer is not the null pointer val pts_to_not_null (#a:Type u#0) (x:ref a) (p:perm) (v:a) (m:mem) : Lemma (requires interp (pts_to_sl x p v) m) (ensures x =!= null) /// Exposing the is_witness_invariant from Steel.Memory for references with fractional permissions val pts_to_witinv (#a:Type) (r:ref a) (p:perm) : Lemma (is_witness_invariant (pts_to_sl r p)) /// A stateful version of the pts_to_ref_injective lemma above val pts_to_injective_eq (#a: Type) (#opened:inames) (#p0 #p1:perm) (#v0 #v1: erased a) (r: ref a) : SteelGhost unit opened (pts_to r p0 v0 `star` pts_to r p1 v1) (fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0) (requires fun _ -> True) (ensures fun _ _ _ -> v0 == v1) /// A permission is always no greater than one val pts_to_perm (#a: _) (#u: _) (#p: _) (#v: _) (r: ref a) : SteelGhost unit u (pts_to r p v) (fun _ -> pts_to r p v) (fun _ -> True) (fun _ _ _ -> p `lesser_equal_perm` full_perm) /// Allocates a reference with value [x]. We have full permission on the newly /// allocated reference. val alloc_pt (#a:Type) (x:a) : Steel (ref a) emp (fun r -> pts_to r full_perm x) (requires fun _ -> True) (ensures fun _ r _ -> not (is_null r)) /// Reads the value in reference [r], as long as it initially is valid val read_pt (#a:Type) (#p:perm) (#v:erased a) (r:ref a) : Steel a (pts_to r p v) (fun x -> pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == Ghost.reveal v) /// A variant of read, useful when an existentially quantified predicate /// depends on the value stored in the reference val read_refine_pt (#a:Type0) (#p:perm) (q:a -> vprop) (r:ref a) : SteelT a (h_exists (fun (v:a) -> pts_to r p v `star` q v)) (fun v -> pts_to r p v `star` q v) /// Writes value [x] in the reference [r], as long as we have full ownership of [r] val write_pt (#a:Type0) (#v:erased a) (r:ref a) (x:a) : SteelT unit (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) /// Frees reference [r], as long as we have full ownership of [r] val free_pt (#a:Type0) (#v:erased a) (r:ref a) : SteelT unit (pts_to r full_perm v) (fun _ -> emp) /// Splits the permission on reference [r] into two. /// This function is computationally irrelevant (it has effect SteelGhost) val share_gen_pt (#a:Type0) (#uses:_) (#p:perm) (#v: a) (r:ref a) (p1 p2: perm) : SteelGhost unit uses (pts_to r p v) (fun _ -> pts_to r p1 v `star` pts_to r p2 v) (fun _ -> p == p1 `sum_perm` p2) (fun _ _ _ -> True) val share_pt (#a:Type0) (#uses:_) (#p:perm) (#v:erased a) (r:ref a) : SteelGhostT unit uses (pts_to r p v) (fun _ -> pts_to r (half_perm p) v `star` pts_to r (half_perm p) v) /// Combines permissions on reference [r]. /// This function is computationally irrelevant (it has effect SteelGhost) val gather_pt (#a:Type0) (#uses:_) (#p0:perm) (#p1:perm) (#v0 #v1:erased a) (r:ref a) : SteelGhostT (_:unit{v0 == v1}) uses (pts_to r p0 v0 `star` pts_to r p1 v1) (fun _ -> pts_to r (sum_perm p0 p1) v0) /// Atomic operations, read, write, and cas /// /// These are not polymorphic and are allowed only for small types (e.g. word-sized) /// For now, exporting only for U32 val atomic_read_pt_u32 (#opened:_) (#p:perm) (#v:erased U32.t) (r:ref U32.t) : SteelAtomic U32.t opened (pts_to r p v) (fun x -> pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == Ghost.reveal v) val atomic_write_pt_u32 (#opened:_) (#v:erased U32.t) (r:ref U32.t) (x:U32.t) : SteelAtomicT unit opened (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) val cas_pt_u32 (#uses:inames) (r:ref U32.t) (v:Ghost.erased U32.t) (v_old:U32.t) (v_new:U32.t) : SteelAtomicT (b:bool{b <==> (Ghost.reveal v == v_old)}) uses (pts_to r full_perm v) (fun b -> if b then pts_to r full_perm v_new else pts_to r full_perm v) val cas_pt_bool (#uses:inames) (r:ref bool) (v:Ghost.erased bool) (v_old:bool) (v_new:bool) : SteelAtomicT (b:bool{b <==> (Ghost.reveal v == v_old)}) uses (pts_to r full_perm v) (fun b -> if b then pts_to r full_perm v_new else pts_to r full_perm v) (** Second version of references: The memory contents are available inside the selector, instead of as an index of the predicate **) /// An abstract separation logic predicate stating that reference [r] is valid in memory. val ptrp (#a:Type0) (r:ref a) ([@@@smt_fallback] p: perm) : slprop u#1 [@@ __steel_reduce__; __reduce__] unfold let ptr (#a:Type0) (r:ref a) : slprop u#1 = ptrp r full_perm /// A selector for references, returning the value of type [a] stored in memory val ptrp_sel (#a:Type0) (r:ref a) (p: perm) : selector a (ptrp r p) [@@ __steel_reduce__; __reduce__] unfold let ptr_sel (#a:Type0) (r:ref a) : selector a (ptr r) = ptrp_sel r full_perm /// Some lemmas to interoperate between the two versions of references val ptrp_sel_interp (#a:Type0) (r:ref a) (p: perm) (m:mem) : Lemma (requires interp (ptrp r p) m) (ensures interp (pts_to_sl r p (ptrp_sel r p m)) m) let ptr_sel_interp (#a:Type0) (r:ref a) (m:mem) : Lemma (requires interp (ptr r) m) (ensures interp (pts_to_sl r full_perm (ptr_sel r m)) m) = ptrp_sel_interp r full_perm m val intro_ptrp_interp (#a:Type0) (r:ref a) (p: perm) (v:erased a) (m:mem) : Lemma (requires interp (pts_to_sl r p v) m) (ensures interp (ptrp r p) m) let intro_ptr_interp (#a:Type0) (r:ref a) (v:erased a) (m:mem) : Lemma (requires interp (pts_to_sl r full_perm v) m) (ensures interp (ptr r) m) = intro_ptrp_interp r full_perm v m /// Combining the separation logic predicate and selector into a vprop [@@ __steel_reduce__] let vptr' #a r p : vprop' = {hp = ptrp r p; t = a; sel = ptrp_sel r p} [@@ __steel_reduce__] unfold let vptrp (#a: Type) (r: ref a) ([@@@smt_fallback] p: perm) = VUnit (vptr' r p) [@@ __steel_reduce__; __reduce__] unfold let vptr r = vptrp r full_perm /// A wrapper to access a reference selector more easily. /// Ensuring that the corresponding ptr vprop is in the context is done by /// calling a variant of the framing tactic, as defined in Steel.Effect.Common [@@ __steel_reduce__] let sel (#a:Type) (#p:vprop) (r:ref a) (h:rmem p{FStar.Tactics.with_tactic selector_tactic (can_be_split p (vptr r) /\ True)}) = h (vptr r) /// Moving from indexed pts_to assertions to selector-based vprops and back val intro_vptr (#a:Type) (#opened:inames) (r:ref a) (p: perm) (v:erased a) : SteelGhost unit opened (pts_to r p v) (fun _ -> vptrp r p) (requires fun _ -> True) (ensures fun _ _ h1 -> h1 (vptrp r p) == reveal v) val elim_vptr (#a:Type) (#opened:inames) (r:ref a) (p: perm) : SteelGhost (erased a) opened (vptrp r p) (fun v -> pts_to r p v) (requires fun _ -> True) (ensures fun h0 v _ -> reveal v == h0 (vptrp r p)) /// Allocates a reference with value [x]. val malloc (#a:Type0) (x:a) : Steel (ref a) emp (fun r -> vptr r) (requires fun _ -> True) (ensures fun _ r h1 -> sel r h1 == x /\ not (is_null r)) /// Frees a reference [r] val free (#a:Type0) (r:ref a) : Steel unit (vptr r) (fun _ -> emp) (requires fun _ -> True) (ensures fun _ _ _ -> True) /// Reads the current value of reference [r] val readp (#a:Type0) (r:ref a) (p: perm) : Steel a (vptrp r p) (fun _ -> vptrp r p) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (vptrp r p) == h1 (vptrp r p) /\ x == h1 (vptrp r p)) let read (#a:Type0) (r:ref a) : Steel a (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> sel r h0 == sel r h1 /\ x == sel r h1) = readp r full_perm /// Writes value [x] in reference [r] val write (#a:Type0) (r:ref a) (x:a) : Steel unit (vptr r) (fun _ -> vptr r) (requires fun _ -> True) (ensures fun _ _ h1 -> x == sel r h1) val share (#a:Type0) (#uses:_) (#p: perm) (r:ref a) : SteelGhost unit uses (vptrp r p) (fun _ -> vptrp r (half_perm p) `star` vptrp r (half_perm p)) (fun _ -> True) (fun h _ h' -> h' (vptrp r (half_perm p)) == h (vptrp r p) ) val gather_gen (#a:Type0) (#uses:_) (r:ref a) (p0:perm) (p1:perm) : SteelGhost perm uses (vptrp r p0 `star` vptrp r p1) (fun res -> vptrp r res) (fun _ -> True) (fun h res h' -> res == sum_perm p0 p1 /\ h' (vptrp r res) == h (vptrp r p0) /\ h' (vptrp r res) == h (vptrp r p1) ) val gather (#a: Type0) (#uses: _) (#p: perm) (r: ref a) : SteelGhost unit uses (vptrp r (half_perm p) `star` vptrp r (half_perm p)) (fun _ -> vptrp r p) (fun _ -> True) (fun h _ h' -> h' (vptrp r p) == h (vptrp r (half_perm p)) ) /// A stateful lemma variant of the pts_to_not_null lemma above. /// This stateful function is computationally irrelevant and does not modify memory val vptrp_not_null (#opened: _) (#a: Type) (r: ref a) (p: perm) : SteelGhost unit opened (vptrp r p) (fun _ -> vptrp r p) (fun _ -> True) (fun h0 _ h1 -> h0 (vptrp r p) == h1 (vptrp r p) /\ is_null r == false ) let vptr_not_null (#opened: _) (#a: Type) (r: ref a) : SteelGhost unit opened (vptr r) (fun _ -> vptr r) (fun _ -> True) (fun h0 _ h1 -> sel r h0 == sel r h1 /\ is_null r == false ) = vptrp_not_null r full_perm (*** Ghost references ***) /// We also define a ghost variant of references, useful to do proofs relying on a ghost state /// Ghost references are marked as erasable, ensuring that they are computationally irrelevant, /// and only used in computationally irrelevant contexts. /// The functions below are variants of the reference functions defined above, /// but operating on ghost references, and with the computationally irrelevant SteelGhost effect [@@ erasable] val ghost_ref (a:Type u#0) : Type u#0 val dummy_ghost_ref (a: Type) : Tot (ghost_ref a) (* Textbook separation logic version of ghost references *) val ghost_pts_to_sl (#a:_) (r:ghost_ref a) (p:perm) (v:a) : slprop u#1 [@@ __steel_reduce__] let ghost_pts_to (#a:Type0) (r:ghost_ref a) ([@@@smt_fallback] p:perm) ([@@@ smt_fallback] v:a) : vprop = to_vprop (ghost_pts_to_sl r p v) val ghost_pts_to_witinv (#a:Type) (r:ghost_ref a) (p:perm) : Lemma (is_witness_invariant (ghost_pts_to_sl r p)) val ghost_alloc_pt (#a:Type) (#u:_) (x:erased a) : SteelGhostT (ghost_ref a) u emp (fun r -> ghost_pts_to r full_perm x) val ghost_free_pt (#a:Type0) (#u:_) (#v:erased a) (r:ghost_ref a) : SteelGhostT unit u (ghost_pts_to r full_perm v) (fun _ -> emp) val ghost_share_gen_pt (#a:Type) (#u:_) (#p:perm) (#x:erased a) (r:ghost_ref a) (p1 p2: perm) : SteelGhost unit u (ghost_pts_to r p x) (fun _ -> ghost_pts_to r p1 x `star` ghost_pts_to r p2 x) (fun _ -> p == p1 `sum_perm` p2) (fun _ _ _ -> True) val ghost_share_pt (#a:Type) (#u:_) (#p:perm) (#x:erased a) (r:ghost_ref a) : SteelGhostT unit u (ghost_pts_to r p x) (fun _ -> ghost_pts_to r (half_perm p) x `star` ghost_pts_to r (half_perm p) x) val ghost_gather_pt (#a:Type) (#u:_) (#p0 #p1:perm) (#x0 #x1:erased a) (r:ghost_ref a) : SteelGhost unit u (ghost_pts_to r p0 x0 `star` ghost_pts_to r p1 x1) (fun _ -> ghost_pts_to r (sum_perm p0 p1) x0) (requires fun _ -> true) (ensures fun _ _ _ -> x0 == x1) val ghost_pts_to_injective_eq (#a:_) (#u:_) (#p #q:_) (r:ghost_ref a) (v0 v1:Ghost.erased a) : SteelGhost unit u (ghost_pts_to r p v0 `star` ghost_pts_to r q v1) (fun _ -> ghost_pts_to r p v0 `star` ghost_pts_to r q v0) (requires fun _ -> True) (ensures fun _ _ _ -> v0 == v1) /// A permission is always no greater than one val ghost_pts_to_perm (#a: _) (#u: _) (#p: _) (#v: _) (r: ghost_ref a) : SteelGhost unit u (ghost_pts_to r p v) (fun _ -> ghost_pts_to r p v) (fun _ -> True) (fun _ _ _ -> p `lesser_equal_perm` full_perm) val ghost_read_pt (#a:Type) (#u:_) (#p:perm) (#v:erased a) (r:ghost_ref a) : SteelGhost (erased a) u (ghost_pts_to r p v) (fun x -> ghost_pts_to r p x) (requires fun _ -> True) (ensures fun _ x _ -> x == v) val ghost_write_pt (#a:Type) (#u:_) (#v:erased a) (r:ghost_ref a) (x:erased a) : SteelGhostT unit u (ghost_pts_to r full_perm v) (fun _ -> ghost_pts_to r full_perm x) (* Selector version of ghost references *) val ghost_ptrp (#a: Type0) (r: ghost_ref a) ([@@@smt_fallback] p: perm) : slprop u#1 [@@ __steel_reduce__; __reduce__] unfold let ghost_ptr (#a: Type0) (r: ghost_ref a) : slprop u#1 = ghost_ptrp r full_perm val ghost_ptrp_sel (#a:Type0) (r:ghost_ref a) (p: perm) : selector a (ghost_ptrp r p) [@@ __steel_reduce__; __reduce__] let ghost_ptr_sel (#a:Type0) (r:ghost_ref a) : selector a (ghost_ptr r) = ghost_ptrp_sel r full_perm val ghost_ptrp_sel_interp (#a:Type0) (r:ghost_ref a) (p: perm) (m:mem) : Lemma (requires interp (ghost_ptrp r p) m) (ensures interp (ghost_pts_to_sl r p (ghost_ptrp_sel r p m)) m) let ghost_ptr_sel_interp (#a:Type0) (r:ghost_ref a) (m:mem) : Lemma (requires interp (ghost_ptr r) m) (ensures interp (ghost_pts_to_sl r full_perm (ghost_ptr_sel r m)) m) = ghost_ptrp_sel_interp r full_perm m [@@ __steel_reduce__] let ghost_vptr' #a r p : vprop' = {hp = ghost_ptrp r p; t = a; sel = ghost_ptrp_sel r p} [@@ __steel_reduce__] unfold let ghost_vptrp (#a: Type) (r: ghost_ref a) ([@@@smt_fallback] p: perm) = VUnit (ghost_vptr' r p) [@@ __steel_reduce__; __reduce__] unfold let ghost_vptr r = ghost_vptrp r full_perm [@@ __steel_reduce__] let ghost_sel (#a:Type) (#p:vprop) (r:ghost_ref a) (h:rmem p{FStar.Tactics.with_tactic selector_tactic (can_be_split p (ghost_vptr r) /\ True)}) = h (ghost_vptr r) val ghost_alloc (#a:Type0) (#opened:inames) (x:Ghost.erased a) : SteelGhost (ghost_ref a) opened emp (fun r -> ghost_vptr r) (requires fun _ -> True) (ensures fun _ r h1 -> ghost_sel r h1 == Ghost.reveal x) val ghost_free (#a:Type0) (#opened:inames) (r:ghost_ref a) : SteelGhost unit opened (ghost_vptr r) (fun _ -> emp) (requires fun _ -> True) (ensures fun _ _ _ -> True) val ghost_readp (#a:Type0) (#opened:inames) (r:ghost_ref a) (p: perm) : SteelGhost (Ghost.erased a) opened (ghost_vptrp r p) (fun _ -> ghost_vptrp r p) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (ghost_vptrp r p) == h1 (ghost_vptrp r p) /\ Ghost.reveal x == h1 (ghost_vptrp r p))
{ "checked_file": "/", "dependencies": [ "Steel.Memory.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "Steel.Reference.fsti" }
[ { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.Reference.ghost_ref a -> Steel.Effect.Atomic.SteelGhost (FStar.Ghost.erased a)
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Reference.ghost_ref", "Steel.Reference.ghost_readp", "Steel.FractionalPermission.full_perm", "FStar.Ghost.erased", "Steel.Reference.ghost_vptr", "Steel.Effect.Common.vprop", "Steel.Effect.Common.rmem", "Prims.l_True", "Prims.l_and", "Prims.eq2", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "FStar.Ghost.reveal" ]
[]
false
true
false
false
false
let ghost_read (#a: Type0) (#opened: inames) (r: ghost_ref a) : SteelGhost (Ghost.erased a) opened (ghost_vptr r) (fun _ -> ghost_vptr r) (requires fun _ -> True) (ensures fun h0 x h1 -> h0 (ghost_vptr r) == h1 (ghost_vptr r) /\ Ghost.reveal x == h1 (ghost_vptr r) ) =
ghost_readp r full_perm
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_to_intseq_le
val nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}
val nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}
let nat_to_intseq_le = nat_to_intseq_le_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 40, "end_line": 183, "start_col": 0, "start_line": 183 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) {Lib.Sequence.length b == len /\ n == Lib.ByteSequence.nat_from_intseq_le b}
Prims.Tot
[ "total" ]
[]
[ "Lib.ByteSequence.nat_to_intseq_le_" ]
[]
false
false
false
false
false
let nat_to_intseq_le =
nat_to_intseq_le_
false
Lib.ByteSequence.fst
Lib.ByteSequence.lbytes_eq
val lbytes_eq: #len:size_nat -> b1:lbytes len -> b2:lbytes len -> b:bool{b <==> b1 == b2}
val lbytes_eq: #len:size_nat -> b1:lbytes len -> b2:lbytes len -> b:bool{b <==> b1 == b2}
let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 37, "end_line": 74, "start_col": 0, "start_line": 72 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: Lib.ByteSequence.lbytes len -> b2: Lib.ByteSequence.lbytes len -> b: Prims.bool{b <==> b1 == b2}
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.size_nat", "Lib.ByteSequence.lbytes", "Prims.op_Equality", "FStar.UInt8.t", "Lib.RawIntTypes.u8_to_UInt8", "FStar.UInt8.__uint_to_t", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Prims.l_and", "Prims.l_imp", "Prims.eq2", "Lib.Sequence.lseq", "Prims.l_or", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.l_Forall", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.sub", "Prims.int", "Lib.IntTypes.range", "Lib.IntTypes.v", "Lib.IntTypes.ones", "Prims.l_not", "Lib.IntTypes.zeros", "Lib.ByteSequence.seq_eq_mask", "Prims.bool", "Prims.l_iff" ]
[]
false
false
false
false
false
let lbytes_eq #len b1 b2 =
let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_le_
val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b))
val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b))
let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 139, "start_col": 0, "start_line": 129 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> Prims.Tot (n: Prims.nat{n < Prims.pow2 (Lib.Sequence.length b * Lib.IntTypes.bits t)})
Prims.Tot
[ "total", "" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.Sequence.seq", "Lib.IntTypes.uint_t", "Prims.op_Equality", "Prims.int", "Prims.bool", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.unit", "FStar.Math.Lemmas.pow2_plus", "Lib.IntTypes.bits", "Prims.op_Subtraction", "Lib.IntTypes.range", "Lib.IntTypes.v", "FStar.Seq.Base.index", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.Sequence.length", "Lib.IntTypes.int_t", "Lib.ByteSequence.nat_from_intseq_le_", "FStar.Seq.Base.seq", "FStar.Seq.Base.slice", "Prims.pos" ]
[ "recursion" ]
false
false
false
false
false
let rec nat_from_intseq_le_ #t #l b =
let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); let n = l + shift * n' in n
false
Lib.ByteSequence.fst
Lib.ByteSequence.uint_to_bytes_le
val uint_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> uint_t t l -> lbytes_l l (numbytes t)
val uint_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> uint_t t l -> lbytes_l l (numbytes t)
let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 282, "start_col": 0, "start_line": 281 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Lib.IntTypes.uint_t t l -> Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t)
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.uint_t", "Lib.ByteSequence.nat_to_bytes_le", "Lib.IntTypes.numbytes", "Lib.IntTypes.v", "Lib.ByteSequence.lbytes_l" ]
[]
false
false
false
false
false
let uint_to_bytes_le #t #l n =
nat_to_bytes_le (numbytes t) (v n)
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_to_bytes_le_inner
val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t))
val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t))
let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i]
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 307, "start_col": 0, "start_line": 305 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.lseq (Lib.IntTypes.int_t t l) len -> i: Prims.nat{i < len} -> _: Prims.unit -> Prims.unit * Lib.Sequence.lseq (Lib.IntTypes.uint_t Lib.IntTypes.U8 l) (Lib.IntTypes.numbytes t)
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Prims.nat", "Prims.unit", "FStar.Pervasives.Native.Mktuple2", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.uint_to_bytes_le", "Lib.Sequence.op_String_Access", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
false
false
let uints_to_bytes_le_inner #t #l #len b i () =
let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[ i ]
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_to_bytes_be_inner
val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t))
val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t))
let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i]
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 331, "start_col": 0, "start_line": 329 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.lseq (Lib.IntTypes.int_t t l) len -> i: Prims.nat{i < len} -> _: Prims.unit -> Prims.unit * Lib.Sequence.lseq (Lib.IntTypes.uint_t Lib.IntTypes.U8 l) (Lib.IntTypes.numbytes t)
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Prims.nat", "Prims.unit", "FStar.Pervasives.Native.Mktuple2", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.uint_to_bytes_be", "Lib.Sequence.op_String_Access", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
false
false
let uints_to_bytes_be_inner #t #l #len b i () =
let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[ i ]
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_to_intseq_be_
val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len)
val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len)
let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 161, "start_col": 0, "start_line": 150 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> Prims.Tot (b: Lib.Sequence.seq (Lib.IntTypes.int_t t l) {Lib.Sequence.length b == len /\ n == Lib.ByteSequence.nat_from_intseq_be b})
Prims.Tot
[ "total", "" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.empty", "Lib.IntTypes.int_t", "Prims.bool", "Prims.unit", "FStar.Seq.Properties.append_slices", "Lib.Sequence.create", "FStar.Seq.Base.seq", "FStar.Seq.Base.append", "Lib.Sequence.seq", "Prims.l_and", "Prims.eq2", "Lib.Sequence.length", "Prims.l_or", "Prims.op_Multiply", "Lib.ByteSequence.nat_from_intseq_be", "Lib.ByteSequence.nat_to_intseq_be_", "FStar.Math.Lemmas.lemma_div_lt_nat", "Prims._assert", "Prims.pos", "Lib.IntTypes.modulus", "Prims.op_Division", "Lib.IntTypes.range", "Lib.IntTypes.v", "Prims.op_Modulus", "Lib.IntTypes.uint", "Prims.op_Subtraction", "Lib.IntTypes.uint_t" ]
[ "recursion" ]
false
false
false
false
false
let rec nat_to_intseq_be_ #t #l len n =
if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b
false
Lib.ByteSequence.fst
Lib.ByteSequence.uint_from_bytes_le
val uint_from_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> b:lbytes_l l (numbytes t) -> uint_t t l
val uint_from_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> b:lbytes_l l (numbytes t) -> uint_t t l
let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 295, "start_col": 0, "start_line": 293 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t) -> Lib.IntTypes.uint_t t l
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.ByteSequence.lbytes_l", "Lib.IntTypes.numbytes", "Lib.IntTypes.uint", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.Sequence.length", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.ByteSequence.nat_from_intseq_le", "Lib.IntTypes.uint_t" ]
[]
false
false
false
false
false
let uint_from_bytes_le #t #l b =
let n = nat_from_intseq_le #U8 b in uint #t #l n
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_be_
val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b))
val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b))
let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n'
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 18, "end_line": 120, "start_col": 0, "start_line": 111 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> Prims.Tot (n: Prims.nat{n < Prims.pow2 (Lib.Sequence.length b * Lib.IntTypes.bits t)})
Prims.Tot
[ "total", "" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.Sequence.seq", "Lib.IntTypes.uint_t", "Prims.op_Equality", "Prims.int", "Prims.bool", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.unit", "FStar.Math.Lemmas.pow2_plus", "Lib.IntTypes.bits", "Prims.op_Subtraction", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.Sequence.length", "Lib.IntTypes.int_t", "Lib.ByteSequence.nat_from_intseq_be_", "Prims.pos", "FStar.Seq.Base.seq", "FStar.Seq.Base.slice", "Lib.IntTypes.range", "Lib.IntTypes.v", "FStar.Seq.Base.index" ]
[ "recursion" ]
false
false
false
false
false
let rec nat_from_intseq_be_ #t #l b =
let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n'
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_uint_to_bytes_be
val index_uint_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> Lemma (forall (i:nat{i < numbytes t}). index (uint_to_bytes_be #t #l u) (numbytes t - i - 1) == uint #U8 #l (v u / pow2 (8 * i) % pow2 8))
val index_uint_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> Lemma (forall (i:nat{i < numbytes t}). index (uint_to_bytes_be #t #l u) (numbytes t - i - 1) == uint #U8 #l (v u / pow2 (8 * i) % pow2 8))
let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 75, "end_line": 291, "start_col": 0, "start_line": 290 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u390: Lib.IntTypes.uint_t t l -> FStar.Pervasives.Lemma (ensures forall (i: Prims.nat{i < Lib.IntTypes.numbytes t}). Lib.Sequence.index (Lib.ByteSequence.uint_to_bytes_be u390) (Lib.IntTypes.numbytes t - i - 1) == Lib.IntTypes.uint (Lib.IntTypes.v u390 / Prims.pow2 (8 * i) % Prims.pow2 8))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.uint_t", "FStar.Classical.forall_intro", "Prims.nat", "Prims.op_LessThan", "Lib.IntTypes.numbytes", "Prims.eq2", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "FStar.Seq.Base.index", "Lib.ByteSequence.nat_to_intseq_be", "Lib.IntTypes.v", "Prims.op_Subtraction", "Lib.IntTypes.uint", "Prims.op_Modulus", "Prims.op_Division", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Lib.ByteSequence.index_nat_to_intseq_be", "Prims.unit" ]
[]
false
false
true
false
false
let index_uint_to_bytes_be #t #l u =
Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u))
false
Lib.ByteSequence.fst
Lib.ByteSequence.uint_to_bytes_be
val uint_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> lbytes_l l (numbytes t)
val uint_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> lbytes_l l (numbytes t)
let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 288, "start_col": 0, "start_line": 287 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u378: Lib.IntTypes.uint_t t l -> Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t)
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.uint_t", "Lib.ByteSequence.nat_to_bytes_be", "Lib.IntTypes.numbytes", "Lib.IntTypes.v", "Lib.ByteSequence.lbytes_l" ]
[]
false
false
false
false
false
let uint_to_bytes_be #t #l n =
nat_to_bytes_be (numbytes t) (v n)
false
Lib.ByteSequence.fst
Lib.ByteSequence.uint_from_bytes_be
val uint_from_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> lbytes_l l (numbytes t) -> uint_t t l
val uint_from_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> lbytes_l l (numbytes t) -> uint_t t l
let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 299, "start_col": 0, "start_line": 297 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t) -> Lib.IntTypes.uint_t t l
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.ByteSequence.lbytes_l", "Lib.IntTypes.numbytes", "Lib.IntTypes.uint", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.Sequence.length", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.ByteSequence.nat_from_intseq_be", "Lib.IntTypes.uint_t" ]
[]
false
false
false
false
false
let uint_from_bytes_be #t #l b =
let n = nat_from_intseq_be #U8 b in uint #t #l n
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_to_intseq_le_
val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len)
val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len)
let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 181, "start_col": 0, "start_line": 170 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> Prims.Tot (b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) {Lib.Sequence.length b == len /\ n == Lib.ByteSequence.nat_from_intseq_le b})
Prims.Tot
[ "total", "" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.empty", "Lib.IntTypes.uint_t", "Prims.bool", "Prims.unit", "FStar.Seq.Properties.append_slices", "Lib.Sequence.create", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "FStar.Seq.Base.append", "Lib.Sequence.seq", "Prims.l_and", "Prims.eq2", "Lib.Sequence.length", "Prims.l_or", "Prims.op_Multiply", "Lib.ByteSequence.nat_from_intseq_le", "Lib.ByteSequence.nat_to_intseq_le_", "FStar.Math.Lemmas.lemma_div_lt_nat", "Prims._assert", "Prims.pos", "Lib.IntTypes.modulus", "Prims.op_Division", "Lib.IntTypes.range", "Lib.IntTypes.v", "Prims.op_Modulus", "Lib.IntTypes.uint", "Prims.op_Subtraction" ]
[ "recursion" ]
false
false
false
false
false
let rec nat_to_intseq_le_ #t #l len n =
if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_to_bytes_be
val uints_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (uint_t t l) len -> lbytes_l l (len * numbytes t)
val uints_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (uint_t t l) len -> lbytes_l l (len * numbytes t)
let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 337, "start_col": 0, "start_line": 333 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i]
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ul: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t)
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Lib.Sequence.seq", "Lib.IntTypes.U8", "Prims.eq2", "Prims.int", "Lib.Sequence.length", "Lib.ByteSequence.lbytes_l", "FStar.Pervasives.Native.tuple2", "Lib.IntTypes.int_t", "Prims.op_Multiply", "Lib.Sequence.generate_blocks", "Lib.ByteSequence.uints_to_bytes_be_inner", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.eqtype", "Prims.unit" ]
[]
false
false
false
false
false
let uints_to_bytes_be #t #l #len ul =
let a_spec (i: nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o
false
Lib.ByteSequence.fst
Lib.ByteSequence.seq_eq_mask
val seq_eq_mask: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> res:int_t t SEC{ (sub b1 0 len == sub b2 0 len ==> v res == v (ones t SEC)) /\ (sub b1 0 len =!= sub b2 0 len ==> v res == v (zeros t SEC))}
val seq_eq_mask: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> res:int_t t SEC{ (sub b1 0 len == sub b2 0 len ==> v res == v (ones t SEC)) /\ (sub b1 0 len =!= sub b2 0 len ==> v res == v (zeros t SEC))}
let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 16, "end_line": 70, "start_col": 0, "start_line": 64 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len1 -> b2: Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len2 -> len: Lib.IntTypes.size_nat{len <= len1 /\ len <= len2} -> res: Lib.IntTypes.int_t t Lib.IntTypes.SEC { (Lib.Sequence.sub b1 0 len == Lib.Sequence.sub b2 0 len ==> Lib.IntTypes.v res == Lib.IntTypes.v (Lib.IntTypes.ones t Lib.IntTypes.SEC)) /\ (~(Lib.Sequence.sub b1 0 len == Lib.Sequence.sub b2 0 len) ==> Lib.IntTypes.v res == Lib.IntTypes.v (Lib.IntTypes.zeros t Lib.IntTypes.SEC)) }
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_not", "Prims.b2t", "Lib.IntTypes.uu___is_S128", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.SEC", "Prims.l_and", "Prims.op_LessThanOrEqual", "Lib.LoopCombinators.repeati_inductive", "Prims.nat", "Prims.l_imp", "Prims.eq2", "Prims.l_or", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.l_Forall", "Prims.op_LessThan", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.sub", "Lib.IntTypes.range_t", "Lib.IntTypes.v", "Lib.IntTypes.ones", "Lib.IntTypes.zeros", "Lib.ByteSequence.seq_eq_mask_inner" ]
[]
false
false
false
false
false
let seq_eq_mask #t #len1 #len2 b1 b2 len =
repeati_inductive len (fun (i: nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC)
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_to_bytes_le
val uints_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (uint_t t l) len -> lbytes_l l (len * numbytes t)
val uints_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (uint_t t l) len -> lbytes_l l (len * numbytes t)
let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 313, "start_col": 0, "start_line": 309 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i]
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ul: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t)
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Lib.Sequence.seq", "Lib.IntTypes.U8", "Prims.eq2", "Prims.int", "Lib.Sequence.length", "Lib.ByteSequence.lbytes_l", "FStar.Pervasives.Native.tuple2", "Lib.IntTypes.int_t", "Prims.op_Multiply", "Lib.Sequence.generate_blocks", "Lib.ByteSequence.uints_to_bytes_le_inner", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.eqtype", "Prims.unit" ]
[]
false
false
false
false
false
let uints_to_bytes_le #t #l #len ul =
let a_spec (i: nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_uint_to_bytes_le
val index_uint_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> Lemma (forall (i:nat{i < numbytes t}). index (uint_to_bytes_le #t #l u) i == uint #U8 #l (v u / pow2 (8 * i) % pow2 8))
val index_uint_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> Lemma (forall (i:nat{i < numbytes t}). index (uint_to_bytes_le #t #l u) i == uint #U8 #l (v u / pow2 (8 * i) % pow2 8))
let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 75, "end_line": 285, "start_col": 0, "start_line": 284 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u369: Lib.IntTypes.uint_t t l -> FStar.Pervasives.Lemma (ensures forall (i: Prims.nat{i < Lib.IntTypes.numbytes t}). Lib.Sequence.index (Lib.ByteSequence.uint_to_bytes_le u369) i == Lib.IntTypes.uint (Lib.IntTypes.v u369 / Prims.pow2 (8 * i) % Prims.pow2 8))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.uint_t", "FStar.Classical.forall_intro", "Prims.nat", "Prims.op_LessThan", "Lib.IntTypes.numbytes", "Prims.eq2", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "FStar.Seq.Base.index", "Lib.ByteSequence.nat_to_intseq_le", "Lib.IntTypes.v", "Lib.IntTypes.uint", "Prims.op_Modulus", "Prims.op_Division", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Lib.ByteSequence.index_nat_to_intseq_le", "Prims.unit" ]
[]
false
false
true
false
false
let index_uint_to_bytes_le #t #l u =
Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u))
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le
val uints_from_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lbytes_l l (len * numbytes t) -> lseq (uint_t t l) len
val uints_from_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lbytes_l l (len * numbytes t) -> lseq (uint_t t l) len
let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 71, "end_line": 344, "start_col": 0, "start_line": 342 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Lib.Sequence.createi", "Lib.IntTypes.int_t", "Prims.nat", "Lib.ByteSequence.uint_from_bytes_le", "Lib.Sequence.sub", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.Sequence.lseq" ]
[]
false
false
false
false
false
let uints_from_bytes_le #t #l #len b =
Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_le_slice_lemma
val nat_from_intseq_le_slice_lemma: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (uint_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le b == nat_from_intseq_le (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le (slice b i len))
val nat_from_intseq_le_slice_lemma: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (uint_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le b == nat_from_intseq_le (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le (slice b i len))
let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 37, "end_line": 391, "start_col": 0, "start_line": 390 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> i: Prims.nat{i <= len} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_le b == Lib.ByteSequence.nat_from_intseq_le (Lib.Sequence.slice b 0 i) + Prims.pow2 (i * Lib.IntTypes.bits t) * Lib.ByteSequence.nat_from_intseq_le (Lib.Sequence.slice b i len))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Prims.nat", "Prims.op_LessThanOrEqual", "Lib.ByteSequence.nat_from_intseq_le_slice_lemma_", "Prims.unit" ]
[]
true
false
true
false
false
let nat_from_intseq_le_slice_lemma #t #l #len b i =
nat_from_intseq_le_slice_lemma_ b i
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_be
val uints_from_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lbytes_l l (len * numbytes t) -> lseq (uint_t t l) len
val uints_from_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lbytes_l l (len * numbytes t) -> lseq (uint_t t l) len
let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 71, "end_line": 350, "start_col": 0, "start_line": 348 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Lib.Sequence.createi", "Lib.IntTypes.int_t", "Prims.nat", "Lib.ByteSequence.uint_from_bytes_be", "Lib.Sequence.sub", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.Sequence.lseq" ]
[]
false
false
false
false
false
let uints_from_bytes_be #t #l #len b =
Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_slice_lemma_aux
val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b)
val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b)
let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 45, "end_line": 367, "start_col": 0, "start_line": 364 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.pos -> a: Prims.nat -> b: Prims.nat -> c: Prims.nat -> i: Prims.pos{i <= len} -> FStar.Pervasives.Lemma (ensures Prims.pow2 ((i - 1) * c) * (a + Prims.pow2 c * b) == Prims.pow2 ((i - 1) * c) * a + Prims.pow2 (i * c) * b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Math.Lemmas.pow2_plus", "FStar.Mul.op_Star", "Prims.op_Subtraction", "Prims.unit", "FStar.Math.Lemmas.paren_mul_right", "Prims.pow2", "FStar.Math.Lemmas.distributivity_add_right" ]
[]
true
false
true
false
false
let nat_from_intseq_slice_lemma_aux len a b c i =
FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c
false
Lib.ByteSequence.fst
Lib.ByteSequence.uint_at_index_le
val uint_at_index_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> idx:nat{idx < len} -> u:uint_t t l{u == (uints_from_bytes_le #t #l #len b).[idx]}
val uint_at_index_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> idx:nat{idx < len} -> u:uint_t t l{u == (uints_from_bytes_le #t #l #len b).[idx]}
let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 355, "start_col": 0, "start_line": 354 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> idx: Prims.nat{idx < len} -> u590: Lib.IntTypes.uint_t t l {u590 == (Lib.ByteSequence.uints_from_bytes_le b).[ idx ]}
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Lib.ByteSequence.uint_from_bytes_le", "Lib.Sequence.sub", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.eq2", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.uints_from_bytes_le" ]
[]
false
false
false
false
false
let uint_at_index_le #t #l #len b i =
uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_be_slice_lemma
val nat_from_intseq_be_slice_lemma: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (uint_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_be b == nat_from_intseq_be (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be (slice b 0 i))
val nat_from_intseq_be_slice_lemma: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (uint_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_be b == nat_from_intseq_be (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be (slice b 0 i))
let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 48, "end_line": 417, "start_col": 0, "start_line": 416 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> i: Prims.nat{i <= len} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_be b == Lib.ByteSequence.nat_from_intseq_be (Lib.Sequence.slice b i len) + Prims.pow2 ((len - i) * Lib.IntTypes.bits t) * Lib.ByteSequence.nat_from_intseq_be (Lib.Sequence.slice b 0 i))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Prims.nat", "Prims.op_LessThanOrEqual", "Lib.ByteSequence.nat_from_intseq_be_slice_lemma_", "Prims.unit" ]
[]
true
false
true
false
false
let nat_from_intseq_be_slice_lemma #t #l #len b i =
nat_from_intseq_be_slice_lemma_ #t #l #len b i
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_uints_to_bytes_le
val index_uints_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> ul:lseq (uint_t t l) len -> i:nat{i < len * numbytes t} -> Lemma (index (uints_to_bytes_le #t #l #len ul) i == (uint_to_bytes_le ul.[i / numbytes t]).[i % numbytes t])
val index_uints_to_bytes_le: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> ul:lseq (uint_t t l) len -> i:nat{i < len * numbytes t} -> Lemma (index (uints_to_bytes_le #t #l #len ul) i == (uint_to_bytes_le ul.[i / numbytes t]).[i % numbytes t])
let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 86, "end_line": 323, "start_col": 0, "start_line": 322 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ul: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> i: Prims.nat{i < len * Lib.IntTypes.numbytes t} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.index (Lib.ByteSequence.uints_to_bytes_le ul) i == (Lib.ByteSequence.uint_to_bytes_le ul.[ i / Lib.IntTypes.numbytes t ]).[ i % Lib.IntTypes.numbytes t ])
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Prims.nat", "Lib.Sequence.index_generate_blocks", "Lib.IntTypes.U8", "Lib.ByteSequence.uints_to_bytes_le_inner", "Prims.unit" ]
[]
true
false
true
false
false
let index_uints_to_bytes_le #t #l #len ul i =
index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_uints_to_bytes_be
val index_uints_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> ul:lseq (uint_t t l) len -> i:nat{i < len * numbytes t} -> Lemma (index (uints_to_bytes_be #t #l #len ul) i == (uint_to_bytes_be ul.[i / numbytes t]).[i % numbytes t])
val index_uints_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> ul:lseq (uint_t t l) len -> i:nat{i < len * numbytes t} -> Lemma (index (uints_to_bytes_be #t #l #len ul) i == (uint_to_bytes_be ul.[i / numbytes t]).[i % numbytes t])
let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 86, "end_line": 340, "start_col": 0, "start_line": 339 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ul: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> i: Prims.nat{i < len * Lib.IntTypes.numbytes t} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.index (Lib.ByteSequence.uints_to_bytes_be ul) i == (Lib.ByteSequence.uint_to_bytes_be ul.[ i / Lib.IntTypes.numbytes t ]).[ i % Lib.IntTypes.numbytes t ])
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Prims.nat", "Lib.Sequence.index_generate_blocks", "Lib.IntTypes.U8", "Lib.ByteSequence.uints_to_bytes_be_inner", "Prims.unit" ]
[]
true
false
true
false
false
let index_uints_to_bytes_be #t #l #len ul i =
index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i
false
Lib.ByteSequence.fst
Lib.ByteSequence.uint_at_index_be
val uint_at_index_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> idx:nat{idx < len} -> u:uint_t t l{u == (uints_from_bytes_be #t #l #len b).[idx]}
val uint_at_index_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> idx:nat{idx < len} -> u:uint_t t l{u == (uints_from_bytes_be #t #l #len b).[idx]}
let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 358, "start_col": 0, "start_line": 357 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> idx: Prims.nat{idx < len} -> u605: Lib.IntTypes.uint_t t l {u605 == (Lib.ByteSequence.uints_from_bytes_be b).[ idx ]}
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Lib.ByteSequence.uint_from_bytes_be", "Lib.Sequence.sub", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.eq2", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.uints_from_bytes_be" ]
[]
false
false
false
false
false
let uint_at_index_be #t #l #len b i =
uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le_nat_lemma
val uints_from_bytes_le_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le (uints_from_bytes_le #t #l #len b) == nat_from_bytes_le b)
val uints_from_bytes_le_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le (uints_from_bytes_le #t #l #len b) == nat_from_bytes_le b)
let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 45, "end_line": 480, "start_col": 0, "start_line": 479 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_le (Lib.ByteSequence.uints_from_bytes_le b) == Lib.ByteSequence.nat_from_bytes_le b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Lib.ByteSequence.uints_from_bytes_le_nat_lemma_", "Prims.unit" ]
[]
true
false
true
false
false
let uints_from_bytes_le_nat_lemma #t #l #len b =
uints_from_bytes_le_nat_lemma_ #t #l #len b
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le_slice_lemma_lp
val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 82, "end_line": 427, "start_col": 0, "start_line": 424 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> i: Prims.nat -> j: Prims.nat{i <= j /\ j <= len} -> k: Prims.nat{k < j - i} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.index (Lib.Sequence.slice (Lib.ByteSequence.uints_from_bytes_le b) i j) k == Lib.ByteSequence.uint_from_bytes_le (Lib.Sequence.sub b ((i + k) * Lib.IntTypes.numbytes t) (Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Subtraction", "Prims._assert", "Prims.eq2", "Lib.IntTypes.uint_t", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.uint_from_bytes_le", "Lib.Sequence.sub", "Lib.IntTypes.U8", "Prims.op_Addition", "Prims.unit", "Lib.ByteSequence.index_uints_from_bytes_le", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Lib.ByteSequence.uints_from_bytes_le", "Prims.l_Forall", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.slice" ]
[]
true
false
true
false
false
let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k =
let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[ k ] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_nat_to_intseq_be
val index_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_nat -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - i - 1) == uint #t #l (n / pow2 (bits t * i) % pow2 (bits t)))
val index_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_nat -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - i - 1) == uint #t #l (n / pow2 (bits t * i) % pow2 (bits t)))
let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 278, "start_col": 0, "start_line": 255 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Lib.IntTypes.size_nat -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> i: Prims.nat{i < len} -> FStar.Pervasives.Lemma (ensures FStar.Seq.Base.index (Lib.ByteSequence.nat_to_intseq_be len n) (len - i - 1) == Lib.IntTypes.uint (n / Prims.pow2 (Lib.IntTypes.bits t * i) % Prims.pow2 (Lib.IntTypes.bits t) ))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.op_Equality", "Prims.int", "Prims.bool", "Lib.ByteSequence.head_nat_to_intseq_be", "Prims.unit", "Lib.ByteSequence.nat_to_intseq_be_pos", "FStar.Calc.calc_finish", "Lib.IntTypes.uint_t", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.nat_to_intseq_be", "Prims.op_Subtraction", "Lib.IntTypes.uint", "Prims.op_Modulus", "Prims.op_Division", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "FStar.Calc.calc_step", "Prims.op_Addition", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Lib.ByteSequence.index_nat_to_intseq_be", "Prims.squash", "FStar.Math.Lemmas.division_multiplication_lemma", "FStar.Math.Lemmas.pow2_plus", "FStar.Math.Lemmas.distributivity_add_right", "FStar.Math.Lemmas.lemma_div_lt_nat" ]
[ "recursion" ]
false
false
true
false
false
let rec index_nat_to_intseq_be #t #l len n i =
if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc ( == ) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); ( == ) { index_nat_to_intseq_be #t #l len' n' i' } uint (n' / pow2 (bits t * i') % pow2 (bits t)); ( == ) { () } uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); ( == ) { Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i')) } uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); ( == ) { Math.Lemmas.pow2_plus (bits t) (bits t * i') } uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); ( == ) { Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1) } uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le_slice_lemma_rp
val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 82, "end_line": 439, "start_col": 0, "start_line": 434 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> i: Prims.nat -> j: Prims.nat{i <= j /\ j <= len} -> k: Prims.nat{k < j - i} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.index (Lib.ByteSequence.uints_from_bytes_le (Lib.Sequence.slice b (i * Lib.IntTypes.numbytes t) (j * Lib.IntTypes.numbytes t))) k == Lib.ByteSequence.uint_from_bytes_le (Lib.Sequence.sub b ((i + k) * Lib.IntTypes.numbytes t) (Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Subtraction", "Prims._assert", "Prims.eq2", "Lib.IntTypes.uint_t", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.uint_from_bytes_le", "Lib.Sequence.sub", "Lib.IntTypes.U8", "Prims.op_Addition", "Prims.unit", "Lib.ByteSequence.index_uints_from_bytes_le", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.ByteSequence.uints_from_bytes_le", "Prims.op_Multiply", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.l_Forall", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.slice" ]
[]
true
false
true
false
false
let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k =
let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j - i) b1 in index_uints_from_bytes_le #t #l #(j - i) b1 k; assert (r.[ k ] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[ k ] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_nat_from_to_intseq_be_preserves_value
val lemma_nat_from_to_intseq_be_preserves_value: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> b:seq (uint_t t l){length b == len} -> Lemma (nat_to_intseq_be len (nat_from_intseq_be b) == b)
val lemma_nat_from_to_intseq_be_preserves_value: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> b:seq (uint_t t l){length b == len} -> Lemma (nat_to_intseq_be len (nat_from_intseq_be b) == b)
let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 72, "end_line": 750, "start_col": 0, "start_line": 749 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat -> b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) {Lib.Sequence.length b == len} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_to_intseq_be len (Lib.ByteSequence.nat_from_intseq_be b) == b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Prims.nat", "Lib.Sequence.seq", "Lib.IntTypes.uint_t", "Prims.eq2", "Lib.Sequence.length", "Lib.ByteSequence.nat_from_intseq_be_inj", "Lib.ByteSequence.nat_to_intseq_be", "Lib.ByteSequence.nat_from_intseq_be", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_nat_from_to_intseq_be_preserves_value #t #l len b =
nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b
false
Lib.ByteSequence.fst
Lib.ByteSequence.seq_eq_mask_inner
val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))}
val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))}
let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 62, "start_col": 0, "start_line": 42 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len1 -> b2: Lib.Sequence.lseq (Lib.IntTypes.int_t t Lib.IntTypes.SEC) len2 -> len: Lib.IntTypes.size_nat{len <= len1 /\ len <= len2} -> i: Lib.IntTypes.size_nat{i < len} -> res: Lib.IntTypes.int_t t Lib.IntTypes.SEC { (Lib.Sequence.sub b1 0 i == Lib.Sequence.sub b2 0 i ==> Lib.IntTypes.v res == Lib.IntTypes.v (Lib.IntTypes.ones t Lib.IntTypes.SEC)) /\ (~(Lib.Sequence.sub b1 0 i == Lib.Sequence.sub b2 0 i) ==> Lib.IntTypes.v res == Lib.IntTypes.v (Lib.IntTypes.zeros t Lib.IntTypes.SEC)) } -> res': Lib.IntTypes.int_t t Lib.IntTypes.SEC { (Lib.Sequence.sub b1 0 (i + 1) == Lib.Sequence.sub b2 0 (i + 1) ==> Lib.IntTypes.v res' == Lib.IntTypes.v (Lib.IntTypes.ones t Lib.IntTypes.SEC)) /\ (~(Lib.Sequence.sub b1 0 (i + 1) == Lib.Sequence.sub b2 0 (i + 1)) ==> Lib.IntTypes.v res' == Lib.IntTypes.v (Lib.IntTypes.zeros t Lib.IntTypes.SEC)) }
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_not", "Prims.b2t", "Lib.IntTypes.uu___is_S128", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.SEC", "Prims.l_and", "Prims.op_LessThanOrEqual", "Prims.op_LessThan", "Prims.l_imp", "Prims.eq2", "Prims.l_or", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.l_Forall", "Prims.nat", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.sub", "Lib.IntTypes.range_t", "Lib.IntTypes.v", "Lib.IntTypes.ones", "Lib.IntTypes.zeros", "Prims.unit", "Prims.op_Equality", "Prims.int", "Lib.IntTypes.ones_v", "Prims._assert", "Lib.Sequence.equal", "FStar.Seq.Properties.lemma_split", "Prims.bool", "Lib.ByteSequence.lemma_not_equal_slice", "Lib.ByteSequence.lemma_not_equal_last", "Lib.IntTypes.logand_spec", "Lib.IntTypes.eq_mask", "Lib.Sequence.op_String_Access", "Lib.IntTypes.op_Amp_Dot", "Lib.IntTypes.range", "Lib.IntTypes.logand_ones", "Lib.IntTypes.logand_zeros" ]
[]
false
false
false
false
false
let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res =
logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[ i ] b2.[ i ] &. z0 in logand_spec (eq_mask b1.[ i ] b2.[ i ]) z0; if v res = ones_v t then let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_uint_to_from_bytes_le_preserves_value
val lemma_uint_to_from_bytes_le_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> i : uint_t t l -> Lemma(uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i) == i)
val lemma_uint_to_from_bytes_le_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> i : uint_t t l -> Lemma(uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i) == i)
let lemma_uint_to_from_bytes_le_preserves_value #t #l i = lemma_reveal_uint_to_bytes_le #t #l (uint_to_bytes_le #t #l i); assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v (uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i))); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 767, "start_col": 0, "start_line": 762 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len b let lemma_reveal_uint_to_bytes_le #t #l b = () let lemma_reveal_uint_to_bytes_be #t #l b = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i: Lib.IntTypes.uint_t t l -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.uint_from_bytes_le (Lib.ByteSequence.uint_to_bytes_le i) == i)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.uint_t", "Prims._assert", "Prims.eq2", "Prims.int", "Prims.l_or", "Lib.IntTypes.range", "Prims.op_GreaterThanOrEqual", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.Sequence.length", "Lib.IntTypes.U8", "Lib.ByteSequence.uint_to_bytes_le", "Lib.ByteSequence.nat_from_bytes_le", "Lib.IntTypes.uint_v", "Prims.unit", "Lib.ByteSequence.lemma_uint_to_bytes_le_preserves_value", "Lib.ByteSequence.uint_from_bytes_le", "Lib.ByteSequence.lemma_reveal_uint_to_bytes_le" ]
[]
true
false
true
false
false
let lemma_uint_to_from_bytes_le_preserves_value #t #l i =
lemma_reveal_uint_to_bytes_le #t #l (uint_to_bytes_le #t #l i); assert (nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v (uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i))); lemma_uint_to_bytes_le_preserves_value #t #l i; assert (nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.to_felem
val to_felem (f:G.field) (p:poly) : G.felem f
val to_felem (f:G.field) (p:poly) : G.felem f
let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n))
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 69, "end_line": 22, "start_col": 0, "start_line": 18 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> p: Vale.Math.Poly2_s.poly -> Spec.GaloisField.felem f
Prims.Tot
[ "total" ]
[]
[ "Spec.GaloisField.field", "Vale.Math.Poly2_s.poly", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Lib.IntTypes.Compatibility.nat_to_uint", "Spec.GaloisField.__proj__GF__item__t", "FStar.UInt.from_vec", "Vale.Math.Poly2_s.to_seq", "Vale.Math.Poly2_s.reverse", "Prims.op_Subtraction", "Prims.int", "Lib.IntTypes.bits", "Spec.GaloisField.felem" ]
[]
false
false
false
false
false
let to_felem f p =
let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n))
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_uints_to_bytes_le_aux
val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t))
val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t))
let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 567, "start_col": 0, "start_line": 563 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat{len * Lib.IntTypes.numbytes t < Prims.pow2 32} -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> i: Prims.nat{i < len * Lib.IntTypes.numbytes t} -> FStar.Pervasives.Lemma (ensures (let s = Lib.ByteSequence.nat_to_intseq_le len n in FStar.Seq.Base.index (Lib.ByteSequence.uints_to_bytes_le s) i == FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_le (Lib.IntTypes.numbytes t) (Lib.IntTypes.v s.[ i / Lib.IntTypes.numbytes t ])) (i % Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.IntTypes.bits", "Lib.Sequence.index_generate_blocks", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.uints_to_bytes_le_inner", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.ByteSequence.nat_to_intseq_le", "Prims.unit" ]
[]
true
false
true
false
false
let index_uints_to_bytes_le_aux #t #l len n i =
let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_be_slice_lemma_lp
val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 82, "end_line": 491, "start_col": 0, "start_line": 488 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> i: Prims.nat -> j: Prims.nat{i <= j /\ j <= len} -> k: Prims.nat{k < j - i} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.index (Lib.Sequence.slice (Lib.ByteSequence.uints_from_bytes_le b) i j) k == Lib.ByteSequence.uint_from_bytes_le (Lib.Sequence.sub b ((i + k) * Lib.IntTypes.numbytes t) (Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Subtraction", "Prims._assert", "Prims.eq2", "Lib.IntTypes.uint_t", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.uint_from_bytes_le", "Lib.Sequence.sub", "Lib.IntTypes.U8", "Prims.op_Addition", "Prims.unit", "Lib.ByteSequence.index_uints_from_bytes_be", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Lib.ByteSequence.uints_from_bytes_le", "Prims.l_Forall", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.slice" ]
[]
true
false
true
false
false
let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k =
let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[ k ] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_uint_to_from_bytes_be_preserves_value
val lemma_uint_to_from_bytes_be_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> i : uint_t t l -> Lemma(uint_from_bytes_be #t #l (uint_to_bytes_be #t #l i) == i)
val lemma_uint_to_from_bytes_be_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> i : uint_t t l -> Lemma(uint_from_bytes_be #t #l (uint_to_bytes_be #t #l i) == i)
let lemma_uint_to_from_bytes_be_preserves_value #t #l i = lemma_reveal_uint_to_bytes_be #t #l (uint_to_bytes_be #t #l i); lemma_uint_to_bytes_be_preserves_value #t #l i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 48, "end_line": 771, "start_col": 0, "start_line": 769 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len b let lemma_reveal_uint_to_bytes_le #t #l b = () let lemma_reveal_uint_to_bytes_be #t #l b = () let lemma_uint_to_from_bytes_le_preserves_value #t #l i = lemma_reveal_uint_to_bytes_le #t #l (uint_to_bytes_le #t #l i); assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v (uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i))); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i: Lib.IntTypes.uint_t t l -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.uint_from_bytes_be (Lib.ByteSequence.uint_to_bytes_be i) == i)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.uint_t", "Lib.ByteSequence.lemma_uint_to_bytes_be_preserves_value", "Prims.unit", "Lib.ByteSequence.lemma_reveal_uint_to_bytes_be", "Lib.ByteSequence.uint_to_bytes_be" ]
[]
true
false
true
false
false
let lemma_uint_to_from_bytes_be_preserves_value #t #l i =
lemma_reveal_uint_to_bytes_be #t #l (uint_to_bytes_be #t #l i); lemma_uint_to_bytes_be_preserves_value #t #l i
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_nat_from_to_intseq_le_preserves_value
val lemma_nat_from_to_intseq_le_preserves_value: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> b:seq (uint_t t l){length b == len} -> Lemma (nat_to_intseq_le len (nat_from_intseq_le b) == b)
val lemma_nat_from_to_intseq_le_preserves_value: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> b:seq (uint_t t l){length b == len} -> Lemma (nat_to_intseq_le len (nat_from_intseq_le b) == b)
let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 72, "end_line": 747, "start_col": 0, "start_line": 746 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat -> b: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) {Lib.Sequence.length b == len} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_to_intseq_le len (Lib.ByteSequence.nat_from_intseq_le b) == b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Prims.nat", "Lib.Sequence.seq", "Lib.IntTypes.uint_t", "Prims.eq2", "Lib.Sequence.length", "Lib.ByteSequence.nat_from_intseq_le_inj", "Lib.ByteSequence.nat_to_intseq_le", "Lib.ByteSequence.nat_from_intseq_le", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_nat_from_to_intseq_le_preserves_value #t #l len b =
nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.to_poly
val to_poly (#f:G.field) (e:G.felem f) : poly
val to_poly (#f:G.field) (e:G.felem f) : poly
let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 48, "end_line": 16, "start_col": 0, "start_line": 13 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0"
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
e: Spec.GaloisField.felem f -> Vale.Math.Poly2_s.poly
Prims.Tot
[ "total" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2_s.reverse", "Vale.Math.Poly2_s.of_seq", "FStar.UInt.to_vec", "Lib.IntTypes.v", "Spec.GaloisField.__proj__GF__item__t", "Prims.op_Subtraction", "Prims.int", "Lib.IntTypes.bits", "Vale.Math.Poly2_s.poly" ]
[]
false
false
false
false
false
let to_poly #f e =
let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1)
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_be_nat_lemma
val uints_from_bytes_be_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be (uints_from_bytes_be #t #l #len b) == nat_from_bytes_be b)
val uints_from_bytes_be_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be (uints_from_bytes_be #t #l #len b) == nat_from_bytes_be b)
let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 45, "end_line": 550, "start_col": 0, "start_line": 549 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_be (Lib.ByteSequence.uints_from_bytes_be b) == Lib.ByteSequence.nat_from_bytes_be b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Lib.ByteSequence.uints_from_bytes_be_nat_lemma_", "Prims.unit" ]
[]
true
false
true
false
false
let uints_from_bytes_be_nat_lemma #t #l #len b =
uints_from_bytes_be_nat_lemma_ #t #l #len b
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_uints_to_bytes_be_aux
val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t))
val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t))
let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 583, "start_col": 0, "start_line": 579 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat{len * Lib.IntTypes.numbytes t < Prims.pow2 32} -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> i: Prims.nat{i < len * Lib.IntTypes.numbytes t} -> FStar.Pervasives.Lemma (ensures (let s = Lib.ByteSequence.nat_to_intseq_be len n in FStar.Seq.Base.index (Lib.ByteSequence.uints_to_bytes_be s) i == FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_be (Lib.IntTypes.numbytes t) (Lib.IntTypes.v s.[ i / Lib.IntTypes.numbytes t ])) (i % Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.IntTypes.bits", "Lib.Sequence.index_generate_blocks", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.uints_to_bytes_be_inner", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.ByteSequence.nat_to_intseq_be", "Prims.unit" ]
[]
true
false
true
false
false
let index_uints_to_bytes_be_aux #t #l len n i =
let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le_nat_lemma_
val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b)
val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b)
let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 477, "start_col": 0, "start_line": 468 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_le_ (Lib.ByteSequence.uints_from_bytes_le b) == Lib.ByteSequence.nat_from_intseq_le_ b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.op_Equality", "Prims.int", "Prims.bool", "Lib.ByteSequence.uints_from_bytes_le_nat_lemma_aux", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.nat", "Prims.l_or", "Lib.Sequence.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.bits", "Lib.ByteSequence.uints_from_bytes_le", "Prims.op_Subtraction", "Lib.ByteSequence.nat_from_intseq_le_", "Lib.ByteSequence.uints_from_bytes_le_nat_lemma_", "Prims.op_Addition", "Lib.Sequence.slice", "Lib.ByteSequence.nat_from_intseq_le_slice_lemma_", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Prims.op_Multiply", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.l_Forall", "FStar.Seq.Base.index", "Lib.Sequence.index" ]
[ "recursion" ]
false
false
true
false
false
let rec uints_from_bytes_le_nat_lemma_ #t #l #len b =
if len = 0 then () else let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_nat_from_to_bytes_le_preserves_value
val lemma_nat_from_to_bytes_le_preserves_value: #l:secrecy_level -> b:bytes_l l -> len:size_nat{len == Lib.Sequence.length b} -> Lemma (nat_to_bytes_le #l len (nat_from_bytes_le b) == b)
val lemma_nat_from_to_bytes_le_preserves_value: #l:secrecy_level -> b:bytes_l l -> len:size_nat{len == Lib.Sequence.length b} -> Lemma (nat_to_bytes_le #l len (nat_from_bytes_le b) == b)
let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 753, "start_col": 0, "start_line": 752 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.bytes_l l -> len: Lib.IntTypes.size_nat{len == Lib.Sequence.length b} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_to_bytes_le len (Lib.ByteSequence.nat_from_bytes_le b) == b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.secrecy_level", "Lib.ByteSequence.bytes_l", "Lib.IntTypes.size_nat", "Prims.eq2", "Prims.nat", "Lib.Sequence.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.lemma_nat_from_to_intseq_le_preserves_value", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_nat_from_to_bytes_le_preserves_value #l b len =
lemma_nat_from_to_intseq_le_preserves_value len b
false
Spec.SHA3.Incremental.fst
Spec.SHA3.Incremental.sha3_is_incremental1
val sha3_is_incremental1 (a: keccak_alg) (input: bytes) (out_length: output_length a): Lemma (hash_incremental a input out_length `S.equal` ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last rateInBytes input in let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length))
val sha3_is_incremental1 (a: keccak_alg) (input: bytes) (out_length: output_length a): Lemma (hash_incremental a input out_length `S.equal` ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last rateInBytes input in let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length))
let sha3_is_incremental1 a input out_length = calc (==) { hash_incremental a input out_length; (==) { } (let s = init a in let bs, l = split_blocks a input in let s = update_multi a s () bs in let s = update_last a s () l in finish a s out_length); (==) { } (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a/8 in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in let s = update_multi a s () bs in let s = update_last a s () l in finish a s out_length); (==) { } ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () bs in let s = Spec.SHA3.absorb_inner rateInBytes l s in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length ); (==) { ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () bs in update_is_update_multi a l s else () ) } ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () bs in let s = update_multi a s () l in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length ); (==) { ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then Lib.Sequence.Lemmas.repeat_blocks_multi_split (block_length a) (S.length bs) (bs `S.append` l) (Spec.SHA3.absorb_inner rateInBytes) s else () ) } ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () (bs `S.append` l) in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length ); }; calc (S.equal) { ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () (bs `S.append` l) in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length ); (S.equal) { let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in let s = update_multi a s () bs in if S.length l = rateInBytes then begin let bs', l' = UpdateMulti.split_at_last rateInBytes input in // TODO: strengthen this... NL arith! assert (bs' `S.equal` (bs `S.append` l)); assert (l' `S.equal` S.empty) end else () } ( let s = Lib.Sequence.create 25 (u64 0) in // Also the block size let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last rateInBytes input in let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length ); }
{ "file_name": "specs/lemmas/Spec.SHA3.Incremental.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 171, "start_col": 0, "start_line": 56 }
module Spec.SHA3.Incremental module S = FStar.Seq open Spec.Agile.Hash open Spec.Hash.Definitions open Spec.Hash.Incremental.Definitions open Spec.Hash.Lemmas friend Spec.Agile.Hash open FStar.Mul module Loops = Lib.LoopCombinators module UpdateMulti = Lib.UpdateMulti open Lib.IntTypes #set-options "--fuel 0 --ifuel 0 --z3rlimit 200" let update_is_update_multi (a:keccak_alg) (inp:bytes{S.length inp == block_length a}) (s:words_state a) : Lemma (Spec.SHA3.absorb_inner (rate a/8) inp s == update_multi a s () inp) = let rateInBytes = rate a/8 in let f = Spec.SHA3.absorb_inner rateInBytes in let bs = block_length a in let f' = Lib.Sequence.repeat_blocks_f bs inp f 1 in assert (bs == rateInBytes); calc (==) { update_multi a s () inp; (==) { } Lib.Sequence.repeat_blocks_multi #_ #(words_state a) rateInBytes inp f s; (==) { Lib.Sequence.lemma_repeat_blocks_multi #_ #(words_state a) bs inp f s } (let len = S.length inp in let nb = len / bs in Loops.repeati #(words_state a) nb (Lib.Sequence.repeat_blocks_f bs inp f nb) s); (==) { Loops.unfold_repeati 1 f' s 0; Loops.eq_repeati0 1 f' s } f' 0 s; (==) { assert (Seq.slice inp (0 * bs) (0 * bs + bs) `S.equal` inp) } f inp s; } let suffix (a: keccak_alg) = if is_shake a then byte 0x1f else byte 0x06 val sha3_is_incremental1 (a: keccak_alg) (input: bytes) (out_length: output_length a): Lemma (hash_incremental a input out_length `S.equal` ( let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last rateInBytes input in let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length))
{ "checked_file": "/", "dependencies": [ "Spec.SHA3.fst.checked", "Spec.Hash.Lemmas.fsti.checked", "Spec.Hash.Incremental.Definitions.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.Hash.fst.checked", "Spec.Agile.Hash.fst.checked", "prims.fst.checked", "Lib.UpdateMulti.fst.checked", "Lib.Sequence.Lemmas.fsti.checked", "Lib.Sequence.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Spec.SHA3.Incremental.fst" }
[ { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "Lib.UpdateMulti", "short_module": "UpdateMulti" }, { "abbrev": true, "full_module": "Lib.LoopCombinators", "short_module": "Loops" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Lemmas", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Incremental.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile.Hash", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "Spec.Hash.Incremental.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile.Hash", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "Spec.SHA3", "short_module": null }, { "abbrev": false, "full_module": "Spec.SHA3", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 200, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Spec.Hash.Definitions.keccak_alg -> input: Spec.Hash.Definitions.bytes -> out_length: Spec.Hash.Definitions.output_length a -> FStar.Pervasives.Lemma (ensures FStar.Seq.Base.equal (Spec.Hash.Incremental.Definitions.hash_incremental a input out_length) (let s = Lib.Sequence.create 25 (Lib.IntTypes.u64 0) in let rateInBytes = Spec.Hash.Definitions.rate a / 8 in let delimitedSuffix = Spec.SHA3.Incremental.suffix a in let _ = Lib.UpdateMulti.split_at_last rateInBytes input in (let FStar.Pervasives.Native.Mktuple2 #_ #_ bs l = _ in let s = Spec.Agile.Hash.update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (FStar.Seq.Base.length l) l s in Spec.Agile.Hash.finish a s out_length) <: FStar.Seq.Base.seq (Lib.IntTypes.uint_t Lib.IntTypes.U8 Lib.IntTypes.SEC)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.Hash.Definitions.keccak_alg", "Spec.Hash.Definitions.bytes", "Spec.Hash.Definitions.output_length", "FStar.Calc.calc_finish", "FStar.Seq.Base.seq", "Lib.IntTypes.uint8", "FStar.Seq.Base.equal", "Lib.UpdateMulti.uint8", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "Spec.Agile.Hash.finish", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "Spec.SHA3.absorb_last", "FStar.Seq.Base.empty", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Spec.Hash.Definitions.words_state", "Spec.Agile.Hash.update_multi", "FStar.Seq.Base.append", "Prims.bool", "FStar.Pervasives.Native.tuple2", "Lib.UpdateMulti.split_at_last_lazy", "Lib.IntTypes.PUB", "Prims.l_or", "Prims.eq2", "Lib.IntTypes.range", "Prims.l_and", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "Prims.op_LessThan", "Lib.IntTypes.v", "Spec.SHA3.Incremental.suffix", "Prims.op_Division", "Spec.Hash.Definitions.rate", "Spec.Hash.Definitions.word", "Lib.Sequence.to_seq", "FStar.Seq.Base.create", "Lib.IntTypes.mk_int", "Prims.l_Forall", "Prims.nat", "Prims.l_imp", "Lib.Sequence.index", "Lib.Sequence.create", "Lib.IntTypes.u64", "Lib.UpdateMulti.split_at_last", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Prims._assert", "Prims.squash", "Lib.ByteSequence.bytes", "Spec.Hash.Definitions.hash_length'", "Spec.Hash.Incremental.Definitions.hash_incremental", "Spec.SHA3.absorb_inner", "Spec.Hash.Incremental.Definitions.update_last", "Lib.Sequence.seq", "Spec.Hash.Incremental.Definitions.split_blocks", "Spec.Hash.Definitions.init_t", "Spec.Agile.Hash.init", "Spec.SHA3.Incremental.update_is_update_multi", "Lib.Sequence.Lemmas.repeat_blocks_multi_split", "Spec.SHA3.state", "Spec.Hash.Definitions.block_length", "Lib.IntTypes.uint64" ]
[]
false
false
true
false
false
let sha3_is_incremental1 a input out_length =
calc ( == ) { hash_incremental a input out_length; ( == ) { () } (let s = init a in let bs, l = split_blocks a input in let s = update_multi a s () bs in let s = update_last a s () l in finish a s out_length); ( == ) { () } (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in let s = update_multi a s () bs in let s = update_last a s () l in finish a s out_length); ( == ) { () } (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () bs in let s = Spec.SHA3.absorb_inner rateInBytes l s in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length); ( == ) { (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () bs in update_is_update_multi a l s) } (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () bs in let s = update_multi a s () l in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length); ( == ) { (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then Lib.Sequence.Lemmas.repeat_blocks_multi_split (block_length a) (S.length bs) (bs `S.append` l) (Spec.SHA3.absorb_inner rateInBytes) s) } (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () (bs `S.append` l) in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length); }; calc (S.equal) { (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in if S.length l = rateInBytes then let s = update_multi a s () (bs `S.append` l) in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes 0 S.empty s in finish a s out_length else let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length); (S.equal) { let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let bs, l = UpdateMulti.split_at_last_lazy rateInBytes input in let s = update_multi a s () bs in if S.length l = rateInBytes then let bs', l' = UpdateMulti.split_at_last rateInBytes input in assert (bs' `S.equal` (bs `S.append` l)); assert (l' `S.equal` S.empty) } (let s = Lib.Sequence.create 25 (u64 0) in let rateInBytes = rate a / 8 in let delimitedSuffix = suffix a in let bs, l = UpdateMulti.split_at_last rateInBytes input in let s = update_multi a s () bs in let s = Spec.SHA3.absorb_last delimitedSuffix rateInBytes (S.length l) l s in finish a s out_length); }
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_nat_from_to_bytes_be_preserves_value
val lemma_nat_from_to_bytes_be_preserves_value: #l:secrecy_level -> b:bytes_l l -> len:size_nat{len == Lib.Sequence.length b} -> Lemma (nat_to_bytes_be #l len (nat_from_bytes_be b) == b)
val lemma_nat_from_to_bytes_be_preserves_value: #l:secrecy_level -> b:bytes_l l -> len:size_nat{len == Lib.Sequence.length b} -> Lemma (nat_to_bytes_be #l len (nat_from_bytes_be b) == b)
let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len b
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 756, "start_col": 0, "start_line": 755 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.bytes_l l -> len: Lib.IntTypes.size_nat{len == Lib.Sequence.length b} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_to_bytes_be len (Lib.ByteSequence.nat_from_bytes_be b) == b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.secrecy_level", "Lib.ByteSequence.bytes_l", "Lib.IntTypes.size_nat", "Prims.eq2", "Prims.nat", "Lib.Sequence.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.lemma_nat_from_to_intseq_be_preserves_value", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_nat_from_to_bytes_be_preserves_value #l b len =
lemma_nat_from_to_intseq_be_preserves_value len b
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le_slice_lemma
val uints_from_bytes_le_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_le #t #l #len b) i j == uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
val uints_from_bytes_le_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_le #t #l #len b) i j == uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 136, "end_line": 444, "start_col": 0, "start_line": 441 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> i: Prims.nat -> j: Prims.nat{i <= j /\ j <= len} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.slice (Lib.ByteSequence.uints_from_bytes_le b) i j == Lib.ByteSequence.uints_from_bytes_le (Lib.Sequence.slice b (i * Lib.IntTypes.numbytes t) (j * Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Prims.op_LessThanOrEqual", "Lib.Sequence.eq_intro", "Lib.IntTypes.uint_t", "Prims.op_Subtraction", "Lib.Sequence.slice", "Lib.ByteSequence.uints_from_bytes_le", "Lib.IntTypes.U8", "Prims.unit", "FStar.Classical.forall_intro", "Prims.eq2", "Lib.Sequence.index", "Lib.ByteSequence.uint_from_bytes_le", "Lib.Sequence.sub", "Prims.op_Addition", "Lib.ByteSequence.uints_from_bytes_le_slice_lemma_rp", "Lib.ByteSequence.uints_from_bytes_le_slice_lemma_lp" ]
[]
false
false
true
false
false
let uints_from_bytes_le_slice_lemma #t #l #len b i j =
FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j - i) (slice b (i * numbytes t) (j * numbytes t)))
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_to_poly_degree
val lemma_to_poly_degree (#f:G.field) (e:G.felem f) : Lemma (requires True) (ensures degree (to_poly e) < I.bits f.G.t) [SMTPat (to_poly e)]
val lemma_to_poly_degree (#f:G.field) (e:G.felem f) : Lemma (requires True) (ensures degree (to_poly e) < I.bits f.G.t) [SMTPat (to_poly e)]
let lemma_to_poly_degree #f e = reveal_defs ()
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 16, "end_line": 25, "start_col": 0, "start_line": 24 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n))
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
e: Spec.GaloisField.felem f -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2_s.degree (Vale.Math.Poly2.Galois.to_poly e) < Lib.IntTypes.bits (GF?.t f)) [SMTPat (Vale.Math.Poly2.Galois.to_poly e)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Vale.Math.Poly2_s.reveal_defs", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_to_poly_degree #f e =
reveal_defs ()
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_irred_degree
val lemma_irred_degree (f:G.field) : Lemma (requires True) (ensures degree (irred_poly f) == I.bits f.G.t) [SMTPat (irred_poly f)]
val lemma_irred_degree (f:G.field) : Lemma (requires True) (ensures degree (irred_poly f) == I.bits f.G.t) [SMTPat (irred_poly f)]
let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; ()
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 4, "end_line": 34, "start_col": 0, "start_line": 27 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs ()
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2_s.degree (Vale.Math.Poly2.Galois.irred_poly f) == Lib.IntTypes.bits (GF?.t f)) [SMTPat (Vale.Math.Poly2.Galois.irred_poly f)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Prims.unit", "Vale.Math.Poly2.Lemmas.lemma_degree_is", "Vale.Math.Poly2.Galois.irred_poly", "Vale.Math.Poly2.Lemmas.lemma_add_define_all", "Vale.Math.Poly2.Lemmas.lemma_monomial_define", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Prims.int", "Lib.IntTypes.bits" ]
[]
false
false
true
false
false
let lemma_irred_degree f =
let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; ()
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_poly_felem
val lemma_poly_felem (f:G.field) (p:poly) : Lemma (requires degree p < I.bits (G.GF?.t f)) (ensures to_poly (to_felem f p) == p) [SMTPat (to_poly (to_felem f p))]
val lemma_poly_felem (f:G.field) (p:poly) : Lemma (requires degree p < I.bits (G.GF?.t f)) (ensures to_poly (to_felem f p) == p) [SMTPat (to_poly (to_felem f p))]
let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; ()
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 4, "end_line": 51, "start_col": 0, "start_line": 36 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; ()
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> p: Vale.Math.Poly2_s.poly -> FStar.Pervasives.Lemma (requires Vale.Math.Poly2_s.degree p < Lib.IntTypes.bits (GF?.t f)) (ensures Vale.Math.Poly2.Galois.to_poly (Vale.Math.Poly2.Galois.to_felem f p) == p) [SMTPat (Vale.Math.Poly2.Galois.to_poly (Vale.Math.Poly2.Galois.to_felem f p))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Vale.Math.Poly2_s.poly", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Prims.unit", "Vale.Math.Poly2.lemma_equal", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Vale.Math.Poly2_s.reverse", "Prims.op_Subtraction", "Vale.Math.Poly2_s.of_seq", "FStar.BitVector.bv_t", "FStar.UInt.to_vec", "Prims.int", "Lib.IntTypes.range", "Lib.IntTypes.v", "Lib.IntTypes.int_t", "Prims.eq2", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "Prims.op_LessThanOrEqual", "Lib.IntTypes.maxint", "Lib.IntTypes.Compatibility.nat_to_uint", "FStar.UInt.uint_t", "FStar.UInt.from_vec", "FStar.Seq.Base.seq", "Prims.bool", "Vale.Math.Poly2_s.to_seq", "Lib.IntTypes.bits" ]
[]
false
false
true
false
false
let lemma_poly_felem f p =
let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; ()
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_be_nat_lemma_
val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b)
val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b)
let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 547, "start_col": 0, "start_line": 541 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_be_ (Lib.ByteSequence.uints_from_bytes_be b) == Lib.ByteSequence.nat_from_intseq_be_ b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.op_Equality", "Prims.int", "Prims.bool", "Lib.ByteSequence.uints_from_bytes_be_nat_lemma_", "Prims.op_Subtraction", "Lib.Sequence.slice", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.unit", "Lib.ByteSequence.nat_from_intseq_be_slice_lemma_", "Lib.ByteSequence.uints_from_bytes_be_nat_lemma_aux" ]
[ "recursion" ]
false
false
true
false
false
let rec uints_from_bytes_be_nat_lemma_ #t #l #len b =
if len = 0 then () else (uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_le_slice_lemma_
val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len))
val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len))
let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 386, "start_col": 0, "start_line": 374 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Sequence.lseq (Lib.IntTypes.int_t t l) len -> i: Prims.nat{i <= len} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_le_ b == Lib.ByteSequence.nat_from_intseq_le_ (Lib.Sequence.slice b 0 i) + Prims.pow2 (i * Lib.IntTypes.bits t) * Lib.ByteSequence.nat_from_intseq_le_ (Lib.Sequence.slice b i len))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Equality", "Prims.int", "Prims.bool", "Lib.ByteSequence.nat_from_intseq_slice_lemma_aux", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Prims.op_Subtraction", "Lib.ByteSequence.nat_from_intseq_le_", "Lib.Sequence.slice", "Lib.IntTypes.bits", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.pow2", "Lib.ByteSequence.nat_from_intseq_le_slice_lemma_", "Prims.l_and", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.l_Forall", "Prims.op_LessThan", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index" ]
[ "recursion" ]
false
false
true
false
false
let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i =
if len = 0 then () else if i = 0 then () else let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[ i - 1 ]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[ i - 1 ]) (nat_from_intseq_le_ (slice b i len)) (bits t) i
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_nat_to_intseq_le
val index_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_nat -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) i == uint #t #l (n / pow2 (bits t * i) % pow2 (bits t)))
val index_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_nat -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) i == uint #t #l (n / pow2 (bits t * i) % pow2 (bits t)))
let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 252, "start_col": 0, "start_line": 232 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Lib.IntTypes.size_nat -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> i: Prims.nat{i < len} -> FStar.Pervasives.Lemma (ensures FStar.Seq.Base.index (Lib.ByteSequence.nat_to_intseq_le len n) i == Lib.IntTypes.uint (n / Prims.pow2 (Lib.IntTypes.bits t * i) % Prims.pow2 (Lib.IntTypes.bits t) ))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.op_Equality", "Prims.int", "Prims.bool", "Lib.ByteSequence.head_nat_to_intseq_le", "Prims.unit", "Lib.ByteSequence.nat_to_intseq_le_pos", "FStar.Calc.calc_finish", "Lib.IntTypes.uint_t", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.nat_to_intseq_le", "Prims.op_Subtraction", "Prims.op_Division", "Lib.IntTypes.modulus", "Lib.IntTypes.uint", "Prims.op_Modulus", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "FStar.Calc.calc_step", "Prims.op_Addition", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Lib.ByteSequence.index_nat_to_intseq_le", "Prims.squash", "FStar.Math.Lemmas.division_multiplication_lemma", "FStar.Math.Lemmas.pow2_plus", "FStar.Math.Lemmas.distributivity_add_right", "Prims.op_Minus", "FStar.Math.Lemmas.lemma_div_lt_nat" ]
[ "recursion" ]
false
false
true
false
false
let rec index_nat_to_intseq_le #t #l len n i =
if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else (FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc ( == ) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); ( == ) { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); ( == ) { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); ( == ) { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); ( == ) { Math.Lemmas.distributivity_add_right (bits t) i (- 1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); ( == ) { () } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n)
false
Lib.ByteSequence.fst
Lib.ByteSequence.modulo_pow2_prop
val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r)
val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r)
let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; }
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 599, "start_col": 0, "start_line": 588 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Prims.pos -> a: Prims.nat -> b: Prims.nat -> c: Prims.nat{c < b} -> FStar.Pervasives.Lemma (ensures a % Prims.pow2 (r * b) / Prims.pow2 (r * c) % Prims.pow2 r == a / Prims.pow2 (r * c) % Prims.pow2 r)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "Prims.op_Division", "Prims.pow2", "FStar.Mul.op_Star", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "Prims.op_Subtraction", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Math.Lemmas.pow2_modulo_division_lemma_1", "Prims.squash", "FStar.Math.Lemmas.lemma_mul_sub_distr", "FStar.Math.Lemmas.pow2_plus", "FStar.Math.Lemmas.modulo_modulo_lemma" ]
[]
false
false
true
false
false
let modulo_pow2_prop r a b c =
calc ( == ) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; ( == ) { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; ( == ) { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; ( == ) { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; ( == ) { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r)) } (a / pow2 (r * c)) % pow2 r; }
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_be_slice_lemma_rp
val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t)))
val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t)))
let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 82, "end_line": 509, "start_col": 0, "start_line": 504 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> i: Prims.nat -> j: Prims.nat{i <= j /\ j <= len} -> k: Prims.nat{k < j - i} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.index (Lib.ByteSequence.uints_from_bytes_be (Lib.Sequence.slice b (i * Lib.IntTypes.numbytes t) (j * Lib.IntTypes.numbytes t))) k == Lib.ByteSequence.uint_from_bytes_be (Lib.Sequence.sub b ((i + k) * Lib.IntTypes.numbytes t) (Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Subtraction", "Prims._assert", "Prims.eq2", "Lib.IntTypes.uint_t", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.uint_from_bytes_be", "Lib.Sequence.sub", "Lib.IntTypes.U8", "Prims.op_Addition", "Prims.unit", "Lib.ByteSequence.index_uints_from_bytes_be", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.ByteSequence.uints_from_bytes_be", "Prims.op_Multiply", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.l_Forall", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index", "Lib.Sequence.slice" ]
[]
true
false
true
false
false
let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k =
let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j - i) b1 in index_uints_from_bytes_be #t #l #(j - i) b1 k; assert (r.[ k ] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[ k ] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_be_slice_lemma
val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 136, "end_line": 518, "start_col": 0, "start_line": 515 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> i: Prims.nat -> j: Prims.nat{i <= j /\ j <= len} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.slice (Lib.ByteSequence.uints_from_bytes_be b) i j == Lib.ByteSequence.uints_from_bytes_be (Lib.Sequence.slice b (i * Lib.IntTypes.numbytes t) (j * Lib.IntTypes.numbytes t)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims.nat", "Prims.op_LessThanOrEqual", "Lib.Sequence.eq_intro", "Lib.IntTypes.uint_t", "Prims.op_Subtraction", "Lib.Sequence.slice", "Lib.ByteSequence.uints_from_bytes_be", "Lib.IntTypes.U8", "Prims.unit", "FStar.Classical.forall_intro", "Prims.eq2", "Lib.Sequence.index", "Lib.ByteSequence.uint_from_bytes_be", "Lib.Sequence.sub", "Prims.op_Addition", "Lib.ByteSequence.uints_from_bytes_be_slice_lemma_rp", "Lib.ByteSequence.uints_from_bytes_le", "Lib.ByteSequence.uint_from_bytes_le", "Lib.ByteSequence.uints_from_bytes_be_slice_lemma_lp" ]
[]
false
false
true
false
false
let uints_from_bytes_be_slice_lemma #t #l #len b i j =
FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j - i) (slice b (i * numbytes t) (j * numbytes t)))
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_felem_poly
val lemma_felem_poly (#f:G.field) (e:G.felem f) : Lemma (requires True) (ensures to_felem f (to_poly e) == e) [SMTPat (to_felem f (to_poly e))]
val lemma_felem_poly (#f:G.field) (e:G.felem f) : Lemma (requires True) (ensures to_felem f (to_poly e) == e) [SMTPat (to_felem f (to_poly e))]
let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; ()
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 4, "end_line": 69, "start_col": 0, "start_line": 53 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; ()
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
e: Spec.GaloisField.felem f -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_felem f (Vale.Math.Poly2.Galois.to_poly e) == e) [SMTPat (Vale.Math.Poly2.Galois.to_felem f (Vale.Math.Poly2.Galois.to_poly e))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Prims.unit", "Lib.IntTypes.Compatibility.uintv_extensionality", "Spec.GaloisField.__proj__GF__item__t", "Prims._assert", "FStar.Seq.Base.equal", "Prims.bool", "Vale.Math.Poly2.lemma_equal", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Lib.IntTypes.int_t", "Prims.eq2", "Prims.int", "Prims.l_or", "Lib.IntTypes.range", "Prims.op_GreaterThanOrEqual", "Prims.op_LessThanOrEqual", "Lib.IntTypes.maxint", "Lib.IntTypes.v", "Lib.IntTypes.Compatibility.nat_to_uint", "FStar.UInt.uint_t", "FStar.UInt.from_vec", "FStar.Seq.Base.seq", "Vale.Math.Poly2_s.to_seq", "Vale.Math.Poly2_s.poly", "Vale.Math.Poly2_s.reverse", "Prims.op_Subtraction", "Vale.Math.Poly2_s.of_seq", "FStar.BitVector.bv_t", "FStar.UInt.to_vec", "Lib.IntTypes.bits" ]
[]
false
false
true
false
false
let lemma_felem_poly #f e =
let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; ()
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.sum_of_bools
val sum_of_bools : j: Prims.int -> k: Prims.int -> f: (_: Prims.int -> Prims.bool) -> Prims.Tot Prims.bool
let sum_of_bools = D.sum_of_bools
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 40, "end_line": 166, "start_col": 7, "start_line": 166 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
j: Prims.int -> k: Prims.int -> f: (_: Prims.int -> Prims.bool) -> Prims.Tot Prims.bool
Prims.Tot
[ "total", "" ]
[]
[ "Vale.Math.Poly2.Defs_s.sum_of_bools" ]
[]
false
false
false
true
false
let sum_of_bools =
D.sum_of_bools
false
Lib.ByteSequence.fst
Lib.ByteSequence.index_nat_to_intseq_to_bytes_le
val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i)
val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i)
let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 23, "end_line": 647, "start_col": 0, "start_line": 637 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat{len * Lib.IntTypes.numbytes t < Prims.pow2 32} -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> i: Prims.nat{i < len * Lib.IntTypes.numbytes t} -> FStar.Pervasives.Lemma (ensures (let s = Lib.ByteSequence.nat_to_intseq_le len n in FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_le (Lib.IntTypes.numbytes t) (Lib.IntTypes.v s.[ i / Lib.IntTypes.numbytes t ])) (i % Lib.IntTypes.numbytes t) == FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_le (len * Lib.IntTypes.numbytes t) n) i) )
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.IntTypes.bits", "Lib.ByteSequence.some_arithmetic", "Prims.unit", "Lib.ByteSequence.index_nat_to_intseq_le", "Prims.op_Division", "Prims._assert", "Prims.eq2", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "FStar.Seq.Base.index", "Lib.IntTypes.uint_t", "Lib.ByteSequence.nat_to_bytes_le", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Prims.op_Modulus", "Lib.IntTypes.uint", "Prims.int", "Lib.Sequence.lseq", "Lib.ByteSequence.nat_to_intseq_le" ]
[]
true
false
true
false
false
let index_nat_to_intseq_to_bytes_le #t #l len n i =
let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[ i / m ]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[ i / m ])) (i % m) == uint (v s.[ i / m ] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.mul_element
val mul_element (a b: poly) (k: int) : bool
val mul_element (a b: poly) (k: int) : bool
let mul_element (a b:poly) (k:int) : bool = sum_of_bools 0 (k + 1) (mul_element_fun a b k)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 48, "end_line": 170, "start_col": 0, "start_line": 169 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p unfold let sum_of_bools = D.sum_of_bools let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i]
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Math.Poly2_s.poly -> b: Vale.Math.Poly2_s.poly -> k: Prims.int -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Prims.int", "Vale.Math.Poly2.Galois.sum_of_bools", "Prims.op_Addition", "Vale.Math.Poly2.Galois.mul_element_fun", "Prims.bool" ]
[]
false
false
false
true
false
let mul_element (a b: poly) (k: int) : bool =
sum_of_bools 0 (k + 1) (mul_element_fun a b k)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.mul_element_fun
val mul_element_fun (a b: poly) (k i: int) : bool
val mul_element_fun (a b: poly) (k i: int) : bool
let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i]
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 68, "end_line": 167, "start_col": 0, "start_line": 167 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Math.Poly2_s.poly -> b: Vale.Math.Poly2_s.poly -> k: Prims.int -> i: Prims.int -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Prims.int", "Prims.op_AmpAmp", "Vale.Math.Poly2_s.op_String_Access", "Prims.op_Subtraction", "Prims.bool" ]
[]
false
false
false
true
false
let mul_element_fun (a b: poly) (k i: int) : bool =
a.[ i ] && b.[ k - i ]
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_to_bytes_le_nat_lemma
val uints_to_bytes_le_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> Lemma (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n) == nat_to_bytes_le (len * numbytes t) n)
val uints_to_bytes_le_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> Lemma (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n) == nat_to_bytes_le (len * numbytes t) n)
let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 49, "end_line": 670, "start_col": 0, "start_line": 667 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat{len * Lib.IntTypes.numbytes t < Prims.pow2 32} -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.uints_to_bytes_le (Lib.ByteSequence.nat_to_intseq_le len n) == Lib.ByteSequence.nat_to_bytes_le (len * Lib.IntTypes.numbytes t) n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.IntTypes.bits", "Lib.Sequence.eq_intro", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.uints_to_bytes_le", "Lib.ByteSequence.nat_to_intseq_le", "Lib.ByteSequence.nat_to_bytes_le", "Prims.unit", "FStar.Classical.forall_intro", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.uints_to_bytes_le_nat_lemma_" ]
[]
false
false
true
false
false
let uints_to_bytes_le_nat_lemma #t #l len n =
Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.pmul
val pmul (a b: poly) : poly
val pmul (a b: poly) : poly
let pmul (a b:poly) : poly = pmul_rec a b (poly_length b)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 30, "end_line": 192, "start_col": 0, "start_line": 191 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p unfold let sum_of_bools = D.sum_of_bools let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i] let mul_element (a b:poly) (k:int) : bool = sum_of_bools 0 (k + 1) (mul_element_fun a b k) [@"opaque_to_smt"] let mul_def (a b:poly) : Pure poly (requires True) (ensures fun p -> let len = poly_length a + poly_length b in poly_length p <= len /\ (forall (i:nat).{:pattern p.[i]} i < len ==> p.[i] == mul_element a b i) ) = let len = poly_length a + poly_length b in of_fun len (fun (i:nat) -> mul_element a b i) let rec pmul_rec (a b:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = pmul_rec a b n' in if b.[n'] then p +. shift a n' else p
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Math.Poly2_s.poly -> b: Vale.Math.Poly2_s.poly -> Vale.Math.Poly2_s.poly
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Vale.Math.Poly2.Galois.pmul_rec", "Vale.Math.Poly2.Galois.poly_length" ]
[]
false
false
false
true
false
let pmul (a b: poly) : poly =
pmul_rec a b (poly_length b)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.mod_bit1
val mod_bit1 (a m: poly) : poly
val mod_bit1 (a m: poly) : poly
let mod_bit1 (a m:poly) : poly = if a.[degree m] then a +. m else a
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 202, "start_col": 0, "start_line": 201 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p unfold let sum_of_bools = D.sum_of_bools let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i] let mul_element (a b:poly) (k:int) : bool = sum_of_bools 0 (k + 1) (mul_element_fun a b k) [@"opaque_to_smt"] let mul_def (a b:poly) : Pure poly (requires True) (ensures fun p -> let len = poly_length a + poly_length b in poly_length p <= len /\ (forall (i:nat).{:pattern p.[i]} i < len ==> p.[i] == mul_element a b i) ) = let len = poly_length a + poly_length b in of_fun len (fun (i:nat) -> mul_element a b i) let rec pmul_rec (a b:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = pmul_rec a b n' in if b.[n'] then p +. shift a n' else p let pmul (a b:poly) : poly = pmul_rec a b (poly_length b) let rec mmul (a b m:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = mmul a b m n' in if b.[n'] then p +. (shift a n' %. m) else p
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Math.Poly2_s.poly -> m: Vale.Math.Poly2_s.poly -> Vale.Math.Poly2_s.poly
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Vale.Math.Poly2_s.op_String_Access", "Vale.Math.Poly2_s.degree", "Vale.Math.Poly2.op_Plus_Dot", "Prims.bool" ]
[]
false
false
false
true
false
let mod_bit1 (a m: poly) : poly =
if a.[ degree m ] then a +. m else a
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_le_inj
val nat_from_intseq_le_inj: #t:inttype{unsigned t} -> #l:secrecy_level -> b1:seq (uint_t t l) -> b2:seq (uint_t t l) -> Lemma (requires length b1 == length b2 /\ nat_from_intseq_le b1 == nat_from_intseq_le b2) (ensures Seq.equal b1 b2) (decreases (length b1))
val nat_from_intseq_le_inj: #t:inttype{unsigned t} -> #l:secrecy_level -> b1:seq (uint_t t l) -> b2:seq (uint_t t l) -> Lemma (requires length b1 == length b2 /\ nat_from_intseq_le b1 == nat_from_intseq_le b2) (ensures Seq.equal b1 b2) (decreases (length b1))
let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 728, "start_col": 0, "start_line": 722 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> b2: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> FStar.Pervasives.Lemma (requires Lib.Sequence.length b1 == Lib.Sequence.length b2 /\ Lib.ByteSequence.nat_from_intseq_le b1 == Lib.ByteSequence.nat_from_intseq_le b2) (ensures FStar.Seq.Base.equal b1 b2) (decreases Lib.Sequence.length b1)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.Sequence.seq", "Lib.IntTypes.uint_t", "Prims.op_Equality", "Prims.int", "Lib.Sequence.length", "Prims.bool", "FStar.Seq.Properties.lemma_split", "Prims.unit", "Lib.ByteSequence.nat_from_intseq_le_inj", "FStar.Seq.Base.slice" ]
[ "recursion" ]
false
false
true
false
false
let rec nat_from_intseq_le_inj #t #l b1 b2 =
if length b1 = 0 then () else (nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.fmul_t
val fmul_t : f: Spec.GaloisField.field -> Type0
let fmul_t (f:G.field) = G.felem f & G.felem f & G.felem f
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 221, "start_col": 0, "start_line": 221 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p unfold let sum_of_bools = D.sum_of_bools let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i] let mul_element (a b:poly) (k:int) : bool = sum_of_bools 0 (k + 1) (mul_element_fun a b k) [@"opaque_to_smt"] let mul_def (a b:poly) : Pure poly (requires True) (ensures fun p -> let len = poly_length a + poly_length b in poly_length p <= len /\ (forall (i:nat).{:pattern p.[i]} i < len ==> p.[i] == mul_element a b i) ) = let len = poly_length a + poly_length b in of_fun len (fun (i:nat) -> mul_element a b i) let rec pmul_rec (a b:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = pmul_rec a b n' in if b.[n'] then p +. shift a n' else p let pmul (a b:poly) : poly = pmul_rec a b (poly_length b) let rec mmul (a b m:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = mmul a b m n' in if b.[n'] then p +. (shift a n' %. m) else p let mod_bit1 (a m:poly) : poly = if a.[degree m] then a +. m else a let rec smul_rec (a b m:poly) (n:nat) : Tot (poly & poly & poly) (decreases n) = if n = 0 then (zero, a, b) else let (p, a, b) = smul_rec a b m (n - 1) in let p = p +. (if b.[0] then a else zero) in let a = mod_bit1 (shift a 1) m in // mod_bit1 is equivalent to mod in this case let b = shift b (-1) in (p, a, b) let smul (a b m:poly) (n:nat) : poly = let (p, _, _) = smul_rec a b m n in p let mod_shift1 (a irred:poly) (k:nat) : poly = let s = shift a 1 %. monomial k in s +. (if a.[k - 1] then irred else zero)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.GaloisField.field", "FStar.Pervasives.Native.tuple3", "Spec.GaloisField.felem" ]
[]
false
false
false
true
true
let fmul_t (f: G.field) =
G.felem f & G.felem f & G.felem f
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.mod_shift1
val mod_shift1 (a irred: poly) (k: nat) : poly
val mod_shift1 (a irred: poly) (k: nat) : poly
let mod_shift1 (a irred:poly) (k:nat) : poly = let s = shift a 1 %. monomial k in s +. (if a.[k - 1] then irred else zero)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 219, "start_col": 0, "start_line": 217 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p unfold let sum_of_bools = D.sum_of_bools let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i] let mul_element (a b:poly) (k:int) : bool = sum_of_bools 0 (k + 1) (mul_element_fun a b k) [@"opaque_to_smt"] let mul_def (a b:poly) : Pure poly (requires True) (ensures fun p -> let len = poly_length a + poly_length b in poly_length p <= len /\ (forall (i:nat).{:pattern p.[i]} i < len ==> p.[i] == mul_element a b i) ) = let len = poly_length a + poly_length b in of_fun len (fun (i:nat) -> mul_element a b i) let rec pmul_rec (a b:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = pmul_rec a b n' in if b.[n'] then p +. shift a n' else p let pmul (a b:poly) : poly = pmul_rec a b (poly_length b) let rec mmul (a b m:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = mmul a b m n' in if b.[n'] then p +. (shift a n' %. m) else p let mod_bit1 (a m:poly) : poly = if a.[degree m] then a +. m else a let rec smul_rec (a b m:poly) (n:nat) : Tot (poly & poly & poly) (decreases n) = if n = 0 then (zero, a, b) else let (p, a, b) = smul_rec a b m (n - 1) in let p = p +. (if b.[0] then a else zero) in let a = mod_bit1 (shift a 1) m in // mod_bit1 is equivalent to mod in this case let b = shift b (-1) in (p, a, b) let smul (a b m:poly) (n:nat) : poly = let (p, _, _) = smul_rec a b m n in p
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Math.Poly2_s.poly -> irred: Vale.Math.Poly2_s.poly -> k: Prims.nat -> Vale.Math.Poly2_s.poly
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Prims.nat", "Vale.Math.Poly2.op_Plus_Dot", "Vale.Math.Poly2_s.op_String_Access", "Prims.op_Subtraction", "Prims.bool", "Vale.Math.Poly2_s.zero", "Vale.Math.Poly2.op_Percent_Dot", "Vale.Math.Poly2_s.shift", "Vale.Math.Poly2_s.monomial" ]
[]
false
false
false
true
false
let mod_shift1 (a irred: poly) (k: nat) : poly =
let s = shift a 1 %. monomial k in s +. (if a.[ k - 1 ] then irred else zero)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.smul
val smul (a b m: poly) (n: nat) : poly
val smul (a b m: poly) (n: nat) : poly
let smul (a b m:poly) (n:nat) : poly = let (p, _, _) = smul_rec a b m n in p
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 215, "start_col": 0, "start_line": 213 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *) let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p unfold let sum_of_bools = D.sum_of_bools let mul_element_fun (a b:poly) (k i:int) : bool = a.[i] && b.[k - i] let mul_element (a b:poly) (k:int) : bool = sum_of_bools 0 (k + 1) (mul_element_fun a b k) [@"opaque_to_smt"] let mul_def (a b:poly) : Pure poly (requires True) (ensures fun p -> let len = poly_length a + poly_length b in poly_length p <= len /\ (forall (i:nat).{:pattern p.[i]} i < len ==> p.[i] == mul_element a b i) ) = let len = poly_length a + poly_length b in of_fun len (fun (i:nat) -> mul_element a b i) let rec pmul_rec (a b:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = pmul_rec a b n' in if b.[n'] then p +. shift a n' else p let pmul (a b:poly) : poly = pmul_rec a b (poly_length b) let rec mmul (a b m:poly) (n:nat) : Tot poly (decreases n) = if n = 0 then zero else let n' = n - 1 in let p = mmul a b m n' in if b.[n'] then p +. (shift a n' %. m) else p let mod_bit1 (a m:poly) : poly = if a.[degree m] then a +. m else a let rec smul_rec (a b m:poly) (n:nat) : Tot (poly & poly & poly) (decreases n) = if n = 0 then (zero, a, b) else let (p, a, b) = smul_rec a b m (n - 1) in let p = p +. (if b.[0] then a else zero) in let a = mod_bit1 (shift a 1) m in // mod_bit1 is equivalent to mod in this case let b = shift b (-1) in (p, a, b)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Math.Poly2_s.poly -> b: Vale.Math.Poly2_s.poly -> m: Vale.Math.Poly2_s.poly -> n: Prims.nat -> Vale.Math.Poly2_s.poly
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Prims.nat", "FStar.Pervasives.Native.tuple3", "Vale.Math.Poly2.Galois.smul_rec" ]
[]
false
false
false
true
false
let smul (a b m: poly) (n: nat) : poly =
let p, _, _ = smul_rec a b m n in p
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_to_bytes_be_nat_lemma
val uints_to_bytes_be_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> Lemma (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n) == nat_to_bytes_be (len * numbytes t) n)
val uints_to_bytes_be_nat_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> Lemma (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n) == nat_to_bytes_be (len * numbytes t) n)
let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 49, "end_line": 718, "start_col": 0, "start_line": 715 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
len: Prims.nat{len * Lib.IntTypes.numbytes t < Prims.pow2 32} -> n: Prims.nat{n < Prims.pow2 (Lib.IntTypes.bits t * len)} -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.uints_to_bytes_be (Lib.ByteSequence.nat_to_intseq_be len n) == Lib.ByteSequence.nat_to_bytes_be (len * Lib.IntTypes.numbytes t) n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Prims.nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.IntTypes.bits", "Lib.Sequence.eq_intro", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.ByteSequence.uints_to_bytes_be", "Lib.ByteSequence.nat_to_intseq_be", "Lib.ByteSequence.nat_to_bytes_be", "Prims.unit", "FStar.Classical.forall_intro", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.uints_to_bytes_be_nat_lemma_" ]
[]
false
false
true
false
false
let uints_to_bytes_be_nat_lemma #t #l len n =
Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_zero
val lemma_zero (f:G.field) : Lemma (requires True) (ensures to_poly #f G.zero == zero) [SMTPat (to_poly #f G.zero)]
val lemma_zero (f:G.field) : Lemma (requires True) (ensures to_poly #f G.zero == zero) [SMTPat (to_poly #f G.zero)]
let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 83, "start_col": 0, "start_line": 71 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; ()
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_poly Spec.GaloisField.zero == Vale.Math.Poly2_s.zero) [SMTPat (Vale.Math.Poly2.Galois.to_poly Spec.GaloisField.zero)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2.Lemmas.lemma_pointwise_equal", "Prims.int", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.bool", "Vale.Math.Poly2_s.poly_index", "Prims.Nil", "FStar.Pervasives.pattern", "Prims.op_AmpAmp", "Prims.op_LessThanOrEqual", "Prims.op_LessThan", "FStar.UInt.zero_to_vec_lemma", "Prims.op_Subtraction", "Vale.Math.Poly2.Lemmas.lemma_zero_define", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Vale.Math.Poly2_s.op_String_Access", "Vale.Math.Poly2_s.poly", "Vale.Math.Poly2.Galois.to_poly", "Spec.GaloisField.zero", "Vale.Math.Poly2_s.zero", "Lib.IntTypes.bits" ]
[]
false
false
true
false
false
let lemma_zero f =
let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i: int) : Lemma (a.[ i ] == b.[ i ]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_uint_from_to_bytes_be_preserves_value
val lemma_uint_from_to_bytes_be_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> s : lbytes_l l (numbytes t) -> Lemma(uint_to_bytes_be #t #l (uint_from_bytes_be #t #l s) `equal` s)
val lemma_uint_from_to_bytes_be_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> s : lbytes_l l (numbytes t) -> Lemma(uint_to_bytes_be #t #l (uint_from_bytes_be #t #l s) `equal` s)
let lemma_uint_from_to_bytes_be_preserves_value #t #l s = let i = uint_from_bytes_be #t #l s in let s' = uint_to_bytes_be #t #l i in lemma_nat_from_to_bytes_be_preserves_value #l s' (length s'); assert(nat_to_bytes_be #l (length s') (nat_from_bytes_be s') == s'); lemma_uint_to_bytes_be_preserves_value #t #l i; assert(nat_from_bytes_be (uint_to_bytes_be #t #l i) == uint_v i); assert(s' == nat_to_bytes_be #l (length s') (uint_v i)); assert(s' == nat_to_bytes_be #l (length s') (uint_v (uint_from_bytes_be #t #l s))); lemma_reveal_uint_to_bytes_be #t #l s; assert(s' == nat_to_bytes_be #l (length s') (nat_from_bytes_be s)); lemma_nat_from_to_bytes_be_preserves_value #l s (length s)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 60, "end_line": 797, "start_col": 0, "start_line": 786 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len b let lemma_reveal_uint_to_bytes_le #t #l b = () let lemma_reveal_uint_to_bytes_be #t #l b = () let lemma_uint_to_from_bytes_le_preserves_value #t #l i = lemma_reveal_uint_to_bytes_le #t #l (uint_to_bytes_le #t #l i); assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v (uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i))); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i) let lemma_uint_to_from_bytes_be_preserves_value #t #l i = lemma_reveal_uint_to_bytes_be #t #l (uint_to_bytes_be #t #l i); lemma_uint_to_bytes_be_preserves_value #t #l i let lemma_uint_from_to_bytes_le_preserves_value #t #l s = let i = uint_from_bytes_le #t #l s in let s' = uint_to_bytes_le #t #l i in lemma_nat_from_to_bytes_le_preserves_value #l s' (length s'); assert(nat_to_bytes_le #l (length s') (nat_from_bytes_le s') == s'); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i); assert(s' == nat_to_bytes_le #l (length s') (uint_v i)); assert(s' == nat_to_bytes_le #l (length s') (uint_v (uint_from_bytes_le #t #l s))); lemma_reveal_uint_to_bytes_le #t #l s; assert(s' == nat_to_bytes_le #l (length s') (nat_from_bytes_le s)); lemma_nat_from_to_bytes_le_preserves_value #l s (length s)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.Sequence.equal (Lib.ByteSequence.uint_to_bytes_be (Lib.ByteSequence.uint_from_bytes_be s)) s)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.ByteSequence.lbytes_l", "Lib.IntTypes.numbytes", "Lib.ByteSequence.lemma_nat_from_to_bytes_be_preserves_value", "Lib.Sequence.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.unit", "Prims._assert", "Prims.eq2", "Lib.Sequence.seq", "Prims.l_or", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Lib.ByteSequence.nat_from_bytes_be", "Lib.ByteSequence.nat_from_intseq_be", "FStar.Seq.Base.length", "Lib.ByteSequence.nat_to_bytes_be", "Lib.ByteSequence.lemma_reveal_uint_to_bytes_be", "Lib.IntTypes.uint_v", "Lib.ByteSequence.uint_from_bytes_be", "Prims.int", "Prims.op_GreaterThanOrEqual", "Lib.ByteSequence.uint_to_bytes_be", "Lib.IntTypes.range", "Lib.ByteSequence.lemma_uint_to_bytes_be_preserves_value", "Lib.Sequence.lseq", "Lib.IntTypes.int_t" ]
[]
true
false
true
false
false
let lemma_uint_from_to_bytes_be_preserves_value #t #l s =
let i = uint_from_bytes_be #t #l s in let s' = uint_to_bytes_be #t #l i in lemma_nat_from_to_bytes_be_preserves_value #l s' (length s'); assert (nat_to_bytes_be #l (length s') (nat_from_bytes_be s') == s'); lemma_uint_to_bytes_be_preserves_value #t #l i; assert (nat_from_bytes_be (uint_to_bytes_be #t #l i) == uint_v i); assert (s' == nat_to_bytes_be #l (length s') (uint_v i)); assert (s' == nat_to_bytes_be #l (length s') (uint_v (uint_from_bytes_be #t #l s))); lemma_reveal_uint_to_bytes_be #t #l s; assert (s' == nat_to_bytes_be #l (length s') (nat_from_bytes_be s)); lemma_nat_from_to_bytes_be_preserves_value #l s (length s)
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_uints_to_from_bytes_le_preserves_value
val lemma_uints_to_from_bytes_le_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> s : lseq (uint_t t l) len -> Lemma(uints_from_bytes_le #t #l (uints_to_bytes_le #t #l s) == s)
val lemma_uints_to_from_bytes_le_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> s : lseq (uint_t t l) len -> Lemma(uints_from_bytes_le #t #l (uints_to_bytes_le #t #l s) == s)
let lemma_uints_to_from_bytes_le_preserves_value #t #l #len s = let lemma_uints_to_from_bytes_le_preserves_value_i (i : size_nat {i < len}) : Lemma(index (uints_from_bytes_le #t #l (uints_to_bytes_le #t #l s)) i == index s i) = let b8 = uints_to_bytes_le #t #l s in index_uints_from_bytes_le #t #l #len b8 i; assert (index (uints_from_bytes_le b8) i == uint_from_bytes_le (sub b8 (i * numbytes t) (numbytes t))); lemma_uints_to_bytes_le_sub s i; assert (sub b8 (i * numbytes t) (numbytes t) == uint_to_bytes_le (index s i)); lemma_uint_to_from_bytes_le_preserves_value (index s i) in Classical.forall_intro lemma_uints_to_from_bytes_le_preserves_value_i; eq_intro (uints_from_bytes_le #t #l (uints_to_bytes_le #t #l s)) s
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 68, "end_line": 860, "start_col": 0, "start_line": 848 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len b let lemma_reveal_uint_to_bytes_le #t #l b = () let lemma_reveal_uint_to_bytes_be #t #l b = () let lemma_uint_to_from_bytes_le_preserves_value #t #l i = lemma_reveal_uint_to_bytes_le #t #l (uint_to_bytes_le #t #l i); assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v (uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i))); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i) let lemma_uint_to_from_bytes_be_preserves_value #t #l i = lemma_reveal_uint_to_bytes_be #t #l (uint_to_bytes_be #t #l i); lemma_uint_to_bytes_be_preserves_value #t #l i let lemma_uint_from_to_bytes_le_preserves_value #t #l s = let i = uint_from_bytes_le #t #l s in let s' = uint_to_bytes_le #t #l i in lemma_nat_from_to_bytes_le_preserves_value #l s' (length s'); assert(nat_to_bytes_le #l (length s') (nat_from_bytes_le s') == s'); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i); assert(s' == nat_to_bytes_le #l (length s') (uint_v i)); assert(s' == nat_to_bytes_le #l (length s') (uint_v (uint_from_bytes_le #t #l s))); lemma_reveal_uint_to_bytes_le #t #l s; assert(s' == nat_to_bytes_le #l (length s') (nat_from_bytes_le s)); lemma_nat_from_to_bytes_le_preserves_value #l s (length s) let lemma_uint_from_to_bytes_be_preserves_value #t #l s = let i = uint_from_bytes_be #t #l s in let s' = uint_to_bytes_be #t #l i in lemma_nat_from_to_bytes_be_preserves_value #l s' (length s'); assert(nat_to_bytes_be #l (length s') (nat_from_bytes_be s') == s'); lemma_uint_to_bytes_be_preserves_value #t #l i; assert(nat_from_bytes_be (uint_to_bytes_be #t #l i) == uint_v i); assert(s' == nat_to_bytes_be #l (length s') (uint_v i)); assert(s' == nat_to_bytes_be #l (length s') (uint_v (uint_from_bytes_be #t #l s))); lemma_reveal_uint_to_bytes_be #t #l s; assert(s' == nat_to_bytes_be #l (length s') (nat_from_bytes_be s)); lemma_nat_from_to_bytes_be_preserves_value #l s (length s) let rec nat_from_intseq_be_public_to_secret #t len b = if len = 1 then begin nat_from_intseq_be_lemma0 b; nat_from_intseq_be_lemma0 (map secret b) end else begin let b_secret = map secret b in let b1 = slice b 1 len in let b1_secret = slice b_secret 1 len in nat_from_intseq_be_public_to_secret #t (len - 1) b1; //assert (nat_from_intseq_be b1 == nat_from_intseq_be (map secret b1)); nat_from_intseq_be_slice_lemma b 1; nat_from_intseq_be_lemma0 (slice b 0 1); //assert (nat_from_intseq_be b == nat_from_intseq_be b1 + pow2 ((len - 1) * bits t) * v b.[0]); nat_from_intseq_be_slice_lemma b_secret 1; nat_from_intseq_be_lemma0 (slice b_secret 0 1); //assert (nat_from_intseq_be b_secret == nat_from_intseq_be b1_secret + pow2 ((len - 1) * bits t) * v b_secret.[0]); //assert (v b.[0] == v b_secret.[0]); eq_intro (slice (map secret b) 1 len) (map secret (slice b 1 len)); () end let rec nat_from_intseq_le_public_to_secret #t len b = if len = 1 then begin nat_from_intseq_le_lemma0 b; nat_from_intseq_le_lemma0 (map secret b) end else begin let b_secret = map secret b in let b1 = slice b 1 len in let b1_secret = slice b_secret 1 len in nat_from_intseq_le_public_to_secret #t (len - 1) b1; //assert (nat_from_intseq_le b1 == nat_from_intseq_le (map secret b1)); nat_from_intseq_le_slice_lemma b 1; nat_from_intseq_le_lemma0 (slice b 0 1); //assert (nat_from_intseq_le b == nat_from_intseq_le (slice b 0 1) + pow2 (bits t) * nat_from_intseq_le (slice b 1 len)); nat_from_intseq_le_slice_lemma b_secret 1; nat_from_intseq_le_lemma0 (slice b_secret 0 1); eq_intro (slice (map secret b) 1 len) (map secret (slice b 1 len)); () end let lemma_uints_to_bytes_le_sub #t #l #len s i = let lemma_uints_to_bytes_le_i_j (j : size_nat {j < numbytes t}): Lemma(index (uints_to_bytes_le #t #l s) (i * numbytes t + j) == index (uint_to_bytes_le (index s i)) j) = index_uints_to_bytes_le #t #l #len s (i*numbytes t + j); assert (index (uints_to_bytes_le #t #l s) (i*numbytes t + j) == index (uint_to_bytes_le (index s i)) j) in Classical.forall_intro lemma_uints_to_bytes_le_i_j; eq_intro (sub (uints_to_bytes_le #t #l s) (i * numbytes t) (numbytes t)) (uint_to_bytes_le (index s i))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.uints_from_bytes_le (Lib.ByteSequence.uints_to_bytes_le s) == s)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_nat", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Lib.Sequence.eq_intro", "Lib.ByteSequence.uints_from_bytes_le", "Lib.ByteSequence.uints_to_bytes_le", "Prims.unit", "FStar.Classical.forall_intro", "Prims.eq2", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.to_seq", "Lib.Sequence.index", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Subtraction", "Prims.l_True", "Prims.squash", "Lib.IntTypes.int_t", "Prims.Nil", "FStar.Pervasives.pattern", "Lib.ByteSequence.lemma_uint_to_from_bytes_le_preserves_value", "Prims._assert", "Lib.IntTypes.U8", "Lib.Sequence.sub", "Lib.ByteSequence.uint_to_bytes_le", "Lib.ByteSequence.lemma_uints_to_bytes_le_sub", "Lib.ByteSequence.uint_from_bytes_le", "Lib.ByteSequence.index_uints_from_bytes_le", "Prims.op_Multiply" ]
[]
false
false
true
false
false
let lemma_uints_to_from_bytes_le_preserves_value #t #l #len s =
let lemma_uints_to_from_bytes_le_preserves_value_i (i: size_nat{i < len}) : Lemma (index (uints_from_bytes_le #t #l (uints_to_bytes_le #t #l s)) i == index s i) = let b8 = uints_to_bytes_le #t #l s in index_uints_from_bytes_le #t #l #len b8 i; assert (index (uints_from_bytes_le b8) i == uint_from_bytes_le (sub b8 (i * numbytes t) (numbytes t))); lemma_uints_to_bytes_le_sub s i; assert (sub b8 (i * numbytes t) (numbytes t) == uint_to_bytes_le (index s i)); lemma_uint_to_from_bytes_le_preserves_value (index s i) in Classical.forall_intro lemma_uints_to_from_bytes_le_preserves_value_i; eq_intro (uints_from_bytes_le #t #l (uints_to_bytes_le #t #l s)) s
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_and
val lemma_and (f:G.field) (e1 e2:G.felem f) : Lemma (requires True) (ensures to_poly (I.logand e1 e2) == (to_poly e1 &. to_poly e2)) [SMTPat (to_poly (I.logand e1 e2))]
val lemma_and (f:G.field) (e1 e2:G.felem f) : Lemma (requires True) (ensures to_poly (I.logand e1 e2) == (to_poly e1 &. to_poly e2)) [SMTPat (to_poly (I.logand e1 e2))]
let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 67, "end_line": 119, "start_col": 0, "start_line": 113 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> e1: Spec.GaloisField.felem f -> e2: Spec.GaloisField.felem f -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_poly (Lib.IntTypes.logand e1 e2) == (Vale.Math.Poly2.Galois.to_poly e1 &. Vale.Math.Poly2.Galois.to_poly e2)) [SMTPat (Vale.Math.Poly2.Galois.to_poly (Lib.IntTypes.logand e1 e2))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2.lemma_equal", "Vale.Math.Poly2.Galois.to_poly", "Lib.IntTypes.logand", "Spec.GaloisField.__proj__GF__item__t", "Vale.Math.Poly2.Galois.op_Amp_Dot", "Prims.unit", "Vale.Math.Poly2.Lemmas.lemma_and_define_all", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Vale.Math.Poly2.Galois.IntTypes.define_logand" ]
[]
false
false
true
false
false
let lemma_and f e1 e2 =
let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.poly_length
val poly_length (p: poly) : nat
val poly_length (p: poly) : nat
let poly_length (p:poly) : nat = (if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 164, "start_col": 0, "start_line": 162 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2) let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) let lemma_shift_right f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); assert (I.uint_v (I.shift_right e n) == U.shift_right #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_right e n)) (shift (to_poly e) (-(I.uint_v n))) #reset-options (* Various definitions of multiply, ranging from Poly2 definition to GaloisField definition: - Vale.Math.Poly2.Defs_s.mul - mul_def: same as Defs_s.mul, but using abstract definition of poly - pmul: equivalent to mul_def, but defined recursively based on adds and shifts by n - mmul: same as pmul, but also do a "mod m" in the add - smul: same as mmul, but incrementally shift by 1 rather than shifting by n and implement "mod m" using add - gmul: same as fmul, but defined using recursion instead of repeati - fmul: same as Spec.GaloisField.fmul, but use named function instead of lambda - Spec.GaloisField.fmul *)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Vale.Math.Poly2_s.poly -> Prims.nat
Prims.Tot
[ "total" ]
[]
[ "Vale.Math.Poly2_s.poly", "Prims.op_Addition", "Vale.Math.Poly2_s.degree", "Prims.unit", "Prims.op_LessThan", "Vale.Math.Poly2.Lemmas.lemma_degree_negative", "Prims.bool", "Prims.nat" ]
[]
false
false
false
true
false
let poly_length (p: poly) : nat =
(if degree p < 0 then PL.lemma_degree_negative p); 1 + degree p
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_add
val lemma_add (f:G.field) (e1 e2:G.felem f) : Lemma (requires True) (ensures to_poly (G.fadd e1 e2) == to_poly e1 +. to_poly e2) [SMTPat (to_poly (G.fadd e1 e2))]
val lemma_add (f:G.field) (e1 e2:G.felem f) : Lemma (requires True) (ensures to_poly (G.fadd e1 e2) == to_poly e1 +. to_poly e2) [SMTPat (to_poly (G.fadd e1 e2))]
let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 65, "end_line": 111, "start_col": 0, "start_line": 105 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> e1: Spec.GaloisField.felem f -> e2: Spec.GaloisField.felem f -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_poly (Spec.GaloisField.fadd e1 e2) == Vale.Math.Poly2.Galois.to_poly e1 +. Vale.Math.Poly2.Galois.to_poly e2) [SMTPat (Vale.Math.Poly2.Galois.to_poly (Spec.GaloisField.fadd e1 e2))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2.lemma_equal", "Vale.Math.Poly2.Galois.to_poly", "Spec.GaloisField.fadd", "Vale.Math.Poly2.op_Plus_Dot", "Prims.unit", "Vale.Math.Poly2.Lemmas.lemma_add_define_all", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Vale.Math.Poly2.Galois.IntTypes.define_logxor" ]
[]
false
false
true
false
false
let lemma_add f e1 e2 =
let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_or
val lemma_or (f:G.field) (e1 e2:G.felem f) : Lemma (requires True) (ensures to_poly (I.logor e1 e2) == (to_poly e1 |. to_poly e2)) [SMTPat (to_poly (I.logor e1 e2))]
val lemma_or (f:G.field) (e1 e2:G.felem f) : Lemma (requires True) (ensures to_poly (I.logor e1 e2) == (to_poly e1 |. to_poly e2)) [SMTPat (to_poly (I.logor e1 e2))]
let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2)
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 127, "start_col": 0, "start_line": 121 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> e1: Spec.GaloisField.felem f -> e2: Spec.GaloisField.felem f -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_poly (Lib.IntTypes.logor e1 e2) == (Vale.Math.Poly2.Galois.to_poly e1 |. Vale.Math.Poly2.Galois.to_poly e2)) [SMTPat (Vale.Math.Poly2.Galois.to_poly (Lib.IntTypes.logor e1 e2))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2.lemma_equal", "Vale.Math.Poly2.Galois.to_poly", "Lib.IntTypes.logor", "Spec.GaloisField.__proj__GF__item__t", "Vale.Math.Poly2.Galois.op_Bar_Dot", "Prims.unit", "Vale.Math.Poly2.Lemmas.lemma_or_define_all", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Vale.Math.Poly2.Galois.IntTypes.define_logor" ]
[]
false
false
true
false
false
let lemma_or f e1 e2 =
let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_one
val lemma_one (f:G.field) : Lemma (requires True) (ensures to_poly #f G.one == one) [SMTPat (to_poly #f G.one)]
val lemma_one (f:G.field) : Lemma (requires True) (ensures to_poly #f G.one == one) [SMTPat (to_poly #f G.one)]
let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 103, "start_col": 0, "start_line": 91 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_poly Spec.GaloisField.one == Vale.Math.Poly2_s.one) [SMTPat (Vale.Math.Poly2.Galois.to_poly Spec.GaloisField.one)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2.Lemmas.lemma_pointwise_equal", "Prims.int", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.bool", "Vale.Math.Poly2_s.poly_index", "Prims.Nil", "FStar.Pervasives.pattern", "Prims.op_AmpAmp", "Prims.op_LessThanOrEqual", "Prims.op_LessThan", "FStar.UInt.one_to_vec_lemma", "Prims.op_Subtraction", "Vale.Math.Poly2.Lemmas.lemma_one_define", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all", "Vale.Math.Poly2_s.op_String_Access", "Vale.Math.Poly2_s.poly", "Vale.Math.Poly2.Galois.to_poly", "Spec.GaloisField.one", "Vale.Math.Poly2_s.one", "Lib.IntTypes.bits" ]
[]
false
false
true
false
false
let lemma_one f =
let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i: int) : Lemma (a.[ i ] == b.[ i ]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i
false
Lib.ByteSequence.fst
Lib.ByteSequence.uints_from_bytes_le_nat_lemma_aux
val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))))
val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))))
let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 104, "end_line": 460, "start_col": 0, "start_line": 455 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) *
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_le_ (Lib.ByteSequence.uints_from_bytes_le b) == Lib.ByteSequence.nat_from_intseq_le_ (Lib.Sequence.slice b 0 (Lib.IntTypes.numbytes t)) + Prims.pow2 (Lib.IntTypes.bits t) * Lib.ByteSequence.nat_from_intseq_le_ (Lib.ByteSequence.uints_from_bytes_le (Lib.Sequence.slice b (Lib.IntTypes.numbytes t) (len * Lib.IntTypes.numbytes t))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.numbytes", "Prims.pow2", "Lib.ByteSequence.lbytes_l", "Prims._assert", "Prims.eq2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Prims.op_Subtraction", "Lib.Sequence.slice", "Lib.ByteSequence.uints_from_bytes_le", "Lib.IntTypes.U8", "Prims.unit", "Lib.ByteSequence.uints_from_bytes_le_slice_lemma", "Prims.int", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "Lib.Sequence.length", "Lib.IntTypes.bits", "Lib.IntTypes.range", "Lib.ByteSequence.nat_from_intseq_le_", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Prims.op_Addition", "Lib.IntTypes.int_t" ]
[]
true
false
true
false
false
let uints_from_bytes_le_nat_lemma_aux #t #l #len b =
let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[ 0 ] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[ 0 ]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)))
false
Lib.ByteSequence.fst
Lib.ByteSequence.nat_from_intseq_be_inj
val nat_from_intseq_be_inj: #t:inttype{unsigned t} -> #l:secrecy_level -> b1:seq (uint_t t l) -> b2:seq (uint_t t l) -> Lemma (requires length b1 == length b2 /\ nat_from_intseq_be b1 == nat_from_intseq_be b2) (ensures Seq.equal b1 b2) (decreases (length b1))
val nat_from_intseq_be_inj: #t:inttype{unsigned t} -> #l:secrecy_level -> b1:seq (uint_t t l) -> b2:seq (uint_t t l) -> Lemma (requires length b1 == length b2 /\ nat_from_intseq_be b1 == nat_from_intseq_be b2) (ensures Seq.equal b1 b2) (decreases (length b1))
let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 736, "start_col": 0, "start_line": 730 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> b2: Lib.Sequence.seq (Lib.IntTypes.uint_t t l) -> FStar.Pervasives.Lemma (requires Lib.Sequence.length b1 == Lib.Sequence.length b2 /\ Lib.ByteSequence.nat_from_intseq_be b1 == Lib.ByteSequence.nat_from_intseq_be b2) (ensures FStar.Seq.Base.equal b1 b2) (decreases Lib.Sequence.length b1)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.secrecy_level", "Lib.Sequence.seq", "Lib.IntTypes.uint_t", "Prims.op_Equality", "Prims.int", "Lib.Sequence.length", "Prims.bool", "FStar.Seq.Properties.lemma_split", "Prims.op_Subtraction", "Prims.unit", "Lib.ByteSequence.nat_from_intseq_be_inj", "FStar.Seq.Base.slice" ]
[ "recursion" ]
false
false
true
false
false
let rec nat_from_intseq_be_inj #t #l b1 b2 =
if length b1 = 0 then () else (nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1))
false
Lib.ByteSequence.fst
Lib.ByteSequence.lemma_uint_from_to_bytes_le_preserves_value
val lemma_uint_from_to_bytes_le_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> s : lbytes_l l (numbytes t) -> Lemma(uint_to_bytes_le #t #l (uint_from_bytes_le #t #l s) `equal` s)
val lemma_uint_from_to_bytes_le_preserves_value : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> s : lbytes_l l (numbytes t) -> Lemma(uint_to_bytes_le #t #l (uint_from_bytes_le #t #l s) `equal` s)
let lemma_uint_from_to_bytes_le_preserves_value #t #l s = let i = uint_from_bytes_le #t #l s in let s' = uint_to_bytes_le #t #l i in lemma_nat_from_to_bytes_le_preserves_value #l s' (length s'); assert(nat_to_bytes_le #l (length s') (nat_from_bytes_le s') == s'); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i); assert(s' == nat_to_bytes_le #l (length s') (uint_v i)); assert(s' == nat_to_bytes_le #l (length s') (uint_v (uint_from_bytes_le #t #l s))); lemma_reveal_uint_to_bytes_le #t #l s; assert(s' == nat_to_bytes_le #l (length s') (nat_from_bytes_le s)); lemma_nat_from_to_bytes_le_preserves_value #l s (length s)
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 60, "end_line": 784, "start_col": 0, "start_line": 773 }
module Lib.ByteSequence open FStar.Mul open Lib.IntTypes open Lib.Sequence open Lib.RawIntTypes open Lib.LoopCombinators #reset-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0" /// BEGIN constant-time sequence equality val lemma_not_equal_slice: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat -> k:nat{i <= j /\ i <= k /\ j <= k /\ k <= Seq.length b1 /\ k <= Seq.length b2 } -> Lemma (requires ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) (ensures ~(Seq.equal (Seq.slice b1 i k) (Seq.slice b2 i k))) let lemma_not_equal_slice #a b1 b2 i j k = assert (forall (n:nat{n < k - i}). Seq.index (Seq.slice b1 i k) n == Seq.index b1 (n + i)) val lemma_not_equal_last: #a:Type -> b1:Seq.seq a -> b2:Seq.seq a -> i:nat -> j:nat{i < j /\ j <= Seq.length b1 /\ j <= Seq.length b2} -> Lemma (requires ~(Seq.index b1 (j - 1) == Seq.index b2 (j - 1))) (ensures ~(Seq.equal (Seq.slice b1 i j) (Seq.slice b2 i j))) let lemma_not_equal_last #a b1 b2 i j = Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1) val seq_eq_mask_inner: #t:inttype{~(S128? t)} -> #len1:size_nat -> #len2:size_nat -> b1:lseq (int_t t SEC) len1 -> b2:lseq (int_t t SEC) len2 -> len:size_nat{len <= len1 /\ len <= len2} -> i:size_nat{i < len} -> res:int_t t SEC{ (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))} -> res':int_t t SEC{ (sub b1 0 (i + 1) == sub b2 0 (i + 1) ==> v res' == v (ones t SEC)) /\ (sub b1 0 (i + 1) =!= sub b2 0 (i + 1) ==> v res' == v (zeros t SEC))} let seq_eq_mask_inner #t #len1 #len2 b1 b2 len i res = logand_zeros (ones t SEC); logand_ones (ones t SEC); logand_zeros (zeros t SEC); logand_ones (zeros t SEC); let z0 = res in let res = eq_mask b1.[i] b2.[i] &. z0 in logand_spec (eq_mask b1.[i] b2.[i]) z0; if v res = ones_v t then begin let s1 = sub b1 0 (i + 1) in let s2 = sub b2 0 (i + 1) in Seq.lemma_split s1 i; Seq.lemma_split s2 i; assert (equal s1 s2) end else if v z0 = 0 then lemma_not_equal_slice b1 b2 0 i (i + 1) else lemma_not_equal_last b1 b2 0 (i + 1); res let seq_eq_mask #t #len1 #len2 b1 b2 len = repeati_inductive len (fun (i:nat{i <= len}) res -> (sub b1 0 i == sub b2 0 i ==> v res == v (ones t SEC)) /\ (sub b1 0 i =!= sub b2 0 i ==> v res == v (zeros t SEC))) (seq_eq_mask_inner b1 b2 len) (ones t SEC) let lbytes_eq #len b1 b2 = let res = seq_eq_mask b1 b2 len in RawIntTypes.u8_to_UInt8 res = 255uy /// END constant-time sequence equality let mask_select #t mask a b = b ^. (mask &. (a ^. b)) let mask_select_lemma #t mask a b = let t1 = mask &. (a ^. b) in let t2 = b ^. t1 in logand_lemma mask (a ^.b); if v mask = 0 then begin assert (v t1 == 0); logxor_lemma b t1; assert (v t2 = v b); () end else begin assert (v t1 == v (a ^. b)); logxor_lemma b a; assert (v t2 = v a); () end let seq_mask_select #t #len a b mask = let res = map2 (mask_select mask) a b in let lemma_aux (i:nat{i < len}) : Lemma (v res.[i] == (if v mask = 0 then v b.[i] else v a.[i])) = mask_select_lemma mask a.[i] b.[i] in Classical.forall_intro lemma_aux; if v mask = 0 then eq_intro res b else eq_intro res a; res val nat_from_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_be_ #t #l b = let len = length b in if len = 0 then 0 else let l = v (Seq.index b (len - 1)) in let pre = Seq.slice b 0 (len - 1) in let shift = pow2 (bits t) in let n' = nat_from_intseq_be_ pre in Math.Lemmas.pow2_plus (bits t) (len * bits t - bits t); l + shift * n' let nat_from_intseq_be = nat_from_intseq_be_ val nat_from_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> b:seq (uint_t t l) -> Tot (n:nat{n < pow2 (length b * bits t)}) (decreases (length b)) let rec nat_from_intseq_le_ #t #l b = let len = length b in if len = 0 then 0 else let shift = pow2 (bits t) in let tt = Seq.slice b 1 len in let n' = nat_from_intseq_le_ #t #l tt in let l = v (Seq.index b 0) in Math.Lemmas.pow2_plus (bits t) ( len * bits t - bits t); let n = l + shift * n' in n let nat_from_intseq_le = nat_from_intseq_le_ #set-options "--max_fuel 1" val nat_to_intseq_be_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (int_t t l){length b == len /\ n == nat_from_intseq_be b}) (decreases len) let rec nat_to_intseq_be_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_be_ len' n' in let b = Seq.append b' (create 1 tt) in Seq.append_slices b' (create 1 tt); b let nat_to_intseq_be = nat_to_intseq_be_ val nat_to_intseq_le_: #t:inttype{unsigned t} -> #l:secrecy_level -> len:nat -> n:nat{n < pow2 (bits t * len)} -> Tot (b:seq (uint_t t l){length b == len /\ n == nat_from_intseq_le b}) (decreases len) let rec nat_to_intseq_le_ #t #l len n = if len = 0 then Seq.empty else let len' = len - 1 in let tt = uint #t #l (n % modulus t) in let n' = n / modulus t in assert (modulus t = pow2 (bits t)); FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); let b' = nat_to_intseq_le_ len' n' in let b = Seq.append (create 1 tt) b' in Seq.append_slices (create 1 tt) b'; b let nat_to_intseq_le = nat_to_intseq_le_ /// These lemmas allow to unfold the definition of nat_to_intseq_{b}e without using /// fuel > 0 below, which makes the proof more expensive val head_nat_to_intseq_le: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_le #t #l len n) 0 == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_le #t #l len n = () val nat_to_intseq_le_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_le #t #l len n == Seq.append (create 1 (uint #t #l (n % modulus t))) (nat_to_intseq_le (len - 1) (n / modulus t))) let nat_to_intseq_le_pos #t #l len n = () val head_nat_to_intseq_be: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (Seq.index (nat_to_intseq_be #t #l len n) (len - 1) == uint #t #l (n % pow2 (bits t))) let head_nat_to_intseq_be #t #l len n = () val nat_to_intseq_be_pos: #t:inttype{unsigned t} -> #l:secrecy_level -> len:size_pos -> n:nat{n < pow2 (bits t * len)} -> Lemma (nat_to_intseq_be #t #l len n == Seq.append (nat_to_intseq_be (len - 1) (n / modulus t)) (create 1 (uint #t #l (n % modulus t)))) let nat_to_intseq_be_pos #t #l len n = () #push-options "--fuel 0 --ifuel 0" let rec index_nat_to_intseq_le #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_le #t #l len n else begin FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_le #t #l (len - 1) (n / modulus t)) (i - 1); == { index_nat_to_intseq_le #t #l (len - 1) (n / modulus t) (i - 1) } uint ((n / modulus t) / pow2 (bits t * (i - 1)) % modulus t); == { Math.Lemmas.division_multiplication_lemma n (modulus t) (pow2 (bits t * (i - 1))) } uint ((n / (pow2 (bits t) * pow2 (bits t * (i - 1)))) % modulus t); == { Math.Lemmas.pow2_plus (bits t) (bits t * (i - 1)) } uint ((n / pow2 (bits t + bits t * (i - 1))) % modulus t); == { Math.Lemmas.distributivity_add_right (bits t) i (-1) } uint (n / pow2 (bits t + (bits t * i - bits t)) % modulus t); == { } uint (n / pow2 (bits t * i) % pow2 (bits t)); }; nat_to_intseq_le_pos #t #l len n end let rec index_nat_to_intseq_be #t #l len n i = if i = 0 then if len = 0 then () else head_nat_to_intseq_be #t #l len n else begin let len' = len - 1 in let i' = i - 1 in let n' = n / pow2 (bits t) in FStar.Math.Lemmas.lemma_div_lt_nat n (bits t * len) (bits t); calc (==) { Seq.index (nat_to_intseq_be #t #l len' n') (len' - i' - 1); == {index_nat_to_intseq_be #t #l len' n' i'} uint (n' / pow2 (bits t * i') % pow2 (bits t)); == {} uint (n / pow2 (bits t) / pow2 (bits t * i') % pow2 (bits t)); == {Math.Lemmas.division_multiplication_lemma n (pow2 (bits t)) (pow2 (bits t * i'))} uint (n / (pow2 (bits t) * pow2 (bits t * i')) % pow2 (bits t)); == {Math.Lemmas.pow2_plus (bits t) (bits t * i')} uint (n / (pow2 (bits t + bits t * i')) % pow2 (bits t)); == {Math.Lemmas.distributivity_add_right (bits t) 1 (i - 1)} uint (n / (pow2 (bits t * i)) % pow2 (bits t)); }; nat_to_intseq_be_pos #t #l len n end let uint_to_bytes_le #t #l n = nat_to_bytes_le (numbytes t) (v n) let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u)) let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n) let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u)) let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n val uints_to_bytes_le_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_le_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_le #t #l b.[i] let uints_to_bytes_le #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_le_inner #t #l #len ul) () in o (* Could also be written more simply as: let uints_to_bytes_le #t #l #len ul = createi (len * numbytes t) (fun i -> let s = uint_to_bytes_le ul.[i / numbytes t] in s.[i % numbytes t]) *) let index_uints_to_bytes_le #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i val uints_to_bytes_be_inner: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> lseq (int_t t l) len -> i:nat{i < len} -> unit -> unit & (lseq (uint_t U8 l) (numbytes t)) let uints_to_bytes_be_inner #t #l #len b i () = let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[i] let uints_to_bytes_be #t #l #len ul = let a_spec (i:nat{i <= len}) = unit in let _, o = generate_blocks (numbytes t) len len a_spec (uints_to_bytes_be_inner #t #l #len ul) () in o let index_uints_to_bytes_be #t #l #len ul i = index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i let uints_from_bytes_le #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_le #t #l #len b i = () let uints_from_bytes_be #t #l #len b = Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))) let index_uints_from_bytes_be #t #l #len b i = () let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t)) let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)) #push-options "--max_fuel 1" val nat_from_intseq_slice_lemma_aux: len:pos -> a:nat -> b:nat -> c:nat -> i:pos{i <= len} -> Lemma (pow2 ((i - 1) * c) * (a + pow2 c * b) == pow2 ((i - 1) * c) * a + pow2 (i * c) * b) let nat_from_intseq_slice_lemma_aux len a b c i = FStar.Math.Lemmas.distributivity_add_right (pow2 ((i - 1) * c)) a (pow2 c * b); FStar.Math.Lemmas.paren_mul_right (pow2 ((i - 1) * c)) (pow2 c) b; FStar.Math.Lemmas.pow2_plus ((i - 1) * c) c val nat_from_intseq_le_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 i) + pow2 (i * bits t) * nat_from_intseq_le_ (slice b i len)) let rec nat_from_intseq_le_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = 0 then () else begin let b1 = slice b 0 i in nat_from_intseq_le_slice_lemma_ #t #l #i b1 (i - 1); assert (nat_from_intseq_le_ b1 == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * v b.[i - 1]); nat_from_intseq_le_slice_lemma_ #t #l #len b (i - 1); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (i - 1)) + pow2 ((i - 1) * bits t) * nat_from_intseq_le_ (slice b (i - 1) len)); nat_from_intseq_slice_lemma_aux len (v b.[i - 1]) (nat_from_intseq_le_ (slice b i len)) (bits t) i end end let nat_from_intseq_le_lemma0 #t #l b = () let nat_from_intseq_le_slice_lemma #t #l #len b i = nat_from_intseq_le_slice_lemma_ b i val nat_from_intseq_be_slice_lemma_: #t:inttype{unsigned t} -> #l:secrecy_level -> #len:size_nat -> b:lseq (int_t t l) len -> i:nat{i <= len} -> Lemma (ensures (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b i len) + pow2 ((len - i) * bits t) * nat_from_intseq_be_ (slice b 0 i))) (decreases (len - i)) let rec nat_from_intseq_be_slice_lemma_ #t #l #len b i = if len = 0 then () else begin if i = len then () else begin let b1 = slice b i len in nat_from_intseq_be_slice_lemma_ #t #l #(len - i) b1 1; assert (nat_from_intseq_be_ b1 == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * v b.[i]); nat_from_intseq_be_slice_lemma_ #t #l #len b (i + 1); assert (nat_from_intseq_be_ b == nat_from_intseq_be_ (slice b (i + 1) len) + pow2 ((len - i - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (i + 1))); nat_from_intseq_slice_lemma_aux len (v b.[i]) (nat_from_intseq_be_ (slice b 0 i)) (bits t) (len - i) end end let nat_from_intseq_be_lemma0 #t #l b = () #pop-options let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b i val uints_from_bytes_le_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_le #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_le_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_le #t #l #(j-i) b1 in index_uints_from_bytes_le #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_le (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_le_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_le_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_le #t #l #len b) i j) (uints_from_bytes_le #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) #push-options "--max_fuel 1" val uints_from_bytes_le_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t)))) let uints_from_bytes_le_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_le #t #l #len b in assert (nat_from_intseq_le_ r == v r.[0] + pow2 (bits t) * nat_from_intseq_le_ (slice r 1 len)); assert (nat_from_intseq_le_ (slice b 0 (numbytes t)) == v r.[0]); uints_from_bytes_le_slice_lemma #t #l #len b 1 len; assert (slice r 1 len == uints_from_bytes_le #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) val uints_from_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_le_ (uints_from_bytes_le #t #l #len b) == nat_from_intseq_le_ b) let rec uints_from_bytes_le_nat_lemma_ #t #l #len b = if len = 0 then () else begin let b1 = slice b (numbytes t) (len * numbytes t) in nat_from_intseq_le_slice_lemma_ #U8 #l #(len * numbytes t) b (numbytes t); assert (nat_from_intseq_le_ b == nat_from_intseq_le_ (slice b 0 (numbytes t)) + pow2 (bits t) * nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_ #t #l #(len - 1) b1; assert (nat_from_intseq_le_ (uints_from_bytes_le #t #l #(len - 1) b1) == nat_from_intseq_le_ b1); uints_from_bytes_le_nat_lemma_aux #t #l #len b end let uints_from_bytes_le_nat_lemma #t #l #len b = uints_from_bytes_le_nat_lemma_ #t #l #len b val uints_from_bytes_be_slice_lemma_lp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (slice (uints_from_bytes_le #t #l #len b) i j) k == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_lp #t #l #len b i j k = let r = slice (uints_from_bytes_le #t #l #len b) i j in index_uints_from_bytes_be #t #l #len b (i + k); assert (r.[k] == uint_from_bytes_le (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma_rp: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> k:nat{k < j - i} -> Lemma (index (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) k == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) let uints_from_bytes_be_slice_lemma_rp #t #l #len b i j k = let b1 = slice b (i * numbytes t) (j * numbytes t) in let r = uints_from_bytes_be #t #l #(j-i) b1 in index_uints_from_bytes_be #t #l #(j-i) b1 k; assert (r.[k] == uint_from_bytes_be (sub b1 (k * numbytes t) (numbytes t))); assert (r.[k] == uint_from_bytes_be (sub b ((i + k) * numbytes t) (numbytes t))) val uints_from_bytes_be_slice_lemma: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> i:nat -> j:nat{i <= j /\ j <= len} -> Lemma (slice (uints_from_bytes_be #t #l #len b) i j == uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) let uints_from_bytes_be_slice_lemma #t #l #len b i j = FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_lp #t #l #len b i j); FStar.Classical.forall_intro (uints_from_bytes_be_slice_lemma_rp #t #l #len b i j); eq_intro (slice (uints_from_bytes_be #t #l #len b) i j) (uints_from_bytes_be #t #l #(j-i) (slice b (i * numbytes t) (j * numbytes t))) val uints_from_bytes_be_nat_lemma_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_pos{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be (uints_from_bytes_be #t #l #(len-1) (slice b (numbytes t) (len * numbytes t))) + pow2 ((len - 1) * bits t) * nat_from_intseq_be_ (slice b 0 (numbytes t))) let uints_from_bytes_be_nat_lemma_aux #t #l #len b = let r = uints_from_bytes_be #t #l #len b in uints_from_bytes_be_slice_lemma #t #l #len b 1 len; nat_from_intseq_be_slice_lemma r 1; assert (nat_from_intseq_be_ r == nat_from_intseq_be (slice r 1 len) + pow2 ((len - 1) * bits t) * uint_v r.[0]) val uints_from_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> b:lbytes_l l (len * numbytes t) -> Lemma (nat_from_intseq_be_ (uints_from_bytes_be #t #l #len b) == nat_from_intseq_be_ b) let rec uints_from_bytes_be_nat_lemma_ #t #l #len b = if len = 0 then () else begin uints_from_bytes_be_nat_lemma_aux #t #l #len b; nat_from_intseq_be_slice_lemma_ b (numbytes t); uints_from_bytes_be_nat_lemma_ #t #l #(len - 1) (slice b (numbytes t) (len * numbytes t)) end let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len b #pop-options val index_uints_to_bytes_le_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (uints_to_bytes_le #t #l #len s) i == Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_le_aux #t #l len n i = let open Lib.Sequence in let s: lseq (int_t t l) len = nat_to_intseq_le #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len s) i val index_uints_to_bytes_be_aux: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (uints_to_bytes_be #t #l #len s) i == Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t)) let index_uints_to_bytes_be_aux #t #l len n i = let open Lib.Sequence in let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len s) i val modulo_pow2_prop: r:pos -> a:nat -> b:nat -> c:nat{c < b} -> Lemma ((a % pow2 (r * b) / pow2 (r * c)) % pow2 r == (a / pow2 (r * c)) % pow2 r) let modulo_pow2_prop r a b c = calc (==) { ((a % pow2 (r * b)) / pow2 (r * c)) % pow2 r; == { Math.Lemmas.pow2_modulo_division_lemma_1 a (r * c) (r * b) } ((a / pow2 (r * c) % pow2 (r * b - r * c))) % pow2 r; == { Math.Lemmas.lemma_mul_sub_distr r b c } ((a / pow2 (r * c) % pow2 (r * (b - c)))) % pow2 r; == { Math.Lemmas.pow2_plus r (r * (b - c) - r) } (a / pow2 (r * c)) % (pow2 r * pow2 (r * (b - c) - r)) % pow2 r; == { Math.Lemmas.modulo_modulo_lemma (a / pow2 (r * c)) (pow2 r) (pow2 (r * (b - c) - r))} (a / pow2 (r * c)) % pow2 r; } val some_arithmetic: t:inttype{~(U1? t)} -> n:nat -> i:nat -> Lemma (let m = numbytes t in n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8 == n / pow2 (8 * i) % pow2 8) #push-options "--z3rlimit 150 --fuel 0 --ifuel 0" let some_arithmetic t n i = let m = numbytes t in calc (==) { n / pow2 (bits t * (i / m)) % pow2 (bits t) / pow2 (8 * (i % m)) % pow2 8; == { assert (bits t == 8 * m) } n / pow2 ((8 * m) * (i / m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { FStar.Math.Lemmas.paren_mul_right 8 m (i / m); FStar.Math.Lemmas.euclidean_division_definition i m } n / pow2 (8 * (i - i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.distributivity_sub_right 8 i (i % m) } n / pow2 (8 * i - 8 * (i % m)) % pow2 (8 * m) / pow2 (8 * (i % m)) % pow2 8; == { modulo_pow2_prop 8 (n / pow2 (8 * i - 8 * (i % m))) m (i % m) } (n / pow2 (8 * i - 8 * (i % m))) / pow2 (8 * (i % m)) % pow2 8; == { Math.Lemmas.division_multiplication_lemma n (pow2 (8 * i - 8 * (i % m))) (pow2 (8 * (i % m))) } (n / (pow2 (8 * i - 8 * (i % m)) * pow2 (8 * (i % m)))) % pow2 8; == { Math.Lemmas.pow2_plus (8 * i - 8 * (i % m)) (8 * (i % m)) } (n / pow2 (8 * i)) % pow2 8; } #pop-options val index_nat_to_intseq_to_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t) == Seq.index (nat_to_bytes_le #l (len * numbytes t) n) i) let index_nat_to_intseq_to_bytes_le #t #l len n i = let s:lseq (int_t t l) len = nat_to_intseq_le #t #l len n in let m = numbytes t in index_nat_to_intseq_le #U8 #l (len * m) n i; assert (Seq.index (nat_to_bytes_le #l (len * m) n) i == uint (n / pow2 (8 * i) % pow2 8)); index_nat_to_intseq_le #U8 #l m (v s.[i / m]) (i % m); assert (Seq.index (nat_to_bytes_le #l m (v s.[i / m])) (i % m) == uint (v s.[i / m] / pow2 (8 * (i % m)) % pow2 8)); index_nat_to_intseq_le #t #l len n (i / m); some_arithmetic t n i val uints_to_bytes_le_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i == Seq.index (nat_to_bytes_le (len * numbytes t) n) i) let uints_to_bytes_le_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_le #t #l len n in calc (==) { Seq.index (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) i; == { index_uints_to_bytes_le_aux #t #l len n i } Seq.index (nat_to_bytes_le #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_le #t #l len n i} Seq.index (nat_to_bytes_le (len * numbytes t) n) i; } let uints_to_bytes_le_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_le_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_le #t #l #len (nat_to_intseq_le #t #l len n)) (nat_to_bytes_le (len * numbytes t) n) val index_nat_to_intseq_to_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (let s:lseq (int_t t l) len = nat_to_intseq_be #t #l len n in Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[len - 1 - i / numbytes t])) (numbytes t - 1 - i % numbytes t) == Seq.index (nat_to_bytes_be #l (len * numbytes t) n) (len * numbytes t - i - 1)) let index_nat_to_intseq_to_bytes_be #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in let m = numbytes t in calc (==) { Seq.index (nat_to_bytes_be #l m (v s.[len - 1 - i / m])) (m - (i % m) - 1); == { index_nat_to_intseq_be #U8 #l m (v s.[len - 1 - i / m]) (i % m) } uint (v s.[len - 1 - i / m] / pow2 (8 * (i % m)) % pow2 8); == { index_nat_to_intseq_be #t #l len n (i / m) } uint ((n / pow2 (bits t * (i / m)) % pow2 (bits t)) / pow2 (8 * (i % m)) % pow2 8); == { some_arithmetic t n i } uint (n / pow2 (8 * i) % pow2 8); == { index_nat_to_intseq_be #U8 #l (len * m) n i } Seq.index (nat_to_bytes_be #l (len * m) n) (len * m - 1 - i); } val uints_to_bytes_be_nat_lemma_: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> len:nat{len * numbytes t < pow2 32} -> n:nat{n < pow2 (bits t * len)} -> i:nat{i < len * numbytes t} -> Lemma (Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i == Seq.index (nat_to_bytes_be (len * numbytes t) n) i) let uints_to_bytes_be_nat_lemma_ #t #l len n i = let s:lseq (uint_t t l) len = nat_to_intseq_be #t #l len n in calc (==) { Seq.index (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) i; == { index_uints_to_bytes_be_aux #t #l len n i } Seq.index (nat_to_bytes_be #l (numbytes t) (v s.[i / numbytes t])) (i % numbytes t); == { index_nat_to_intseq_to_bytes_be #t #l len n (len * numbytes t - 1 - i)} Seq.index (nat_to_bytes_be (len * numbytes t) n) i; } let uints_to_bytes_be_nat_lemma #t #l len n = Classical.forall_intro (uints_to_bytes_be_nat_lemma_ #t #l len n); eq_intro (uints_to_bytes_be #t #l #len (nat_to_intseq_be #t #l len n)) (nat_to_bytes_be (len * numbytes t) n) #push-options "--max_fuel 1" let rec nat_from_intseq_le_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_le_inj (Seq.slice b1 1 (length b1)) (Seq.slice b2 1 (length b2)); Seq.lemma_split b1 1; Seq.lemma_split b2 1 end let rec nat_from_intseq_be_inj #t #l b1 b2 = if length b1 = 0 then () else begin nat_from_intseq_be_inj (Seq.slice b1 0 (length b1 - 1)) (Seq.slice b2 0 (length b2 - 1)); Seq.lemma_split b1 (length b1 - 1); Seq.lemma_split b2 (length b2 - 1) end let lemma_nat_to_from_bytes_be_preserves_value #l b len x = () let lemma_nat_to_from_bytes_le_preserves_value #l b len x = () let lemma_uint_to_bytes_le_preserves_value #t #l x = () let lemma_uint_to_bytes_be_preserves_value #t #l x = () let lemma_nat_from_to_intseq_le_preserves_value #t #l len b = nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b let lemma_nat_from_to_intseq_be_preserves_value #t #l len b = nat_from_intseq_be_inj (nat_to_intseq_be len (nat_from_intseq_be b)) b let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len b let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len b let lemma_reveal_uint_to_bytes_le #t #l b = () let lemma_reveal_uint_to_bytes_be #t #l b = () let lemma_uint_to_from_bytes_le_preserves_value #t #l i = lemma_reveal_uint_to_bytes_le #t #l (uint_to_bytes_le #t #l i); assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v (uint_from_bytes_le #t #l (uint_to_bytes_le #t #l i))); lemma_uint_to_bytes_le_preserves_value #t #l i; assert(nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i) let lemma_uint_to_from_bytes_be_preserves_value #t #l i = lemma_reveal_uint_to_bytes_be #t #l (uint_to_bytes_be #t #l i); lemma_uint_to_bytes_be_preserves_value #t #l i
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Lib.ByteSequence.fst" }
[ { "abbrev": false, "full_module": "Lib.LoopCombinators", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "Lib", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t) -> FStar.Pervasives.Lemma (ensures Lib.Sequence.equal (Lib.ByteSequence.uint_to_bytes_le (Lib.ByteSequence.uint_from_bytes_le s)) s)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.l_not", "Lib.IntTypes.uu___is_U1", "Lib.IntTypes.secrecy_level", "Lib.ByteSequence.lbytes_l", "Lib.IntTypes.numbytes", "Lib.ByteSequence.lemma_nat_from_to_bytes_le_preserves_value", "Lib.Sequence.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.unit", "Prims._assert", "Prims.eq2", "Lib.Sequence.seq", "Prims.l_or", "Prims.nat", "Prims.op_LessThan", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Lib.ByteSequence.nat_from_bytes_le", "Lib.ByteSequence.nat_from_intseq_le", "FStar.Seq.Base.length", "Lib.ByteSequence.nat_to_bytes_le", "Lib.ByteSequence.lemma_reveal_uint_to_bytes_le", "Lib.IntTypes.uint_v", "Lib.ByteSequence.uint_from_bytes_le", "Prims.int", "Prims.op_GreaterThanOrEqual", "Lib.ByteSequence.uint_to_bytes_le", "Lib.IntTypes.range", "Lib.ByteSequence.lemma_uint_to_bytes_le_preserves_value", "Lib.Sequence.lseq", "Lib.IntTypes.int_t" ]
[]
true
false
true
false
false
let lemma_uint_from_to_bytes_le_preserves_value #t #l s =
let i = uint_from_bytes_le #t #l s in let s' = uint_to_bytes_le #t #l i in lemma_nat_from_to_bytes_le_preserves_value #l s' (length s'); assert (nat_to_bytes_le #l (length s') (nat_from_bytes_le s') == s'); lemma_uint_to_bytes_le_preserves_value #t #l i; assert (nat_from_bytes_le (uint_to_bytes_le #t #l i) == uint_v i); assert (s' == nat_to_bytes_le #l (length s') (uint_v i)); assert (s' == nat_to_bytes_le #l (length s') (uint_v (uint_from_bytes_le #t #l s))); lemma_reveal_uint_to_bytes_le #t #l s; assert (s' == nat_to_bytes_le #l (length s') (nat_from_bytes_le s)); lemma_nat_from_to_bytes_le_preserves_value #l s (length s)
false
Vale.Math.Poly2.Galois.fst
Vale.Math.Poly2.Galois.lemma_shift_left
val lemma_shift_left (f:G.field) (e:G.felem f) (n:I.shiftval f.G.t) : Lemma (requires True) (ensures to_poly (I.shift_left e n) == shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) [SMTPat (to_poly (I.shift_left e n))]
val lemma_shift_left (f:G.field) (e:G.felem f) (n:I.shiftval f.G.t) : Lemma (requires True) (ensures to_poly (I.shift_left e n) == shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t)) [SMTPat (to_poly (I.shift_left e n))]
let lemma_shift_left f e n = let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t))
{ "file_name": "vale/code/lib/math/Vale.Math.Poly2.Galois.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 102, "end_line": 137, "start_col": 0, "start_line": 129 }
module Vale.Math.Poly2.Galois open FStar.Mul module U = FStar.UInt module PL = Vale.Math.Poly2.Lemmas module D = Vale.Math.Poly2.Defs_s //module PD = Vale.Math.Poly2.Defs module GI = Vale.Math.Poly2.Galois.IntTypes // recursive definitions of from_vec/to_vec are prone to matching loops, so use no fuel from here on #reset-options "--max_fuel 0 --max_ifuel 0" let to_poly #f e = let G.GF t irred = f in let n = I.bits t in reverse (of_seq (U.to_vec #n (I.v e))) (n - 1) let to_felem f p = let G.GF t irred = f in let n = I.bits t in let p = reverse p (n - 1) in Lib.IntTypes.Compatibility.nat_to_uint (U.from_vec #n (to_seq p n)) let lemma_to_poly_degree #f e = reveal_defs () let lemma_irred_degree f = let G.GF t irred = f in let n = I.bits t in PL.lemma_index_all (); PL.lemma_monomial_define n; PL.lemma_add_define_all (); PL.lemma_degree_is (irred_poly f) n; () let lemma_poly_felem f p = let G.GF t irred = f in let n = I.bits t in let r = reverse p (n - 1) in let s = to_seq r n in let u = U.from_vec #n s in let e = Lib.IntTypes.Compatibility.nat_to_uint u in let u' = I.v #t #I.SEC e in let s' = U.to_vec #n u' in let r' = of_seq s' in let p' = reverse r' (n - 1) in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; lemma_equal p p'; () let lemma_felem_poly #f e = let G.GF t irred = f in let n = I.bits t in let u = I.v #t #I.SEC e in let s = U.to_vec #n u in let r = of_seq s in let p = reverse r (n - 1) in let r' = reverse p (n - 1) in let s' = to_seq r' n in let u' = U.from_vec #n s' in let e' = Lib.IntTypes.Compatibility.nat_to_uint #t #I.SEC u' in PL.lemma_index_all (); PL.lemma_reverse_define_all (); lemma_equal r r'; assert (equal s s'); Lib.IntTypes.Compatibility.uintv_extensionality e e'; () let lemma_zero f = let G.GF t irred = f in let n = I.bits t in let a = zero in let b = to_poly #f G.zero in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_zero_define (); (if 0 <= i && i < n then U.zero_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i //let lemma_zero' (f:G.field) : Lemma // (G.zero == to_felem f zero) // = // lemma_zero f; // lemma_felem_poly #f G.zero let lemma_one f = let G.GF t irred = f in let n = I.bits t in let a = one in let b = to_poly #f G.one in let eq_i (i:int) : Lemma (a.[i] == b.[i]) = PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_one_define (); (if 0 <= i && i < n then U.one_to_vec_lemma #n (n - 1 - i)); () in PL.lemma_pointwise_equal a b eq_i let lemma_add f e1 e2 = let G.GF t irred = f in GI.define_logxor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_add_define_all (); lemma_equal (to_poly (G.fadd e1 e2)) (to_poly e1 +. to_poly e2) let lemma_and f e1 e2 = let G.GF t irred = f in GI.define_logand t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_and_define_all (); lemma_equal (to_poly (I.logand e1 e2)) (to_poly e1 &. to_poly e2) let lemma_or f e1 e2 = let G.GF t irred = f in GI.define_logor t e1 e2; PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_or_define_all (); lemma_equal (to_poly (I.logor e1 e2)) (to_poly e1 |. to_poly e2)
{ "checked_file": "/", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Lemmas.fsti.checked", "Vale.Math.Poly2.Galois.IntTypes.fsti.checked", "Vale.Math.Poly2.Defs_s.fst.checked", "prims.fst.checked", "Lib.LoopCombinators.fsti.checked", "Lib.IntTypes.Compatibility.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.Math.Poly2.Galois.fst" }
[ { "abbrev": true, "full_module": "Vale.Math.Poly2.Galois.IntTypes", "short_module": "GI" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Defs_s", "short_module": "D" }, { "abbrev": true, "full_module": "Vale.Math.Poly2.Lemmas", "short_module": "PL" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": true, "full_module": "Vale.Math.Poly2_s", "short_module": "P" }, { "abbrev": true, "full_module": "Spec.GaloisField", "short_module": "G" }, { "abbrev": true, "full_module": "Lib.IntTypes", "short_module": "I" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: Spec.GaloisField.field -> e: Spec.GaloisField.felem f -> n: Lib.IntTypes.shiftval (GF?.t f) -> FStar.Pervasives.Lemma (ensures Vale.Math.Poly2.Galois.to_poly (Lib.IntTypes.shift_left e n) == Vale.Math.Poly2_s.shift (Vale.Math.Poly2.Galois.to_poly e) (Lib.IntTypes.uint_v n) %. Vale.Math.Poly2_s.monomial (Lib.IntTypes.bits (GF?.t f))) [SMTPat (Vale.Math.Poly2.Galois.to_poly (Lib.IntTypes.shift_left e n))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.GaloisField.field", "Spec.GaloisField.felem", "Lib.IntTypes.shiftval", "Spec.GaloisField.__proj__GF__item__t", "Lib.IntTypes.inttype", "Prims.l_and", "Prims.b2t", "Lib.IntTypes.unsigned", "Prims.op_disEquality", "Lib.IntTypes.U1", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Vale.Math.Poly2.lemma_equal", "Vale.Math.Poly2.Galois.to_poly", "Lib.IntTypes.shift_left", "Vale.Math.Poly2.op_Percent_Dot", "Vale.Math.Poly2_s.shift", "Lib.IntTypes.uint_v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "Vale.Math.Poly2_s.monomial", "Lib.IntTypes.bits", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.int", "Prims.l_or", "Lib.IntTypes.range", "FStar.UInt.size", "FStar.UInt.shift_left", "Vale.Math.Poly2.Lemmas.lemma_mod_monomial", "Vale.Math.Poly2.Lemmas.lemma_shift_define_all", "Vale.Math.Poly2.Lemmas.lemma_reverse_define_all", "Vale.Math.Poly2.Lemmas.lemma_index_all" ]
[]
false
false
true
false
false
let lemma_shift_left f e n =
let G.GF t irred = f in let nf = I.bits t in PL.lemma_index_all (); PL.lemma_reverse_define_all (); PL.lemma_shift_define_all (); PL.lemma_mod_monomial (shift (to_poly e) (I.uint_v n)) nf; assert (I.uint_v (I.shift_left e n) == U.shift_left #nf (I.uint_v e) (I.uint_v n)); lemma_equal (to_poly (I.shift_left e n)) (shift (to_poly e) (I.uint_v n) %. monomial (I.bits f.G.t))
false