effect
stringclasses
48 values
original_source_type
stringlengths
0
23k
opens_and_abbrevs
listlengths
2
92
isa_cross_project_example
bool
1 class
source_definition
stringlengths
9
57.9k
partial_definition
stringlengths
7
23.3k
is_div
bool
2 classes
is_type
null
is_proof
bool
2 classes
completed_definiton
stringlengths
1
250k
dependencies
dict
effect_flags
sequencelengths
0
2
ideal_premises
sequencelengths
0
236
mutual_with
sequencelengths
0
11
file_context
stringlengths
0
407k
interleaved
bool
1 class
is_simply_typed
bool
2 classes
file_name
stringlengths
5
48
vconfig
dict
is_simple_lemma
null
source_type
stringlengths
10
23k
proof_features
sequencelengths
0
1
name
stringlengths
8
95
source
dict
verbose_type
stringlengths
1
7.42k
source_range
dict
Prims.Pure
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))
[ { "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 } ]
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
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 =
false
null
false
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[]
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.seq_mask_select
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 5, "end_line": 104, "start_col": 38, "start_line": 96 }
Prims.Tot
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]}
[ { "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 } ]
false
let uint_at_index_le #t #l #len b i = uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))
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 =
false
null
false
uint_from_bytes_le (sub b (i * numbytes t) (numbytes t))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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 = ()
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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]}
[]
Lib.ByteSequence.uint_at_index_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> idx: Prims.nat{idx < len} -> u186: Lib.IntTypes.uint_t t l {u186 == (Lib.ByteSequence.uints_from_bytes_le b).[ idx ]}
{ "end_col": 58, "end_line": 355, "start_col": 2, "start_line": 355 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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
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 =
false
null
true
nat_from_intseq_le_inj (nat_to_intseq_le len (nat_from_intseq_le b)) b
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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 = ()
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_nat_from_to_intseq_le_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 72, "end_line": 747, "start_col": 2, "start_line": 747 }
Prims.Tot
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]}
[ { "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 } ]
false
let uint_at_index_be #t #l #len b i = uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))
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 =
false
null
false
uint_from_bytes_be (sub b (i * numbytes t) (numbytes t))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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]}
[]
Lib.ByteSequence.uint_at_index_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> idx: Prims.nat{idx < len} -> u191: Lib.IntTypes.uint_t t l {u191 == (Lib.ByteSequence.uints_from_bytes_be b).[ idx ]}
{ "end_col": 58, "end_line": 358, "start_col": 2, "start_line": 358 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.index_uints_to_bytes_be_aux
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 44, "end_line": 583, "start_col": 2, "start_line": 580 }
Prims.Tot
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
[ { "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 } ]
false
let mask_select #t mask a b = b ^. (mask &. (a ^. b))
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 =
false
null
false
b ^. (mask &. (a ^. b))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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
[]
Lib.ByteSequence.mask_select
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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
{ "end_col": 25, "end_line": 79, "start_col": 2, "start_line": 79 }
FStar.Pervasives.Lemma
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])
[ { "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 } ]
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
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 =
false
null
true
index_generate_blocks (numbytes t) len len (uints_to_bytes_le_inner #t #l #len ul) i
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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]) *)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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])
[]
Lib.ByteSequence.index_uints_to_bytes_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 ])
{ "end_col": 86, "end_line": 323, "start_col": 2, "start_line": 323 }
Prims.Tot
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)
[ { "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 } ]
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
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 =
false
null
false
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[ "recursion" ]
Lib.ByteSequence.nat_to_intseq_be_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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})
{ "end_col": 5, "end_line": 161, "start_col": 2, "start_line": 151 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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; }
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 =
false
null
true
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; }
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.modulo_pow2_prop
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 3, "end_line": 599, "start_col": 2, "start_line": 589 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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)))
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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 ==
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_le_slice_lemma_lp
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 82, "end_line": 427, "start_col": 59, "start_line": 424 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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)
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 =
false
null
true
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_uint_from_to_bytes_le_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 60, "end_line": 784, "start_col": 57, "start_line": 773 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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)))
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_be_slice_lemma_lp
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 82, "end_line": 491, "start_col": 59, "start_line": 488 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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)
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 =
false
null
true
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)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_uint_from_to_bytes_be_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 60, "end_line": 797, "start_col": 57, "start_line": 786 }
FStar.Pervasives.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)
[ { "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 } ]
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)
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 =
false
null
true
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)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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_" ]
[]
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; }
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.uints_to_bytes_le_nat_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 49, "end_line": 670, "start_col": 2, "start_line": 668 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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) ==
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.index_nat_to_intseq_to_bytes_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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) )
{ "end_col": 23, "end_line": 647, "start_col": 51, "start_line": 637 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
false
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
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 =
false
null
true
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))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_be_inj
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 5, "end_line": 736, "start_col": 2, "start_line": 731 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
false
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
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 =
false
null
true
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)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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"
false
false
Lib.ByteSequence.fst
{ "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" }
null
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))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_le_inj
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 5, "end_line": 728, "start_col": 2, "start_line": 723 }
Prims.Tot
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))}
[ { "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 } ]
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)
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 =
false
null
false
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))}
[]
Lib.ByteSequence.seq_eq_mask
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)) }
{ "end_col": 16, "end_line": 70, "start_col": 2, "start_line": 65 }
FStar.Pervasives.Lemma
val nat_from_intseq_le_public_to_secret: #t:inttype{unsigned t} -> len:size_pos{len * bits t < pow2 32} -> b:lseq (uint_t t PUB) len -> Lemma (nat_from_intseq_le b == nat_from_intseq_le (map secret b))
[ { "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 } ]
false
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
val nat_from_intseq_le_public_to_secret: #t:inttype{unsigned t} -> len:size_pos{len * bits t < pow2 32} -> b:lseq (uint_t t PUB) len -> Lemma (nat_from_intseq_le b == nat_from_intseq_le (map secret b)) let rec nat_from_intseq_le_public_to_secret #t len b =
false
null
true
if len = 1 then (nat_from_intseq_le_lemma0 b; nat_from_intseq_le_lemma0 (map secret b)) else 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; nat_from_intseq_le_slice_lemma b 1; nat_from_intseq_le_lemma0 (slice b 0 1); 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)); ()
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "lemma" ]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Lib.IntTypes.PUB", "Prims.op_Equality", "Prims.int", "Lib.ByteSequence.nat_from_intseq_le_lemma0", "Lib.IntTypes.SEC", "Lib.Sequence.map", "Lib.IntTypes.secret", "Prims.unit", "Prims.bool", "Lib.Sequence.eq_intro", "Lib.IntTypes.int_t", "Prims.op_Subtraction", "Lib.Sequence.slice", "Lib.ByteSequence.nat_from_intseq_le_slice_lemma", "Lib.ByteSequence.nat_from_intseq_le_public_to_secret", "Prims.l_and", "Prims.eq2", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.l_Forall", "Prims.nat", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index", "Prims.l_imp" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
val nat_from_intseq_le_public_to_secret: #t:inttype{unsigned t} -> len:size_pos{len * bits t < pow2 32} -> b:lseq (uint_t t PUB) len -> Lemma (nat_from_intseq_le b == nat_from_intseq_le (map secret b))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_le_public_to_secret
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
len: Lib.IntTypes.size_pos{len * Lib.IntTypes.bits t < Prims.pow2 32} -> b: Lib.Sequence.lseq (Lib.IntTypes.uint_t t Lib.IntTypes.PUB) len -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_le b == Lib.ByteSequence.nat_from_intseq_le (Lib.Sequence.map Lib.IntTypes.secret b))
{ "end_col": 10, "end_line": 836, "start_col": 2, "start_line": 821 }
FStar.Pervasives.Lemma
val nat_from_intseq_be_public_to_secret: #t:inttype{unsigned t} -> len:size_pos{len * bits t < pow2 32} -> b:lseq (uint_t t PUB) len -> Lemma (nat_from_intseq_be b == nat_from_intseq_be (map secret b))
[ { "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 } ]
false
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
val nat_from_intseq_be_public_to_secret: #t:inttype{unsigned t} -> len:size_pos{len * bits t < pow2 32} -> b:lseq (uint_t t PUB) len -> Lemma (nat_from_intseq_be b == nat_from_intseq_be (map secret b)) let rec nat_from_intseq_be_public_to_secret #t len b =
false
null
true
if len = 1 then (nat_from_intseq_be_lemma0 b; nat_from_intseq_be_lemma0 (map secret b)) else 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; nat_from_intseq_be_slice_lemma b 1; nat_from_intseq_be_lemma0 (slice b 0 1); nat_from_intseq_be_slice_lemma b_secret 1; nat_from_intseq_be_lemma0 (slice b_secret 0 1); eq_intro (slice (map secret b) 1 len) (map secret (slice b 1 len)); ()
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "lemma" ]
[ "Lib.IntTypes.inttype", "Prims.b2t", "Lib.IntTypes.unsigned", "Lib.IntTypes.size_pos", "Prims.op_LessThan", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.pow2", "Lib.Sequence.lseq", "Lib.IntTypes.uint_t", "Lib.IntTypes.PUB", "Prims.op_Equality", "Prims.int", "Lib.ByteSequence.nat_from_intseq_be_lemma0", "Lib.IntTypes.SEC", "Lib.Sequence.map", "Lib.IntTypes.secret", "Prims.unit", "Prims.bool", "Lib.Sequence.eq_intro", "Lib.IntTypes.int_t", "Prims.op_Subtraction", "Lib.Sequence.slice", "Lib.ByteSequence.nat_from_intseq_be_slice_lemma", "Lib.ByteSequence.nat_from_intseq_be_public_to_secret", "Prims.l_and", "Prims.eq2", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.l_Forall", "Prims.nat", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.index", "Prims.l_imp" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
val nat_from_intseq_be_public_to_secret: #t:inttype{unsigned t} -> len:size_pos{len * bits t < pow2 32} -> b:lseq (uint_t t PUB) len -> Lemma (nat_from_intseq_be b == nat_from_intseq_be (map secret b))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_be_public_to_secret
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
len: Lib.IntTypes.size_pos{len * Lib.IntTypes.bits t < Prims.pow2 32} -> b: Lib.Sequence.lseq (Lib.IntTypes.uint_t t Lib.IntTypes.PUB) len -> FStar.Pervasives.Lemma (ensures Lib.ByteSequence.nat_from_intseq_be b == Lib.ByteSequence.nat_from_intseq_be (Lib.Sequence.map Lib.IntTypes.secret b))
{ "end_col": 10, "end_line": 817, "start_col": 2, "start_line": 800 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)))
[ "recursion" ]
Lib.ByteSequence.index_nat_to_intseq_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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) ))
{ "end_col": 7, "end_line": 278, "start_col": 2, "start_line": 256 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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)
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 =
false
null
true
Seq.lemma_index_slice b1 i j (j - i - 1); Seq.lemma_index_slice b2 i j (j - i - 1)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)))
[]
Lib.ByteSequence.lemma_not_equal_last
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 42, "end_line": 29, "start_col": 2, "start_line": 28 }
Prims.Tot
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}
[ { "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 } ]
false
let nat_to_intseq_le = 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} let nat_to_intseq_le =
false
null
false
nat_to_intseq_le_
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "total" ]
[ "Lib.ByteSequence.nat_to_intseq_le_" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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}
[]
Lib.ByteSequence.nat_to_intseq_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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}
{ "end_col": 40, "end_line": 183, "start_col": 23, "start_line": 183 }
Prims.Tot
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))
[ { "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 } ]
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
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 =
false
null
false
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_le_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)})
{ "end_col": 5, "end_line": 139, "start_col": 37, "start_line": 129 }
Prims.Tot
val uint_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> lbytes_l l (numbytes t)
[ { "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 } ]
false
let uint_to_bytes_be #t #l n = nat_to_bytes_be (numbytes t) (v n)
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 =
false
null
false
nat_to_bytes_be (numbytes t) (v n)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val uint_to_bytes_be: #t:inttype{unsigned t} -> #l:secrecy_level -> u:uint_t t l -> lbytes_l l (numbytes t)
[]
Lib.ByteSequence.uint_to_bytes_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
u121: Lib.IntTypes.uint_t t l -> Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t)
{ "end_col": 36, "end_line": 288, "start_col": 2, "start_line": 288 }
Prims.Tot
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))
[ { "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 } ]
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]
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 () =
false
null
false
let open Lib.Sequence in (), uint_to_bytes_be #t #l b.[ i ]
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.uints_to_bytes_be_inner
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 34, "end_line": 331, "start_col": 2, "start_line": 330 }
Prims.Tot
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))
[ { "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 } ]
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'
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 =
false
null
false
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_be_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)})
{ "end_col": 18, "end_line": 120, "start_col": 37, "start_line": 111 }
Prims.Tot
val uint_from_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> b:lbytes_l l (numbytes t) -> uint_t t l
[ { "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 } ]
false
let uint_from_bytes_le #t #l b = let n = nat_from_intseq_le #U8 b in uint #t #l n
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 =
false
null
false
let n = nat_from_intseq_le #U8 b in uint #t #l n
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val uint_from_bytes_le: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> b:lbytes_l l (numbytes t) -> uint_t t l
[]
Lib.ByteSequence.uint_from_bytes_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
b: Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t) -> Lib.IntTypes.uint_t t l
{ "end_col": 14, "end_line": 295, "start_col": 32, "start_line": 293 }
FStar.Pervasives.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))
[ { "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 } ]
false
let nat_from_intseq_be_slice_lemma #t #l #len b i = nat_from_intseq_be_slice_lemma_ #t #l #len b 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 =
false
null
true
nat_from_intseq_be_slice_lemma_ #t #l #len b i
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.nat_from_intseq_be_slice_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 48, "end_line": 417, "start_col": 2, "start_line": 417 }
Prims.Tot
val uint_from_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> lbytes_l l (numbytes t) -> uint_t t l
[ { "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 } ]
false
let uint_from_bytes_be #t #l b = let n = nat_from_intseq_be #U8 b in uint #t #l n
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 =
false
null
false
let n = nat_from_intseq_be #U8 b in uint #t #l n
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val uint_from_bytes_be: #t:inttype{unsigned t /\ ~(U1? t)} -> #l:secrecy_level -> lbytes_l l (numbytes t) -> uint_t t l
[]
Lib.ByteSequence.uint_from_bytes_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
b: Lib.ByteSequence.lbytes_l l (Lib.IntTypes.numbytes t) -> Lib.IntTypes.uint_t t l
{ "end_col": 14, "end_line": 299, "start_col": 32, "start_line": 297 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
false
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 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 =
false
null
true
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); }
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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", "FStar.Calc.calc_finish", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.nat_to_bytes_be", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Lib.IntTypes.int_t", "Prims.op_Subtraction", "Prims.op_Division", "Prims.op_Modulus", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "Lib.IntTypes.uint", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Lib.ByteSequence.index_nat_to_intseq_be", "Prims.squash", "Lib.ByteSequence.some_arithmetic", "Prims.int", "Lib.Sequence.lseq", "Lib.ByteSequence.nat_to_intseq_be" ]
[]
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) ==
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.index_nat_to_intseq_to_bytes_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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.nat_to_bytes_be (Lib.IntTypes.numbytes t) (Lib.IntTypes.v s.[ len - 1 - i / Lib.IntTypes.numbytes t ])) (Lib.IntTypes.numbytes t - 1 - i % Lib.IntTypes.numbytes t) == FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_be (len * Lib.IntTypes.numbytes t) n) (len * Lib.IntTypes.numbytes t - i - 1)))
{ "end_col": 3, "end_line": 695, "start_col": 51, "start_line": 682 }
Prims.Tot
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))}
[ { "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 } ]
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 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
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 =
false
null
false
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))}
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))}
[]
Lib.ByteSequence.seq_eq_mask_inner
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)) }
{ "end_col": 5, "end_line": 62, "start_col": 2, "start_line": 43 }
Prims.Tot
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
[ { "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 } ]
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)))
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 =
false
null
false
Lib.Sequence.createi #(int_t t l) len (fun i -> uint_from_bytes_be (sub b (i * numbytes t) (numbytes t)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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 = ()
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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
[]
Lib.ByteSequence.uints_from_bytes_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
b: Lib.ByteSequence.lbytes_l l (len * Lib.IntTypes.numbytes t) -> Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len
{ "end_col": 71, "end_line": 350, "start_col": 2, "start_line": 349 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
false
let index_uint_to_bytes_le #t #l u = Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u))
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 =
false
null
true
Classical.forall_intro (index_nat_to_intseq_le #U8 #l (numbytes t) (v u))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.index_uint_to_bytes_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
u118: 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 u118) i == Lib.IntTypes.uint (Lib.IntTypes.v u118 / Prims.pow2 (8 * i) % Prims.pow2 8))
{ "end_col": 75, "end_line": 285, "start_col": 2, "start_line": 285 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
false
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
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 =
false
null
true
if len = 0 then () else if i = len then () else 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)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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", "Prims.l_or", "Lib.IntTypes.max_size_t", "Lib.ByteSequence.nat_from_intseq_slice_lemma_aux", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Lib.ByteSequence.nat_from_intseq_be_", "Lib.Sequence.slice", "Lib.IntTypes.bits", "Prims.op_Subtraction", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.pow2", "Lib.ByteSequence.nat_from_intseq_be_slice_lemma_", "Prims.l_and", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.slice", "Prims.l_Forall", "Prims.op_LessThan", "FStar.Seq.Base.index", "Lib.Sequence.index" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_be_slice_lemma_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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_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)) (decreases len - i)
{ "end_col": 5, "end_line": 411, "start_col": 2, "start_line": 400 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
false
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; }
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) let some_arithmetic t n i =
false
null
true
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; }
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "lemma" ]
[ "Lib.IntTypes.inttype", "Prims.l_not", "Prims.b2t", "Lib.IntTypes.uu___is_U1", "Prims.nat", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "Prims.op_Division", "Prims.pow2", "FStar.Mul.op_Star", "Lib.IntTypes.bits", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "Prims.op_Subtraction", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Prims._assert", "Prims.squash", "FStar.Math.Lemmas.euclidean_division_definition", "FStar.Math.Lemmas.paren_mul_right", "FStar.Math.Lemmas.distributivity_sub_right", "Lib.ByteSequence.modulo_pow2_prop", "FStar.Math.Lemmas.division_multiplication_lemma", "FStar.Math.Lemmas.pow2_plus", "Lib.IntTypes.numbytes" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 150, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.some_arithmetic
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
t: Lib.IntTypes.inttype{~(U1? t)} -> n: Prims.nat -> i: Prims.nat -> FStar.Pervasives.Lemma (ensures (let m = Lib.IntTypes.numbytes t in n / Prims.pow2 (Lib.IntTypes.bits t * (i / m)) % Prims.pow2 (Lib.IntTypes.bits t) / Prims.pow2 (8 * (i % m)) % Prims.pow2 8 == n / Prims.pow2 (8 * i) % Prims.pow2 8))
{ "end_col": 3, "end_line": 625, "start_col": 27, "start_line": 607 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
false
let index_uint_to_bytes_be #t #l u = Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u))
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 =
false
null
true
Classical.forall_intro (index_nat_to_intseq_be #U8 #l (numbytes t) (v u))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.index_uint_to_bytes_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
u124: 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 u124) (Lib.IntTypes.numbytes t - i - 1) == Lib.IntTypes.uint (Lib.IntTypes.v u124 / Prims.pow2 (8 * i) % Prims.pow2 8))
{ "end_col": 75, "end_line": 291, "start_col": 2, "start_line": 291 }
Prims.Tot
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)
[ { "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 } ]
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
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 =
false
null
false
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[ "recursion" ]
Lib.ByteSequence.nat_to_intseq_le_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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})
{ "end_col": 5, "end_line": 181, "start_col": 2, "start_line": 171 }
FStar.Pervasives.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)
[ { "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 } ]
false
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_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 =
false
null
true
uints_from_bytes_le_nat_lemma_ #t #l #len b
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.uints_from_bytes_le_nat_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 45, "end_line": 480, "start_col": 2, "start_line": 480 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
false
let lemma_nat_from_to_bytes_be_preserves_value #l b len = lemma_nat_from_to_intseq_be_preserves_value len 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 =
false
null
true
lemma_nat_from_to_intseq_be_preserves_value len b
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_nat_from_to_bytes_be_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 51, "end_line": 756, "start_col": 2, "start_line": 756 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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
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 =
false
null
true
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_uint_to_from_bytes_be_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 48, "end_line": 771, "start_col": 2, "start_line": 770 }
FStar.Pervasives.Lemma
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])
[ { "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 } ]
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
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 =
false
null
true
index_generate_blocks (numbytes t) len len (uints_to_bytes_be_inner #t #l #len ul) i
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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])
[]
Lib.ByteSequence.index_uints_to_bytes_be
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 ])
{ "end_col": 86, "end_line": 340, "start_col": 2, "start_line": 340 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
false
let lemma_nat_from_to_bytes_le_preserves_value #l b len = lemma_nat_from_to_intseq_le_preserves_value len 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 =
false
null
true
lemma_nat_from_to_intseq_le_preserves_value len b
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_nat_from_to_bytes_le_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 51, "end_line": 753, "start_col": 2, "start_line": 753 }
FStar.Pervasives.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)
[ { "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 } ]
false
let uints_from_bytes_be_nat_lemma #t #l #len b = uints_from_bytes_be_nat_lemma_ #t #l #len 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 =
false
null
true
uints_from_bytes_be_nat_lemma_ #t #l #len b
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.uints_from_bytes_be_nat_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 45, "end_line": 550, "start_col": 2, "start_line": 550 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
false
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_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 =
false
null
true
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 ])
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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", "Prims.int", "Lib.ByteSequence.nat_from_intseq_be_", "Prims.op_Addition", "Lib.ByteSequence.nat_from_intseq_be", "Lib.Sequence.slice", "Lib.IntTypes.uint_t", "Prims.op_Subtraction", "Lib.IntTypes.bits", "Lib.IntTypes.uint_v", "Lib.Sequence.op_String_Access", "Prims.unit", "Lib.ByteSequence.nat_from_intseq_be_slice_lemma", "Lib.ByteSequence.uints_from_bytes_be_slice_lemma", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.ByteSequence.uints_from_bytes_be" ]
[]
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)))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_be_nat_lemma_aux
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (Lib.ByteSequence.uints_from_bytes_be (Lib.Sequence.slice b (Lib.IntTypes.numbytes t) (len * Lib.IntTypes.numbytes t))) + Prims.pow2 ((len - 1) * Lib.IntTypes.bits t) * Lib.ByteSequence.nat_from_intseq_be_ (Lib.Sequence.slice b 0 (Lib.IntTypes.numbytes t)))
{ "end_col": 113, "end_line": 532, "start_col": 52, "start_line": 528 }
FStar.Pervasives.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)
[ { "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 } ]
false
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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[ "recursion" ]
Lib.ByteSequence.uints_from_bytes_le_nat_lemma_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 5, "end_line": 477, "start_col": 2, "start_line": 469 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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))
[]
Lib.ByteSequence.index_uints_to_bytes_le_aux
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 44, "end_line": 567, "start_col": 2, "start_line": 564 }
FStar.Pervasives.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)
[ { "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 } ]
false
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
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[ "recursion" ]
Lib.ByteSequence.uints_from_bytes_be_nat_lemma_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 5, "end_line": 547, "start_col": 2, "start_line": 542 }
FStar.Pervasives.Lemma
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)
[ { "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 } ]
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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)
[]
Lib.ByteSequence.lemma_uints_to_from_bytes_le_preserves_value
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 68, "end_line": 860, "start_col": 63, "start_line": 848 }
FStar.Pervasives.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)
[ { "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 } ]
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)
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 =
false
null
true
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)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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_" ]
[]
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; }
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.uints_to_bytes_be_nat_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 49, "end_line": 718, "start_col": 2, "start_line": 716 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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)))
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 =
false
null
true
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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 ==
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_le_slice_lemma_rp
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 82, "end_line": 439, "start_col": 59, "start_line": 434 }
FStar.Pervasives.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)))
[ { "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 } ]
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)))
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)))
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_le_slice_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 136, "end_line": 444, "start_col": 2, "start_line": 442 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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)))
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_be_slice_lemma_rp
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 82, "end_line": 509, "start_col": 59, "start_line": 504 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 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
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 =
false
null
true
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)
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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"
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)))
[ "recursion" ]
Lib.ByteSequence.index_nat_to_intseq_le
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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) ))
{ "end_col": 7, "end_line": 252, "start_col": 2, "start_line": 233 }
FStar.Pervasives.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)} -> 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)
[ { "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 } ]
false
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; }
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 =
false
null
true
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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", "FStar.Calc.calc_finish", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.uints_to_bytes_le", "Lib.ByteSequence.nat_to_intseq_le", "Lib.ByteSequence.nat_to_bytes_le", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Lib.IntTypes.int_t", "Prims.op_Division", "Prims.op_Modulus", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Lib.ByteSequence.index_uints_to_bytes_le_aux", "Prims.squash", "Lib.ByteSequence.index_nat_to_intseq_to_bytes_le", "Lib.Sequence.lseq" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.uints_to_bytes_le_nat_lemma_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 FStar.Seq.Base.index (Lib.ByteSequence.uints_to_bytes_le (Lib.ByteSequence.nat_to_intseq_le len n)) i == FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_le (len * Lib.IntTypes.numbytes t) n) i)
{ "end_col": 3, "end_line": 665, "start_col": 48, "start_line": 657 }
FStar.Pervasives.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)))
[ { "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 } ]
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)))
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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)))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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)))
[]
Lib.ByteSequence.uints_from_bytes_be_slice_lemma
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 136, "end_line": 518, "start_col": 2, "start_line": 516 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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)))
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 =
false
null
true
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)))
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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) *
false
false
Lib.ByteSequence.fst
{ "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" }
null
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))))
[]
Lib.ByteSequence.uints_from_bytes_le_nat_lemma_aux
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))))
{ "end_col": 104, "end_line": 460, "start_col": 52, "start_line": 455 }
FStar.Pervasives.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))
[ { "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 } ]
false
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
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 =
false
null
true
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
{ "checked_file": "Lib.ByteSequence.fst.checked", "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" }
[ "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" ]
[]
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))
false
false
Lib.ByteSequence.fst
{ "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" }
null
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))
[ "recursion" ]
Lib.ByteSequence.nat_from_intseq_le_slice_lemma_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 5, "end_line": 386, "start_col": 2, "start_line": 375 }
FStar.Pervasives.Lemma
val lemma_uints_to_bytes_le_sub : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> s : lseq (uint_t t l) len -> i : size_nat {i < len} -> Lemma(sub (uints_to_bytes_le #t #l s) (i * numbytes t) (numbytes t) == uint_to_bytes_le (index s i))
[ { "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 } ]
false
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))
val lemma_uints_to_bytes_le_sub : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> s : lseq (uint_t t l) len -> i : size_nat {i < len} -> Lemma(sub (uints_to_bytes_le #t #l s) (i * numbytes t) (numbytes t) == uint_to_bytes_le (index s i)) let lemma_uints_to_bytes_le_sub #t #l #len s i =
false
null
true
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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.IntTypes.U8", "Lib.Sequence.sub", "Lib.ByteSequence.uints_to_bytes_le", "Lib.ByteSequence.uint_to_bytes_le", "Lib.Sequence.index", "Prims.unit", "FStar.Classical.forall_intro", "Prims.eq2", "Prims.l_or", "FStar.Seq.Base.index", "Lib.Sequence.to_seq", "Prims.op_Addition", "Prims.nat", "Prims.op_LessThanOrEqual", "Prims.op_Subtraction", "Prims.l_True", "Prims.squash", "Lib.IntTypes.int_t", "Prims.op_Multiply", "Prims.Nil", "FStar.Pervasives.pattern", "Prims._assert", "Lib.ByteSequence.index_uints_to_bytes_le" ]
[]
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
false
false
Lib.ByteSequence.fst
{ "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" }
null
val lemma_uints_to_bytes_le_sub : #t : inttype{unsigned t /\ ~(U1? t)} -> #l : secrecy_level -> #len:size_nat{len * numbytes t < pow2 32} -> s : lseq (uint_t t l) len -> i : size_nat {i < len} -> Lemma(sub (uints_to_bytes_le #t #l s) (i * numbytes t) (numbytes t) == uint_to_bytes_le (index s i))
[]
Lib.ByteSequence.lemma_uints_to_bytes_le_sub
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s: Lib.Sequence.lseq (Lib.IntTypes.uint_t t l) len -> i: Lib.IntTypes.size_nat{i < len} -> FStar.Pervasives.Lemma (ensures Lib.Sequence.sub (Lib.ByteSequence.uints_to_bytes_le s) (i * Lib.IntTypes.numbytes t) (Lib.IntTypes.numbytes t) == Lib.ByteSequence.uint_to_bytes_le (Lib.Sequence.index s i))
{ "end_col": 105, "end_line": 846, "start_col": 48, "start_line": 838 }
FStar.Pervasives.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)} -> 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)
[ { "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 } ]
false
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; }
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 =
false
null
true
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": "Lib.ByteSequence.fst.checked", "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" }
[ "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", "FStar.Calc.calc_finish", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Prims.eq2", "FStar.Seq.Base.index", "Lib.ByteSequence.uints_to_bytes_be", "Lib.ByteSequence.nat_to_intseq_be", "Lib.ByteSequence.nat_to_bytes_be", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "Lib.IntTypes.v", "Lib.Sequence.op_String_Access", "Lib.IntTypes.int_t", "Prims.op_Division", "Prims.op_Modulus", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Lib.ByteSequence.index_uints_to_bytes_be_aux", "Prims.squash", "Lib.ByteSequence.index_nat_to_intseq_to_bytes_be", "Prims.op_Subtraction", "Lib.Sequence.lseq" ]
[]
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)
false
false
Lib.ByteSequence.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
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)
[]
Lib.ByteSequence.uints_to_bytes_be_nat_lemma_
{ "file_name": "lib/Lib.ByteSequence.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 FStar.Seq.Base.index (Lib.ByteSequence.uints_to_bytes_be (Lib.ByteSequence.nat_to_intseq_be len n)) i == FStar.Seq.Base.index (Lib.ByteSequence.nat_to_bytes_be (len * Lib.IntTypes.numbytes t) n) i)
{ "end_col": 3, "end_line": 713, "start_col": 48, "start_line": 705 }
Prims.GTot
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s)
let store_pre (#a: Type) (#rrel #rel: MB.srel a) (b: MB.mbuffer a rrel rel) (i: nat) (j: nat{i + j <= MB.length b}) (predicate: (Seq.seq a -> Type0)) =
false
null
false
fun (h: HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s: Seq.seq a). (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s)
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[ "sometrivial" ]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "LowStar.Monotonic.Buffer.length", "FStar.Seq.Base.seq", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.l_Forall", "Prims.l_imp", "Prims.eq2", "FStar.Seq.Base.length", "FStar.Seq.Base.equal", "FStar.Seq.Base.slice", "LowStar.Monotonic.Buffer.as_seq", "Prims.logical" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel)
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store_pre : b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: Prims.nat -> j: Prims.nat{i + j <= LowStar.Monotonic.Buffer.length b} -> predicate: (_: FStar.Seq.Base.seq a -> Type0) -> h: FStar.Monotonic.HyperStack.mem -> Prims.GTot Prims.logical
[]
LowStar.Endianness.store_pre
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: Prims.nat -> j: Prims.nat{i + j <= LowStar.Monotonic.Buffer.length b} -> predicate: (_: FStar.Seq.Base.seq a -> Type0) -> h: FStar.Monotonic.HyperStack.mem -> Prims.GTot Prims.logical
{ "end_col": 20, "end_line": 84, "start_col": 4, "start_line": 69 }
Prims.GTot
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j)))
let store_post (#a: Type) (#rrel #rel: MB.srel a) (b: MB.mbuffer a rrel rel) (i: nat) (j: nat{i + j <= MB.length b}) (predicate: (Seq.seq a -> Type0)) =
false
null
false
fun (h0: HS.mem) (_: unit) (h1: HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j)))
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[ "sometrivial" ]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "LowStar.Monotonic.Buffer.length", "FStar.Seq.Base.seq", "FStar.Monotonic.HyperStack.mem", "Prims.unit", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "FStar.Seq.Base.equal", "FStar.Seq.Base.slice", "LowStar.Monotonic.Buffer.as_seq", "Prims.logical" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel)
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store_post : b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: Prims.nat -> j: Prims.nat{i + j <= LowStar.Monotonic.Buffer.length b} -> predicate: (_: FStar.Seq.Base.seq a -> Type0) -> h0: FStar.Monotonic.HyperStack.mem -> _: Prims.unit -> h1: FStar.Monotonic.HyperStack.mem -> Prims.GTot Prims.logical
[]
LowStar.Endianness.store_post
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: Prims.nat -> j: Prims.nat{i + j <= LowStar.Monotonic.Buffer.length b} -> predicate: (_: FStar.Seq.Base.seq a -> Type0) -> h0: FStar.Monotonic.HyperStack.mem -> _: Prims.unit -> h1: FStar.Monotonic.HyperStack.mem -> Prims.GTot Prims.logical
{ "end_col": 41, "end_line": 102, "start_col": 5, "start_line": 92 }
FStar.HyperStack.ST.Stack
val load64_be (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul
val load64_be (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) let load64_be (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) =
true
null
false
load64_be_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load64_be_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u64", "FStar.UInt64.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt64.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt64.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load64_be (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z))
[]
LowStar.Endianness.load64_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 8} -> FStar.HyperStack.ST.Stack FStar.UInt64.t
{ "end_col": 19, "end_line": 367, "start_col": 2, "start_line": 367 }
FStar.HyperStack.ST.Stack
val store64_be (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z
val store64_be (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) let store64_be (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) =
true
null
false
store64_be_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt64.t", "LowStar.Endianness.store64_be_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt64.n", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt64.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store64_be (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z))
[]
LowStar.Endianness.store64_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 8} -> z: FStar.UInt64.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 377, "start_col": 2, "start_line": 377 }
FStar.HyperStack.ST.Stack
val load32_le (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul
val load32_le (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) let load32_le (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) =
true
null
false
load32_le_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load32_le_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u32", "FStar.UInt32.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt32.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt32.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load32_le (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z))
[]
LowStar.Endianness.load32_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 4} -> FStar.HyperStack.ST.Stack FStar.UInt32.t
{ "end_col": 19, "end_line": 317, "start_col": 2, "start_line": 317 }
FStar.HyperStack.ST.Stack
val store32_be (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z
val store32_be (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) let store32_be (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) =
true
null
false
store32_be_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt32.t", "LowStar.Endianness.store32_be_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt32.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store32_be (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z))
[]
LowStar.Endianness.store32_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 4} -> z: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 328, "start_col": 2, "start_line": 328 }
FStar.HyperStack.ST.Stack
val store16_le (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z
val store16_le (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) let store16_le (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) =
true
null
false
store16_le_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt16.t", "LowStar.Endianness.store16_le_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt16.n", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt16.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store16_le (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z))
[]
LowStar.Endianness.store16_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 2} -> z: FStar.UInt16.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 268, "start_col": 2, "start_line": 268 }
FStar.HyperStack.ST.Stack
val load64_le (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul
val load64_le (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) let load64_le (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) =
true
null
false
load64_le_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load64_le_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u64", "FStar.UInt64.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt64.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt64.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load64_le (b: B.buffer UInt8.t {B.length b == 8}) : Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z))
[]
LowStar.Endianness.load64_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 8} -> FStar.HyperStack.ST.Stack FStar.UInt64.t
{ "end_col": 19, "end_line": 357, "start_col": 2, "start_line": 357 }
FStar.HyperStack.ST.Stack
val load16_be (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul
val load16_be (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) let load16_be (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) =
true
null
false
load16_be_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load16_be_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u16", "FStar.UInt16.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt16.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt16.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load16_be (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z))
[]
LowStar.Endianness.load16_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 2} -> FStar.HyperStack.ST.Stack FStar.UInt16.t
{ "end_col": 19, "end_line": 297, "start_col": 2, "start_line": 297 }
FStar.HyperStack.ST.Stack
val store32_le (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z
val store32_le (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) let store32_le (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) =
true
null
false
store32_le_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt32.t", "LowStar.Endianness.store32_le_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt32.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store32_le (b: B.buffer UInt8.t {B.length b == 4}) (z: UInt32.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z))
[]
LowStar.Endianness.store32_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 4} -> z: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 308, "start_col": 2, "start_line": 308 }
FStar.HyperStack.ST.Stack
val store16_be (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z
val store16_be (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) let store16_be (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) =
true
null
false
store16_be_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt16.t", "LowStar.Endianness.store16_be_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt16.n", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt16.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store16_be (b: B.buffer UInt8.t {B.length b == 2}) (z: UInt16.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z))
[]
LowStar.Endianness.store16_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 2} -> z: FStar.UInt16.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 288, "start_col": 2, "start_line": 288 }
FStar.HyperStack.ST.Stack
val load32_be (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul
val load32_be (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) let load32_be (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) =
true
null
false
load32_be_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load32_be_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u32", "FStar.UInt32.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt32.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt32.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load32_be (b: B.buffer UInt8.t {B.length b == 4}) : Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z))
[]
LowStar.Endianness.load32_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 4} -> FStar.HyperStack.ST.Stack FStar.UInt32.t
{ "end_col": 19, "end_line": 337, "start_col": 2, "start_line": 337 }
FStar.HyperStack.ST.Stack
val load16_le (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul
val load16_le (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) let load16_le (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) =
true
null
false
load16_le_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load16_le_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u16", "FStar.UInt16.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt16.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt16.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load16_le (b: B.buffer UInt8.t {B.length b == 2}) : Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z))
[]
LowStar.Endianness.load16_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 2} -> FStar.HyperStack.ST.Stack FStar.UInt16.t
{ "end_col": 19, "end_line": 277, "start_col": 2, "start_line": 277 }
FStar.HyperStack.ST.Stack
val store64_le (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z
val store64_le (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) let store64_le (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) =
true
null
false
store64_le_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt64.t", "LowStar.Endianness.store64_le_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt64.n", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt64.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store64_le (b: B.buffer UInt8.t {B.length b == 8}) (z: UInt64.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z))
[]
LowStar.Endianness.store64_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 8} -> z: FStar.UInt64.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 348, "start_col": 2, "start_line": 348 }
FStar.HyperStack.ST.Stack
val store128_le (b: B.buffer UInt8.t {B.length b == 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z
val store128_le (b: B.buffer UInt8.t {B.length b == 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) let store128_le (b: B.buffer UInt8.t {B.length b == 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) =
true
null
false
store128_le_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt128.t", "LowStar.Endianness.store128_le_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt128.n", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt128.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store128_le (b: B.buffer UInt8.t {B.length b == 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z))
[]
LowStar.Endianness.store128_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 16} -> z: FStar.UInt128.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 23, "end_line": 397, "start_col": 2, "start_line": 397 }
FStar.HyperStack.ST.Stack
val store128_be (b: B.buffer UInt8.t {B.length b = 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_be_i b 0ul z
val store128_be (b: B.buffer UInt8.t {B.length b = 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) let store128_be (b: B.buffer UInt8.t {B.length b = 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) =
true
null
false
store128_be_i b 0ul z
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.b2t", "Prims.op_Equality", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "FStar.UInt128.t", "LowStar.Endianness.store128_be_i", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "Prims.eq2", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt128.n", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt128.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val store128_be (b: B.buffer UInt8.t {B.length b = 16}) (z: UInt128.t) : Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z))
[]
LowStar.Endianness.store128_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b = 16} -> z: FStar.UInt128.t -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 23, "end_line": 417, "start_col": 2, "start_line": 417 }
FStar.HyperStack.ST.Stack
val index_64_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let index_64_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) = load64_be_i b FStar.UInt32.(8ul *^ i)
val index_64_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) let index_64_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) =
true
null
false
load64_be_i b FStar.UInt32.(8ul *^ i)
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Endianness.u8", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Endianness.u32", "LowStar.Endianness.load64_be_i", "FStar.UInt32.op_Star_Hat", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u64", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "LowStar.Monotonic.Buffer.length", "Prims.op_LessThan", "FStar.UInt32.v", "Prims.op_Division", "Prims.eq2", "FStar.UInt64.t", "FStar.Seq.Base.index", "FStar.Endianness.seq_uint64_of_be", "LowStar.Monotonic.Buffer.as_seq" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_be_i b 0ul z /// Index and update /// ---------------- /// /// These are more sophisticated than load/store above, because they reason /// over the underlying sequence of bytes interpreted as a sequence of (little|big)-endian /// integers. #set-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 0" inline_for_extraction let index_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_be_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_32_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_le_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_64_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val index_64_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i))
[]
LowStar.Endianness.index_64_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer LowStar.Endianness.u8 rrel rel -> i: LowStar.Endianness.u32 -> FStar.HyperStack.ST.Stack LowStar.Endianness.u64
{ "end_col": 41, "end_line": 465, "start_col": 4, "start_line": 465 }
FStar.HyperStack.ST.Stack
val load128_le (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul
val load128_le (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) let load128_le (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) =
true
null
false
load128_le_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load128_le_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u128", "FStar.UInt128.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt128.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.le_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt128.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load128_le (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z))
[]
LowStar.Endianness.load128_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 16} -> FStar.HyperStack.ST.Stack FStar.UInt128.t
{ "end_col": 20, "end_line": 387, "start_col": 2, "start_line": 387 }
FStar.HyperStack.ST.Stack
val load128_be (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul
val load128_be (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) let load128_be (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) =
true
null
false
load128_be_i b 0ul
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Endianness.load128_be_i", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u128", "FStar.UInt128.t", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.l_and", "Prims.l_or", "FStar.UInt.size", "FStar.UInt128.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Endianness.be_to_n", "LowStar.Monotonic.Buffer.as_seq", "FStar.UInt128.v" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z))
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val load128_be (b: B.buffer UInt8.t {B.length b == 16}) : Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z))
[]
LowStar.Endianness.load128_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Buffer.buffer FStar.UInt8.t {LowStar.Monotonic.Buffer.length b == 16} -> FStar.HyperStack.ST.Stack FStar.UInt128.t
{ "end_col": 20, "end_line": 407, "start_col": 2, "start_line": 407 }
FStar.HyperStack.ST.Stack
val index_64_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_le (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let index_64_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_le (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) = load64_le_i b FStar.UInt32.(8ul *^ i)
val index_64_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_le (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) let index_64_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_le (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) =
true
null
false
load64_le_i b FStar.UInt32.(8ul *^ i)
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Endianness.u8", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Endianness.u32", "LowStar.Endianness.load64_le_i", "FStar.UInt32.op_Star_Hat", "FStar.UInt32.__uint_to_t", "LowStar.Endianness.u64", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "LowStar.Monotonic.Buffer.length", "Prims.op_LessThan", "FStar.UInt32.v", "Prims.op_Division", "Prims.eq2", "FStar.UInt64.t", "FStar.Seq.Base.index", "FStar.Endianness.seq_uint64_of_le", "LowStar.Monotonic.Buffer.as_seq" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_be_i b 0ul z /// Index and update /// ---------------- /// /// These are more sophisticated than load/store above, because they reason /// over the underlying sequence of bytes interpreted as a sequence of (little|big)-endian /// integers. #set-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 0" inline_for_extraction let index_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_be_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_32_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_le_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_64_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) = load64_be_i b FStar.UInt32.(8ul *^ i) inline_for_extraction let index_64_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val index_64_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_le (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i))
[]
LowStar.Endianness.index_64_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer LowStar.Endianness.u8 rrel rel -> i: LowStar.Endianness.u32 -> FStar.HyperStack.ST.Stack LowStar.Endianness.u64
{ "end_col": 41, "end_line": 478, "start_col": 4, "start_line": 478 }
FStar.HyperStack.ST.Stack
val index_32_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let index_32_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_le_i b FStar.UInt32.(4ul *^ i)
val index_32_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) let index_32_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) =
true
null
false
load32_le_i b FStar.UInt32.(4ul *^ i)
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Endianness.u8", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Endianness.u32", "LowStar.Endianness.load32_le_i", "FStar.UInt32.op_Star_Hat", "FStar.UInt32.__uint_to_t", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "LowStar.Monotonic.Buffer.length", "Prims.op_LessThan", "FStar.UInt32.v", "Prims.op_Division", "Prims.eq2", "FStar.UInt32.t", "FStar.Seq.Base.index", "FStar.Endianness.seq_uint32_of_le", "LowStar.Monotonic.Buffer.as_seq" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_be_i b 0ul z /// Index and update /// ---------------- /// /// These are more sophisticated than load/store above, because they reason /// over the underlying sequence of bytes interpreted as a sequence of (little|big)-endian /// integers. #set-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 0" inline_for_extraction let index_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_be_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_32_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val index_32_le (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i))
[]
LowStar.Endianness.index_32_le
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer LowStar.Endianness.u8 rrel rel -> i: LowStar.Endianness.u32 -> FStar.HyperStack.ST.Stack LowStar.Endianness.u32
{ "end_col": 41, "end_line": 452, "start_col": 4, "start_line": 452 }
FStar.HyperStack.ST.Stack
val index_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i))
[ { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let index_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_be_i b FStar.UInt32.(4ul *^ i)
val index_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) let index_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) =
true
null
false
load32_be_i b FStar.UInt32.(4ul *^ i)
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Endianness.u8", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Endianness.u32", "LowStar.Endianness.load32_be_i", "FStar.UInt32.op_Star_Hat", "FStar.UInt32.__uint_to_t", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "LowStar.Monotonic.Buffer.length", "Prims.op_LessThan", "FStar.UInt32.v", "Prims.op_Division", "Prims.eq2", "FStar.UInt32.t", "FStar.Seq.Base.index", "FStar.Endianness.seq_uint32_of_be", "LowStar.Monotonic.Buffer.as_seq" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_be_i b 0ul z /// Index and update /// ---------------- /// /// These are more sophisticated than load/store above, because they reason /// over the underlying sequence of bytes interpreted as a sequence of (little|big)-endian /// integers. #set-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 0" inline_for_extraction let index_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val index_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i: u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i))
[]
LowStar.Endianness.index_32_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer LowStar.Endianness.u8 rrel rel -> i: LowStar.Endianness.u32 -> FStar.HyperStack.ST.Stack LowStar.Endianness.u32
{ "end_col": 41, "end_line": 439, "start_col": 4, "start_line": 439 }
FStar.HyperStack.ST.Stack
val upd_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i v: u32) : Stack unit (requires fun h -> MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4 /\ store_pre b (let open U32 in v (4ul *^ i)) 4 (fun s -> be_to_n s == U32.v v) h) (ensures fun h0 _ h1 -> MB.(modifies (loc_buffer b) h0 h1) /\ (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b)) `Seq.equal` (Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i) v))
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.UInt128", "short_module": "U128" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.Endianness", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "LowStar.Monotonic.Buffer", "short_module": "MB" }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "LowStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let upd_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) (v:u32) : Stack unit (requires fun h -> MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4 /\ store_pre b (U32.(v (4ul *^ i))) 4 (fun s -> be_to_n s == U32.v v) h) (ensures fun h0 _ h1 -> MB.(modifies (loc_buffer b) h0 h1) /\ seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b) `Seq.equal` Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i) v) = let h0 = get () in store32_be_i b U32.(4ul *^ i) v; let h1 = get () in //AR: 03/01: the following 3 assertions say how the buffer changed assert (be_to_n (Seq.slice (MB.as_seq h1 b) (U32.(v (4ul *^ i))) (U32.(v (4ul *^ i) + 4))) == U32.v v); assert (Seq.equal (Seq.slice (MB.as_seq h0 b) 0 (U32.(v (4ul *^ i)))) (Seq.slice (MB.as_seq h1 b) 0 (U32.(v (4ul *^ i))))); assert (Seq.equal (Seq.slice (MB.as_seq h0 b) (U32.(v (4ul *^ i) + 4)) (MB.length b)) (Seq.slice (MB.as_seq h1 b) (U32.(v (4ul *^ i) + 4)) (MB.length b))); let f () : Lemma (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b) `Seq.equal` Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (UInt32.v i) v) = let s0 = MB.as_seq h0 b in let s1 = MB.as_seq h1 b in let n = MB.length b / 4 in assert (4 `Prims.op_Multiply` n == MB.length b); let s0' = seq_uint32_of_be n s0 in let s1' = seq_uint32_of_be n s1 in let lo = UInt32.v i in let hi = lo + 1 in let s2' = Seq.upd s0' lo v in assert (Seq.length s1' == Seq.length s2'); let i' = UInt32.v i in let g (j' : nat) : Lemma (requires (j' < n)) (ensures (j' < n /\ Seq.index s1' j' == Seq.index s2' j')) = if j' = UInt32.v i then () else begin let u () : Lemma (Seq.slice s0 (4 * j') (4 * j' + 4) == Seq.slice s1 (4 * j') (4 * j' + 4)) = if j' < UInt32.v i then begin Seq.slice_slice s0 0 (4 * i') (4 * j') (4 * j' + 4); Seq.slice_slice s1 0 (4 * i') (4 * j') (4 * j' + 4) end else begin Seq.slice_slice s0 (4 * i' + 4) (MB.length b) (4 * (j' - i' - 1)) (4 * (j' - i')); Seq.slice_slice s1 (4 * i' + 4) (MB.length b) (4 * (j' - i' - 1)) (4 * (j' - i')) end in u () end in Classical.forall_intro (Classical.move_requires g) in f ()
val upd_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i v: u32) : Stack unit (requires fun h -> MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4 /\ store_pre b (let open U32 in v (4ul *^ i)) 4 (fun s -> be_to_n s == U32.v v) h) (ensures fun h0 _ h1 -> MB.(modifies (loc_buffer b) h0 h1) /\ (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b)) `Seq.equal` (Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i) v)) let upd_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i v: u32) : Stack unit (requires fun h -> MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4 /\ store_pre b (let open U32 in v (4ul *^ i)) 4 (fun s -> be_to_n s == U32.v v) h) (ensures fun h0 _ h1 -> MB.(modifies (loc_buffer b) h0 h1) /\ (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b)) `Seq.equal` (Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i) v)) =
true
null
false
let h0 = get () in store32_be_i b U32.(4ul *^ i) v; let h1 = get () in assert (be_to_n (Seq.slice (MB.as_seq h1 b) (let open U32 in v (4ul *^ i)) (let open U32 in v (4ul *^ i) + 4)) == U32.v v); assert (Seq.equal (Seq.slice (MB.as_seq h0 b) 0 (let open U32 in v (4ul *^ i))) (Seq.slice (MB.as_seq h1 b) 0 (let open U32 in v (4ul *^ i)))); assert (Seq.equal (Seq.slice (MB.as_seq h0 b) (let open U32 in v (4ul *^ i) + 4) (MB.length b)) (Seq.slice (MB.as_seq h1 b) (let open U32 in v (4ul *^ i) + 4) (MB.length b))); let f () : Lemma ((seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b)) `Seq.equal` (Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (UInt32.v i) v)) = let s0 = MB.as_seq h0 b in let s1 = MB.as_seq h1 b in let n = MB.length b / 4 in assert (4 `Prims.op_Multiply` n == MB.length b); let s0' = seq_uint32_of_be n s0 in let s1' = seq_uint32_of_be n s1 in let lo = UInt32.v i in let hi = lo + 1 in let s2' = Seq.upd s0' lo v in assert (Seq.length s1' == Seq.length s2'); let i' = UInt32.v i in let g (j': nat) : Lemma (requires (j' < n)) (ensures (j' < n /\ Seq.index s1' j' == Seq.index s2' j')) = if j' = UInt32.v i then () else let u () : Lemma (Seq.slice s0 (4 * j') (4 * j' + 4) == Seq.slice s1 (4 * j') (4 * j' + 4)) = if j' < UInt32.v i then (Seq.slice_slice s0 0 (4 * i') (4 * j') (4 * j' + 4); Seq.slice_slice s1 0 (4 * i') (4 * j') (4 * j' + 4)) else (Seq.slice_slice s0 (4 * i' + 4) (MB.length b) (4 * (j' - i' - 1)) (4 * (j' - i')); Seq.slice_slice s1 (4 * i' + 4) (MB.length b) (4 * (j' - i' - 1)) (4 * (j' - i'))) in u () in Classical.forall_intro (Classical.move_requires g) in f ()
{ "checked_file": "LowStar.Endianness.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Monotonic.Buffer.fsti.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.UInt128.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Endianness.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowStar.Endianness.fst" }
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Endianness.u8", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Endianness.u32", "Prims.unit", "Prims.l_True", "Prims.squash", "FStar.Seq.Base.equal", "FStar.UInt32.t", "FStar.Endianness.seq_uint32_of_be", "Prims.op_Division", "LowStar.Monotonic.Buffer.length", "LowStar.Monotonic.Buffer.as_seq", "FStar.Seq.Base.upd", "FStar.UInt32.v", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Classical.forall_intro", "Prims.nat", "Prims.l_imp", "Prims.b2t", "Prims.op_LessThan", "Prims.l_and", "Prims.eq2", "FStar.Seq.Base.index", "FStar.Classical.move_requires", "Prims.op_Equality", "Prims.int", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "Prims.bool", "FStar.Seq.Base.seq", "FStar.Seq.Base.slice", "Prims.op_Multiply", "Prims.op_Addition", "FStar.Seq.Properties.slice_slice", "FStar.Mul.op_Star", "Prims.op_Subtraction", "FStar.UInt.uint_t", "Prims._assert", "FStar.Seq.Base.length", "FStar.UInt32.op_Star_Hat", "FStar.UInt32.__uint_to_t", "FStar.Endianness.be_to_n", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "LowStar.Endianness.store32_be_i", "Prims.op_Modulus", "LowStar.Endianness.store_pre", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer" ]
[]
module LowStar.Endianness /// Stateful operations between machine integers and buffers of uint8s. Most of /// these operations are implemented natively using the target's system endianness /// headers, relying on macros or static inline declarations. /// /// .. note:: /// /// This module supersedes ``C.Endianness``. module MB = LowStar.Monotonic.Buffer module B = LowStar.Buffer open FStar.HyperStack.ST open FStar.Endianness open LowStar.BufferOps module U8 = FStar.UInt8 module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module U128 = FStar.UInt128 module HS = FStar.HyperStack inline_for_extraction type u8 = U8.t inline_for_extraction type u16 = U16.t inline_for_extraction type u32 = U32.t inline_for_extraction type u64 = U64.t inline_for_extraction type u128 = U128.t /// Byte-swapping operations /// ------------------------ /// /// TODO these are totally unspecified assume val htole16: u16 -> u16 assume val le16toh: u16 -> u16 assume val htole32: u32 -> u32 assume val le32toh: u32 -> u32 assume val htole64: u64 -> u64 assume val le64toh: u64 -> u64 assume val htobe16: u16 -> u16 assume val be16toh: u16 -> u16 assume val htobe32: u32 -> u32 assume val be32toh: u32 -> u32 assume val htobe64: u64 -> u64 assume val be64toh: u64 -> u64 /// Precondition for store functions /// /// Parametrized by the predicate that different store functions can pass accordingly /// /// Typically saying sequence contents are le_to_n or be_to_n etc. unfold let store_pre (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h:HS.mem) -> let sb = MB.as_seq h b in let len = MB.length b in MB.live h b /\ (forall (s:Seq.seq a). //monotonicity precondition that once the contents of the buffer //between [i, j) are replaced as per the predicate, the //preorder rel is satisfied (Seq.length s == len /\ Seq.equal (Seq.slice s 0 i) (Seq.slice sb 0 i) /\ Seq.equal (Seq.slice s (i + j) len) (Seq.slice sb (i + j) len) /\ predicate (Seq.slice s i (i + j))) ==> rel sb s) /// Common postcondition unfold let store_post (#a:Type) (#rrel #rel:MB.srel a) (b:MB.mbuffer a rrel rel) (i:nat) (j:nat{i + j <= MB.length b}) (predicate:Seq.seq a -> Type0) = fun (h0:HS.mem) (_:unit) (h1:HS.mem) -> MB.live h1 b /\ MB.(modifies (loc_buffer b) h0 h1) /\ (let s1 = MB.as_seq h0 b in let s2 = MB.as_seq h1 b in let len = MB.length b in //the buffer only changes in the interval [i, j) as per the predicate Seq.equal (Seq.slice s2 0 i) (Seq.slice s1 0 i) /\ Seq.equal (Seq.slice s2 (i + j) len) (Seq.slice s1 (i + j) len) /\ predicate (Seq.slice s2 i (i + j))) /// Loads and stores /// ---------------- /// /// These are primitive assume val store16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> le_to_n s == U16.v z))) assume val load16_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) (z:u16) : Stack unit (requires (store_pre b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) (ensures (store_post b (U32.v i) 2 (fun s -> be_to_n s == U16.v z))) assume val load16_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 2 <= MB.length b}) : Stack u16 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 2)) == U16.v z) assume val store32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> le_to_n s == U32.v z))) assume val load32_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) (z:u32) : Stack unit (requires (store_pre b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) (ensures (store_post b (U32.v i) 4 (fun s -> be_to_n s == U32.v z))) assume val load32_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 4 <= MB.length b}) : Stack u32 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 4)) == U32.v z) assume val store64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> le_to_n s == U64.v z))) assume val load64_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) (z:u64) : Stack unit (requires (store_pre b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) (ensures (store_post b (U32.v i) 8 (fun s -> be_to_n s == U64.v z))) assume val load64_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 8 <= MB.length b}) : Stack u64 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 8)) == U64.v z) assume val store128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> le_to_n s == U128.v z))) assume val load128_le_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ le_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) assume val store128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) (z:u128) : Stack unit (requires (store_pre b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) (ensures (store_post b (U32.v i) 16 (fun s -> be_to_n s == U128.v z))) assume val load128_be_i (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32{U32.v i + 16 <= MB.length b}) : Stack u128 (requires fun h -> MB.live h b) (ensures fun h0 z h1 -> h0 == h1 /\ MB.live h1 b /\ be_to_n (Seq.slice (MB.as_seq h1 b) (U32.v i) (U32.v i + 16)) == U128.v z) /// Loads and stores, on buffers of the right size. /// ----------------------------------------------- /// /// There is bunch of legacy code that wants these operators that operate on buffers of exactly the right size. This is actually more restrictive than the version above, which operates on monotonic buffers, so we offer specialized operators. let store16_le (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_le_i b 0ul z let load16_le (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_le_i b 0ul let store16_be (b:B.buffer UInt8.t{B.length b == 2}) (z:UInt16.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = store16_be_i b 0ul z let load16_be (b:B.buffer UInt8.t{B.length b == 2}): Stack UInt16.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt16.v z)) = load16_be_i b 0ul let store32_le (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_le_i b 0ul z let load32_le (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_le_i b 0ul let store32_be (b:B.buffer UInt8.t{B.length b == 4}) (z:UInt32.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = store32_be_i b 0ul z let load32_be (b:B.buffer UInt8.t{B.length b == 4}): Stack UInt32.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt32.v z)) = load32_be_i b 0ul let store64_le (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_le_i b 0ul z let load64_le (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_le_i b 0ul let load64_be (b:B.buffer UInt8.t{B.length b == 8}): Stack UInt64.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = load64_be_i b 0ul let store64_be (b:B.buffer UInt8.t{B.length b == 8}) (z:UInt64.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt64.v z)) = store64_be_i b 0ul z let load128_le (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_le_i b 0ul let store128_le (b:B.buffer UInt8.t{B.length b == 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ le_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_le_i b 0ul z let load128_be (b:B.buffer UInt8.t{B.length b == 16}): Stack UInt128.t (requires (fun h -> B.live h b)) (ensures (fun h0 z h1 -> h0 == h1 /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = load128_be_i b 0ul let store128_be (b:B.buffer UInt8.t{B.length b = 16}) (z:UInt128.t): Stack unit (requires (fun h -> B.live h b)) (ensures (fun h0 _ h1 -> B.(modifies (loc_buffer b) h0 h1) /\ B.live h1 b /\ be_to_n (B.as_seq h1 b) == UInt128.v z)) = store128_be_i b 0ul z /// Index and update /// ---------------- /// /// These are more sophisticated than load/store above, because they reason /// over the underlying sequence of bytes interpreted as a sequence of (little|big)-endian /// integers. #set-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 0" inline_for_extraction let index_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_be_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_32_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u32 (requires fun h -> MB.live h b /\ MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint32_of_le (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i)) = load32_le_i b FStar.UInt32.(4ul *^ i) inline_for_extraction let index_64_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_be (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) = load64_be_i b FStar.UInt32.(8ul *^ i) inline_for_extraction let index_64_le (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) : Stack u64 (requires fun h -> MB.live h b /\ MB.length b % 8 = 0 /\ U32.v i < MB.length b / 8) (ensures fun h0 r h1 -> h0 == h1 /\ r = Seq.index (seq_uint64_of_le (MB.length b / 8) (MB.as_seq h0 b)) (UInt32.v i)) = load64_le_i b FStar.UInt32.(8ul *^ i) #reset-options "--using_facts_from 'Prims'" let interval_4_disjoint (i j: nat) : Lemma (requires (i <> j)) (ensures (let open FStar.Mul in 4 * i + 4 <= 4 * j \/ 4 * j + 4 <= 4 * i)) = () #reset-options "--z3rlimit 16 --max_fuel 0 --max_ifuel 0" open FStar.Mul inline_for_extraction let upd_32_be (#rrel #rel:MB.srel u8) (b:MB.mbuffer u8 rrel rel) (i:u32) (v:u32) : Stack unit (requires fun h -> MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4 /\ store_pre b (U32.(v (4ul *^ i))) 4 (fun s -> be_to_n s == U32.v v) h) (ensures fun h0 _ h1 -> MB.(modifies (loc_buffer b) h0 h1) /\
false
false
LowStar.Endianness.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val upd_32_be (#rrel #rel: MB.srel u8) (b: MB.mbuffer u8 rrel rel) (i v: u32) : Stack unit (requires fun h -> MB.length b % 4 = 0 /\ U32.v i < MB.length b / 4 /\ store_pre b (let open U32 in v (4ul *^ i)) 4 (fun s -> be_to_n s == U32.v v) h) (ensures fun h0 _ h1 -> MB.(modifies (loc_buffer b) h0 h1) /\ (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h1 b)) `Seq.equal` (Seq.upd (seq_uint32_of_be (MB.length b / 4) (MB.as_seq h0 b)) (U32.v i) v))
[]
LowStar.Endianness.upd_32_be
{ "file_name": "ulib/LowStar.Endianness.fst", "git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
b: LowStar.Monotonic.Buffer.mbuffer LowStar.Endianness.u8 rrel rel -> i: LowStar.Endianness.u32 -> v: LowStar.Endianness.u32 -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 8, "end_line": 551, "start_col": 3, "start_line": 504 }
Prims.Tot
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let bytes32 = B32.bytes
let bytes32 =
false
null
false
B32.bytes
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "FStar.Bytes.bytes" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val bytes32 : t: Type0{Prims.hasEq t}
[]
LowParse.SLow.Base.bytes32
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t: Type0{Prims.hasEq t}
{ "end_col": 23, "end_line": 7, "start_col": 14, "start_line": 7 }
Prims.GTot
val serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input
val serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 =
false
null
false
(B32.reveal res) `bytes_equal` (s input)
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.SLow.Base.bytes32", "LowParse.Bytes.bytes_equal", "FStar.Bytes.reveal" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0
[]
LowParse.SLow.Base.serializer32_correct'
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> input: t -> res: LowParse.SLow.Base.bytes32 -> Prims.GTot Type0
{ "end_col": 38, "end_line": 118, "start_col": 2, "start_line": 118 }
Prims.GTot
val serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input
val serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 =
false
null
false
B32.reveal res == s input
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.SLow.Base.bytes32", "Prims.eq2", "FStar.Seq.Base.seq", "FStar.Bytes.byte", "FStar.Bytes.reveal" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0
[]
LowParse.SLow.Base.serializer32_correct
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> input: t -> res: LowParse.SLow.Base.bytes32 -> Prims.GTot Type0
{ "end_col": 27, "end_line": 108, "start_col": 2, "start_line": 108 }
Prims.Tot
val make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32 -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res)))) : Tot (parser32 p)
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } )))
val make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32 -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res)))) : Tot (parser32 p) let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32 -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res)))) : Tot (parser32 p) =
false
null
false
(fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) {parser32_correct p input res})))
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.SLow.Base.bytes32", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "FStar.UInt32.t", "Prims.l_True", "LowParse.SLow.Base.parser32_correct", "LowParse.SLow.Base.parser32" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res)))
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32 -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res)))) : Tot (parser32 p)
[]
LowParse.SLow.Base.make_parser32
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> p32: (input: LowParse.SLow.Base.bytes32 -> Prims.Pure (FStar.Pervasives.Native.option (t * FStar.UInt32.t))) -> LowParse.SLow.Base.parser32 p
{ "end_col": 102, "end_line": 64, "start_col": 2, "start_line": 64 }
Prims.GTot
val size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0 = k.parser_kind_high == Some k.parser_kind_low /\ U32.v len32 == k.parser_kind_low
val size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0 let size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0 =
false
null
false
k.parser_kind_high == Some k.parser_kind_low /\ U32.v len32 == k.parser_kind_low
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "FStar.UInt32.t", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "Prims.int", "Prims.l_or", "FStar.UInt.size", "FStar.UInt32.n", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt32.v" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p inline_for_extraction let u32_max : (y: U32.t { forall (x: U32.t) . {:pattern (U32.v x)} U32.v x <= U32.v y } ) = 4294967295ul inline_for_extraction let add_overflow (x y: U32.t) : Pure U32.t (requires True) (ensures (fun z -> if U32.v x + U32.v y > U32.v u32_max then z == u32_max else U32.v z == U32.v x + U32.v y )) = if U32.lt (U32.sub u32_max y) x then u32_max else U32.add x y let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 = let sz = Seq.length (serialize s x) in if sz > U32.v u32_max then y == u32_max else U32.v y == sz [@unifier_hint_injective] inline_for_extraction let size32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (x: t) -> Tot (y: U32.t { size32_postcond s x y }) let size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0
[]
LowParse.SLow.Base.size32_constant_precond
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> len32: FStar.UInt32.t -> Prims.GTot Type0
{ "end_col": 34, "end_line": 352, "start_col": 2, "start_line": 351 }
Prims.GTot
val parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) )
val parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 =
false
null
false
let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ (let Some (hres', consumed') = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat))
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.SLow.Base.bytes32", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "FStar.UInt32.t", "Prims.eq2", "LowParse.Spec.Base.consumed_length", "FStar.Bytes.reveal", "FStar.Pervasives.Native.None", "Prims.l_and", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "Prims.int", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "FStar.UInt32.v", "Prims.nat", "Prims.logical", "LowParse.Spec.Base.parse" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t))
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0
[]
LowParse.SLow.Base.parser32_correct
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> input: LowParse.SLow.Base.bytes32 -> res: FStar.Pervasives.Native.option (t * FStar.UInt32.t) -> Prims.GTot Type0
{ "end_col": 5, "end_line": 24, "start_col": 1, "start_line": 16 }
Prims.GTot
val validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) )
val validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 =
false
null
false
let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some consumed -> Some? gp /\ (let Some (_, consumed') = gp in U32.v consumed == (consumed' <: nat))
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.SLow.Base.bytes32", "FStar.Pervasives.Native.option", "FStar.UInt32.t", "Prims.eq2", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "FStar.Bytes.reveal", "FStar.Pervasives.Native.None", "Prims.l_and", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "Prims.int", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "FStar.UInt32.v", "Prims.nat", "Prims.logical", "LowParse.Spec.Base.parse" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0
[]
LowParse.SLow.Base.validator_correct
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> input: LowParse.SLow.Base.bytes32 -> res: FStar.Pervasives.Native.option FStar.UInt32.t -> Prims.GTot Type0
{ "end_col": 5, "end_line": 91, "start_col": 1, "start_line": 84 }
Prims.GTot
val size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 = let sz = Seq.length (serialize s x) in if sz > U32.v u32_max then y == u32_max else U32.v y == sz
val size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 =
false
null
false
let sz = Seq.length (serialize s x) in if sz > U32.v u32_max then y == u32_max else U32.v y == sz
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "FStar.UInt32.t", "Prims.op_GreaterThan", "FStar.UInt32.v", "LowParse.SLow.Base.u32_max", "Prims.eq2", "Prims.bool", "Prims.int", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "Prims.nat", "FStar.Seq.Base.length", "LowParse.Bytes.byte", "LowParse.Spec.Base.serialize" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p inline_for_extraction let u32_max : (y: U32.t { forall (x: U32.t) . {:pattern (U32.v x)} U32.v x <= U32.v y } ) = 4294967295ul inline_for_extraction let add_overflow (x y: U32.t) : Pure U32.t (requires True) (ensures (fun z -> if U32.v x + U32.v y > U32.v u32_max then z == u32_max else U32.v z == U32.v x + U32.v y )) = if U32.lt (U32.sub u32_max y) x then u32_max else U32.add x y let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0
[]
LowParse.SLow.Base.size32_postcond
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> x: t -> y: FStar.UInt32.t -> Prims.GTot Type0
{ "end_col": 20, "end_line": 329, "start_col": 1, "start_line": 326 }
FStar.Pervasives.Lemma
val serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures (let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ (match k.parser_kind_high with | Some max -> len <= max | _ -> True))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)]
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input
val serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures (let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ (match k.parser_kind_high with | Some max -> len <= max | _ -> True))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures (let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ (match k.parser_kind_high with | Some max -> len <= max | _ -> True))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] =
false
null
true
serialize_length s input
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.SLow.Base.bytes32", "LowParse.Spec.Base.serialize_length", "Prims.unit", "LowParse.SLow.Base.serializer32_correct", "Prims.squash", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "Prims.nat", "FStar.Pervasives.Native.option", "Prims.l_True", "Prims.logical", "FStar.UInt.uint_t", "FStar.Bytes.length", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True )))
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures (let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ (match k.parser_kind_high with | Some max -> len <= max | _ -> True))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)]
[]
LowParse.SLow.Base.serializer32_correct_length
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> input: t -> res: LowParse.SLow.Base.bytes32 -> FStar.Pervasives.Lemma (requires LowParse.SLow.Base.serializer32_correct s input res) (ensures (let len = FStar.Bytes.length res in Mkparser_kind'?.parser_kind_low k <= len /\ (match Mkparser_kind'?.parser_kind_high k with | FStar.Pervasives.Native.Some #_ max -> len <= max | _ -> Prims.l_True))) [ SMTPat (LowParse.SLow.Base.serializer32_correct s input res); SMTPat (FStar.Bytes.length res) ]
{ "end_col": 26, "end_line": 147, "start_col": 2, "start_line": 147 }
FStar.Pervasives.Lemma
val parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires (k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input)) (ensures (Some? (p32 input)))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p
val parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires (k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input)) (ensures (Some? (p32 input))) let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires (k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input)) (ensures (Some? (p32 input))) =
false
null
true
parser_kind_prop_equiv k p
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.SLow.Base.parser32", "LowParse.SLow.Base.bytes32", "LowParse.Spec.Base.parser_kind_prop_equiv", "Prims.unit", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "LowParse.Spec.Base.parser_kind_metadata_some", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata", "LowParse.Spec.Base.ParserKindMetadataTotal", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Bytes.length", "Prims.squash", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "FStar.UInt32.t", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires (k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input)) (ensures (Some? (p32 input)))
[]
LowParse.SLow.Base.parse32_total
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p32: LowParse.SLow.Base.parser32 p -> input: LowParse.SLow.Base.bytes32 -> FStar.Pervasives.Lemma (requires Mkparser_kind'?.parser_kind_high k == FStar.Pervasives.Native.Some (Mkparser_kind'?.parser_kind_low k) /\ Mkparser_kind'?.parser_kind_metadata k == FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserKindMetadataTotal /\ Mkparser_kind'?.parser_kind_low k <= FStar.Bytes.length input) (ensures Some? (p32 input))
{ "end_col": 28, "end_line": 298, "start_col": 2, "start_line": 298 }
FStar.Pervasives.Lemma
val parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures (k.parser_kind_low <= U32.v consumed /\ (Some? k.parser_kind_high ==> (let Some hi = k.parser_kind_high in U32.v consumed <= hi))))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p
val parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures (k.parser_kind_low <= U32.v consumed /\ (Some? k.parser_kind_high ==> (let Some hi = k.parser_kind_high in U32.v consumed <= hi)))) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures (k.parser_kind_low <= U32.v consumed /\ (Some? k.parser_kind_high ==> (let Some hi = k.parser_kind_high in U32.v consumed <= hi)))) =
false
null
true
parser_kind_prop_equiv k p
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.SLow.Base.parser32", "LowParse.SLow.Base.bytes32", "FStar.UInt32.t", "LowParse.Spec.Base.parser_kind_prop_equiv", "Prims.unit", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Prims.squash", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "FStar.UInt32.v", "Prims.l_imp", "FStar.Pervasives.Native.uu___is_Some", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "Prims.logical", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures (k.parser_kind_low <= U32.v consumed /\ (Some? k.parser_kind_high ==> (let Some hi = k.parser_kind_high in U32.v consumed <= hi))))
[]
LowParse.SLow.Base.parse32_size
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p32: LowParse.SLow.Base.parser32 p -> input: LowParse.SLow.Base.bytes32 -> data: t -> consumed: FStar.UInt32.t -> FStar.Pervasives.Lemma (requires p32 input == FStar.Pervasives.Native.Some (data, consumed)) (ensures Mkparser_kind'?.parser_kind_low k <= FStar.UInt32.v consumed /\ (Some? (Mkparser_kind'?.parser_kind_high k) ==> (let _ = Mkparser_kind'?.parser_kind_high k in (let FStar.Pervasives.Native.Some #_ hi = _ in FStar.UInt32.v consumed <= hi) <: Prims.logical)))
{ "end_col": 28, "end_line": 281, "start_col": 2, "start_line": 281 }
Prims.Tot
val bytes_of_seq (x: Seq.seq byte {Seq.length x < 4294967296}) : Tot (y: bytes32{(B32.reveal y) `Seq.equal` x})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let bytes_of_seq (x: Seq.seq byte { Seq.length x < 4294967296 }) : Tot (y: bytes32 { B32.reveal y `Seq.equal` x }) = bytes_of_seq' x B32.empty_bytes
val bytes_of_seq (x: Seq.seq byte {Seq.length x < 4294967296}) : Tot (y: bytes32{(B32.reveal y) `Seq.equal` x}) let bytes_of_seq (x: Seq.seq byte {Seq.length x < 4294967296}) : Tot (y: bytes32{(B32.reveal y) `Seq.equal` x}) =
false
null
false
bytes_of_seq' x B32.empty_bytes
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "FStar.Seq.Base.seq", "LowParse.Bytes.byte", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.length", "LowParse.SLow.Base.bytes_of_seq'", "FStar.Bytes.empty_bytes", "LowParse.SLow.Base.bytes32", "FStar.Seq.Base.equal", "FStar.Bytes.byte", "FStar.Bytes.reveal" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p inline_for_extraction let u32_max : (y: U32.t { forall (x: U32.t) . {:pattern (U32.v x)} U32.v x <= U32.v y } ) = 4294967295ul inline_for_extraction let add_overflow (x y: U32.t) : Pure U32.t (requires True) (ensures (fun z -> if U32.v x + U32.v y > U32.v u32_max then z == u32_max else U32.v z == U32.v x + U32.v y )) = if U32.lt (U32.sub u32_max y) x then u32_max else U32.add x y let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 = let sz = Seq.length (serialize s x) in if sz > U32.v u32_max then y == u32_max else U32.v y == sz [@unifier_hint_injective] inline_for_extraction let size32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (x: t) -> Tot (y: U32.t { size32_postcond s x y }) let size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0 = k.parser_kind_high == Some k.parser_kind_low /\ U32.v len32 == k.parser_kind_low inline_for_extraction let size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit { size32_constant_precond s len32 } ) : Tot (size32 s) = fun x -> [@inline_let] let (z: U32.t { size32_postcond s x z } ) = len32 in z inline_for_extraction let size32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': size32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (size32 (serialize_ext p1 s1 p2)) = fun input -> s1' input (* Total parsers for sequences *) [@"opaque_to_smt"] irreducible let rec bytes_of_seq' (x: Seq.seq byte) (accu: bytes32 { B32.length accu + Seq.length x < 4294967296 }) : Tot (y: bytes32 { B32.reveal y `Seq.equal` (B32.reveal accu `Seq.append` x) }) (decreases (Seq.length x)) = if Seq.length x = 0 then accu else bytes_of_seq' (Seq.tail x) (B32.append accu (B32.create 1ul (Seq.head x))) [@"opaque_to_smt"] inline_for_extraction let bytes_of_seq (x: Seq.seq byte { Seq.length x < 4294967296 })
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val bytes_of_seq (x: Seq.seq byte {Seq.length x < 4294967296}) : Tot (y: bytes32{(B32.reveal y) `Seq.equal` x})
[]
LowParse.SLow.Base.bytes_of_seq
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
x: FStar.Seq.Base.seq LowParse.Bytes.byte {FStar.Seq.Base.length x < 4294967296} -> y: LowParse.SLow.Base.bytes32{FStar.Seq.Base.equal (FStar.Bytes.reveal y) x}
{ "end_col": 33, "end_line": 400, "start_col": 2, "start_line": 400 }
Prims.Tot
val seq_of_bytes (x: bytes32) : Tot (y: Seq.seq byte {y `Seq.equal` (B32.reveal x)})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let seq_of_bytes (x: bytes32) : Tot (y: Seq.seq byte { y `Seq.equal` B32.reveal x }) = seq_of_bytes' x Seq.empty
val seq_of_bytes (x: bytes32) : Tot (y: Seq.seq byte {y `Seq.equal` (B32.reveal x)}) let seq_of_bytes (x: bytes32) : Tot (y: Seq.seq byte {y `Seq.equal` (B32.reveal x)}) =
false
null
false
seq_of_bytes' x Seq.empty
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "LowParse.SLow.Base.bytes32", "LowParse.SLow.Base.seq_of_bytes'", "FStar.Seq.Base.empty", "LowParse.Bytes.byte", "FStar.Seq.Base.seq", "FStar.Seq.Base.equal", "FStar.Bytes.reveal" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p inline_for_extraction let u32_max : (y: U32.t { forall (x: U32.t) . {:pattern (U32.v x)} U32.v x <= U32.v y } ) = 4294967295ul inline_for_extraction let add_overflow (x y: U32.t) : Pure U32.t (requires True) (ensures (fun z -> if U32.v x + U32.v y > U32.v u32_max then z == u32_max else U32.v z == U32.v x + U32.v y )) = if U32.lt (U32.sub u32_max y) x then u32_max else U32.add x y let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 = let sz = Seq.length (serialize s x) in if sz > U32.v u32_max then y == u32_max else U32.v y == sz [@unifier_hint_injective] inline_for_extraction let size32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (x: t) -> Tot (y: U32.t { size32_postcond s x y }) let size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0 = k.parser_kind_high == Some k.parser_kind_low /\ U32.v len32 == k.parser_kind_low inline_for_extraction let size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit { size32_constant_precond s len32 } ) : Tot (size32 s) = fun x -> [@inline_let] let (z: U32.t { size32_postcond s x z } ) = len32 in z inline_for_extraction let size32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': size32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (size32 (serialize_ext p1 s1 p2)) = fun input -> s1' input (* Total parsers for sequences *) [@"opaque_to_smt"] irreducible let rec bytes_of_seq' (x: Seq.seq byte) (accu: bytes32 { B32.length accu + Seq.length x < 4294967296 }) : Tot (y: bytes32 { B32.reveal y `Seq.equal` (B32.reveal accu `Seq.append` x) }) (decreases (Seq.length x)) = if Seq.length x = 0 then accu else bytes_of_seq' (Seq.tail x) (B32.append accu (B32.create 1ul (Seq.head x))) [@"opaque_to_smt"] inline_for_extraction let bytes_of_seq (x: Seq.seq byte { Seq.length x < 4294967296 }) : Tot (y: bytes32 { B32.reveal y `Seq.equal` x }) = bytes_of_seq' x B32.empty_bytes inline_for_extraction let parse_tot_seq_of_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p { k.parser_kind_subkind == Some ParserStrong /\ begin match k.parser_kind_high with | None -> False | Some max -> max < 4294967296 end }) (x: Seq.seq byte) : Tot (y: _ { y == parse p x }) = match k.parser_kind_high with | Some max -> if Seq.length x < max then match p32 (bytes_of_seq x) with | None -> None | Some (x, consumed) -> Some (x, U32.v consumed) else begin [@inline_let] let max32 = U32.uint_to_t max in let res = p32 (bytes_of_seq (Seq.slice x 0 max)) in Classical.move_requires (parse_strong_prefix p x) (Seq.slice x 0 max); Classical.move_requires (parse_strong_prefix p (Seq.slice x 0 max)) x; parser_kind_prop_equiv k p; match res with | None -> None | Some (x, consumed) -> Some (x, U32.v consumed) end [@"opaque_to_smt"] irreducible let rec seq_of_bytes' (x: bytes32) (accu: Seq.seq byte) : Tot (y: Seq.seq byte { y `Seq.equal` (accu `Seq.append` B32.reveal x) }) (decreases (B32.length x)) = if B32.len x = 0ul then accu else (seq_of_bytes' (B32.slice x 1ul (B32.len x)) (Seq.append accu (Seq.create 1 (B32.index x 0))) <: Seq.seq byte) [@"opaque_to_smt"] inline_for_extraction let seq_of_bytes (x: bytes32)
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val seq_of_bytes (x: bytes32) : Tot (y: Seq.seq byte {y `Seq.equal` (B32.reveal x)})
[]
LowParse.SLow.Base.seq_of_bytes
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
x: LowParse.SLow.Base.bytes32 -> y: FStar.Seq.Base.seq LowParse.Bytes.byte {FStar.Seq.Base.equal y (FStar.Bytes.reveal x)}
{ "end_col": 27, "end_line": 451, "start_col": 2, "start_line": 451 }
FStar.Pervasives.Lemma
val serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2))
val serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) =
false
null
true
assert (parse p (serialize s input1) == parse p (serialize s input2))
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.SLow.Base.serializer32", "Prims._assert", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.serialize", "LowParse.Spec.Base.parse", "Prims.unit", "LowParse.SLow.Base.bytes32", "Prims.l_or", "LowParse.SLow.Base.serializer32_correct", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2))
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2))
[]
LowParse.SLow.Base.serializer32_injective
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> s32: LowParse.SLow.Base.serializer32 s -> input1: t -> input2: t -> FStar.Pervasives.Lemma (requires s32 input1 == s32 input2) (ensures input1 == input2)
{ "end_col": 71, "end_line": 263, "start_col": 2, "start_line": 263 }
Prims.Tot
val size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit{size32_constant_precond s len32}) : Tot (size32 s)
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit { size32_constant_precond s len32 } ) : Tot (size32 s) = fun x -> [@inline_let] let (z: U32.t { size32_postcond s x z } ) = len32 in z
val size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit{size32_constant_precond s len32}) : Tot (size32 s) let size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit{size32_constant_precond s len32}) : Tot (size32 s) =
false
null
false
fun x -> [@@ inline_let ]let z:z: U32.t{size32_postcond s x z} = len32 in z
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "FStar.UInt32.t", "Prims.unit", "LowParse.SLow.Base.size32_constant_precond", "LowParse.SLow.Base.size32_postcond", "LowParse.SLow.Base.size32" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p inline_for_extraction let u32_max : (y: U32.t { forall (x: U32.t) . {:pattern (U32.v x)} U32.v x <= U32.v y } ) = 4294967295ul inline_for_extraction let add_overflow (x y: U32.t) : Pure U32.t (requires True) (ensures (fun z -> if U32.v x + U32.v y > U32.v u32_max then z == u32_max else U32.v z == U32.v x + U32.v y )) = if U32.lt (U32.sub u32_max y) x then u32_max else U32.add x y let size32_postcond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) (y: U32.t) : GTot Type0 = let sz = Seq.length (serialize s x) in if sz > U32.v u32_max then y == u32_max else U32.v y == sz [@unifier_hint_injective] inline_for_extraction let size32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (x: t) -> Tot (y: U32.t { size32_postcond s x y }) let size32_constant_precond (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) : GTot Type0 = k.parser_kind_high == Some k.parser_kind_low /\ U32.v len32 == k.parser_kind_low inline_for_extraction let size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit { size32_constant_precond s len32 } )
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val size32_constant (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (len32: U32.t) (u: unit{size32_constant_precond s len32}) : Tot (size32 s)
[]
LowParse.SLow.Base.size32_constant
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> len32: FStar.UInt32.t -> u147: u148: Prims.unit{LowParse.SLow.Base.size32_constant_precond s len32} -> LowParse.SLow.Base.size32 s
{ "end_col": 3, "end_line": 366, "start_col": 2, "start_line": 363 }
Prims.Tot
val u32_max:(y: U32.t{forall (x: U32.t). {:pattern (U32.v x)} U32.v x <= U32.v y})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let u32_max : (y: U32.t { forall (x: U32.t) . {:pattern (U32.v x)} U32.v x <= U32.v y } ) = 4294967295ul
val u32_max:(y: U32.t{forall (x: U32.t). {:pattern (U32.v x)} U32.v x <= U32.v y}) let u32_max:(y: U32.t{forall (x: U32.t). {:pattern (U32.v x)} U32.v x <= U32.v y}) =
false
null
false
4294967295ul
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "FStar.UInt32.__uint_to_t" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32 let validator_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option U32.t) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (consumed) -> Some? gp /\ ( let (Some (_ , consumed')) = gp in U32.v consumed == (consumed' <: nat) ) let validator (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option U32.t { validator_correct p input res } ) let serializer32_correct (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res == s input let serializer32_correct' (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : GTot Type0 = B32.reveal res `bytes_equal` s input [@unifier_hint_injective] inline_for_extraction let serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_correct_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (input: t) (res: bytes32) : Lemma (requires (serializer32_correct s input res)) (ensures ( let len = FStar.Bytes.length res in k.parser_kind_low <= len /\ ( match k.parser_kind_high with | Some max -> len <= max | _ -> True ))) [SMTPat (serializer32_correct s input res); SMTPat (FStar.Bytes.length res)] = serialize_length s input inline_for_extraction let serialize32_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (s1': serializer32 s1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) (u: squash (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) : Tot (serializer32 (serialize_ext p1 s1 p2)) = fun input -> s1' input inline_for_extraction let partial_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) : Tot Type = (input: t { Seq.length (s input) < 4294967296 } ) -> Tot (res: bytes32 { serializer32_correct s input res } ) let serializer32_then_parser32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: t) : Lemma (p32 (s32 input) == Some (input, B32.len (s32 input))) = () let parser32_then_serializer32 (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) : Lemma (requires (Some? (p32 input))) (ensures ( let (Some (v, consumed)) = p32 input in U32.v consumed <= B32.length input /\ s32 v == B32.b32slice input 0ul consumed )) = serializer_correct_implies_complete p s let parser32_then_serializer32' (#k: parser_kind) (#t: Type) (#p: parser k t) (#s: serializer p) (p32: parser32 p) (s32: serializer32 s) (input: bytes32) (v: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (v, consumed))) (ensures ( B32.length (s32 v) == U32.v consumed /\ U32.v consumed <= B32.length input /\ B32.reveal (s32 v) == Seq.slice (B32.reveal input) 0 (U32.v consumed) )) = parser32_then_serializer32 s p32 s32 input let parser32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input1 input2: bytes32) : Lemma (requires ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, _)) = p1 in let (Some (v2, _)) = p2 in v1 == v2 ))) (ensures ( let p1 = p32 input1 in let p2 = p32 input2 in Some? p1 /\ Some? p2 /\ ( let (Some (v1, consumed1)) = p1 in let (Some (v2, consumed2)) = p2 in v1 == v2 /\ consumed1 == consumed2 /\ U32.v consumed1 <= B32.length input1 /\ U32.v consumed2 <= B32.length input2 /\ B32.b32slice input1 0ul consumed1 == B32.b32slice input2 0ul consumed2 ))) = parser_kind_prop_equiv k p; assert (injective_precond p (B32.reveal input1) (B32.reveal input2)); assert (injective_postcond p (B32.reveal input1) (B32.reveal input2)) let serializer32_injective (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (s32: serializer32 s) (input1 input2: t) : Lemma (requires (s32 input1 == s32 input2)) (ensures (input1 == input2)) = assert (parse p (serialize s input1) == parse p (serialize s input2)) let parse32_size (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) (data: t) (consumed: U32.t) : Lemma (requires (p32 input == Some (data, consumed))) (ensures ( k.parser_kind_low <= U32.v consumed /\ ( Some? k.parser_kind_high ==> ( let (Some hi) = k.parser_kind_high in U32.v consumed <= hi )))) = parser_kind_prop_equiv k p let parse32_total (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (requires ( k.parser_kind_high == Some k.parser_kind_low /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low <= B32.length input )) (ensures ( Some? (p32 input) )) = parser_kind_prop_equiv k p inline_for_extraction
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val u32_max:(y: U32.t{forall (x: U32.t). {:pattern (U32.v x)} U32.v x <= U32.v y})
[]
LowParse.SLow.Base.u32_max
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
y: FStar.UInt32.t {forall (x: FStar.UInt32.t). {:pattern FStar.UInt32.v x} FStar.UInt32.v x <= FStar.UInt32.v y}
{ "end_col": 14, "end_line": 302, "start_col": 2, "start_line": 302 }
Prims.Tot
val coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit{t2 == t1}) : Tot (parser32 (coerce_parser t2 p))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "LowParse.Bytes32", "short_module": "B32" }, { "abbrev": false, "full_module": "LowParse.Spec.Base", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "LowParse.SLow", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
false
let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } ) : Tot (parser32 (coerce_parser t2 p)) = p32
val coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit{t2 == t1}) : Tot (parser32 (coerce_parser t2 p)) let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit{t2 == t1}) : Tot (parser32 (coerce_parser t2 p)) =
false
null
false
p32
{ "checked_file": "LowParse.SLow.Base.fst.checked", "dependencies": [ "prims.fst.checked", "LowParse.Spec.Base.fsti.checked", "LowParse.Bytes32.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Bytes.fsti.checked" ], "interface_file": false, "source_file": "LowParse.SLow.Base.fst" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.SLow.Base.parser32", "Prims.unit", "Prims.eq2", "LowParse.Spec.Base.coerce_parser" ]
[]
module LowParse.SLow.Base include LowParse.Spec.Base module B32 = LowParse.Bytes32 module U32 = FStar.UInt32 let bytes32 = B32.bytes let parser32_correct (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes32) (res: option (t * U32.t)) : GTot Type0 = let gp = parse p (B32.reveal input) in match res with | None -> gp == None | Some (hres, consumed) -> Some? gp /\ ( let (Some (hres' , consumed')) = gp in hres == hres' /\ U32.v consumed == (consumed' <: nat) ) [@unifier_hint_injective] inline_for_extraction let parser32 (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (input: bytes32) -> Tot (res: option (t * U32.t) { parser32_correct p input res } ) let parser32_consumes (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (Some? (p32 input) ==> (let (Some (_, consumed)) = p32 input in U32.v consumed <= B32.length input)) = () let parser32_consumes' (#k: parser_kind) (#t: Type) (#p: parser k t) (p32: parser32 p) (input: bytes32) : Lemma (match p32 input with | Some (_, consumed) -> U32.v consumed <= B32.length input | _ -> True) = () inline_for_extraction let make_parser32 (#k: parser_kind) (#t: Type) (p: parser k t) (p32: (input: bytes32) -> Pure (option (t * U32.t)) (requires True) (ensures (fun res -> parser32_correct p input res))) : Tot (parser32 p) = (fun (input: bytes32) -> (p32 input <: (res: option (t * U32.t) { parser32_correct p input res } ))) inline_for_extraction let coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit { t2 == t1 } )
false
false
LowParse.SLow.Base.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val coerce_parser32 (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (p32: parser32 p) (u: unit{t2 == t1}) : Tot (parser32 (coerce_parser t2 p))
[]
LowParse.SLow.Base.coerce_parser32
{ "file_name": "src/lowparse/LowParse.SLow.Base.fst", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t2: Type -> p32: LowParse.SLow.Base.parser32 p -> u33: u34: Prims.unit{t2 == t1} -> LowParse.SLow.Base.parser32 (LowParse.Spec.Base.coerce_parser t2 p)
{ "end_col": 5, "end_line": 75, "start_col": 2, "start_line": 75 }