file_name
stringlengths
5
52
name
stringlengths
4
95
original_source_type
stringlengths
0
23k
source_type
stringlengths
9
23k
source_definition
stringlengths
9
57.9k
source
dict
source_range
dict
file_context
stringlengths
0
721k
dependencies
dict
opens_and_abbrevs
listlengths
2
94
vconfig
dict
interleaved
bool
1 class
verbose_type
stringlengths
1
7.42k
effect
stringclasses
118 values
effect_flags
sequencelengths
0
2
mutual_with
sequencelengths
0
11
ideal_premises
sequencelengths
0
236
proof_features
sequencelengths
0
1
is_simple_lemma
bool
2 classes
is_div
bool
2 classes
is_proof
bool
2 classes
is_simply_typed
bool
2 classes
is_type
bool
2 classes
partial_definition
stringlengths
5
3.99k
completed_definiton
stringlengths
1
1.63M
isa_cross_project_example
bool
1 class
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.base_nonce_of_ctx
val base_nonce_of_ctx : cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> Spec.Agile.HPKE.nonce_aead_s cs
let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 45, "end_line": 391, "start_col": 0, "start_line": 390 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> Spec.Agile.HPKE.nonce_aead_s cs
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_aead_s", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.seq_aead_s", "Spec.Agile.HPKE.exporter_secret_s" ]
[]
false
false
false
false
false
let base_nonce_of_ctx (cs: ciphersuite) (ctx: encryption_context cs) =
let _, base_nonce, _, _ = ctx in base_nonce
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.verify_psk_inputs
val verify_psk_inputs (cs: ciphersuite) (m: mode) (opsk: option (psk_s cs & psk_id_s cs)) : bool
val verify_psk_inputs (cs: ciphersuite) (m: mode) (opsk: option (psk_s cs & psk_id_s cs)) : bool
let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 17, "end_line": 328, "start_col": 0, "start_line": 322 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> m: Spec.Agile.HPKE.mode -> opsk: FStar.Pervasives.Native.option (Spec.Agile.HPKE.psk_s cs * Spec.Agile.HPKE.psk_id_s cs) -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.mode", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "FStar.Pervasives.Native.Mktuple2", "Prims.bool" ]
[]
false
false
false
false
false
let verify_psk_inputs (cs: ciphersuite) (m: mode) (opsk: option (psk_s cs & psk_id_s cs)) : bool =
match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.suite_id_hpke
val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke)
val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke)
let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 62, "end_line": 53, "start_col": 0, "start_line": 52 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> Lib.ByteSequence.lbytes Spec.Agile.HPKE.size_suite_id_hpke
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "FStar.Seq.Base.append", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Agile.HPKE.label_HPKE", "Lib.Sequence.op_At_Bar", "Lib.IntTypes.uint_t", "Spec.Agile.HPKE.id_kem", "Spec.Agile.HPKE.id_kdf", "Spec.Agile.HPKE.id_aead", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.size_suite_id_hpke" ]
[]
false
false
false
true
false
let suite_id_hpke cs =
Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.seq_of_ctx
val seq_of_ctx : cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> Spec.Agile.HPKE.seq_aead_s cs
let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 31, "end_line": 394, "start_col": 0, "start_line": 393 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> Spec.Agile.HPKE.seq_aead_s cs
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_aead_s", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.seq_aead_s", "Spec.Agile.HPKE.exporter_secret_s" ]
[]
false
false
false
false
false
let seq_of_ctx (cs: ciphersuite) (ctx: encryption_context cs) =
let _, _, seq, _ = ctx in seq
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.set_seq
val set_seq : cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> seq: Spec.Agile.HPKE.seq_aead_s cs -> ((Spec.Agile.HPKE.key_aead_s cs * Spec.Agile.HPKE.nonce_aead_s cs) * Spec.Agile.HPKE.seq_aead_s cs) * Spec.Agile.HPKE.exporter_secret_s cs
let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 33, "end_line": 401, "start_col": 0, "start_line": 399 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> seq: Spec.Agile.HPKE.seq_aead_s cs -> ((Spec.Agile.HPKE.key_aead_s cs * Spec.Agile.HPKE.nonce_aead_s cs) * Spec.Agile.HPKE.seq_aead_s cs) * Spec.Agile.HPKE.exporter_secret_s cs
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.seq_aead_s", "Spec.Agile.HPKE.key_aead_s", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.exporter_secret_s", "FStar.Pervasives.Native.Mktuple4", "FStar.Pervasives.Native.tuple4" ]
[]
false
false
false
false
false
let set_seq (cs: ciphersuite) (ctx: encryption_context cs) (seq: seq_aead_s cs) =
let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.id_aead
val id_aead: cs:ciphersuite -> Tot (lbytes 2)
val id_aead: cs:ciphersuite -> Tot (lbytes 2)
let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 56, "end_line": 45, "start_col": 0, "start_line": 40 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> Lib.ByteSequence.lbytes 2
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.DH.algorithm", "Spec.Agile.HPKE.hash_algorithm", "Spec.Agile.HPKE.aead", "Spec.Hash.Definitions.hash_alg", "Lib.Sequence.op_At_Bar", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Lib.Sequence.create", "Lib.IntTypes.u8", "Lib.ByteSequence.lbytes" ]
[]
false
false
false
false
false
let id_aead cs =
let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.id_kem
val id_kem: cs:ciphersuite -> Tot (lbytes 2)
val id_kem: cs:ciphersuite -> Tot (lbytes 2)
let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 74, "end_line": 30, "start_col": 0, "start_line": 27 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> Lib.ByteSequence.lbytes 2
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.DH.algorithm", "Spec.Agile.HPKE.hash_algorithm", "Spec.Agile.HPKE.aead", "Spec.Hash.Definitions.hash_alg", "FStar.Pervasives.Native.Mktuple2", "Lib.Sequence.op_At_Bar", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Lib.Sequence.create", "Lib.IntTypes.u8", "Lib.ByteSequence.lbytes" ]
[]
false
false
false
false
false
let id_kem cs =
let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.labeled_expand
val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True)
val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True)
let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 106, "start_col": 0, "start_line": 100 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Spec.Agile.HPKE.hash_algorithm -> suite_id: Lib.ByteSequence.bytes -> prk: Lib.ByteSequence.bytes -> label: Lib.ByteSequence.bytes -> info: Lib.ByteSequence.bytes -> l: Lib.IntTypes.size_nat -> Prims.Pure (Lib.ByteSequence.lbytes l)
Prims.Pure
[]
[]
[ "Spec.Agile.HPKE.hash_algorithm", "Lib.ByteSequence.bytes", "Lib.IntTypes.size_nat", "Spec.Agile.HKDF.expand", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Seq.Base.append", "Lib.IntTypes.uint_t", "Spec.Agile.HPKE.label_version", "Lib.Sequence.seq", "Prims.l_and", "Prims.eq2", "Prims.nat", "Lib.Sequence.length", "Prims.l_or", "Prims.b2t", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.ByteSequence.nat_from_intseq_be", "Lib.ByteSequence.nat_to_bytes_be", "Lib.ByteSequence.lbytes" ]
[]
false
false
false
false
false
let labeled_expand a suite_id prk label info l =
let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.labeled_extract
val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True)
val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True)
let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 82, "start_col": 0, "start_line": 78 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Spec.Agile.HPKE.hash_algorithm -> suite_id: Lib.ByteSequence.bytes -> salt: Lib.ByteSequence.bytes -> label: Lib.ByteSequence.bytes -> ikm: Lib.ByteSequence.bytes -> Prims.Pure (Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length a))
Prims.Pure
[]
[]
[ "Spec.Agile.HPKE.hash_algorithm", "Lib.ByteSequence.bytes", "Spec.Agile.HKDF.extract", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Seq.Base.append", "Spec.Agile.HPKE.label_version", "Lib.ByteSequence.lbytes", "Spec.Hash.Definitions.hash_length" ]
[]
false
false
false
false
false
let labeled_extract a suite_id salt label ikm =
let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.serialize_public_key
val serialize_public_key: cs:ciphersuite -> pk:DH.serialized_point (kem_dh_of_cs cs) -> Tot (key_dh_public_s cs)
val serialize_public_key: cs:ciphersuite -> pk:DH.serialized_point (kem_dh_of_cs cs) -> Tot (key_dh_public_s cs)
let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 39, "end_line": 137, "start_col": 0, "start_line": 134 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> pk: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> Spec.Agile.HPKE.key_dh_public_s cs
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Lib.Sequence.op_At_Bar", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Agile.DH.size_public", "Lib.Sequence.create", "Lib.IntTypes.u8", "Spec.Agile.HPKE.key_dh_public_s" ]
[]
false
false
false
false
false
let serialize_public_key cs pk =
match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk | DH.DH_P256 -> create 1 (u8 4) @| pk
false
Vale.Stdcalls.X64.Fadd.fst
Vale.Stdcalls.X64.Fadd.lowstar_fadd
val lowstar_fadd:lowstar_fadd_t
val lowstar_fadd:lowstar_fadd_t
let lowstar_fadd : lowstar_fadd_t = assert_norm (List.length fadd_dom + List.length ([]<:list arg) <= 4); IX64.wrap_weak_stdcall code_Fadd fadd_dom (W.mk_prediction code_Fadd fadd_dom [] (fadd_lemma code_Fadd IA.win))
{ "file_name": "vale/code/arch/x64/interop/Vale.Stdcalls.X64.Fadd.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 73, "end_line": 47, "start_col": 0, "start_line": 42 }
module Vale.Stdcalls.X64.Fadd open FStar.Mul #reset-options "--z3rlimit 50" let z3rlimit_hack x = () open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module DV = LowStar.BufferView.Down module UV = LowStar.BufferView.Up open Vale.Def.Types_s open Vale.Interop.Base module IX64 = Vale.Interop.X64 module VSig = Vale.AsLowStar.ValeSig module LSig = Vale.AsLowStar.LowStarSig module ME = Vale.X64.Memory module V = Vale.X64.Decls module IA = Vale.Interop.Assumptions module W = Vale.AsLowStar.Wrapper open Vale.X64.MemoryAdapters module VS = Vale.X64.State module MS = Vale.X64.Machine_s module FU = Vale.Curve25519.X64.FastUtil module FH = Vale.Curve25519.X64.FastHybrid module FW = Vale.Curve25519.X64.FastWide (* And here's the fadd wrapper itself *) let lowstar_add1 : lowstar_add1_t = assert_norm (List.length dom + List.length ([]<:list arg) <= 4); IX64.wrap_weak_stdcall code_add1 dom (W.mk_prediction code_add1 dom [] (add1_lemma code_add1 IA.win)) let add_scalar_e //: normal lowstar_add1_t = as_normal_t #lowstar_add1_t lowstar_add1
{ "checked_file": "/", "dependencies": [ "Vale.X64.State.fsti.checked", "Vale.X64.MemoryAdapters.fsti.checked", "Vale.X64.Memory.fsti.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Decls.fsti.checked", "Vale.Interop.X64.fsti.checked", "Vale.Interop.Base.fst.checked", "Vale.Interop.Assumptions.fst.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.X64.FastWide.fsti.checked", "Vale.Curve25519.X64.FastUtil.fsti.checked", "Vale.Curve25519.X64.FastHybrid.fsti.checked", "Vale.AsLowStar.Wrapper.fsti.checked", "Vale.AsLowStar.ValeSig.fst.checked", "Vale.AsLowStar.LowStarSig.fst.checked", "prims.fst.checked", "LowStar.BufferView.Up.fsti.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Vale.Stdcalls.X64.Fadd.fst" }
[ { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastWide", "short_module": "FW" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastHybrid", "short_module": "FH" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastUtil", "short_module": "FU" }, { "abbrev": true, "full_module": "Vale.X64.Machine_s", "short_module": "MS" }, { "abbrev": true, "full_module": "Vale.X64.State", "short_module": "VS" }, { "abbrev": false, "full_module": "Vale.X64.MemoryAdapters", "short_module": null }, { "abbrev": true, "full_module": "Vale.AsLowStar.Wrapper", "short_module": "W" }, { "abbrev": true, "full_module": "Vale.Interop.Assumptions", "short_module": "IA" }, { "abbrev": true, "full_module": "Vale.X64.Decls", "short_module": "V" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "ME" }, { "abbrev": true, "full_module": "Vale.AsLowStar.LowStarSig", "short_module": "LSig" }, { "abbrev": true, "full_module": "Vale.AsLowStar.ValeSig", "short_module": "VSig" }, { "abbrev": true, "full_module": "Vale.Interop.X64", "short_module": "IX64" }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": true, "full_module": "LowStar.BufferView.Up", "short_module": "UV" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastWide", "short_module": "FW" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastHybrid", "short_module": "FH" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastUtil", "short_module": "FU" }, { "abbrev": true, "full_module": "Vale.X64.Machine_s", "short_module": "MS" }, { "abbrev": true, "full_module": "Vale.X64.State", "short_module": "VS" }, { "abbrev": false, "full_module": "Vale.X64.MemoryAdapters", "short_module": null }, { "abbrev": true, "full_module": "Vale.AsLowStar.Wrapper", "short_module": "W" }, { "abbrev": true, "full_module": "Vale.Interop.Assumptions", "short_module": "IA" }, { "abbrev": true, "full_module": "Vale.X64.Decls", "short_module": "V" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "ME" }, { "abbrev": true, "full_module": "Vale.AsLowStar.LowStarSig", "short_module": "LSig" }, { "abbrev": true, "full_module": "Vale.AsLowStar.ValeSig", "short_module": "VSig" }, { "abbrev": true, "full_module": "Vale.Interop.X64", "short_module": "IX64" }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": true, "full_module": "LowStar.BufferView.Up", "short_module": "UV" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Stdcalls.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.Stdcalls.X64", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Vale.Stdcalls.X64.Fadd.lowstar_fadd_t
Prims.Tot
[ "total" ]
[]
[ "Vale.Interop.X64.wrap_weak_stdcall", "Vale.Stdcalls.X64.Fadd.code_Fadd", "Vale.Stdcalls.X64.Fadd.fadd_dom", "Vale.AsLowStar.Wrapper.pre_rel_generic", "Vale.Interop.X64.max_stdcall", "Vale.Interop.X64.arg_reg_stdcall", "Prims.Nil", "Vale.Interop.Base.arg", "Vale.Stdcalls.X64.Fadd.fadd_pre", "Vale.AsLowStar.Wrapper.post_rel_generic", "Vale.Stdcalls.X64.Fadd.fadd_post", "Vale.AsLowStar.Wrapper.mk_prediction", "Vale.Interop.X64.regs_modified_stdcall", "Vale.Interop.X64.xmms_modified_stdcall", "Vale.Stdcalls.X64.Fadd.fadd_lemma", "Vale.Interop.Assumptions.win", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.List.Tot.Base.length", "Vale.Interop.Base.td", "Prims.list" ]
[]
false
false
false
true
false
let lowstar_fadd:lowstar_fadd_t =
assert_norm (List.length fadd_dom + List.length ([] <: list arg) <= 4); IX64.wrap_weak_stdcall code_Fadd fadd_dom (W.mk_prediction code_Fadd fadd_dom [] (fadd_lemma code_Fadd IA.win))
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.id_kdf
val id_kdf: cs:ciphersuite -> Tot (lbytes 2)
val id_kdf: cs:ciphersuite -> Tot (lbytes 2)
let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 55, "end_line": 37, "start_col": 0, "start_line": 33 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> Lib.ByteSequence.lbytes 2
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.DH.algorithm", "Spec.Agile.HPKE.hash_algorithm", "Spec.Agile.HPKE.aead", "Spec.Hash.Definitions.hash_alg", "Lib.Sequence.op_At_Bar", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Lib.Sequence.create", "Lib.IntTypes.u8", "Lib.ByteSequence.lbytes" ]
[]
false
false
false
false
false
let id_kdf cs =
let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3)
false
Vale.Stdcalls.X64.Fadd.fst
Vale.Stdcalls.X64.Fadd.lowstar_add1
val lowstar_add1:lowstar_add1_t
val lowstar_add1:lowstar_add1_t
let lowstar_add1 : lowstar_add1_t = assert_norm (List.length dom + List.length ([]<:list arg) <= 4); IX64.wrap_weak_stdcall code_add1 dom (W.mk_prediction code_add1 dom [] (add1_lemma code_add1 IA.win))
{ "file_name": "vale/code/arch/x64/interop/Vale.Stdcalls.X64.Fadd.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 68, "end_line": 36, "start_col": 0, "start_line": 31 }
module Vale.Stdcalls.X64.Fadd open FStar.Mul #reset-options "--z3rlimit 50" let z3rlimit_hack x = () open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module DV = LowStar.BufferView.Down module UV = LowStar.BufferView.Up open Vale.Def.Types_s open Vale.Interop.Base module IX64 = Vale.Interop.X64 module VSig = Vale.AsLowStar.ValeSig module LSig = Vale.AsLowStar.LowStarSig module ME = Vale.X64.Memory module V = Vale.X64.Decls module IA = Vale.Interop.Assumptions module W = Vale.AsLowStar.Wrapper open Vale.X64.MemoryAdapters module VS = Vale.X64.State module MS = Vale.X64.Machine_s module FU = Vale.Curve25519.X64.FastUtil module FH = Vale.Curve25519.X64.FastHybrid module FW = Vale.Curve25519.X64.FastWide
{ "checked_file": "/", "dependencies": [ "Vale.X64.State.fsti.checked", "Vale.X64.MemoryAdapters.fsti.checked", "Vale.X64.Memory.fsti.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Decls.fsti.checked", "Vale.Interop.X64.fsti.checked", "Vale.Interop.Base.fst.checked", "Vale.Interop.Assumptions.fst.checked", "Vale.Def.Types_s.fst.checked", "Vale.Curve25519.X64.FastWide.fsti.checked", "Vale.Curve25519.X64.FastUtil.fsti.checked", "Vale.Curve25519.X64.FastHybrid.fsti.checked", "Vale.AsLowStar.Wrapper.fsti.checked", "Vale.AsLowStar.ValeSig.fst.checked", "Vale.AsLowStar.LowStarSig.fst.checked", "prims.fst.checked", "LowStar.BufferView.Up.fsti.checked", "LowStar.BufferView.Down.fsti.checked", "LowStar.Buffer.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Vale.Stdcalls.X64.Fadd.fst" }
[ { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastWide", "short_module": "FW" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastHybrid", "short_module": "FH" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastUtil", "short_module": "FU" }, { "abbrev": true, "full_module": "Vale.X64.Machine_s", "short_module": "MS" }, { "abbrev": true, "full_module": "Vale.X64.State", "short_module": "VS" }, { "abbrev": false, "full_module": "Vale.X64.MemoryAdapters", "short_module": null }, { "abbrev": true, "full_module": "Vale.AsLowStar.Wrapper", "short_module": "W" }, { "abbrev": true, "full_module": "Vale.Interop.Assumptions", "short_module": "IA" }, { "abbrev": true, "full_module": "Vale.X64.Decls", "short_module": "V" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "ME" }, { "abbrev": true, "full_module": "Vale.AsLowStar.LowStarSig", "short_module": "LSig" }, { "abbrev": true, "full_module": "Vale.AsLowStar.ValeSig", "short_module": "VSig" }, { "abbrev": true, "full_module": "Vale.Interop.X64", "short_module": "IX64" }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": true, "full_module": "LowStar.BufferView.Up", "short_module": "UV" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastWide", "short_module": "FW" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastHybrid", "short_module": "FH" }, { "abbrev": true, "full_module": "Vale.Curve25519.X64.FastUtil", "short_module": "FU" }, { "abbrev": true, "full_module": "Vale.X64.Machine_s", "short_module": "MS" }, { "abbrev": true, "full_module": "Vale.X64.State", "short_module": "VS" }, { "abbrev": false, "full_module": "Vale.X64.MemoryAdapters", "short_module": null }, { "abbrev": true, "full_module": "Vale.AsLowStar.Wrapper", "short_module": "W" }, { "abbrev": true, "full_module": "Vale.Interop.Assumptions", "short_module": "IA" }, { "abbrev": true, "full_module": "Vale.X64.Decls", "short_module": "V" }, { "abbrev": true, "full_module": "Vale.X64.Memory", "short_module": "ME" }, { "abbrev": true, "full_module": "Vale.AsLowStar.LowStarSig", "short_module": "LSig" }, { "abbrev": true, "full_module": "Vale.AsLowStar.ValeSig", "short_module": "VSig" }, { "abbrev": true, "full_module": "Vale.Interop.X64", "short_module": "IX64" }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": true, "full_module": "LowStar.BufferView.Up", "short_module": "UV" }, { "abbrev": true, "full_module": "LowStar.BufferView.Down", "short_module": "DV" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Stdcalls.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.Stdcalls.X64", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Vale.Stdcalls.X64.Fadd.lowstar_add1_t
Prims.Tot
[ "total" ]
[]
[ "Vale.Interop.X64.wrap_weak_stdcall", "Vale.Stdcalls.X64.Fadd.code_add1", "Vale.Stdcalls.X64.Fadd.dom", "Vale.AsLowStar.Wrapper.pre_rel_generic", "Vale.Interop.X64.max_stdcall", "Vale.Interop.X64.arg_reg_stdcall", "Prims.Nil", "Vale.Interop.Base.arg", "Vale.Stdcalls.X64.Fadd.add1_pre", "Vale.AsLowStar.Wrapper.post_rel_generic", "Vale.Stdcalls.X64.Fadd.add1_post", "Vale.AsLowStar.Wrapper.mk_prediction", "Vale.Interop.X64.regs_modified_stdcall", "Vale.Interop.X64.xmms_modified_stdcall", "Vale.Stdcalls.X64.Fadd.add1_lemma", "Vale.Interop.Assumptions.win", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.List.Tot.Base.length", "Vale.Interop.Base.td", "Prims.list" ]
[]
false
false
false
true
false
let lowstar_add1:lowstar_add1_t =
assert_norm (List.length dom + List.length ([] <: list arg) <= 4); IX64.wrap_weak_stdcall code_add1 dom (W.mk_prediction code_add1 dom [] (add1_lemma code_add1 IA.win))
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.derive_key_pair
val derive_key_pair: cs:ciphersuite -> ikm:dkp_ikm_s cs -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs))
val derive_key_pair: cs:ciphersuite -> ikm:dkp_ikm_s cs -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs))
let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 32, "end_line": 168, "start_col": 0, "start_line": 158 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> ikm: Spec.Agile.HPKE.dkp_ikm_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_secret_s cs * Spec.Agile.HPKE.key_dh_public_s cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.dkp_ikm_s", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.DH.secret_to_public", "Spec.Agile.DH.serialized_point", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.key_dh_public_s", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.serialize_public_key", "FStar.Pervasives.Native.option", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Agile.HPKE.size_dh_key", "Spec.Agile.HPKE.labeled_expand", "Spec.Agile.HPKE.kem_hash_of_cs", "Spec.Agile.HPKE.suite_id_kem", "Spec.Agile.HPKE.label_sk", "Lib.ByteSequence.lbytes_empty", "Spec.Hash.Definitions.hash_length", "Spec.Agile.HPKE.labeled_extract", "Spec.Agile.HPKE.label_dkp_prk", "Spec.Agile.HPKE.dkp_nist_p", "Lib.IntTypes.u8" ]
[]
false
false
false
false
false
let derive_key_pair cs ikm =
match kem_dh_of_cs cs with | DH.DH_Curve25519 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in (match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk)) | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.extract_and_expand
val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True)
val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True)
let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 112, "end_line": 126, "start_col": 0, "start_line": 124 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> dh: Lib.ByteSequence.bytes -> kem_context: Lib.ByteSequence.bytes -> Prims.Pure (Spec.Agile.HPKE.key_kem_s cs)
Prims.Pure
[]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Lib.ByteSequence.bytes", "Spec.Agile.HPKE.labeled_expand", "Spec.Agile.HPKE.kem_hash_of_cs", "Spec.Agile.HPKE.suite_id_kem", "Spec.Agile.HPKE.label_shared_secret", "Spec.Agile.HPKE.size_kem_key", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Hash.Definitions.hash_length", "Spec.Agile.HPKE.labeled_extract", "Lib.ByteSequence.lbytes_empty", "Spec.Agile.HPKE.label_eae_prk", "Spec.Agile.HPKE.key_kem_s" ]
[]
false
false
false
false
false
let extract_and_expand cs dh kem_context =
let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.build_context
val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs))
val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs))
let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 9, "end_line": 320, "start_col": 0, "start_line": 316 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> m: Spec.Agile.HPKE.mode -> psk_id_hash: Lib.ByteSequence.lbytes (Spec.Agile.HPKE.size_kdf cs) -> info_hash: Lib.ByteSequence.lbytes (Spec.Agile.HPKE.size_kdf cs) -> Lib.ByteSequence.lbytes (Spec.Agile.HPKE.size_ks_ctx cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.mode", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.size_kdf", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Seq.Base.append", "Lib.IntTypes.uint_t", "Lib.Sequence.lseq", "Spec.Agile.HPKE.size_mode_identifier", "Spec.Agile.HPKE.id_of_mode", "Spec.Agile.HPKE.size_ks_ctx" ]
[]
false
false
false
false
false
let build_context cs m psk_id_hash info_hash =
let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.context_increment_seq
val context_increment_seq: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> Tot (option (encryption_context cs))
val context_increment_seq: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> Tot (option (encryption_context cs))
let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1))
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 33, "end_line": 421, "start_col": 0, "start_line": 418 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite_not_export_only -> ctx: Spec.Agile.HPKE.encryption_context cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite_not_export_only", "Spec.Agile.HPKE.encryption_context", "Prims.op_Equality", "Prims.nat", "Spec.Agile.HPKE.max_seq", "FStar.Pervasives.Native.None", "Prims.bool", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.set_seq", "Prims.op_Addition", "FStar.Pervasives.Native.option", "Spec.Agile.HPKE.seq_aead_s", "Spec.Agile.HPKE.seq_of_ctx" ]
[]
false
false
false
false
false
let context_increment_seq cs ctx =
let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1))
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.dkp_nist_p
val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter)
val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter)
let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1))
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 52, "end_line": 154, "start_col": 0, "start_line": 141 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> dkp_prk: Lib.ByteSequence.lbytes (Spec.Agile.HPKE.size_kem_kdf cs) -> counter: Lib.IntTypes.uint8 -> Prims.Tot (FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_secret_s cs * Spec.Agile.HPKE.key_dh_public_s cs))
Prims.Tot
[ "total", "" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.size_kem_kdf", "Lib.IntTypes.uint8", "Prims.op_BarBar", "Prims.op_Equality", "Prims.int", "Prims.op_GreaterThanOrEqual", "Spec.P256.PointOps.prime", "Lib.IntTypes.v", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.key_dh_public_s", "Prims.bool", "Spec.Agile.HPKE.dkp_nist_p", "Lib.IntTypes.op_Plus_Bang", "Lib.IntTypes.u8", "FStar.Pervasives.Native.option", "Spec.Agile.DH.secret_to_public", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.DH.serialized_point", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.serialize_public_key", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.Sequence.length", "Lib.IntTypes.int_t", "Lib.ByteSequence.nat_from_intseq_be", "Lib.Sequence.lseq", "Spec.Agile.HPKE.size_dh_key", "Prims.l_Forall", "Prims.l_imp", "Prims.eq2", "Lib.Sequence.index", "Lib.IntTypes.logand", "FStar.Seq.Base.create", "Lib.IntTypes.mk_int", "Lib.Sequence.map2", "Lib.IntTypes.uint_t", "Spec.Agile.HPKE.labeled_expand", "Spec.Agile.HPKE.kem_hash_of_cs", "Spec.Agile.HPKE.suite_id_kem", "Spec.Agile.HPKE.label_candidate", "Lib.Sequence.seq", "Prims.l_and", "Prims.l_or", "Lib.ByteSequence.nat_to_intseq_be" ]
[ "recursion" ]
false
false
false
false
false
let rec dkp_nist_p cs dkp_prk counter =
let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1))
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.context_compute_nonce
val context_compute_nonce: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> seq:seq_aead_s cs -> Tot (nonce_aead_s cs)
val context_compute_nonce: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> seq:seq_aead_s cs -> Tot (nonce_aead_s cs)
let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 416, "start_col": 0, "start_line": 413 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite_not_export_only -> ctx: Spec.Agile.HPKE.encryption_context cs -> seq: Spec.Agile.HPKE.seq_aead_s cs -> Spec.Agile.HPKE.nonce_aead_s cs
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite_not_export_only", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.seq_aead_s", "Spec.Loops.seq_map2", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Lib.IntTypes.logxor", "Lib.Sequence.seq", "Lib.IntTypes.int_t", "Prims.l_and", "Prims.eq2", "Prims.nat", "Lib.Sequence.length", "Spec.Agile.HPKE.size_aead_nonce", "Prims.l_or", "Prims.b2t", "Prims.op_LessThan", "Prims.pow2", "Prims.op_Multiply", "Lib.ByteSequence.nat_from_intseq_be", "Lib.ByteSequence.nat_to_bytes_be", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.base_nonce_of_ctx" ]
[]
false
false
false
false
false
let context_compute_nonce cs ctx seq =
let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.context_export
val context_export: cs:ciphersuite -> ctx:encryption_context cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> Tot (lbytes l)
val context_export: cs:ciphersuite -> ctx:encryption_context cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> Tot (lbytes l)
let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 79, "end_line": 410, "start_col": 0, "start_line": 408 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context ///
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> ctx: Spec.Agile.HPKE.encryption_context cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> Lib.ByteSequence.lbytes l
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.labeled_expand", "Spec.Agile.HPKE.hash_of_cs", "Spec.Agile.HPKE.suite_id_hpke", "Spec.Agile.HPKE.label_sec", "Spec.Agile.HPKE.exporter_secret_s", "Spec.Agile.HPKE.exp_sec_of_ctx", "Lib.ByteSequence.lbytes" ]
[]
false
false
false
false
false
let context_export cs ctx exp_ctx l =
let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l
false
FStar.Vector.Base.fst
FStar.Vector.Base.raw
val raw ([@@@strictly_positive] a:Type u#a) (l:len_t) : Type u#a
val raw ([@@@strictly_positive] a:Type u#a) (l:len_t) : Type u#a
let raw a l = s:S.seq a{S.length s = U32.v l}
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 21, "start_col": 0, "start_line": 21 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> l: FStar.Vector.Base.len_t -> Type
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Seq.Base.seq", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "FStar.Seq.Base.length", "FStar.UInt32.v" ]
[]
false
false
false
true
true
let raw a l =
s: S.seq a {S.length s = U32.v l}
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.sendExportBase
val sendExportBase: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> Tot (option (key_dh_public_s cs & lbytes l))
val sendExportBase: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> Tot (option (key_dh_public_s cs & lbytes l))
let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 482, "start_col": 0, "start_line": 478 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.setupBaseS", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let sendExportBase cs skE pkR info exp_ctx l =
match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
false
FStar.Vector.Base.fst
FStar.Vector.Base.reveal
val reveal: #a:Type -> #l:len_t -> v:raw a l -> GTot (s:S.seq a{S.length s = raw_length v})
val reveal: #a:Type -> #l:len_t -> v:raw a l -> GTot (s:S.seq a{S.length s = raw_length v})
let reveal #a #l v = v
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 25, "start_col": 0, "start_line": 25 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: FStar.Vector.Base.raw a l -> Prims.GTot (s: FStar.Seq.Base.seq a {FStar.Seq.Base.length s = FStar.Vector.Base.raw_length v})
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "FStar.Seq.Base.seq", "Prims.b2t", "Prims.op_Equality", "Prims.nat", "FStar.Seq.Base.length", "FStar.Vector.Base.raw_length" ]
[]
false
false
false
false
false
let reveal #a #l v =
v
false
ScalarUnion.fst
ScalarUnion.zero_u32_ref
val zero_u32_ref (#v: Ghost.erased (scalar_t U32.t)) (p: ref (scalar U32.t)) : ST unit (p `pts_to` v) (fun _ -> p `pts_to` (mk_scalar 0ul)) (requires full (scalar U32.t) v) (ensures fun _ -> True)
val zero_u32_ref (#v: Ghost.erased (scalar_t U32.t)) (p: ref (scalar U32.t)) : ST unit (p `pts_to` v) (fun _ -> p `pts_to` (mk_scalar 0ul)) (requires full (scalar U32.t) v) (ensures fun _ -> True)
let zero_u32_ref (#v: Ghost.erased (scalar_t U32.t)) (p:ref (scalar U32.t)) : ST unit (p `pts_to` v) (fun _ -> p `pts_to` mk_scalar 0ul) (requires full (scalar U32.t) v) (ensures fun _ -> True) = write p 0ul; return ()
{ "file_name": "share/steel/examples/steelc/ScalarUnion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 11, "end_line": 63, "start_col": 0, "start_line": 56 }
module ScalarUnion open Steel.ST.Util open Steel.ST.C.Types module U32 = FStar.UInt32 module U16 = FStar.UInt16 (** Like structs, unions are labelled by tags to enforce nominality. For a more detailed explanation see PointStruct2.fst *) noextract inline_for_extraction [@@ norm_field_attr] let u32_or_u16_fields = field_description_cons "as_u32" (scalar U32.t) ( field_description_cons "as_u16" (scalar U16.t) ( field_description_nil)) (** Define the union. Like with mk_c_struct, Karamel detects this definition at extraction type and emits the corresponding typedef. *) let _ = define_union "ScalarUnion2.u32_or_u16" u32_or_u16_fields (** The type of (union u32_or_u16) values. *) noextract inline_for_extraction let u32_or_u16 = union "ScalarUnion2.u32_or_u16" u32_or_u16_fields noextract inline_for_extraction let u32_or_u16_t = typeof u32_or_u16 #push-options "--fuel 0" #push-options "--print_universes --print_implicits" // --z3rlimit 30" (** Switch a case of the union to the u16 case, by writing x to it. *) val switch_to_u16 (#v: Ghost.erased u32_or_u16_t) (p: ref u32_or_u16) (x: U16.t) : ST unit (p `pts_to` v) (fun _ -> p `pts_to` union_set_field _ _ _ "as_u16" (mk_scalar x)) (requires full u32_or_u16 v) (ensures fun _ -> True) #push-options "--fuel 0 --print_bound_var_types" let switch_to_u16 p x = let p16 = union_switch_field p "as_u16" () in write p16 x; let _ = ununion_field p "as_u16" _ in drop (has_union_field _ _ _); return ()
{ "checked_file": "/", "dependencies": [ "Steel.ST.Util.fsti.checked", "Steel.ST.C.Types.fst.checked", "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "ScalarUnion.fst" }
[ { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "Steel.ST.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Steel.ST.Util", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.ST.C.Types.Base.ref (Steel.ST.C.Types.Scalar.scalar FStar.UInt32.t) -> Steel.ST.Effect.ST Prims.unit
Steel.ST.Effect.ST
[]
[]
[ "FStar.Ghost.erased", "Steel.ST.C.Types.Scalar.scalar_t", "FStar.UInt32.t", "Steel.ST.C.Types.Base.ref", "Steel.ST.C.Types.Scalar.scalar", "Steel.ST.Util.return", "Prims.unit", "FStar.Ghost.hide", "FStar.Set.set", "Steel.Memory.iname", "FStar.Set.empty", "Steel.ST.C.Types.Base.pts_to", "Steel.ST.C.Types.Scalar.mk_scalar", "FStar.UInt32.__uint_to_t", "Steel.Effect.Common.vprop", "Steel.ST.C.Types.Scalar.write", "Steel.ST.C.Types.Base.full", "FStar.Ghost.reveal", "Prims.l_True" ]
[]
false
true
false
false
false
let zero_u32_ref (#v: Ghost.erased (scalar_t U32.t)) (p: ref (scalar U32.t)) : ST unit (p `pts_to` v) (fun _ -> p `pts_to` (mk_scalar 0ul)) (requires full (scalar U32.t) v) (ensures fun _ -> True) =
write p 0ul; return ()
false
FStar.Vector.Base.fst
FStar.Vector.Base.hide
val hide: #a:Type -> s:S.seq a{S.length s < pow2 32} -> GTot (raw a (U32.uint_to_t (S.length s)))
val hide: #a:Type -> s:S.seq a{S.length s < pow2 32} -> GTot (raw a (U32.uint_to_t (S.length s)))
let hide #a s = s
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 17, "end_line": 26, "start_col": 0, "start_line": 26 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.Seq.Base.seq a {FStar.Seq.Base.length s < Prims.pow2 32} -> Prims.GTot (FStar.Vector.Base.raw a (FStar.UInt32.uint_to_t (FStar.Seq.Base.length s)))
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Seq.Base.seq", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.length", "Prims.pow2", "FStar.Vector.Base.raw", "FStar.UInt32.uint_to_t" ]
[]
false
false
false
false
false
let hide #a s =
s
false
FStar.Vector.Base.fst
FStar.Vector.Base.index
val index: #a:Type -> #l:len_t -> v:raw a l -> i:index_t v -> Tot a
val index: #a:Type -> #l:len_t -> v:raw a l -> i:index_t v -> Tot a
let index #a #l v i = Seq.index v (U32.v i)
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 44, "start_col": 0, "start_line": 44 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: FStar.Vector.Base.raw a l -> i: FStar.Vector.Base.index_t v -> a
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "FStar.Vector.Base.index_t", "FStar.Seq.Base.index", "FStar.UInt32.v" ]
[]
false
false
false
false
false
let index #a #l v i =
Seq.index v (U32.v i)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.sendExportPSK
val sendExportPSK: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (key_dh_public_s cs & lbytes l))
val sendExportPSK: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (key_dh_public_s cs & lbytes l))
let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 531, "start_col": 0, "start_line": 527 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.setupPSKS", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let sendExportPSK cs skE pkR info exp_ctx l psk psk_id =
match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupBaseS
val setupBaseS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
val setupBaseS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 23, "end_line": 452, "start_col": 0, "start_line": 447 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode ///
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.encap", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.Base", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let setupBaseS cs skE pkR info =
match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx)
false
FStar.Vector.Base.fst
FStar.Vector.Base.update
val update: #a:Type -> #l:len_t -> v:raw a l -> i:index_t v -> x:a -> Tot (raw a l)
val update: #a:Type -> #l:len_t -> v:raw a l -> i:index_t v -> x:a -> Tot (raw a l)
let update #a #l v i x = Seq.upd v (U32.v i) x
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 48, "start_col": 0, "start_line": 48 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`:
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: FStar.Vector.Base.raw a l -> i: FStar.Vector.Base.index_t v -> x: a -> FStar.Vector.Base.raw a l
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "FStar.Vector.Base.index_t", "FStar.Seq.Base.upd", "FStar.UInt32.v" ]
[]
false
false
false
false
false
let update #a #l v i x =
Seq.upd v (U32.v i) x
false
FStar.Vector.Base.fst
FStar.Vector.Base.init
val init: #a:Type -> l:len_t -> contents: (i:nat { i < U32.v l } -> Tot a) -> Tot (raw a l)
val init: #a:Type -> l:len_t -> contents: (i:nat { i < U32.v l } -> Tot a) -> Tot (raw a l)
let init #a l contents = Seq.init (U32.v l) contents
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 52, "end_line": 41, "start_col": 0, "start_line": 41 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`:
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: FStar.Vector.Base.len_t -> contents: (i: Prims.nat{i < FStar.UInt32.v l} -> a) -> FStar.Vector.Base.raw a l
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.UInt32.v", "FStar.Seq.Base.init", "FStar.Vector.Base.raw" ]
[]
false
false
false
false
false
let init #a l contents =
Seq.init (U32.v l) contents
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.receiveExportPSK
val receiveExportPSK: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (lbytes l))
val receiveExportPSK: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (lbytes l))
let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 537, "start_col": 0, "start_line": 533 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> FStar.Pervasives.Native.option (Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.setupPSKR", "FStar.Pervasives.Native.None", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let receiveExportPSK cs enc skR info exp_ctx l psk psk_id =
match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupBaseR
val setupBaseR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> Tot (option (encryption_context cs))
val setupBaseR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> Tot (option (encryption_context cs))
let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 460, "start_col": 0, "start_line": 454 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.Base", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.decap", "Spec.Agile.DH.secret_to_public" ]
[]
false
false
false
false
false
let setupBaseR cs enc skR info =
let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupAuthR
val setupAuthR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (encryption_context cs))
val setupAuthR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (encryption_context cs))
let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 556, "start_col": 0, "start_line": 550 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> pkS: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.Auth", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.auth_decap", "Spec.Agile.DH.secret_to_public" ]
[]
false
false
false
false
false
let setupAuthR cs enc skR info pkS =
let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupPSKR
val setupPSKR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (encryption_context cs))
val setupPSKR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (encryption_context cs))
let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 508, "start_col": 0, "start_line": 502 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.PSK", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.None", "Spec.Agile.HPKE.decap", "Spec.Agile.DH.secret_to_public" ]
[]
false
false
false
false
false
let setupPSKR cs enc skR info psk psk_id =
let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None
false
FStar.Vector.Base.fst
FStar.Vector.Base.t
val t: a:Type u#a -> Type u#a
val t: a:Type u#a -> Type u#a
let t a = (l:len_t & raw a l)
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 72, "start_col": 0, "start_line": 72 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`: /// a new vector that differs from `v` only at index `i`, where it contains `x`. let update #a #l v i x = Seq.upd v (U32.v i) x /// `append v1 v2`: /// requires proving that the sum of the lengths of v1 and v2 still fit in a u32 let append #a #l1 #l2 v1 v2 = Seq.append v1 v2 /// `sub v i j`: /// the sub-vector of `v` starting from index `i` up to, but not including, `j` let sub #a #l v i j = Seq.slice v (U32.v i) (U32.v j) //////////////////////////////////////////////////////////////////////////////// /// Lemmas about the basic operations, all rather boring /// -- Each is just a lifting specifying the corresponding operation on seq //////////////////////////////////////////////////////////////////////////////// let reveal_init #a l contents = () let reveal_index #a #l v i = () let reveal_update #a #l v i x = () let reveal_append #a #l1 #l2 v1 v2 = () let reveal_sub #a #l v i j = () //////////////////////////////////////////////////////////////////////////////// /// Dynamically sized vectors ////////////////////////////////////////////////////////////////////////////////
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> Type
Prims.Tot
[ "total" ]
[]
[ "Prims.dtuple2", "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw" ]
[]
false
false
false
true
true
let t a =
(l: len_t & raw a l)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.receiveExportBase
val receiveExportBase: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> Tot (option (lbytes l))
val receiveExportBase: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> Tot (option (lbytes l))
let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 488, "start_col": 0, "start_line": 484 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> FStar.Pervasives.Native.option (Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.setupBaseR", "FStar.Pervasives.Native.None", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let receiveExportBase cs enc skR info exp_ctx l =
match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.sendExportAuth
val sendExportAuth: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & lbytes l))
val sendExportAuth: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & lbytes l))
let sendExportAuth cs skE pkR info exp_ctx l skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 578, "start_col": 0, "start_line": 574 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx) let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None let sealAuth cs skE pkR info aad pt skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuth cs enc skR info aad ct pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> skS: Spec.Agile.HPKE.key_dh_secret_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.setupAuthS", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let sendExportAuth cs skE pkR info exp_ctx l skS =
match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.sendExportAuthPSK
val sendExportAuthPSK: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & lbytes l))
val sendExportAuthPSK: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & lbytes l))
let sendExportAuthPSK cs skE pkR info exp_ctx l psk psk_id skS = match setupAuthPSKS cs skE pkR info psk psk_id skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 625, "start_col": 0, "start_line": 621 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx) let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None let sealAuth cs skE pkR info aad pt skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuth cs enc skR info aad ct pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportAuth cs skE pkR info exp_ctx l skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportAuth cs enc skR info exp_ctx l pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// AuthPSK mode /// let setupAuthPSKS cs skE pkR info psk psk_id skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupAuthPSKR cs enc skR info psk psk_id pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealAuthPSK cs skE pkR info aad pt psk psk_id skS = match setupAuthPSKS cs skE pkR info psk psk_id skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuthPSK cs enc skR info aad ct psk psk_id pkS = match setupAuthPSKR cs enc skR info psk psk_id pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> skS: Spec.Agile.HPKE.key_dh_secret_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.setupAuthPSKS", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let sendExportAuthPSK cs skE pkR info exp_ctx l psk psk_id skS =
match setupAuthPSKS cs skE pkR info psk psk_id skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
false
FStar.Vector.Base.fst
FStar.Vector.Base.append
val append: #a:Type -> #l1:len_t -> #l2:len_t -> v1:raw a l1 -> v2:raw a l2{UInt.size U32.(v l1 + v l2) U32.n} -> Tot (raw a U32.(l1 +^ l2))
val append: #a:Type -> #l1:len_t -> #l2:len_t -> v1:raw a l1 -> v2:raw a l2{UInt.size U32.(v l1 + v l2) U32.n} -> Tot (raw a U32.(l1 +^ l2))
let append #a #l1 #l2 v1 v2 = Seq.append v1 v2
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 52, "start_col": 0, "start_line": 52 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`: /// a new vector that differs from `v` only at index `i`, where it contains `x`. let update #a #l v i x = Seq.upd v (U32.v i) x /// `append v1 v2`:
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v1: FStar.Vector.Base.raw a l1 -> v2: FStar.Vector.Base.raw a l2 {FStar.UInt.size (FStar.UInt32.v l1 + FStar.UInt32.v l2) FStar.UInt32.n} -> FStar.Vector.Base.raw a (l1 +^ l2)
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "FStar.UInt.size", "Prims.op_Addition", "FStar.UInt32.v", "FStar.UInt32.n", "FStar.Seq.Base.append", "FStar.UInt32.op_Plus_Hat" ]
[]
false
false
false
false
false
let append #a #l1 #l2 v1 v2 =
Seq.append v1 v2
false
FStar.Vector.Base.fst
FStar.Vector.Base.len
val len: #a:Type -> t a -> len_t
val len: #a:Type -> t a -> len_t
let len #a (| l , _ |) = l
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 79, "start_col": 0, "start_line": 79 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`: /// a new vector that differs from `v` only at index `i`, where it contains `x`. let update #a #l v i x = Seq.upd v (U32.v i) x /// `append v1 v2`: /// requires proving that the sum of the lengths of v1 and v2 still fit in a u32 let append #a #l1 #l2 v1 v2 = Seq.append v1 v2 /// `sub v i j`: /// the sub-vector of `v` starting from index `i` up to, but not including, `j` let sub #a #l v i j = Seq.slice v (U32.v i) (U32.v j) //////////////////////////////////////////////////////////////////////////////// /// Lemmas about the basic operations, all rather boring /// -- Each is just a lifting specifying the corresponding operation on seq //////////////////////////////////////////////////////////////////////////////// let reveal_init #a l contents = () let reveal_index #a #l v i = () let reveal_update #a #l v i x = () let reveal_append #a #l1 #l2 v1 v2 = () let reveal_sub #a #l v i j = () //////////////////////////////////////////////////////////////////////////////// /// Dynamically sized vectors //////////////////////////////////////////////////////////////////////////////// let t a = (l:len_t & raw a l) /// Unlike raw vectors, t-vectors support decidable equality let t_has_eq a = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Vector.Base.t a -> FStar.Vector.Base.len_t
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.t", "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw" ]
[]
false
false
false
true
false
let len #a (| l , _ |) =
l
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupAuthS
val setupAuthS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
val setupAuthS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 23, "end_line": 548, "start_col": 0, "start_line": 543 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode ///
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> skS: Spec.Agile.HPKE.key_dh_secret_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.auth_encap", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.Auth", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let setupAuthS cs skE pkR info skS =
match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx)
false
FStar.Vector.Base.fst
FStar.Vector.Base.sub
val sub: #a:Type -> #l:len_t -> v:raw a l -> i:len_t -> j:len_t{U32.(v i <= v j /\ v j <= v l)} -> Tot (raw a U32.(j -^ i))
val sub: #a:Type -> #l:len_t -> v:raw a l -> i:len_t -> j:len_t{U32.(v i <= v j /\ v j <= v l)} -> Tot (raw a U32.(j -^ i))
let sub #a #l v i j = Seq.slice v (U32.v i) (U32.v j)
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 53, "end_line": 56, "start_col": 0, "start_line": 56 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`: /// a new vector that differs from `v` only at index `i`, where it contains `x`. let update #a #l v i x = Seq.upd v (U32.v i) x /// `append v1 v2`: /// requires proving that the sum of the lengths of v1 and v2 still fit in a u32 let append #a #l1 #l2 v1 v2 = Seq.append v1 v2 /// `sub v i j`:
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: FStar.Vector.Base.raw a l -> i: FStar.Vector.Base.len_t -> j: FStar.Vector.Base.len_t {FStar.UInt32.v i <= FStar.UInt32.v j /\ FStar.UInt32.v j <= FStar.UInt32.v l} -> FStar.Vector.Base.raw a (j -^ i)
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "FStar.Seq.Base.slice", "FStar.UInt32.op_Subtraction_Hat" ]
[]
false
false
false
false
false
let sub #a #l v i j =
Seq.slice v (U32.v i) (U32.v j)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.receiveExportAuth
val receiveExportAuth: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (lbytes l))
val receiveExportAuth: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (lbytes l))
let receiveExportAuth cs enc skR info exp_ctx l pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 584, "start_col": 0, "start_line": 580 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx) let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None let sealAuth cs skE pkR info aad pt skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuth cs enc skR info aad ct pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportAuth cs skE pkR info exp_ctx l skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> pkS: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> FStar.Pervasives.Native.option (Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.setupAuthR", "FStar.Pervasives.Native.None", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let receiveExportAuth cs enc skR info exp_ctx l pkS =
match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupAuthPSKS
val setupAuthPSKS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
val setupAuthPSKS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> skS:key_dh_secret_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
let setupAuthPSKS cs skE pkR info psk psk_id skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 23, "end_line": 595, "start_col": 0, "start_line": 590 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx) let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None let sealAuth cs skE pkR info aad pt skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuth cs enc skR info aad ct pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportAuth cs skE pkR info exp_ctx l skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportAuth cs enc skR info exp_ctx l pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// AuthPSK mode ///
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> skS: Spec.Agile.HPKE.key_dh_secret_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.auth_encap", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.AuthPSK", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let setupAuthPSKS cs skE pkR info psk psk_id skS =
match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupPSKS
val setupPSKS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
val setupPSKS: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (key_dh_public_s cs & encryption_context cs))
let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 23, "end_line": 500, "start_col": 0, "start_line": 494 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode ///
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.encap", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.PSK", "Prims.unit", "Prims._assert", "Prims.b2t", "Spec.Agile.HPKE.verify_psk_inputs", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let setupPSKS cs skE pkR info psk psk_id =
match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.receiveExportAuthPSK
val receiveExportAuthPSK: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (lbytes l))
val receiveExportAuthPSK: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> exp_ctx:exp_ctx_s cs -> l:exp_len cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (lbytes l))
let receiveExportAuthPSK cs enc skR info exp_ctx l psk psk_id pkS = match setupAuthPSKR cs enc skR info psk psk_id pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 631, "start_col": 0, "start_line": 627 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx) let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None let sealAuth cs skE pkR info aad pt skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuth cs enc skR info aad ct pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportAuth cs skE pkR info exp_ctx l skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportAuth cs enc skR info exp_ctx l pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// AuthPSK mode /// let setupAuthPSKS cs skE pkR info psk psk_id skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupAuthPSKR cs enc skR info psk psk_id pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealAuthPSK cs skE pkR info aad pt psk psk_id skS = match setupAuthPSKS cs skE pkR info psk psk_id skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuthPSK cs enc skR info aad ct psk psk_id pkS = match setupAuthPSKR cs enc skR info psk psk_id pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportAuthPSK cs skE pkR info exp_ctx l psk psk_id skS = match setupAuthPSKS cs skE pkR info psk psk_id skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> exp_ctx: Spec.Agile.HPKE.exp_ctx_s cs -> l: Spec.Agile.HPKE.exp_len cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> pkS: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> FStar.Pervasives.Native.option (Lib.ByteSequence.lbytes l)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.exp_ctx_s", "Spec.Agile.HPKE.exp_len", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.setupAuthPSKR", "FStar.Pervasives.Native.None", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.encryption_context", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.context_export", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let receiveExportAuthPSK cs enc skR info exp_ctx l psk psk_id pkS =
match setupAuthPSKR cs enc skR info psk psk_id pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l)
false
Steel.ST.Array.Util.fst
Steel.ST.Array.Util.array_literal_loop_body
val array_literal_loop_body (#a: Type0) (n: US.t) (arr: A.array a {A.length arr == US.v n}) (f: (i: US.t{US.v i < US.v n} -> a)) (i: Loops.u32_between 0sz n) : STT unit (exists_ (array_literal_inv n arr f (US.v i))) (fun _ -> exists_ (array_literal_inv n arr f (US.v i + 1)))
val array_literal_loop_body (#a: Type0) (n: US.t) (arr: A.array a {A.length arr == US.v n}) (f: (i: US.t{US.v i < US.v n} -> a)) (i: Loops.u32_between 0sz n) : STT unit (exists_ (array_literal_inv n arr f (US.v i))) (fun _ -> exists_ (array_literal_inv n arr f (US.v i + 1)))
let array_literal_loop_body (#a:Type0) (n:US.t) (arr:A.array a{A.length arr == US.v n}) (f:(i:US.t{US.v i < US.v n} -> a)) : i:Loops.u32_between 0sz n -> STT unit (exists_ (array_literal_inv n arr f (US.v i))) (fun _ -> exists_ (array_literal_inv n arr f (US.v i + 1))) = fun i -> let s = elim_exists () in (); A.pts_to_length arr s; elim_pure (array_literal_inv_pure n f (US.v i) s); A.write arr i (f i); intro_pure (array_literal_inv_pure n f (US.v i + 1) (Seq.upd s (US.v i) (f i))); intro_exists (Seq.upd s (US.v i) (f i)) (array_literal_inv n arr f (US.v i + 1))
{ "file_name": "lib/steel/Steel.ST.Array.Util.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 46, "end_line": 78, "start_col": 0, "start_line": 57 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module Steel.ST.Array.Util module G = FStar.Ghost module US = FStar.SizeT module R = Steel.ST.Reference module A = Steel.ST.Array module Loops = Steel.ST.Loops open Steel.FractionalPermission open Steel.ST.Effect open Steel.ST.Util /// Implementation of array_literal using a for loop let array_literal_inv_pure (#a:Type0) (n:US.t) (f:(i:US.t{US.v i < US.v n} -> a)) (i:Loops.nat_at_most n) (s:Seq.seq a) : prop = forall (j:nat). (j < i /\ j < Seq.length s) ==> Seq.index s j == f (US.uint_to_t j) [@@ __reduce__] let array_literal_inv (#a:Type0) (n:US.t) (arr:A.array a) (f:(i:US.t{US.v i < US.v n} -> a)) (i:Loops.nat_at_most n) : Seq.seq a -> vprop = fun s -> A.pts_to arr full_perm s `star` pure (array_literal_inv_pure n f i s)
{ "checked_file": "/", "dependencies": [ "Steel.ST.Util.fsti.checked", "Steel.ST.Reference.fsti.checked", "Steel.ST.Loops.fsti.checked", "Steel.ST.Effect.fsti.checked", "Steel.ST.Array.fsti.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.SizeT.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "Steel.ST.Array.Util.fst" }
[ { "abbrev": false, "full_module": "Steel.ST.Util", "short_module": null }, { "abbrev": false, "full_module": "Steel.ST.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": true, "full_module": "Steel.ST.Loops", "short_module": "Loops" }, { "abbrev": true, "full_module": "Steel.ST.Array", "short_module": "A" }, { "abbrev": true, "full_module": "Steel.ST.Reference", "short_module": "R" }, { "abbrev": true, "full_module": "FStar.SizeT", "short_module": "US" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": false, "full_module": "Steel.ST.Util", "short_module": null }, { "abbrev": false, "full_module": "Steel.ST.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": true, "full_module": "Steel.ST.Array", "short_module": "A" }, { "abbrev": true, "full_module": "FStar.SizeT", "short_module": "US" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": false, "full_module": "Steel.ST.Array", "short_module": null }, { "abbrev": false, "full_module": "Steel.ST.Array", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: FStar.SizeT.t -> arr: Steel.ST.Array.array a {Steel.ST.Array.length arr == FStar.SizeT.v n} -> f: (i: FStar.SizeT.t{FStar.SizeT.v i < FStar.SizeT.v n} -> a) -> i: Steel.ST.Loops.u32_between 0sz n -> Steel.ST.Effect.STT Prims.unit
Steel.ST.Effect.STT
[]
[]
[ "FStar.SizeT.t", "Steel.ST.Array.array", "Prims.eq2", "Prims.nat", "Steel.ST.Array.length", "FStar.SizeT.v", "Prims.b2t", "Prims.op_LessThan", "Steel.ST.Loops.u32_between", "FStar.SizeT.__uint_to_t", "Steel.ST.Util.intro_exists", "FStar.Seq.Base.seq", "FStar.Ghost.hide", "FStar.Set.set", "Steel.Memory.iname", "FStar.Set.empty", "FStar.Seq.Base.upd", "FStar.Ghost.reveal", "Steel.ST.Array.Util.array_literal_inv", "Prims.op_Addition", "Prims.unit", "Steel.ST.Util.intro_pure", "Steel.ST.Array.Util.array_literal_inv_pure", "Steel.ST.Array.write", "Steel.ST.Util.elim_pure", "Steel.ST.Array.pts_to_length", "Steel.FractionalPermission.full_perm", "FStar.Ghost.erased", "Steel.ST.Util.elim_exists", "Steel.Effect.Common.VStar", "Steel.ST.Array.pts_to", "Steel.ST.Util.pure", "Steel.Effect.Common.vprop", "Steel.ST.Util.exists_" ]
[]
false
true
false
false
false
let array_literal_loop_body (#a: Type0) (n: US.t) (arr: A.array a {A.length arr == US.v n}) (f: (i: US.t{US.v i < US.v n} -> a)) (i: Loops.u32_between 0sz n) : STT unit (exists_ (array_literal_inv n arr f (US.v i))) (fun _ -> exists_ (array_literal_inv n arr f (US.v i + 1))) =
fun i -> let s = elim_exists () in (); A.pts_to_length arr s; elim_pure (array_literal_inv_pure n f (US.v i) s); A.write arr i (f i); intro_pure (array_literal_inv_pure n f (US.v i + 1) (Seq.upd s (US.v i) (f i))); intro_exists (Seq.upd s (US.v i) (f i)) (array_literal_inv n arr f (US.v i + 1))
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.setupAuthPSKR
val setupAuthPSKR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (encryption_context cs))
val setupAuthPSKR: cs:ciphersuite -> enc:key_dh_public_s cs -> skR:key_dh_secret_s cs -> info:info_s cs -> psk:psk_s cs -> psk_id:psk_id_s cs -> pkS:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (encryption_context cs))
let setupAuthPSKR cs enc skR info psk psk_id pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id))) | _ -> None
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 603, "start_col": 0, "start_line": 597 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) #restart-solver let openPSK cs enc skR info aad ct psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportPSK cs skE pkR info exp_ctx l psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportPSK cs enc skR info exp_ctx l psk psk_id = match setupPSKR cs enc skR info psk psk_id with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// Auth mode /// let setupAuthS cs skE pkR info skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Auth shared_secret info None in Some (enc, enc_ctx) let setupAuthR cs enc skR info pkS = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Auth shared_secret info None) | _ -> None let sealAuth cs skE pkR info aad pt skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openAuth cs enc skR info aad ct pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportAuth cs skE pkR info exp_ctx l skS = match setupAuthS cs skE pkR info skS with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportAuth cs enc skR info exp_ctx l pkS = match setupAuthR cs enc skR info pkS with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// AuthPSK mode /// let setupAuthPSKS cs skE pkR info psk psk_id skS = match auth_encap cs skE pkR skS with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> info: Spec.Agile.HPKE.info_s cs -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> pkS: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.HPKE.info_s", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Spec.Agile.HPKE.key_kem_s", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.key_schedule", "Spec.Agile.HPKE.AuthPSK", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.None", "Spec.Agile.HPKE.auth_decap", "Spec.Agile.DH.secret_to_public" ]
[]
false
false
false
false
false
let setupAuthPSKR cs enc skR info psk psk_id pkS =
let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = auth_decap cs enc skR pkS in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs AuthPSK shared_secret info (Some (psk, psk_id))) | _ -> None
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.context_seal
val context_seal: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> aad:AEAD.ad (aead_alg_of cs) -> pt:AEAD.plain (aead_alg_of cs) -> Tot (option (encryption_context cs & AEAD.cipher (aead_alg_of cs)))
val context_seal: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> aad:AEAD.ad (aead_alg_of cs) -> pt:AEAD.plain (aead_alg_of cs) -> Tot (option (encryption_context cs & AEAD.cipher (aead_alg_of cs)))
let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 38, "end_line": 430, "start_col": 0, "start_line": 423 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite_not_export_only -> ctx: Spec.Agile.HPKE.encryption_context cs -> aad: Spec.Agile.AEAD.ad (Spec.Agile.HPKE.aead_alg_of cs) -> pt: Spec.Agile.AEAD.plain (Spec.Agile.HPKE.aead_alg_of cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs * Spec.Agile.AEAD.cipher (Spec.Agile.HPKE.aead_alg_of cs))
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite_not_export_only", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.AEAD.ad", "Spec.Agile.HPKE.aead_alg_of", "Spec.Agile.AEAD.plain", "Spec.Agile.HPKE.context_increment_seq", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.AEAD.cipher", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Spec.Agile.AEAD.encrypted", "Spec.Agile.AEAD.encrypt", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.context_compute_nonce", "Spec.Agile.HPKE.seq_aead_s", "Spec.Agile.HPKE.seq_of_ctx", "Spec.Agile.HPKE.key_aead_s", "Spec.Agile.HPKE.key_of_ctx" ]
[]
false
false
false
false
false
let context_seal cs ctx aad pt =
let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct)
false
FStar.Vector.Base.fst
FStar.Vector.Base.as_raw
val as_raw: #a:Type -> x:t a -> raw a (len x)
val as_raw: #a:Type -> x:t a -> raw a (len x)
let as_raw #a (|_, v|) = v
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 82, "start_col": 0, "start_line": 82 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`: /// a new vector that differs from `v` only at index `i`, where it contains `x`. let update #a #l v i x = Seq.upd v (U32.v i) x /// `append v1 v2`: /// requires proving that the sum of the lengths of v1 and v2 still fit in a u32 let append #a #l1 #l2 v1 v2 = Seq.append v1 v2 /// `sub v i j`: /// the sub-vector of `v` starting from index `i` up to, but not including, `j` let sub #a #l v i j = Seq.slice v (U32.v i) (U32.v j) //////////////////////////////////////////////////////////////////////////////// /// Lemmas about the basic operations, all rather boring /// -- Each is just a lifting specifying the corresponding operation on seq //////////////////////////////////////////////////////////////////////////////// let reveal_init #a l contents = () let reveal_index #a #l v i = () let reveal_update #a #l v i x = () let reveal_append #a #l1 #l2 v1 v2 = () let reveal_sub #a #l v i j = () //////////////////////////////////////////////////////////////////////////////// /// Dynamically sized vectors //////////////////////////////////////////////////////////////////////////////// let t a = (l:len_t & raw a l) /// Unlike raw vectors, t-vectors support decidable equality let t_has_eq a = () /// The length of a t-vector is a dynamically computable u32 let len #a (| l , _ |) = l
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.Vector.Base.t a -> FStar.Vector.Base.raw a (FStar.Vector.Base.len x)
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.t", "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "FStar.Vector.Base.len" ]
[]
false
false
false
false
false
let as_raw #a (| _ , v |) =
v
false
FStar.Vector.Base.fst
FStar.Vector.Base.from_raw
val from_raw: #a:Type -> #l:len_t -> v:raw a l -> x:t a{len x = l}
val from_raw: #a:Type -> #l:len_t -> v:raw a l -> x:t a{len x = l}
let from_raw #a #l v = (| l, v |)
{ "file_name": "ulib/FStar.Vector.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 85, "start_col": 0, "start_line": 85 }
(* Copyright 2008-2017 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Vector.Base module U32 = FStar.UInt32 module S = FStar.Seq /// The raw vector type: the main type provided by this module let raw a l = s:S.seq a{S.length s = U32.v l} /// Abstractly, a `raw a l` is just a sequence whose length is `U32.v l`. /// `reveal` and `hide` build an isomorphism establishing this let reveal #a #l v = v let hide #a s = s let hide_reveal #a #l v = () let reveal_hide #a s = () /// Extensional equality can be used to prove syntactic equality let extensionality #a #l v1 v2 = () //////////////////////////////////////////////////////////////////////////////// /// A small set of basic operations on vectors, corresponding to the operations on /// sequences. Other operations can be derived from these, as we do for seq. /// -- init, index, update, append, slice //////////////////////////////////////////////////////////////////////////////// /// `init l contents`: /// initialize an `l`-sized vector using `contents i` for the `i`th element let init #a l contents = Seq.init (U32.v l) contents /// `index v i`: get the `i`th element of `v` let index #a #l v i = Seq.index v (U32.v i) /// `update v i x`: /// a new vector that differs from `v` only at index `i`, where it contains `x`. let update #a #l v i x = Seq.upd v (U32.v i) x /// `append v1 v2`: /// requires proving that the sum of the lengths of v1 and v2 still fit in a u32 let append #a #l1 #l2 v1 v2 = Seq.append v1 v2 /// `sub v i j`: /// the sub-vector of `v` starting from index `i` up to, but not including, `j` let sub #a #l v i j = Seq.slice v (U32.v i) (U32.v j) //////////////////////////////////////////////////////////////////////////////// /// Lemmas about the basic operations, all rather boring /// -- Each is just a lifting specifying the corresponding operation on seq //////////////////////////////////////////////////////////////////////////////// let reveal_init #a l contents = () let reveal_index #a #l v i = () let reveal_update #a #l v i x = () let reveal_append #a #l1 #l2 v1 v2 = () let reveal_sub #a #l v i j = () //////////////////////////////////////////////////////////////////////////////// /// Dynamically sized vectors //////////////////////////////////////////////////////////////////////////////// let t a = (l:len_t & raw a l) /// Unlike raw vectors, t-vectors support decidable equality let t_has_eq a = () /// The length of a t-vector is a dynamically computable u32 let len #a (| l , _ |) = l /// Access the underlying raw vector let as_raw #a (|_, v|) = v
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Vector.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Vector", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: FStar.Vector.Base.raw a l -> x: FStar.Vector.Base.t a {FStar.Vector.Base.len x = l}
Prims.Tot
[ "total" ]
[]
[ "FStar.Vector.Base.len_t", "FStar.Vector.Base.raw", "Prims.Mkdtuple2", "FStar.Vector.Base.t", "Prims.b2t", "Prims.op_Equality", "FStar.Vector.Base.len" ]
[]
false
false
false
false
false
let from_raw #a #l v =
(| l, v |)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.context_open
val context_open: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> aad:AEAD.ad (aead_alg_of cs) -> ct:AEAD.cipher (aead_alg_of cs) -> Tot (option (encryption_context cs & AEAD.plain (aead_alg_of cs)))
val context_open: cs:ciphersuite_not_export_only -> ctx:encryption_context cs -> aad:AEAD.ad (aead_alg_of cs) -> ct:AEAD.cipher (aead_alg_of cs) -> Tot (option (encryption_context cs & AEAD.plain (aead_alg_of cs)))
let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 40, "end_line": 441, "start_col": 0, "start_line": 432 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite_not_export_only -> ctx: Spec.Agile.HPKE.encryption_context cs -> aad: Spec.Agile.AEAD.ad (Spec.Agile.HPKE.aead_alg_of cs) -> ct: Spec.Agile.AEAD.cipher (Spec.Agile.HPKE.aead_alg_of cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.encryption_context cs * Spec.Agile.AEAD.plain (Spec.Agile.HPKE.aead_alg_of cs))
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite_not_export_only", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.AEAD.ad", "Spec.Agile.HPKE.aead_alg_of", "Spec.Agile.AEAD.cipher", "Spec.Agile.AEAD.decrypt", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.AEAD.plain", "Spec.Agile.AEAD.decrypted", "Spec.Agile.HPKE.context_increment_seq", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.context_compute_nonce", "Spec.Agile.HPKE.seq_aead_s", "Spec.Agile.HPKE.seq_of_ctx", "Spec.Agile.HPKE.key_aead_s", "Spec.Agile.HPKE.key_of_ctx" ]
[]
false
false
false
false
false
let context_open cs ctx aad ct =
let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.key_schedule_end
val key_schedule_end (cs: ciphersuite) (m: mode) (context: lbytes (size_ks_ctx cs)) (exporter_secret: exporter_secret_s cs) (secret: (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs
val key_schedule_end (cs: ciphersuite) (m: mode) (context: lbytes (size_ks_ctx cs)) (exporter_secret: exporter_secret_s cs) (secret: (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs
let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret))
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 53, "end_line": 381, "start_col": 0, "start_line": 364 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> m: Spec.Agile.HPKE.mode -> context: Lib.ByteSequence.lbytes (Spec.Agile.HPKE.size_ks_ctx cs) -> exporter_secret: Spec.Agile.HPKE.exporter_secret_s cs -> secret: Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length (Spec.Agile.HPKE.hash_of_cs cs)) -> Spec.Agile.HPKE.encryption_context cs
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.mode", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.size_ks_ctx", "Spec.Agile.HPKE.exporter_secret_s", "Spec.Hash.Definitions.hash_length", "Spec.Agile.HPKE.hash_of_cs", "Spec.Agile.HPKE.is_valid_not_export_only_ciphersuite", "FStar.Pervasives.Native.Mktuple4", "Spec.Agile.HPKE.key_aead_s", "Spec.Agile.HPKE.nonce_aead_s", "Spec.Agile.HPKE.seq_aead_s", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Agile.HPKE.size_aead_nonce", "Spec.Agile.HPKE.labeled_expand", "Spec.Agile.HPKE.suite_id_hpke", "Spec.Agile.HPKE.label_base_nonce", "Spec.Agile.HPKE.size_aead_key", "Spec.Agile.HPKE.label_key", "Prims.bool", "Lib.ByteSequence.lbytes_empty", "Prims.unit", "Prims._assert", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Spec.Agile.HPKE.encryption_context" ]
[]
false
false
false
false
false
let key_schedule_end (cs: ciphersuite) (m: mode) (context: lbytes (size_ks_ctx cs)) (exporter_secret: exporter_secret_s cs) (secret: (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs =
if is_valid_not_export_only_ciphersuite cs then (let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret)) else (assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret))
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.decap
val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs))
val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs))
let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 26, "end_line": 225, "start_col": 0, "start_line": 209 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_kem_s cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.dh", "Spec.Agile.HPKE.kem_dh_of_cs", "FStar.Pervasives.Native.None", "Spec.Agile.HPKE.key_kem_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.DH.secret_to_public", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.extract_and_expand", "Prims.unit", "Prims._assert", "Prims.b2t", "Spec.Agile.HPKE.extract_and_expand_ctx_pred", "FStar.Seq.Base.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Prims.op_Equality", "Prims.int", "FStar.Mul.op_Star", "Spec.Agile.HPKE.size_dh_public", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Spec.Agile.HPKE.prepare_dh", "Prims.op_Addition", "Prims.eq2", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.append", "Lib.Sequence.concat", "Spec.Agile.HPKE.serialize_public_key", "FStar.Pervasives.Native.option", "Spec.Agile.HPKE.deserialize_public_key", "FStar.Pervasives.allow_inversion", "Spec.Hash.Definitions.hash_alg", "Spec.Agile.DH.algorithm" ]
[]
false
false
false
false
false
let decap cs enc skR =
let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2 * size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.encap
val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs))
val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs))
let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 31, "end_line": 199, "start_col": 0, "start_line": 184 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_kem_s cs * Spec.Agile.HPKE.key_dh_public_s cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.DH.secret_to_public", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_kem_s", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.DH.dh", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.extract_and_expand", "Prims.unit", "Prims._assert", "Prims.b2t", "Spec.Agile.HPKE.extract_and_expand_ctx_pred", "FStar.Seq.Base.length", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Prims.op_Equality", "Prims.int", "FStar.Mul.op_Star", "Spec.Agile.HPKE.size_dh_public", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Spec.Agile.HPKE.prepare_dh", "Prims.op_Addition", "Prims.eq2", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.append", "Lib.Sequence.concat", "Spec.Agile.HPKE.serialize_public_key", "FStar.Pervasives.Native.option", "FStar.Pervasives.allow_inversion", "Spec.Agile.DH.algorithm" ]
[]
false
false
false
false
false
let encap cs skE pkR =
let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2 * size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.key_schedule_core
val key_schedule_core (cs: ciphersuite) (m: mode) (shared_secret: key_kem_s cs) (info: info_s cs) (opsk: option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs))))
val key_schedule_core (cs: ciphersuite) (m: mode) (shared_secret: key_kem_s cs) (info: info_s cs) (opsk: option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs))))
let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 362, "start_col": 0, "start_line": 344 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> m: Spec.Agile.HPKE.mode -> shared_secret: Spec.Agile.HPKE.key_kem_s cs -> info: Spec.Agile.HPKE.info_s cs -> opsk: FStar.Pervasives.Native.option (Spec.Agile.HPKE.psk_s cs * Spec.Agile.HPKE.psk_id_s cs) -> (Lib.ByteSequence.lbytes (Spec.Agile.HPKE.size_ks_ctx cs) * Spec.Agile.HPKE.exporter_secret_s cs) * Lib.ByteSequence.lbytes (Spec.Hash.Definitions.hash_length (Spec.Agile.HPKE.hash_of_cs cs))
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.mode", "Spec.Agile.HPKE.key_kem_s", "Spec.Agile.HPKE.info_s", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Lib.ByteSequence.bytes", "FStar.Pervasives.Native.Mktuple3", "Lib.ByteSequence.lbytes", "Spec.Agile.HPKE.size_ks_ctx", "Spec.Agile.HPKE.exporter_secret_s", "Spec.Hash.Definitions.hash_length", "Spec.Agile.HPKE.hash_of_cs", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Agile.HPKE.size_kdf", "Spec.Agile.HPKE.labeled_expand", "Spec.Agile.HPKE.suite_id_hpke", "Spec.Agile.HPKE.label_exp", "Spec.Agile.HPKE.labeled_extract", "Spec.Agile.HPKE.label_secret", "Spec.Agile.HPKE.build_context", "Lib.ByteSequence.lbytes_empty", "Spec.Agile.HPKE.label_info_hash", "Spec.Agile.HPKE.label_psk_id_hash", "FStar.Pervasives.Native.tuple3", "Lib.Sequence.seq", "FStar.Pervasives.Native.Mktuple2", "Spec.Agile.HPKE.default_psk", "Spec.Agile.HPKE.default_psk_id" ]
[]
false
false
false
false
false
let key_schedule_core (cs: ciphersuite) (m: mode) (shared_secret: key_kem_s cs) (info: info_s cs) (opsk: option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) =
let psk, psk_id = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.lemma_prime
val lemma_prime: unit -> Lemma (pow2 130 % prime = 5)
val lemma_prime: unit -> Lemma (pow2 130 % prime = 5)
let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 40, "end_line": 18, "start_col": 0, "start_line": 15 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Prims.unit -> FStar.Pervasives.Lemma (ensures Prims.pow2 130 % Hacl.Spec.Poly1305.Vec.prime = 5)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "FStar.Math.Lemmas.modulo_lemma", "Hacl.Spec.Poly1305.Vec.prime", "FStar.Pervasives.assert_norm", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "Prims.pow2" ]
[]
true
false
true
false
false
let lemma_prime () =
assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_fits_lemma
val smul_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (felem_wide_fits5 (smul_felem5 #w u1 f2) (m1 *^ m2))
val smul_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (felem_wide_fits5 (smul_felem5 #w u1 f2) (m1 *^ m2))
let smul_felem5_fits_lemma #w #m1 #m2 u1 f2 = let (f20, f21, f22, f23, f24) = f2 in let (m20, m21, m22, m23, m24) = m2 in smul_felem5_fits_lemma1 #w #m1 #m20 u1 f20; smul_felem5_fits_lemma1 #w #m1 #m21 u1 f21; smul_felem5_fits_lemma1 #w #m1 #m22 u1 f22; smul_felem5_fits_lemma1 #w #m1 #m23 u1 f23; smul_felem5_fits_lemma1 #w #m1 #m24 u1 f24
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 197, "start_col": 0, "start_line": 190 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26) let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26) val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i = let o = smul_felem5 #w u1 f2 in let (m20, m21, m22, m23, m24) = m2 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[i] == vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert (vu1 * (fas_nat5 f2).[i] == (fas_nat5 o).[i]) val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2)) let smul_felem5_fits_lemma1 #w #m1 #m2 u1 f2 = match w with | 1 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0 | 2 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1 | 4 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 2; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 3 val smul_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (felem_wide_fits5 (smul_felem5 #w u1 f2) (m1 *^ m2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.felem5 w {Hacl.Spec.Poly1305.Field32xN.felem_fits5 f2 m2} -> FStar.Pervasives.Lemma (ensures Hacl.Spec.Poly1305.Field32xN.felem_wide_fits5 (Hacl.Spec.Poly1305.Field32xN.smul_felem5 u1 f2) (m1 *^ m2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.scale32_5", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Hacl.Spec.Poly1305.Field32xN.felem5", "Hacl.Spec.Poly1305.Field32xN.felem_fits5", "Prims.nat", "Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_fits_lemma1", "Prims.unit" ]
[]
false
false
true
false
false
let smul_felem5_fits_lemma #w #m1 #m2 u1 f2 =
let f20, f21, f22, f23, f24 = f2 in let m20, m21, m22, m23, m24 = m2 in smul_felem5_fits_lemma1 #w #m1 #m20 u1 f20; smul_felem5_fits_lemma1 #w #m1 #m21 u1 f21; smul_felem5_fits_lemma1 #w #m1 #m22 u1 f22; smul_felem5_fits_lemma1 #w #m1 #m23 u1 f23; smul_felem5_fits_lemma1 #w #m1 #m24 u1 f24
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_mod_lemma
val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b)
val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b)
let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64)
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 50, "end_line": 44, "start_col": 0, "start_line": 41 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat{a <= m1 * Hacl.Spec.Poly1305.Field32xN.max26} -> b: Prims.nat{b <= m2 * Hacl.Spec.Poly1305.Field32xN.max26} -> FStar.Pervasives.Lemma (ensures a * b % Prims.pow2 64 == a * b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.scale32", "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Mul.op_Star", "Hacl.Spec.Poly1305.Field32xN.max26", "FStar.Math.Lemmas.modulo_lemma", "Prims.pow2", "Prims.unit", "Prims._assert", "Hacl.Poly1305.Field32xN.Lemmas0.lemma_mult_le" ]
[]
true
false
true
false
false
let smul_mod_lemma #m1 #m2 a b =
lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= ((m1 * m2) * max26) * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64)
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_add_mod_lemma
val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b)
val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b)
let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64)
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 54, "end_line": 60, "start_col": 0, "start_line": 56 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat{a <= m1 * Hacl.Spec.Poly1305.Field32xN.max26} -> b: Prims.nat{b <= m2 * Hacl.Spec.Poly1305.Field32xN.max26} -> c: Prims.nat{c <= (m3 * Hacl.Spec.Poly1305.Field32xN.max26) * Hacl.Spec.Poly1305.Field32xN.max26} -> FStar.Pervasives.Lemma (ensures (c + a * b % Prims.pow2 64) % Prims.pow2 64 == c + a * b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.scale64", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.nat", "Hacl.Spec.Poly1305.Field32xN.max26", "FStar.Math.Lemmas.modulo_lemma", "Prims.pow2", "Prims.unit", "Prims._assert", "Hacl.Poly1305.Field32xN.Lemmas0.lemma_mult_le", "FStar.Pervasives.assert_norm", "Prims.op_LessThan" ]
[]
true
false
true
false
false
let smul_add_mod_lemma #m1 #m2 #m3 a b c =
assert_norm (((m3 + m1 * m2) * max26) * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= (m3 * max26) * max26 + ((m1 * m2) * max26) * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64)
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.add5_lemma1
val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26)
val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26)
let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32)
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 45, "end_line": 72, "start_col": 0, "start_line": 67 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ma: Hacl.Spec.Poly1305.Field32xN.scale64 -> mb: Hacl.Spec.Poly1305.Field32xN.scale64 -> a: Lib.IntTypes.uint64 -> b: Lib.IntTypes.uint64 -> FStar.Pervasives.Lemma (requires Lib.IntTypes.v a <= ma * Hacl.Spec.Poly1305.Field32xN.max26 /\ Lib.IntTypes.v b <= mb * Hacl.Spec.Poly1305.Field32xN.max26 /\ ma + mb <= 64) (ensures Lib.IntTypes.v (a +. b) == Lib.IntTypes.v a + Lib.IntTypes.v b /\ Lib.IntTypes.v (a +. b) <= (ma + mb) * Hacl.Spec.Poly1305.Field32xN.max26)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.scale64", "Lib.IntTypes.uint64", "FStar.Math.Lemmas.small_mod", "Prims.op_Addition", "Lib.IntTypes.v", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "Prims.pow2", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.b2t", "Prims.op_LessThan", "FStar.Mul.op_Star", "Hacl.Spec.Poly1305.Field32xN.max26", "Prims._assert", "Prims.op_LessThanOrEqual", "FStar.Math.Lemmas.lemma_mult_le_right" ]
[]
true
false
true
false
false
let add5_lemma1 ma mb a b =
assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32)
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_fits_lemma_i
val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26)
val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26)
let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26)
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 81, "end_line": 116, "start_col": 0, "start_line": 112 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 f2 m2} -> i: Prims.nat{i < w} -> FStar.Pervasives.Lemma (ensures (Hacl.Spec.Poly1305.Field32xN.uint64xN_v (Lib.IntVector.vec_mul_mod f2 u1)).[ i ] <= ((m1 * m2) * Hacl.Spec.Poly1305.Field32xN.max26) * Hacl.Spec.Poly1305.Field32xN.max26)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Hacl.Poly1305.Field32xN.Lemmas0.lemma_mult_le", "Lib.Sequence.op_String_Access", "Hacl.Spec.Poly1305.Field32xN.uint64xN_v", "FStar.Mul.op_Star", "Hacl.Spec.Poly1305.Field32xN.max26", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.int", "Hacl.Poly1305.Field32xN.Lemmas0.smul_mod_lemma", "Lib.IntVector.vec_t", "Lib.IntTypes.U64", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Lib.IntTypes.SEC", "Lib.IntVector.vec_v", "Lib.Sequence.map2", "Lib.IntTypes.mul_mod", "Lib.IntVector.vec_mul_mod" ]
[]
true
false
true
false
false
let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i =
let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[ i ] (uint64xN_v f2).[ i ]; assert ((uint64xN_v o).[ i ] == (uint64xN_v u1).[ i ] * (uint64xN_v f2).[ i ]); lemma_mult_le (uint64xN_v u1).[ i ] (m1 * max26) (uint64xN_v f2).[ i ] (m2 * max26)
false
FStar.Endianness.fsti
FStar.Endianness.uint32_of_le
val uint32_of_le : b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 4} -> FStar.UInt32.t
let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 145, "start_col": 0, "start_line": 142 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 4} -> FStar.UInt32.t
Prims.Tot
[ "total" ]
[]
[ "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t", "FStar.UInt32.uint_to_t", "Prims.unit", "FStar.Endianness.lemma_le_to_n_is_bounded", "Prims.nat", "FStar.Endianness.le_to_n", "FStar.UInt32.t" ]
[]
false
false
false
false
false
let uint32_of_le (b: bytes{S.length b = 4}) =
let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n
false
FStar.Endianness.fsti
FStar.Endianness.be_of_uint32
val be_of_uint32 (x: UInt32.t) : b: bytes{S.length b = 4}
val be_of_uint32 (x: UInt32.t) : b: bytes{S.length b = 4}
let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x)
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 156, "start_col": 0, "start_line": 155 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt32.t -> b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 4}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt32.t", "FStar.Endianness.n_to_be", "FStar.UInt32.v", "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t" ]
[]
false
false
false
false
false
let be_of_uint32 (x: UInt32.t) : b: bytes{S.length b = 4} =
n_to_be 4 (UInt32.v x)
false
FStar.Endianness.fsti
FStar.Endianness.le_of_uint32
val le_of_uint32 (x: UInt32.t) : b: bytes{S.length b = 4}
val le_of_uint32 (x: UInt32.t) : b: bytes{S.length b = 4}
let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x)
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 148, "start_col": 0, "start_line": 147 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt32.t -> b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 4}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt32.t", "FStar.Endianness.n_to_le", "FStar.UInt32.v", "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t" ]
[]
false
false
false
false
false
let le_of_uint32 (x: UInt32.t) : b: bytes{S.length b = 4} =
n_to_le 4 (UInt32.v x)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.auth_decap
val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs))
val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs))
let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 28, "end_line": 302, "start_col": 0, "start_line": 277 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite -> enc: Spec.Agile.HPKE.key_dh_public_s cs -> skR: Spec.Agile.HPKE.key_dh_secret_s cs -> pkS: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_kem_s cs)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.DH.dh", "FStar.Pervasives.Native.None", "Spec.Agile.HPKE.key_kem_s", "Spec.Agile.DH.secret_to_public", "FStar.Pervasives.Native.Some", "Spec.Agile.HPKE.extract_and_expand", "Prims.unit", "Prims._assert", "Prims.b2t", "Spec.Agile.HPKE.extract_and_expand_dh_pred", "FStar.Seq.Base.length", "Lib.IntTypes.uint8", "Spec.Agile.HPKE.labeled_extract_ikm_length_pred", "Spec.Agile.HPKE.kem_hash_of_cs", "Prims.op_Addition", "Spec.Agile.HPKE.size_suite_id_kem", "Spec.Agile.HPKE.size_label_eae_prk", "Prims.op_Equality", "Prims.int", "Spec.Agile.HPKE.extract_and_expand_ctx_pred", "Lib.IntTypes.uint_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Spec.Agile.HPKE.labeled_expand_info_length_pred", "Spec.Agile.HPKE.size_label_shared_secret", "FStar.Mul.op_Star", "Spec.Agile.HPKE.size_dh_public", "Lib.Sequence.lseq", "Lib.IntTypes.int_t", "Prims.eq2", "FStar.Seq.Base.seq", "Lib.Sequence.to_seq", "FStar.Seq.Base.append", "Lib.Sequence.concat", "Spec.Agile.HPKE.serialize_public_key", "FStar.Pervasives.Native.option", "Spec.Agile.HPKE.prepare_dh", "Spec.Agile.HPKE.deserialize_public_key", "FStar.Pervasives.allow_inversion", "Spec.Agile.DH.algorithm" ]
[]
false
false
false
false
false
let auth_decap cs enc skR pkS =
let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3 * size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret)
false
FStar.Endianness.fsti
FStar.Endianness.uint64_of_le
val uint64_of_le : b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 8} -> FStar.UInt64.t
let uint64_of_le (b: bytes { S.length b = 8 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 161, "start_col": 0, "start_line": 158 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 8} -> FStar.UInt64.t
Prims.Tot
[ "total" ]
[]
[ "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t", "FStar.UInt64.uint_to_t", "Prims.unit", "FStar.Endianness.lemma_le_to_n_is_bounded", "Prims.nat", "FStar.Endianness.le_to_n", "FStar.UInt64.t" ]
[]
false
false
false
false
false
let uint64_of_le (b: bytes{S.length b = 8}) =
let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_eval_lemma
val smul_felem5_eval_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (fas_nat5 (smul_felem5 #w u1 f2) == map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2))
val smul_felem5_eval_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (fas_nat5 (smul_felem5 #w u1 f2) == map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2))
let smul_felem5_eval_lemma #w #m1 #m2 u1 f2 = FStar.Classical.forall_intro (smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2); eq_intro (fas_nat5 (smul_felem5 #w u1 f2)) (map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2))
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 74, "end_line": 212, "start_col": 0, "start_line": 209 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26) let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26) val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i = let o = smul_felem5 #w u1 f2 in let (m20, m21, m22, m23, m24) = m2 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[i] == vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert (vu1 * (fas_nat5 f2).[i] == (fas_nat5 o).[i]) val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2)) let smul_felem5_fits_lemma1 #w #m1 #m2 u1 f2 = match w with | 1 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0 | 2 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1 | 4 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 2; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 3 val smul_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (felem_wide_fits5 (smul_felem5 #w u1 f2) (m1 *^ m2)) let smul_felem5_fits_lemma #w #m1 #m2 u1 f2 = let (f20, f21, f22, f23, f24) = f2 in let (m20, m21, m22, m23, m24) = m2 in smul_felem5_fits_lemma1 #w #m1 #m20 u1 f20; smul_felem5_fits_lemma1 #w #m1 #m21 u1 f21; smul_felem5_fits_lemma1 #w #m1 #m22 u1 f22; smul_felem5_fits_lemma1 #w #m1 #m23 u1 f23; smul_felem5_fits_lemma1 #w #m1 #m24 u1 f24 val smul_felem5_eval_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (fas_nat5 (smul_felem5 #w u1 f2) == map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.felem5 w {Hacl.Spec.Poly1305.Field32xN.felem_fits5 f2 m2} -> FStar.Pervasives.Lemma (ensures Hacl.Spec.Poly1305.Field32xN.fas_nat5 (Hacl.Spec.Poly1305.Field32xN.smul_felem5 u1 f2) == Lib.Sequence.map2 (fun a b -> a * b) (Hacl.Spec.Poly1305.Field32xN.uint64xN_v u1) (Hacl.Spec.Poly1305.Field32xN.fas_nat5 f2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.scale32_5", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Hacl.Spec.Poly1305.Field32xN.felem5", "Hacl.Spec.Poly1305.Field32xN.felem_fits5", "Lib.Sequence.eq_intro", "Prims.nat", "Hacl.Spec.Poly1305.Field32xN.fas_nat5", "Hacl.Spec.Poly1305.Field32xN.smul_felem5", "Lib.Sequence.map2", "FStar.Mul.op_Star", "Hacl.Spec.Poly1305.Field32xN.uint64xN_v", "Prims.unit", "FStar.Classical.forall_intro", "Prims.b2t", "Prims.op_LessThan", "Prims.eq2", "Prims.int", "Lib.Sequence.op_String_Access", "Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_eval_lemma_i" ]
[]
false
false
true
false
false
let smul_felem5_eval_lemma #w #m1 #m2 u1 f2 =
FStar.Classical.forall_intro (smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2); eq_intro (fas_nat5 (smul_felem5 #w u1 f2)) (map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2))
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_fits_lemma1
val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2))
val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2))
let smul_felem5_fits_lemma1 #w #m1 #m2 u1 f2 = match w with | 1 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0 | 2 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1 | 4 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 2; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 3
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 179, "start_col": 0, "start_line": 168 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26) let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26) val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i = let o = smul_felem5 #w u1 f2 in let (m20, m21, m22, m23, m24) = m2 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[i] == vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert (vu1 * (fas_nat5 f2).[i] == (fas_nat5 o).[i]) val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 f2 m2} -> FStar.Pervasives.Lemma (ensures Hacl.Spec.Poly1305.Field32xN.felem_wide_fits1 (Lib.IntVector.vec_mul_mod f2 u1) (m1 * m2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_fits_lemma_i", "Prims.unit" ]
[]
false
false
true
false
false
let smul_felem5_fits_lemma1 #w #m1 #m2 u1 f2 =
match w with | 1 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0 | 2 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1 | 4 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 2; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 3
false
FStar.Endianness.fsti
FStar.Endianness.uint32_of_be
val uint32_of_be : b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 4} -> FStar.UInt32.t
let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 153, "start_col": 0, "start_line": 150 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 4} -> FStar.UInt32.t
Prims.Tot
[ "total" ]
[]
[ "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t", "FStar.UInt32.uint_to_t", "Prims.unit", "FStar.Endianness.lemma_be_to_n_is_bounded", "Prims.nat", "FStar.Endianness.be_to_n", "FStar.UInt32.t" ]
[]
false
false
false
false
false
let uint32_of_be (b: bytes{S.length b = 4}) =
let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n
false
FStar.Endianness.fsti
FStar.Endianness.le_of_uint64
val le_of_uint64 (x: UInt64.t) : b: bytes{S.length b = 8}
val le_of_uint64 (x: UInt64.t) : b: bytes{S.length b = 8}
let le_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_le 8 (UInt64.v x)
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 164, "start_col": 0, "start_line": 163 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x) let uint64_of_le (b: bytes { S.length b = 8 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt64.t -> b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 8}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.Endianness.n_to_le", "FStar.UInt64.v", "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t" ]
[]
false
false
false
false
false
let le_of_uint64 (x: UInt64.t) : b: bytes{S.length b = 8} =
n_to_le 8 (UInt64.v x)
false
FStar.Endianness.fsti
FStar.Endianness.uint64_of_be
val uint64_of_be : b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 8} -> FStar.UInt64.t
let uint64_of_be (b: bytes { S.length b = 8 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt64.uint_to_t n
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 169, "start_col": 0, "start_line": 166 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x) let uint64_of_le (b: bytes { S.length b = 8 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n let le_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_le 8 (UInt64.v x)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 8} -> FStar.UInt64.t
Prims.Tot
[ "total" ]
[]
[ "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t", "FStar.UInt64.uint_to_t", "Prims.unit", "FStar.Endianness.lemma_be_to_n_is_bounded", "Prims.nat", "FStar.Endianness.be_to_n", "FStar.UInt64.t" ]
[]
false
false
false
false
false
let uint64_of_be (b: bytes{S.length b = 8}) =
let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt64.uint_to_t n
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.fadd5_eval_lemma_i
val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i])
val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i])
let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 89, "end_line": 100, "start_col": 0, "start_line": 84 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i])
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f1: Hacl.Spec.Poly1305.Field32xN.felem5 w {Hacl.Spec.Poly1305.Field32xN.felem_fits5 f1 (2, 2, 2, 2, 2)} -> f2: Hacl.Spec.Poly1305.Field32xN.felem5 w {Hacl.Spec.Poly1305.Field32xN.felem_fits5 f2 (1, 1, 1, 1, 1)} -> i: Prims.nat{i < w} -> FStar.Pervasives.Lemma (ensures (Hacl.Spec.Poly1305.Field32xN.feval5 (Hacl.Spec.Poly1305.Field32xN.fadd5 f1 f2)).[ i ] == Hacl.Spec.Poly1305.Vec.pfadd (Hacl.Spec.Poly1305.Field32xN.feval5 f1).[ i ] (Hacl.Spec.Poly1305.Field32xN.feval5 f2).[ i ])
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.felem5", "Hacl.Spec.Poly1305.Field32xN.felem_fits5", "FStar.Pervasives.Native.Mktuple5", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Lib.IntTypes.uint64", "FStar.Math.Lemmas.lemma_mod_plus_distr_r", "Prims.op_Modulus", "Hacl.Spec.Poly1305.Field32xN.as_nat5", "Hacl.Spec.Poly1305.Vec.prime", "Prims.unit", "FStar.Math.Lemmas.lemma_mod_plus_distr_l", "Prims._assert", "Prims.eq2", "Prims.int", "Prims.op_Addition", "Hacl.Poly1305.Field32xN.Lemmas0.add5_lemma1", "Hacl.Spec.Poly1305.Field32xN.tup64_5", "Hacl.Spec.Poly1305.Field32xN.as_tup64_i", "Hacl.Spec.Poly1305.Field32xN.fadd5" ]
[]
false
false
true
false
false
let fadd5_eval_lemma_i #w f1 f2 i =
let o = fadd5 f1 f2 in let f10, f11, f12, f13, f14 = as_tup64_i f1 i in let f20, f21, f22, f23, f24 = as_tup64_i f2 i in let o0, o1, o2, o3, o4 = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_add_felem5_fits_lemma
val smul_add_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> #m3:scale64_5{m3 +* m1 *^ m2 <=* s64x5 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> acc1:felem_wide5 w{felem_wide_fits5 acc1 m3} -> Lemma (felem_wide_fits5 (smul_add_felem5 #w u1 f2 acc1) (m3 +* m1 *^ m2))
val smul_add_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> #m3:scale64_5{m3 +* m1 *^ m2 <=* s64x5 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> acc1:felem_wide5 w{felem_wide_fits5 acc1 m3} -> Lemma (felem_wide_fits5 (smul_add_felem5 #w u1 f2 acc1) (m3 +* m1 *^ m2))
let smul_add_felem5_fits_lemma #w #m1 #m2 #m3 u1 f2 acc1 = let (f20, f21, f22, f23, f24) = f2 in let (m20, m21, m22, m23, m24) = m2 in let (a0, a1, a2, a3, a4) = acc1 in let (m30, m31, m32, m33, m34) = m3 in smul_add_felem5_fits_lemma1 #w #m1 #m20 #m30 u1 f20 a0; smul_add_felem5_fits_lemma1 #w #m1 #m21 #m31 u1 f21 a1; smul_add_felem5_fits_lemma1 #w #m1 #m22 #m32 u1 f22 a2; smul_add_felem5_fits_lemma1 #w #m1 #m23 #m33 u1 f23 a3; smul_add_felem5_fits_lemma1 #w #m1 #m24 #m34 u1 f24 a4
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 56, "end_line": 339, "start_col": 0, "start_line": 330 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26) let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26) val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i = let o = smul_felem5 #w u1 f2 in let (m20, m21, m22, m23, m24) = m2 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[i] == vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert (vu1 * (fas_nat5 f2).[i] == (fas_nat5 o).[i]) val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2)) let smul_felem5_fits_lemma1 #w #m1 #m2 u1 f2 = match w with | 1 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0 | 2 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1 | 4 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 2; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 3 val smul_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (felem_wide_fits5 (smul_felem5 #w u1 f2) (m1 *^ m2)) let smul_felem5_fits_lemma #w #m1 #m2 u1 f2 = let (f20, f21, f22, f23, f24) = f2 in let (m20, m21, m22, m23, m24) = m2 in smul_felem5_fits_lemma1 #w #m1 #m20 u1 f20; smul_felem5_fits_lemma1 #w #m1 #m21 u1 f21; smul_felem5_fits_lemma1 #w #m1 #m22 u1 f22; smul_felem5_fits_lemma1 #w #m1 #m23 u1 f23; smul_felem5_fits_lemma1 #w #m1 #m24 u1 f24 val smul_felem5_eval_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (fas_nat5 (smul_felem5 #w u1 f2) == map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2)) let smul_felem5_eval_lemma #w #m1 #m2 u1 f2 = FStar.Classical.forall_intro (smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2); eq_intro (fas_nat5 (smul_felem5 #w u1 f2)) (map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2)) val smul_add_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> acc1:uint64xN w{felem_wide_fits1 acc1 m3} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_add_mod acc1 (vec_mul_mod f2 u1))).[i] <= (m3 + m1 * m2) * max26 * max26) #push-options "--z3rlimit 200" let smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 i = let o = vec_add_mod acc1 (vec_mul_mod f2 u1) in smul_add_mod_lemma #m1 #m2 #m3 (uint64xN_v u1).[i] (uint64xN_v f2).[i] (uint64xN_v acc1).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v acc1).[i] + (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26); assert ((uint64xN_v o).[i] <= m3 * max26 * max26 + m1 * m2 * max26 * max26) #pop-options val smul_add_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> #m3:scale64_5{m3 +* m1 *^ m2 <=* s64x5 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> acc1:felem_wide5 w{felem_wide_fits5 acc1 m3} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_add_felem5 #w u1 f2 acc1)).[i] == (fas_nat5 acc1).[i] + (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_add_felem5_eval_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 i = let o = smul_add_felem5 #w u1 f2 acc1 in let (m20, m21, m22, m23, m24) = m2 in let (m30, m31, m32, m33, m34) = m3 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (ta0, ta1, ta2, ta3, ta4) = as_tup64_i acc1 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_add_mod_lemma #m1 #m20 #m30 vu1 (v tf20) (v ta0); smul_add_mod_lemma #m1 #m21 #m31 vu1 (v tf21) (v ta1); smul_add_mod_lemma #m1 #m22 #m32 vu1 (v tf22) (v ta2); smul_add_mod_lemma #m1 #m23 #m33 vu1 (v tf23) (v ta3); smul_add_mod_lemma #m1 #m24 #m34 vu1 (v tf24) (v ta4); calc (==) { (fas_nat5 o).[i]; (==) { } v ta0 + vu1 * v tf20 + (v ta1 + vu1 * v tf21) * pow26 + (v ta2 + vu1 * v tf22) * pow52 + (v ta3 + vu1 * v tf23) * pow78 + (v ta4 + vu1 * v tf24) * pow104; (==) { FStar.Math.Lemmas.distributivity_add_left (v ta1) (vu1 * v tf21) pow26; FStar.Math.Lemmas.distributivity_add_left (v ta2) (vu1 * v tf22) pow52; FStar.Math.Lemmas.distributivity_add_left (v ta3) (vu1 * v tf23) pow78; FStar.Math.Lemmas.distributivity_add_left (v ta1) (vu1 * v tf24) pow104 } v ta0 + v ta1 * pow26 + v ta2 * pow52 + v ta3 * pow78 + v ta4 * pow104 + vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; (==) { } (fas_nat5 acc1).[i] + vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert ((fas_nat5 o).[i] == (fas_nat5 acc1).[i] + vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert ((fas_nat5 o).[i] == (fas_nat5 acc1).[i] + vu1 * (fas_nat5 f2).[i]) val smul_add_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> acc1:uint64xN w{felem_wide_fits1 acc1 m3} -> Lemma (felem_wide_fits1 (vec_add_mod acc1 (vec_mul_mod f2 u1)) (m3 + m1 * m2)) let smul_add_felem5_fits_lemma1 #w #m1 #m2 #m3 u1 f2 acc1 = match w with | 1 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0 | 2 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 1 | 4 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 1; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 2; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 3 val smul_add_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> #m3:scale64_5{m3 +* m1 *^ m2 <=* s64x5 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> acc1:felem_wide5 w{felem_wide_fits5 acc1 m3} -> Lemma (felem_wide_fits5 (smul_add_felem5 #w u1 f2 acc1) (m3 +* m1 *^ m2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.felem5 w {Hacl.Spec.Poly1305.Field32xN.felem_fits5 f2 m2} -> acc1: Hacl.Spec.Poly1305.Field32xN.felem_wide5 w {Hacl.Spec.Poly1305.Field32xN.felem_wide_fits5 acc1 m3} -> FStar.Pervasives.Lemma (ensures Hacl.Spec.Poly1305.Field32xN.felem_wide_fits5 (Hacl.Spec.Poly1305.Field32xN.smul_add_felem5 u1 f2 acc1) (m3 +* m1 *^ m2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.scale32_5", "Hacl.Spec.Poly1305.Field32xN.scale64_5", "Hacl.Spec.Poly1305.Field32xN.op_Less_Equals_Star", "Hacl.Spec.Poly1305.Field32xN.op_Plus_Star", "Hacl.Spec.Poly1305.Field32xN.op_Star_Hat", "Hacl.Spec.Poly1305.Field32xN.s64x5", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Hacl.Spec.Poly1305.Field32xN.felem5", "Hacl.Spec.Poly1305.Field32xN.felem_fits5", "Hacl.Spec.Poly1305.Field32xN.felem_wide5", "Hacl.Spec.Poly1305.Field32xN.felem_wide_fits5", "Prims.nat", "Hacl.Poly1305.Field32xN.Lemmas0.smul_add_felem5_fits_lemma1", "Prims.unit" ]
[]
false
false
true
false
false
let smul_add_felem5_fits_lemma #w #m1 #m2 #m3 u1 f2 acc1 =
let f20, f21, f22, f23, f24 = f2 in let m20, m21, m22, m23, m24 = m2 in let a0, a1, a2, a3, a4 = acc1 in let m30, m31, m32, m33, m34 = m3 in smul_add_felem5_fits_lemma1 #w #m1 #m20 #m30 u1 f20 a0; smul_add_felem5_fits_lemma1 #w #m1 #m21 #m31 u1 f21 a1; smul_add_felem5_fits_lemma1 #w #m1 #m22 #m32 u1 f22 a2; smul_add_felem5_fits_lemma1 #w #m1 #m23 #m33 u1 f23 a3; smul_add_felem5_fits_lemma1 #w #m1 #m24 #m34 u1 f24 a4
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.permission
val permission : v: Type -> Type
let permission (v:Type) = option perm & option v
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 48, "end_line": 76, "start_col": 0, "start_line": 76 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: Type -> Type
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm" ]
[]
false
false
false
true
true
let permission (v: Type) =
option perm & option v
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.has_nonzero
val has_nonzero : p: Steel.FractionalAnchoredPreorder.permission v -> Prims.bool
let has_nonzero #v (p:permission v) = Some? (fst p)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 51, "end_line": 79, "start_col": 0, "start_line": 79 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.FractionalAnchoredPreorder.permission v -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Steel.FractionalAnchoredPreorder.permission", "FStar.Pervasives.Native.uu___is_Some", "Steel.FractionalPermission.perm", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.option", "Prims.bool" ]
[]
false
false
false
true
false
let has_nonzero #v (p: permission v) =
Some? (fst p)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.has_some_ownership
val has_some_ownership : p: Steel.FractionalAnchoredPreorder.permission v -> Prims.bool
let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 74, "end_line": 83, "start_col": 0, "start_line": 83 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p)
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.FractionalAnchoredPreorder.permission v -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Steel.FractionalAnchoredPreorder.permission", "Prims.op_BarBar", "Steel.FractionalAnchoredPreorder.has_nonzero", "Steel.FractionalAnchoredPreorder.has_anchor", "Prims.bool" ]
[]
false
false
false
true
false
let has_some_ownership #v (p: permission v) =
has_nonzero p || has_anchor p
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.has_anchor
val has_anchor : p: Steel.FractionalAnchoredPreorder.permission v -> Prims.bool
let has_anchor #v (p:permission v) = Some? (snd p)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 50, "end_line": 81, "start_col": 0, "start_line": 81 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p)
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.FractionalAnchoredPreorder.permission v -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Steel.FractionalAnchoredPreorder.permission", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.snd", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "Prims.bool" ]
[]
false
false
false
true
false
let has_anchor #v (p: permission v) =
Some? (snd p)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.anchor_rel
val anchor_rel : p: FStar.Preorder.preorder v -> Type
let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // }
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 3, "end_line": 102, "start_col": 0, "start_line": 98 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: FStar.Preorder.preorder v -> Type
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Prims.prop", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp" ]
[]
false
false
false
true
true
let anchor_rel (#v: Type) (p: preorder v) =
anchors: (v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) }
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.avalue
val avalue : anchors: Steel.FractionalAnchoredPreorder.anchor_rel p -> Type
let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv }
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 55, "end_line": 122, "start_col": 0, "start_line": 121 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
anchors: Steel.FractionalAnchoredPreorder.anchor_rel p -> Type
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "FStar.Pervasives.Native.tuple2", "Steel.FractionalAnchoredPreorder.permission", "Steel.Preorder.vhist", "Steel.FractionalAnchoredPreorder.anchored" ]
[]
false
false
false
false
true
let avalue (#v: Type) (#p: preorder v) (anchors: anchor_rel p) =
pv: (permission v & vhist p){anchored anchors pv}
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_felem5_eval_lemma_i
val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i])
val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i])
let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i = let o = smul_felem5 #w u1 f2 in let (m20, m21, m22, m23, m24) = m2 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[i] == vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert (vu1 * (fas_nat5 f2).[i] == (fas_nat5 o).[i])
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 54, "end_line": 157, "start_col": 0, "start_line": 128 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26) let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26) val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i])
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.felem5 w {Hacl.Spec.Poly1305.Field32xN.felem_fits5 f2 m2} -> i: Prims.nat{i < w} -> FStar.Pervasives.Lemma (ensures (Hacl.Spec.Poly1305.Field32xN.fas_nat5 (Hacl.Spec.Poly1305.Field32xN.smul_felem5 u1 f2)).[ i ] == (Hacl.Spec.Poly1305.Field32xN.uint64xN_v u1).[ i ] * (Hacl.Spec.Poly1305.Field32xN.fas_nat5 f2).[ i ])
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.scale32_5", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Hacl.Spec.Poly1305.Field32xN.felem5", "Hacl.Spec.Poly1305.Field32xN.felem_fits5", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Lib.IntTypes.uint64", "Prims._assert", "Prims.eq2", "Prims.int", "FStar.Mul.op_Star", "Lib.Sequence.op_String_Access", "Hacl.Spec.Poly1305.Field32xN.fas_nat5", "Prims.unit", "FStar.Calc.calc_finish", "Prims.op_Addition", "Lib.IntTypes.v", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "Hacl.Spec.Poly1305.Field32xN.pow26", "Hacl.Spec.Poly1305.Field32xN.pow52", "Hacl.Spec.Poly1305.Field32xN.pow78", "Hacl.Spec.Poly1305.Field32xN.pow104", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Prims.squash", "Hacl.Poly1305.Field32xN.Lemmas0.lemma_mul5_distr_l", "FStar.Math.Lemmas.paren_mul_right", "Hacl.Poly1305.Field32xN.Lemmas0.smul_mod_lemma", "Hacl.Spec.Poly1305.Field32xN.tup64_5", "Hacl.Spec.Poly1305.Field32xN.as_tup64_i", "FStar.Seq.Base.index", "Lib.Sequence.to_seq", "Hacl.Spec.Poly1305.Field32xN.uint64xN_v", "Hacl.Spec.Poly1305.Field32xN.felem_wide5", "Hacl.Spec.Poly1305.Field32xN.smul_felem5" ]
[]
false
false
true
false
false
let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i =
let o = smul_felem5 #w u1 f2 in let m20, m21, m22, m23, m24 = m2 in let vu1 = (uint64xN_v u1).[ i ] in let tf20, tf21, tf22, tf23, tf24 = as_tup64_i f2 i in let to0, to1, to2, to3, to4 = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[ i ] == vu1 * v tf20 + (vu1 * v tf21) * pow26 + (vu1 * v tf22) * pow52 + (vu1 * v tf23) * pow78 + (vu1 * v tf24) * pow104); calc ( == ) { vu1 * (fas_nat5 f2).[ i ]; ( == ) { () } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); ( == ) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104) } vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); ( == ) { (FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104) } vu1 * v tf20 + (vu1 * v tf21) * pow26 + (vu1 * v tf22) * pow52 + (vu1 * v tf23) * pow78 + (vu1 * v tf24) * pow104; }; assert (vu1 * (fas_nat5 f2).[ i ] == (fas_nat5 o).[ i ])
false
FStar.Endianness.fsti
FStar.Endianness.be_of_uint64
val be_of_uint64 (x: UInt64.t) : b: bytes{S.length b = 8}
val be_of_uint64 (x: UInt64.t) : b: bytes{S.length b = 8}
let be_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_be 8 (UInt64.v x)
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 172, "start_col": 0, "start_line": 171 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x) let uint64_of_le (b: bytes { S.length b = 8 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n let le_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_le 8 (UInt64.v x) let uint64_of_be (b: bytes { S.length b = 8 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt64.uint_to_t n
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt64.t -> b: FStar.Endianness.bytes{FStar.Seq.Base.length b = 8}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.Endianness.n_to_be", "FStar.UInt64.v", "FStar.Endianness.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t" ]
[]
false
false
false
false
false
let be_of_uint64 (x: UInt64.t) : b: bytes{S.length b = 8} =
n_to_be 8 (UInt64.v x)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.b2p
val b2p (b: bool) : prop
val b2p (b: bool) : prop
let b2p (b:bool) : prop = b == true
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 13, "end_line": 140, "start_col": 0, "start_line": 138 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Prims.bool -> Prims.prop
Prims.Tot
[ "total" ]
[]
[ "Prims.bool", "Prims.eq2", "Prims.prop" ]
[]
false
false
false
true
true
let b2p (b: bool) : prop =
b == true
false
FStar.SizeT.fsti
FStar.SizeT.mod_spec
val mod_spec (a: nat{fits a}) (b: nat{fits b /\ b <> 0}) : GTot (n: nat{fits n})
val mod_spec (a: nat{fits a}) (b: nat{fits b /\ b <> 0}) : GTot (n: nat{fits n})
let mod_spec (a:nat{fits a}) (b:nat{fits b /\ b <> 0}) : GTot (n:nat{fits n}) = let open FStar.Mul in let res = a - ((a/b) * b) in fits_lte res a; res
{ "file_name": "ulib/FStar.SizeT.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 129, "start_col": 0, "start_line": 125 }
module FStar.SizeT open FStar.Mul module U16 = FStar.UInt16 module U32 = FStar.UInt32 module U64 = FStar.UInt64 val t : eqtype val fits (x: nat) : Tot prop /// According to the C standard, "the bit width of t is not less than 16 since c99" /// (https://en.cppreference.com/w/c/types/size_t) val fits_at_least_16 (x:nat) : Lemma (requires x < pow2 16) (ensures fits x) [SMTPat (fits x)] [@@noextract_to "krml"] val v (x: t) : Pure nat (requires True) (ensures (fun y -> fits y)) /// We therefore offer two functions to create a t value. /// Any value that fits in a uint_16 can be cast directly to t /// Any value that might not fit in a uint_16 needs to satisfy the `fits_u32` /// or `fits_u64` predicates. These predicates can only be introduced through a /// stateful function (currently in Steel.ST.HigherArray), which will be extracted /// to a static_assert by krml val uint_to_t (x: nat) : Pure t (requires (fits x)) (ensures (fun y -> v y == x)) /// v and uint_to_t are inverses val size_v_inj (x: t) : Lemma (ensures uint_to_t (v x) == x) [SMTPat (v x)] val size_uint_to_t_inj (x: nat) : Lemma (requires fits x) (ensures v (uint_to_t x) == x) [SMTPat (uint_to_t x)] val fits_u32 : prop val fits_u64 : prop val fits_u64_implies_fits_32 (_:unit) : Lemma (requires fits_u64) (ensures fits_u32) val fits_u32_implies_fits (x:nat) : Lemma (requires fits_u32 /\ x < pow2 32) (ensures fits x) val fits_u64_implies_fits (x:nat) : Lemma (requires fits_u64 /\ x < pow2 64) (ensures fits x) /// Creates a size_t when given a uint32 literal. Note, this will not /// extract if [x] is not a literal (e.g., 12ul). If you want to do a /// cast, see `uint32_to_sizet` below noextract inline_for_extraction val of_u32 (x: U32.t) : Pure t (requires fits_u32) (ensures (fun y -> v y == U32.v x)) /// Creates a size_t when given a uint64 literal. Note, this will not /// extract if [x] is not a literal (e.g., 12uL). If you want to do a /// cast, see `uint64_to_sizet` below noextract inline_for_extraction val of_u64 (x: U64.t) : Pure t (requires fits_u64) (ensures (fun y -> v y == U64.v x)) val uint16_to_sizet (x:U16.t) : Pure t (requires True) (ensures fun y -> v y == U16.v x) val uint32_to_sizet (x:U32.t) : Pure t (requires fits_u32) (ensures fun y -> v y == U32.v x) val uint64_to_sizet (x:U64.t) : Pure t (requires fits_u64) (ensures fun y -> v y == U64.v x) val sizet_to_uint32 (x:t) : Pure U32.t (requires True) (ensures fun y -> U32.v y == v x % pow2 32) val fits_lte (x y: nat) : Lemma (requires (x <= y /\ fits y)) (ensures (fits x)) [SMTPat (fits x); SMTPat (fits y)] (** Non-overflowing arithmetic operations *) val add (x y: t) : Pure t (requires (fits (v x + v y))) (ensures (fun z -> v z == v x + v y)) val sub (x y: t) : Pure t (requires (v x >= v y)) (ensures (fun z -> v z == v x - v y)) val mul (x y: t) : Pure t (requires (fits (v x * v y))) (ensures (fun z -> v z == v x * v y)) (** Euclidean division of [a] and [b], with [b] non-zero *) val div (a:t) (b:t{v b <> 0}) : Pure t (requires (True)) (ensures (fun c -> v a / v b = v c)) (** Modulo specification, similar to FStar.UInt.mod *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "FStar.SizeT.fsti" }
[ { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat{FStar.SizeT.fits a} -> b: Prims.nat{FStar.SizeT.fits b /\ b <> 0} -> Prims.GTot (n: Prims.nat{FStar.SizeT.fits n})
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.nat", "FStar.SizeT.fits", "Prims.l_and", "Prims.b2t", "Prims.op_disEquality", "Prims.int", "Prims.unit", "FStar.SizeT.fits_lte", "Prims.op_Subtraction", "FStar.Mul.op_Star", "Prims.op_Division" ]
[]
false
false
false
false
false
let mod_spec (a: nat{fits a}) (b: nat{fits b /\ b <> 0}) : GTot (n: nat{fits n}) =
let open FStar.Mul in let res = a - ((a / b) * b) in fits_lte res a; res
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.anchored
val anchored : anchors: Steel.FractionalAnchoredPreorder.anchor_rel p -> pv: (Steel.FractionalAnchoredPreorder.permission v * Steel.Preorder.vhist p) -> Prims.logical
let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 72, "end_line": 116, "start_col": 0, "start_line": 114 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v].
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
anchors: Steel.FractionalAnchoredPreorder.anchor_rel p -> pv: (Steel.FractionalAnchoredPreorder.permission v * Steel.Preorder.vhist p) -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "FStar.Pervasives.Native.tuple2", "Steel.FractionalAnchoredPreorder.permission", "Steel.Preorder.vhist", "Prims.l_imp", "Prims.b2t", "Steel.FractionalAnchoredPreorder.has_anchor", "FStar.Pervasives.Native.fst", "Steel.FractionalAnchoredPreorder.anchor_of", "Steel.Preorder.curval", "FStar.Pervasives.Native.snd", "Prims.logical" ]
[]
false
false
false
false
true
let anchored (#v: Type) (#p: preorder v) (anchors: anchor_rel p) (pv: (permission v & vhist p)) =
has_anchor (fst pv) ==> (anchor_of (fst pv)) `anchors` (curval (snd pv))
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.permission_composable
val permission_composable (#v: _) (p0 p1: permission v) : prop
val permission_composable (#v: _) (p0 p1: permission v) : prop
let permission_composable #v (p0 p1 : permission v) : prop = let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ // some of fracs can't exceed 1 not (Some? s0 && Some? s1)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 30, "end_line": 164, "start_col": 0, "start_line": 159 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm) /// Composing them sums the permissions let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1)
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p0: Steel.FractionalAnchoredPreorder.permission v -> p1: Steel.FractionalAnchoredPreorder.permission v -> Prims.prop
Prims.Tot
[ "total" ]
[]
[ "Steel.FractionalAnchoredPreorder.permission", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "Prims.l_and", "Steel.FractionalAnchoredPreorder.perm_opt_composable", "Prims.b2t", "Prims.op_Negation", "Prims.op_AmpAmp", "FStar.Pervasives.Native.uu___is_Some", "Prims.prop" ]
[]
false
false
false
true
true
let permission_composable #v (p0: permission v) (p1: permission v) : prop =
let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ not (Some? s0 && Some? s1)
false
FStar.Endianness.fsti
FStar.Endianness.be_to_n_zeros
val be_to_n_zeros (s: bytes) : Lemma (requires forall (i: nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures be_to_n s == 0) (decreases (Seq.length s))
val be_to_n_zeros (s: bytes) : Lemma (requires forall (i: nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures be_to_n s == 0) (decreases (Seq.length s))
let rec be_to_n_zeros (s:bytes) : Lemma (requires forall (i:nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures be_to_n s == 0) (decreases (Seq.length s)) = reveal_be_to_n s; if Seq.length s = 0 then () else be_to_n_zeros (Seq.slice s 0 (Seq.length s - 1))
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 364, "start_col": 0, "start_line": 356 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x) let uint64_of_le (b: bytes { S.length b = 8 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n let le_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_le 8 (UInt64.v x) let uint64_of_be (b: bytes { S.length b = 8 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt64.uint_to_t n let be_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_be 8 (UInt64.v x) /// Lifting {le,be}_to_n / n_to_{le,be} to sequences /// ------------------------------------------------ /// /// TODO: 16-bit (but is it really needed?) /// TODO: should these be specializations of generic functions that chop on /// N-byte boundaries, and operate on bounded nats instead of uints? val seq_uint32_of_le (l: nat) (b: bytes{ S.length b = 4 * l }): s:S.seq UInt32.t { S.length s = l } val le_of_seq_uint32 (s: S.seq UInt32.t): Tot (b:bytes { S.length b = 4 * S.length s }) (decreases (S.length s)) val seq_uint32_of_be (l: nat) (b: bytes{ S.length b = 4 * l }): s:S.seq UInt32.t { S.length s = l } val be_of_seq_uint32 (s: S.seq UInt32.t): Tot (b:bytes { S.length b = 4 * S.length s }) (decreases (S.length s)) val seq_uint64_of_le (l: nat) (b: bytes{ S.length b = 8 * l }): s:S.seq UInt64.t { S.length s = l } val le_of_seq_uint64 (s: S.seq UInt64.t): Tot (b:bytes { S.length b = 8 * S.length s }) (decreases (S.length s)) val seq_uint64_of_be (l: nat) (b: bytes{ S.length b = 8 * l }): s:S.seq UInt64.t { S.length s = l } val be_of_seq_uint64 (s: S.seq UInt64.t): Tot (b:bytes { S.length b = 8 * S.length s }) (decreases (S.length s)) /// Complete specification of the combinators above, relating them to {le,be}_to_ / n_to_{le,be} /// -------------------------------------------------------------------------------------------- val offset_uint32_be (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 4 * n /\ i < n)) (ensures ( S.index (seq_uint32_of_be n b) i == uint32_of_be (S.slice b (4 * i) (4 * i + 4)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint32_of_be n b) i) ] val offset_uint32_le (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 4 * n /\ i < n)) (ensures ( S.index (seq_uint32_of_le n b) i == uint32_of_le (S.slice b (4 * i) (4 * i + 4)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint32_of_le n b) i) ] val offset_uint64_be (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 8 * n /\ i < n)) (ensures ( S.index (seq_uint64_of_be n b) i == uint64_of_be (S.slice b (8 * i) (8 * i + 8)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint64_of_be n b) i) ] val offset_uint64_le (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 8 * n /\ i < n)) (ensures ( S.index (seq_uint64_of_le n b) i == uint64_of_le (S.slice b (8 * i) (8 * i + 8)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint64_of_le n b) i) ] /// Reasoning about appending such sequences /// ---------------------------------------- /// /// TODO: this is fairly incomplete /// TODO: the *_base cases seem ad-hoc and derivable trivially from offset above; why have them? val be_of_seq_uint32_base (s1: S.seq U32.t) (s2: S.seq U8.t): Lemma (requires ( S.length s1 = 1 /\ S.length s2 = 4 /\ be_to_n s2 = U32.v (S.index s1 0))) (ensures (S.equal s2 (be_of_seq_uint32 s1))) [ SMTPat (be_to_n s2); SMTPat (U32.v (S.index s1 0)) ] val le_of_seq_uint32_base (s1: S.seq U32.t) (s2: S.seq U8.t): Lemma (requires ( S.length s1 = 1 /\ S.length s2 = 4 /\ le_to_n s2 = U32.v (S.index s1 0))) (ensures (S.equal s2 (le_of_seq_uint32 s1))) [ SMTPat (le_to_n s2); SMTPat (U32.v (S.index s1 0)) ] val be_of_seq_uint64_base (s1: S.seq U64.t) (s2: S.seq U8.t): Lemma (requires ( S.length s1 = 1 /\ S.length s2 = 8 /\ be_to_n s2 = U64.v (S.index s1 0))) (ensures (S.equal s2 (be_of_seq_uint64 s1))) [ SMTPat (be_to_n s2); SMTPat (U64.v (S.index s1 0)) ] val be_of_seq_uint32_append (s1 s2: S.seq U32.t): Lemma (ensures ( S.equal (be_of_seq_uint32 (S.append s1 s2)) (S.append (be_of_seq_uint32 s1) (be_of_seq_uint32 s2)))) (decreases ( S.length s1)) [ SMTPat (S.append (be_of_seq_uint32 s1) (be_of_seq_uint32 s2)) ] val le_of_seq_uint32_append (s1 s2: S.seq U32.t): Lemma (ensures ( S.equal (le_of_seq_uint32 (S.append s1 s2)) (S.append (le_of_seq_uint32 s1) (le_of_seq_uint32 s2)))) (decreases ( S.length s1)) [ SMTPat (S.append (le_of_seq_uint32 s1) (le_of_seq_uint32 s2)) ] val be_of_seq_uint64_append (s1 s2: S.seq U64.t): Lemma (ensures ( S.equal (be_of_seq_uint64 (S.append s1 s2)) (S.append (be_of_seq_uint64 s1) (be_of_seq_uint64 s2)))) (decreases ( S.length s1)) [ SMTPat (S.append (be_of_seq_uint64 s1) (be_of_seq_uint64 s2)) ] /// Roundtripping /// ------------- /// /// TODO: also incomplete val seq_uint32_of_be_be_of_seq_uint32 (n: nat) (s: S.seq U32.t) : Lemma (requires (n == S.length s)) (ensures (seq_uint32_of_be n (be_of_seq_uint32 s) `S.equal` s)) (decreases n) [SMTPat (seq_uint32_of_be n (be_of_seq_uint32 s))] val be_of_seq_uint32_seq_uint32_of_be (n: nat) (s: S.seq U8.t) : Lemma (requires (4 * n == S.length s)) (ensures (be_of_seq_uint32 (seq_uint32_of_be n s) `S.equal` s)) (decreases n) [SMTPat (be_of_seq_uint32 (seq_uint32_of_be n s))] /// Reasoning about slicing such sequences /// -------------------------------------- /// /// (Needs SMTPats above for roundtripping in their proof, hence why they're at the end.) val slice_seq_uint32_of_be (n: nat) (s: S.seq U8.t) (lo: nat) (hi: nat) : Lemma (requires (4 * n == S.length s /\ lo <= hi /\ hi <= n)) (ensures (S.slice (seq_uint32_of_be n s) lo hi) `S.equal` seq_uint32_of_be (hi - lo) (S.slice s (4 * lo) (4 * hi))) val be_of_seq_uint32_slice (s: S.seq U32.t) (lo: nat) (hi: nat) : Lemma (requires (lo <= hi /\ hi <= S.length s)) (ensures (be_of_seq_uint32 (S.slice s lo hi) `S.equal` S.slice (be_of_seq_uint32 s) (4 * lo) (4 * hi))) /// Some reasoning about zero bytes let rec le_to_n_zeros (s:bytes) : Lemma (requires forall (i:nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures le_to_n s == 0) (decreases (Seq.length s)) = reveal_le_to_n s; if Seq.length s = 0 then () else le_to_n_zeros (Seq.tail s)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.Endianness.bytes -> FStar.Pervasives.Lemma (requires forall (i: Prims.nat). i < FStar.Seq.Base.length s ==> FStar.Seq.Base.index s i == 0uy) (ensures FStar.Endianness.be_to_n s == 0) (decreases FStar.Seq.Base.length s)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "FStar.Endianness.bytes", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t", "Prims.bool", "FStar.Endianness.be_to_n_zeros", "FStar.Seq.Base.slice", "Prims.op_Subtraction", "Prims.unit", "FStar.Endianness.reveal_be_to_n", "Prims.l_Forall", "Prims.nat", "Prims.l_imp", "Prims.b2t", "Prims.op_LessThan", "Prims.eq2", "FStar.Seq.Base.index", "FStar.UInt8.__uint_to_t", "Prims.squash", "FStar.Endianness.be_to_n", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec be_to_n_zeros (s: bytes) : Lemma (requires forall (i: nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures be_to_n s == 0) (decreases (Seq.length s)) =
reveal_be_to_n s; if Seq.length s = 0 then () else be_to_n_zeros (Seq.slice s 0 (Seq.length s - 1))
false
FStar.Endianness.fsti
FStar.Endianness.le_to_n_zeros
val le_to_n_zeros (s: bytes) : Lemma (requires forall (i: nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures le_to_n s == 0) (decreases (Seq.length s))
val le_to_n_zeros (s: bytes) : Lemma (requires forall (i: nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures le_to_n s == 0) (decreases (Seq.length s))
let rec le_to_n_zeros (s:bytes) : Lemma (requires forall (i:nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures le_to_n s == 0) (decreases (Seq.length s)) = reveal_le_to_n s; if Seq.length s = 0 then () else le_to_n_zeros (Seq.tail s)
{ "file_name": "ulib/FStar.Endianness.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 35, "end_line": 354, "start_col": 0, "start_line": 346 }
module FStar.Endianness /// A library of lemmas for reasoning about sequences of machine integers and /// their (little|big)-endian representation as a sequence of bytes. /// /// The functions in this module aim to be as generic as possible, in order to /// facilitate compatibility with: /// - Vale's model of machine integers (nat64 et al.), which does not rely on /// FStar's machine integers /// - HACL*'s Lib.IntTypes module, which exposes a universal indexed integer /// type but uses F* machine integers under the hood. /// /// To achieve maximum compatibility, we try to state most lemmas using nat /// rather than UIntX. /// /// To limit context pollution, the definitions of the recursive functions are /// abstract; please add lemmas as you see fit. In extreme cases, ``friend``'ing /// might be de rigueur. /// /// .. note:: /// /// This module supersedes the poorly-named ``FStar.Krml.Endianness``. module U8 = FStar.UInt8 module U32 = FStar.UInt32 module U64 = FStar.UInt64 module Math = FStar.Math.Lemmas module S = FStar.Seq [@@ noextract_to "krml"] type bytes = S.seq U8.t open FStar.Mul /// Definition of little and big-endianness /// --------------------------------------- /// /// This is our spec, to be audited. From bytes to nat. /// lt_to_n interprets a byte sequence as a little-endian natural number val le_to_n : b:bytes -> Tot nat /// be_to_n interprets a byte sequence as a big-endian natural number val be_to_n : b:bytes -> Tot nat /// Induction for le_to_n and be_to_n val reveal_le_to_n (b:bytes) : Lemma (le_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.head b) + pow2 8 * le_to_n (S.tail b))) val reveal_be_to_n (b:bytes) : Lemma (be_to_n b == (match Seq.length b with | 0 -> 0 | _ -> U8.v (S.last b) + pow2 8 * be_to_n (S.slice b 0 (S.length b - 1)))) val lemma_le_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (le_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) val lemma_be_to_n_is_bounded: b:bytes -> Lemma (requires True) (ensures (be_to_n b < pow2 (8 * Seq.length b))) (decreases (Seq.length b)) /// Inverse operations /// ------------------ /// /// From nat to bytes, and their functional correctness. /// n_to_le encodes a number as a little-endian byte sequence of a fixed, /// sufficiently large length. val n_to_le : len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == le_to_n b}) (decreases len) /// n_to_be encodes a numbers as a big-endian byte sequence of a fixed, /// sufficiently large length val n_to_be: len:nat -> n:nat{n < pow2 (8 * len)} -> Tot (b:bytes{S.length b == len /\ n == be_to_n b}) (decreases len) /// Injectivity /// ----------- val n_to_le_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})): Lemma (requires (n_to_le len n1 == n_to_le len n2)) (ensures (n1 == n2)) val n_to_be_inj (len: nat) (n1 n2: (n:nat{n < pow2 (8 * len)})) : Lemma (requires (n_to_be len n1 == n_to_be len n2)) (ensures (n1 == n2)) val be_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ be_to_n b1 == be_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) val le_to_n_inj (b1 b2: Seq.seq U8.t) : Lemma (requires (Seq.length b1 == Seq.length b2 /\ le_to_n b1 == le_to_n b2)) (ensures (Seq.equal b1 b2)) (decreases (Seq.length b1)) /// Roundtripping /// ------------- val n_to_be_be_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( be_to_n s < pow2 (8 * len) /\ n_to_be len (be_to_n s) == s )) [SMTPat (n_to_be len (be_to_n s))] val n_to_le_le_to_n (len: nat) (s: Seq.seq U8.t) : Lemma (requires (Seq.length s == len)) (ensures ( le_to_n s < pow2 (8 * len) /\ n_to_le len (le_to_n s) == s )) [SMTPat (n_to_le len (le_to_n s))] /// Specializations for F* machine integers /// --------------------------------------- /// /// These are useful because they take care of calling the right ``*_is_bounded`` lemmas. let uint32_of_le (b: bytes { S.length b = 4 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt32.uint_to_t n let le_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_le 4 (UInt32.v x) let uint32_of_be (b: bytes { S.length b = 4 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt32.uint_to_t n let be_of_uint32 (x: UInt32.t): b:bytes{ S.length b = 4 } = n_to_be 4 (UInt32.v x) let uint64_of_le (b: bytes { S.length b = 8 }) = let n = le_to_n b in lemma_le_to_n_is_bounded b; UInt64.uint_to_t n let le_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_le 8 (UInt64.v x) let uint64_of_be (b: bytes { S.length b = 8 }) = let n = be_to_n b in lemma_be_to_n_is_bounded b; UInt64.uint_to_t n let be_of_uint64 (x: UInt64.t): b:bytes{ S.length b = 8 } = n_to_be 8 (UInt64.v x) /// Lifting {le,be}_to_n / n_to_{le,be} to sequences /// ------------------------------------------------ /// /// TODO: 16-bit (but is it really needed?) /// TODO: should these be specializations of generic functions that chop on /// N-byte boundaries, and operate on bounded nats instead of uints? val seq_uint32_of_le (l: nat) (b: bytes{ S.length b = 4 * l }): s:S.seq UInt32.t { S.length s = l } val le_of_seq_uint32 (s: S.seq UInt32.t): Tot (b:bytes { S.length b = 4 * S.length s }) (decreases (S.length s)) val seq_uint32_of_be (l: nat) (b: bytes{ S.length b = 4 * l }): s:S.seq UInt32.t { S.length s = l } val be_of_seq_uint32 (s: S.seq UInt32.t): Tot (b:bytes { S.length b = 4 * S.length s }) (decreases (S.length s)) val seq_uint64_of_le (l: nat) (b: bytes{ S.length b = 8 * l }): s:S.seq UInt64.t { S.length s = l } val le_of_seq_uint64 (s: S.seq UInt64.t): Tot (b:bytes { S.length b = 8 * S.length s }) (decreases (S.length s)) val seq_uint64_of_be (l: nat) (b: bytes{ S.length b = 8 * l }): s:S.seq UInt64.t { S.length s = l } val be_of_seq_uint64 (s: S.seq UInt64.t): Tot (b:bytes { S.length b = 8 * S.length s }) (decreases (S.length s)) /// Complete specification of the combinators above, relating them to {le,be}_to_ / n_to_{le,be} /// -------------------------------------------------------------------------------------------- val offset_uint32_be (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 4 * n /\ i < n)) (ensures ( S.index (seq_uint32_of_be n b) i == uint32_of_be (S.slice b (4 * i) (4 * i + 4)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint32_of_be n b) i) ] val offset_uint32_le (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 4 * n /\ i < n)) (ensures ( S.index (seq_uint32_of_le n b) i == uint32_of_le (S.slice b (4 * i) (4 * i + 4)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint32_of_le n b) i) ] val offset_uint64_be (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 8 * n /\ i < n)) (ensures ( S.index (seq_uint64_of_be n b) i == uint64_of_be (S.slice b (8 * i) (8 * i + 8)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint64_of_be n b) i) ] val offset_uint64_le (b: bytes) (n: nat) (i: nat): Lemma (requires ( S.length b = 8 * n /\ i < n)) (ensures ( S.index (seq_uint64_of_le n b) i == uint64_of_le (S.slice b (8 * i) (8 * i + 8)))) (decreases ( S.length b)) [ SMTPat (S.index (seq_uint64_of_le n b) i) ] /// Reasoning about appending such sequences /// ---------------------------------------- /// /// TODO: this is fairly incomplete /// TODO: the *_base cases seem ad-hoc and derivable trivially from offset above; why have them? val be_of_seq_uint32_base (s1: S.seq U32.t) (s2: S.seq U8.t): Lemma (requires ( S.length s1 = 1 /\ S.length s2 = 4 /\ be_to_n s2 = U32.v (S.index s1 0))) (ensures (S.equal s2 (be_of_seq_uint32 s1))) [ SMTPat (be_to_n s2); SMTPat (U32.v (S.index s1 0)) ] val le_of_seq_uint32_base (s1: S.seq U32.t) (s2: S.seq U8.t): Lemma (requires ( S.length s1 = 1 /\ S.length s2 = 4 /\ le_to_n s2 = U32.v (S.index s1 0))) (ensures (S.equal s2 (le_of_seq_uint32 s1))) [ SMTPat (le_to_n s2); SMTPat (U32.v (S.index s1 0)) ] val be_of_seq_uint64_base (s1: S.seq U64.t) (s2: S.seq U8.t): Lemma (requires ( S.length s1 = 1 /\ S.length s2 = 8 /\ be_to_n s2 = U64.v (S.index s1 0))) (ensures (S.equal s2 (be_of_seq_uint64 s1))) [ SMTPat (be_to_n s2); SMTPat (U64.v (S.index s1 0)) ] val be_of_seq_uint32_append (s1 s2: S.seq U32.t): Lemma (ensures ( S.equal (be_of_seq_uint32 (S.append s1 s2)) (S.append (be_of_seq_uint32 s1) (be_of_seq_uint32 s2)))) (decreases ( S.length s1)) [ SMTPat (S.append (be_of_seq_uint32 s1) (be_of_seq_uint32 s2)) ] val le_of_seq_uint32_append (s1 s2: S.seq U32.t): Lemma (ensures ( S.equal (le_of_seq_uint32 (S.append s1 s2)) (S.append (le_of_seq_uint32 s1) (le_of_seq_uint32 s2)))) (decreases ( S.length s1)) [ SMTPat (S.append (le_of_seq_uint32 s1) (le_of_seq_uint32 s2)) ] val be_of_seq_uint64_append (s1 s2: S.seq U64.t): Lemma (ensures ( S.equal (be_of_seq_uint64 (S.append s1 s2)) (S.append (be_of_seq_uint64 s1) (be_of_seq_uint64 s2)))) (decreases ( S.length s1)) [ SMTPat (S.append (be_of_seq_uint64 s1) (be_of_seq_uint64 s2)) ] /// Roundtripping /// ------------- /// /// TODO: also incomplete val seq_uint32_of_be_be_of_seq_uint32 (n: nat) (s: S.seq U32.t) : Lemma (requires (n == S.length s)) (ensures (seq_uint32_of_be n (be_of_seq_uint32 s) `S.equal` s)) (decreases n) [SMTPat (seq_uint32_of_be n (be_of_seq_uint32 s))] val be_of_seq_uint32_seq_uint32_of_be (n: nat) (s: S.seq U8.t) : Lemma (requires (4 * n == S.length s)) (ensures (be_of_seq_uint32 (seq_uint32_of_be n s) `S.equal` s)) (decreases n) [SMTPat (be_of_seq_uint32 (seq_uint32_of_be n s))] /// Reasoning about slicing such sequences /// -------------------------------------- /// /// (Needs SMTPats above for roundtripping in their proof, hence why they're at the end.) val slice_seq_uint32_of_be (n: nat) (s: S.seq U8.t) (lo: nat) (hi: nat) : Lemma (requires (4 * n == S.length s /\ lo <= hi /\ hi <= n)) (ensures (S.slice (seq_uint32_of_be n s) lo hi) `S.equal` seq_uint32_of_be (hi - lo) (S.slice s (4 * lo) (4 * hi))) val be_of_seq_uint32_slice (s: S.seq U32.t) (lo: nat) (hi: nat) : Lemma (requires (lo <= hi /\ hi <= S.length s)) (ensures (be_of_seq_uint32 (S.slice s lo hi) `S.equal` S.slice (be_of_seq_uint32 s) (4 * lo) (4 * hi))) /// Some reasoning about zero bytes
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked" ], "interface_file": false, "source_file": "FStar.Endianness.fsti" }
[ { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.Endianness.bytes -> FStar.Pervasives.Lemma (requires forall (i: Prims.nat). i < FStar.Seq.Base.length s ==> FStar.Seq.Base.index s i == 0uy) (ensures FStar.Endianness.le_to_n s == 0) (decreases FStar.Seq.Base.length s)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "FStar.Endianness.bytes", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.UInt8.t", "Prims.bool", "FStar.Endianness.le_to_n_zeros", "FStar.Seq.Properties.tail", "Prims.unit", "FStar.Endianness.reveal_le_to_n", "Prims.l_Forall", "Prims.nat", "Prims.l_imp", "Prims.b2t", "Prims.op_LessThan", "Prims.eq2", "FStar.Seq.Base.index", "FStar.UInt8.__uint_to_t", "Prims.squash", "FStar.Endianness.le_to_n", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec le_to_n_zeros (s: bytes) : Lemma (requires forall (i: nat). i < Seq.length s ==> Seq.index s i == 0uy) (ensures le_to_n s == 0) (decreases (Seq.length s)) =
reveal_le_to_n s; if Seq.length s = 0 then () else le_to_n_zeros (Seq.tail s)
false
Hacl.Poly1305.Field32xN.Lemmas0.fst
Hacl.Poly1305.Field32xN.Lemmas0.smul_add_felem5_fits_lemma1
val smul_add_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> acc1:uint64xN w{felem_wide_fits1 acc1 m3} -> Lemma (felem_wide_fits1 (vec_add_mod acc1 (vec_mul_mod f2 u1)) (m3 + m1 * m2))
val smul_add_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> acc1:uint64xN w{felem_wide_fits1 acc1 m3} -> Lemma (felem_wide_fits1 (vec_add_mod acc1 (vec_mul_mod f2 u1)) (m3 + m1 * m2))
let smul_add_felem5_fits_lemma1 #w #m1 #m2 #m3 u1 f2 acc1 = match w with | 1 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0 | 2 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 1 | 4 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 1; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 2; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 3
{ "file_name": "code/poly1305/Hacl.Poly1305.Field32xN.Lemmas0.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 60, "end_line": 317, "start_col": 0, "start_line": 306 }
module Hacl.Poly1305.Field32xN.Lemmas0 open Lib.IntTypes open Lib.IntVector open Lib.Sequence open FStar.Mul open FStar.Calc open Hacl.Spec.Poly1305.Vec include Hacl.Spec.Poly1305.Field32xN #reset-options "--z3rlimit 50 --using_facts_from '* -FStar.Seq' --max_fuel 0 --max_ifuel 0" val lemma_prime: unit -> Lemma (pow2 130 % prime = 5) let lemma_prime () = assert_norm (pow2 130 % prime = 5 % prime); assert_norm (5 < prime); FStar.Math.Lemmas.modulo_lemma 5 prime val lemma_mult_le: a:nat -> b:nat -> c:nat -> d:nat -> Lemma (requires a <= b /\ c <= d) (ensures a * c <= b * d) let lemma_mult_le a b c d = () val lemma_mul5_distr_l: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma (a * (b + c + d + e + f) == a * b + a * c + a * d + a * e + a * f) let lemma_mul5_distr_l a b c d e f = () val lemma_mul5_distr_r: a:nat -> b:nat -> c:nat -> d:nat -> e:nat -> f:nat -> Lemma ((a + b + c + d + e) * f == a * f + b * f + c * f + d * f + e * f) let lemma_mul5_distr_r a b c d e f = () val smul_mod_lemma: #m1:scale32 -> #m2:scale32 -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> Lemma (a * b % pow2 64 == a * b) let smul_mod_lemma #m1 #m2 a b = lemma_mult_le a (m1 * max26) b (m2 * max26); assert (a * b <= m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (a * b) (pow2 64) val smul_add_mod_lemma: #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> a:nat{a <= m1 * max26} -> b:nat{b <= m2 * max26} -> c:nat{c <= m3 * max26 * max26} -> Lemma ((c + a * b % pow2 64) % pow2 64 == c + a * b) let smul_add_mod_lemma #m1 #m2 #m3 a b c = assert_norm ((m3 + m1 * m2) * max26 * max26 < pow2 64); lemma_mult_le a (m1 * max26) b (m2 * max26); assert (c + a * b <= m3 * max26 * max26 + m1 * m2 * max26 * max26); FStar.Math.Lemmas.modulo_lemma (c + a * b) (pow2 64) val add5_lemma1: ma:scale64 -> mb:scale64 -> a:uint64 -> b:uint64 -> Lemma (requires v a <= ma * max26 /\ v b <= mb * max26 /\ ma + mb <= 64) (ensures v (a +. b) == v a + v b /\ v (a +. b) <= (ma + mb) * max26) let add5_lemma1 ma mb a b = assert (v a + v b <= (ma + mb) * max26); Math.Lemmas.lemma_mult_le_right max26 (ma + mb) 64; assert (v a + v b <= 64 * max26); assert_norm (64 * max26 < pow2 32); Math.Lemmas.small_mod (v a + v b) (pow2 32) #set-options "--ifuel 1" val fadd5_eval_lemma_i: #w:lanes -> f1:felem5 w{felem_fits5 f1 (2,2,2,2,2)} -> f2:felem5 w{felem_fits5 f2 (1,1,1,1,1)} -> i:nat{i < w} -> Lemma ((feval5 (fadd5 f1 f2)).[i] == pfadd (feval5 f1).[i] (feval5 f2).[i]) let fadd5_eval_lemma_i #w f1 f2 i = let o = fadd5 f1 f2 in let (f10, f11, f12, f13, f14) = as_tup64_i f1 i in let (f20, f21, f22, f23, f24) = as_tup64_i f2 i in let (o0, o1, o2, o3, o4) = as_tup64_i o i in add5_lemma1 2 1 f10 f20; add5_lemma1 2 1 f11 f21; add5_lemma1 2 1 f12 f22; add5_lemma1 2 1 f13 f23; add5_lemma1 2 1 f14 f24; assert (as_nat5 (o0, o1, o2, o3, o4) == as_nat5 (f10, f11, f12, f13, f14) + as_nat5 (f20, f21, f22, f23, f24)); FStar.Math.Lemmas.lemma_mod_plus_distr_l (as_nat5 (f10, f11, f12, f13, f14)) (as_nat5 (f20, f21, f22, f23, f24)) prime; FStar.Math.Lemmas.lemma_mod_plus_distr_r (as_nat5 (f10, f11, f12, f13, f14) % prime) (as_nat5 (f20, f21, f22, f23, f24)) prime val smul_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_mul_mod f2 u1)).[i] <= m1 * m2 * max26 * max26) let smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 i = let o = vec_mul_mod f2 u1 in smul_mod_lemma #m1 #m2 (uint64xN_v u1).[i] (uint64xN_v f2).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26) val smul_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_felem5 #w u1 f2)).[i] == (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2 i = let o = smul_felem5 #w u1 f2 in let (m20, m21, m22, m23, m24) = m2 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_mod_lemma #m1 #m20 vu1 (v tf20); smul_mod_lemma #m1 #m21 vu1 (v tf21); smul_mod_lemma #m1 #m22 vu1 (v tf22); smul_mod_lemma #m1 #m23 vu1 (v tf23); smul_mod_lemma #m1 #m24 vu1 (v tf24); assert ((fas_nat5 o).[i] == vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert (vu1 * (fas_nat5 f2).[i] == (fas_nat5 o).[i]) val smul_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> Lemma (felem_wide_fits1 (vec_mul_mod f2 u1) (m1 * m2)) let smul_felem5_fits_lemma1 #w #m1 #m2 u1 f2 = match w with | 1 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0 | 2 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1 | 4 -> smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 0; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 1; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 2; smul_felem5_fits_lemma_i #w #m1 #m2 u1 f2 3 val smul_felem5_fits_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (felem_wide_fits5 (smul_felem5 #w u1 f2) (m1 *^ m2)) let smul_felem5_fits_lemma #w #m1 #m2 u1 f2 = let (f20, f21, f22, f23, f24) = f2 in let (m20, m21, m22, m23, m24) = m2 in smul_felem5_fits_lemma1 #w #m1 #m20 u1 f20; smul_felem5_fits_lemma1 #w #m1 #m21 u1 f21; smul_felem5_fits_lemma1 #w #m1 #m22 u1 f22; smul_felem5_fits_lemma1 #w #m1 #m23 u1 f23; smul_felem5_fits_lemma1 #w #m1 #m24 u1 f24 val smul_felem5_eval_lemma: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> Lemma (fas_nat5 (smul_felem5 #w u1 f2) == map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2)) let smul_felem5_eval_lemma #w #m1 #m2 u1 f2 = FStar.Classical.forall_intro (smul_felem5_eval_lemma_i #w #m1 #m2 u1 f2); eq_intro (fas_nat5 (smul_felem5 #w u1 f2)) (map2 #nat #nat #nat (fun a b -> a * b) (uint64xN_v u1) (fas_nat5 f2)) val smul_add_felem5_fits_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> acc1:uint64xN w{felem_wide_fits1 acc1 m3} -> i:nat{i < w} -> Lemma ((uint64xN_v (vec_add_mod acc1 (vec_mul_mod f2 u1))).[i] <= (m3 + m1 * m2) * max26 * max26) #push-options "--z3rlimit 200" let smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 i = let o = vec_add_mod acc1 (vec_mul_mod f2 u1) in smul_add_mod_lemma #m1 #m2 #m3 (uint64xN_v u1).[i] (uint64xN_v f2).[i] (uint64xN_v acc1).[i]; assert ((uint64xN_v o).[i] == (uint64xN_v acc1).[i] + (uint64xN_v u1).[i] * (uint64xN_v f2).[i]); lemma_mult_le (uint64xN_v u1).[i] (m1 * max26) (uint64xN_v f2).[i] (m2 * max26); assert ((uint64xN_v o).[i] <= m3 * max26 * max26 + m1 * m2 * max26 * max26) #pop-options val smul_add_felem5_eval_lemma_i: #w:lanes -> #m1:scale32 -> #m2:scale32_5 -> #m3:scale64_5{m3 +* m1 *^ m2 <=* s64x5 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:felem5 w{felem_fits5 f2 m2} -> acc1:felem_wide5 w{felem_wide_fits5 acc1 m3} -> i:nat{i < w} -> Lemma ((fas_nat5 (smul_add_felem5 #w u1 f2 acc1)).[i] == (fas_nat5 acc1).[i] + (uint64xN_v u1).[i] * (fas_nat5 f2).[i]) let smul_add_felem5_eval_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 i = let o = smul_add_felem5 #w u1 f2 acc1 in let (m20, m21, m22, m23, m24) = m2 in let (m30, m31, m32, m33, m34) = m3 in let vu1 = (uint64xN_v u1).[i] in let (tf20, tf21, tf22, tf23, tf24) = as_tup64_i f2 i in let (ta0, ta1, ta2, ta3, ta4) = as_tup64_i acc1 i in let (to0, to1, to2, to3, to4) = as_tup64_i o i in smul_add_mod_lemma #m1 #m20 #m30 vu1 (v tf20) (v ta0); smul_add_mod_lemma #m1 #m21 #m31 vu1 (v tf21) (v ta1); smul_add_mod_lemma #m1 #m22 #m32 vu1 (v tf22) (v ta2); smul_add_mod_lemma #m1 #m23 #m33 vu1 (v tf23) (v ta3); smul_add_mod_lemma #m1 #m24 #m34 vu1 (v tf24) (v ta4); calc (==) { (fas_nat5 o).[i]; (==) { } v ta0 + vu1 * v tf20 + (v ta1 + vu1 * v tf21) * pow26 + (v ta2 + vu1 * v tf22) * pow52 + (v ta3 + vu1 * v tf23) * pow78 + (v ta4 + vu1 * v tf24) * pow104; (==) { FStar.Math.Lemmas.distributivity_add_left (v ta1) (vu1 * v tf21) pow26; FStar.Math.Lemmas.distributivity_add_left (v ta2) (vu1 * v tf22) pow52; FStar.Math.Lemmas.distributivity_add_left (v ta3) (vu1 * v tf23) pow78; FStar.Math.Lemmas.distributivity_add_left (v ta1) (vu1 * v tf24) pow104 } v ta0 + v ta1 * pow26 + v ta2 * pow52 + v ta3 * pow78 + v ta4 * pow104 + vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; (==) { } (fas_nat5 acc1).[i] + vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert ((fas_nat5 o).[i] == (fas_nat5 acc1).[i] + vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104); calc (==) { vu1 * (fas_nat5 f2).[i]; (==) { } vu1 * (v tf20 + v tf21 * pow26 + v tf22 * pow52 + v tf23 * pow78 + v tf24 * pow104); (==) { lemma_mul5_distr_l vu1 (v tf20) (v tf21 * pow26) (v tf22 * pow52) (v tf23 * pow78) (v tf24 * pow104)} vu1 * v tf20 + vu1 * (v tf21 * pow26) + vu1 * (v tf22 * pow52) + vu1 * (v tf23 * pow78) + vu1 * (v tf24 * pow104); (==) { FStar.Math.Lemmas.paren_mul_right vu1 (v tf21) pow26; FStar.Math.Lemmas.paren_mul_right vu1 (v tf22) pow52; FStar.Math.Lemmas.paren_mul_right vu1 (v tf23) pow78; FStar.Math.Lemmas.paren_mul_right vu1 (v tf24) pow104} vu1 * v tf20 + vu1 * v tf21 * pow26 + vu1 * v tf22 * pow52 + vu1 * v tf23 * pow78 + vu1 * v tf24 * pow104; }; assert ((fas_nat5 o).[i] == (fas_nat5 acc1).[i] + vu1 * (fas_nat5 f2).[i]) val smul_add_felem5_fits_lemma1: #w:lanes -> #m1:scale32 -> #m2:scale32 -> #m3:scale64{m3 + m1 * m2 <= 4096} -> u1:uint64xN w{felem_fits1 u1 m1} -> f2:uint64xN w{felem_fits1 f2 m2} -> acc1:uint64xN w{felem_wide_fits1 acc1 m3} -> Lemma (felem_wide_fits1 (vec_add_mod acc1 (vec_mul_mod f2 u1)) (m3 + m1 * m2))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntVector.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Spec.Poly1305.Field32xN.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": false, "source_file": "Hacl.Poly1305.Field32xN.Lemmas0.fst" }
[ { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Calc", "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.IntVector", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
u1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 u1 m1} -> f2: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_fits1 f2 m2} -> acc1: Hacl.Spec.Poly1305.Field32xN.uint64xN w {Hacl.Spec.Poly1305.Field32xN.felem_wide_fits1 acc1 m3} -> FStar.Pervasives.Lemma (ensures Hacl.Spec.Poly1305.Field32xN.felem_wide_fits1 (Lib.IntVector.vec_add_mod acc1 (Lib.IntVector.vec_mul_mod f2 u1)) (m3 + m1 * m2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Hacl.Spec.Poly1305.Field32xN.lanes", "Hacl.Spec.Poly1305.Field32xN.scale32", "Hacl.Spec.Poly1305.Field32xN.scale64", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Mul.op_Star", "Hacl.Spec.Poly1305.Field32xN.uint64xN", "Hacl.Spec.Poly1305.Field32xN.felem_fits1", "Hacl.Spec.Poly1305.Field32xN.felem_wide_fits1", "Hacl.Poly1305.Field32xN.Lemmas0.smul_add_felem5_fits_lemma_i", "Prims.unit" ]
[]
false
false
true
false
false
let smul_add_felem5_fits_lemma1 #w #m1 #m2 #m3 u1 f2 acc1 =
match w with | 1 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0 | 2 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 1 | 4 -> smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 0; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 1; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 2; smul_add_felem5_fits_lemma_i #w #m1 #m2 #m3 u1 f2 acc1 3
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.anchor_of
val anchor_of (#v: _) (p: permission v {has_anchor p}) : v
val anchor_of (#v: _) (p: permission v {has_anchor p}) : v
let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 72, "end_line": 85, "start_col": 0, "start_line": 85 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.FractionalAnchoredPreorder.permission v {Steel.FractionalAnchoredPreorder.has_anchor p} -> v
Prims.Tot
[ "total" ]
[]
[ "Steel.FractionalAnchoredPreorder.permission", "Prims.b2t", "Steel.FractionalAnchoredPreorder.has_anchor", "FStar.Pervasives.Native.__proj__Some__item__v", "FStar.Pervasives.Native.snd", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm" ]
[]
false
false
false
false
false
let anchor_of #v (p: permission v {has_anchor p}) : v =
Some?.v (snd p)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.avalue_perm
val avalue_perm : m: Steel.FractionalAnchoredPreorder.avalue s -> Steel.FractionalAnchoredPreorder.permission v
let avalue_perm (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) = fst m
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 11, "end_line": 379, "start_col": 0, "start_line": 375 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm) /// Composing them sums the permissions let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1) /// Anchored permissions are composable when at most one of them has the anchor set let permission_composable #v (p0 p1 : permission v) : prop = let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ // some of fracs can't exceed 1 not (Some? s0 && Some? s1) // at most one can have an anchor /// Compose permissions component wise, keeping the anchor if set let compose_permissions (#v:_) (p0:permission v) (p1:permission v{permission_composable p0 p1}) : permission v = compose_perm_opt (fst p0) (fst p1), (match snd p0, snd p1 with | None, a | a, None -> a) /// This is the central definition of the PCM --- defining when two /// values are composable, or equivalently, when one thread's /// knowledge of [av0] is compatible with another thread's knowledge /// of [av1] /// /// This definition shows the interplay between fractions and anchors. /// It wasn't obvious to me how to factor this further, e.g., adding /// anchors to preorders and then separately adding fractions to it. let avalue_composable (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (av0 av1: avalue anchors) : prop = let (p0, v0) = av0 in let (p1, v1) = av1 in permission_composable p0 p1 /\ (if not (has_some_ownership p0) && not (has_some_ownership p1) then p_composable _ v0 v1 //neither has ownership, one history is older than the other else if not (has_some_ownership p0) && has_some_ownership p1 then ( if has_nonzero p1 then v1 `extends` v0 //the one with ownership is more recent else ( //this part is the most subtle assert (has_anchor p1); p_composable _ v0 v1 /\ (v0 `extends` v1 ==> //if v0 is a more recent snapshot than v1 //then v0 must still be anchored by v1's anchor anchor_of p1 `anchors` curval v0) ) ) else if has_some_ownership p0 && not (has_some_ownership p1) then ( //symmetric if has_nonzero p0 then v0 `extends` v1 else ( assert (has_anchor p0); p_composable _ v0 v1 /\ (v1 `extends` v0 ==> anchor_of p0 `anchors` curval v1) ) ) else ( assert (has_some_ownership p0 && has_some_ownership p1); if has_nonzero p0 && has_nonzero p1 then v0 == v1 //if both have non-zero perm, then they must both only have read permission and must agree on the value else if has_nonzero p0 && has_anchor p1 then ( assert (not (has_nonzero p1)); //The key part of the anchor semantics: v0 `extends` v1 /\ //v0 has advanceable ownership, so extends anchor_of p1 `anchors` curval v0 //but not beyond what is allowed by s ) else if has_anchor p0 && has_nonzero p1 //symmetric then ( assert (not (has_nonzero p0)); v1 `extends` v0 /\ anchor_of p0 `anchors` curval v1 //v1 extends, but not beyond what is allowed by s ) else (assert false; False) ) ) //exhaustive /// Lifting avalue comosability to knowledge, including the unit let composable #v (#p:preorder v) (#a:anchor_rel p) : symrel (knowledge a) = fun (k0 k1:knowledge a) -> match k0, k1 with | Nothing, _ | _, Nothing -> True | Owns m, Owns m' -> avalue_composable m m' /// Compose avalues by composing permissions and composing the values /// using the underlying preorder PCM let compose_avalue (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0:avalue anchors) (m1:avalue anchors { avalue_composable m0 m1 } ) : avalue anchors = let p0, v0 = m0 in let p1, v1 = m1 in let p12 = compose_permissions p0 p1 in let v12 = p_op _ v0 v1 in p12, v12 //////////////////////////////////////////////////////////////////////////////// (** avalue composition is associative and commutative **) let compose_avalue_comm (#v:Type) (#p:preorder v) (#anc:anchor_rel p) (m0: avalue anc) (m1: avalue anc{ avalue_composable m0 m1 }) : Lemma (compose_avalue m0 m1 == compose_avalue m1 m0) = () let avalue_composable_assoc_l (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2) = () let compose_avalue_assoc_l (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_l m0 m1 m2 let avalue_composable_assoc_r (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2)) = () let compose_avalue_assoc_r (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s{ avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_r m0 m1 m2 //////////////////////////////////////////////////////////////////////////////// /// lifting avalue composition to knowledge, including unit let compose (#v:Type) (#p:preorder v) (#s:anchor_rel p) (k0:knowledge s) (k1:knowledge s{ composable k0 k1 }) : knowledge s = match k0, k1 with | Nothing, k | k, Nothing -> k | Owns m, Owns m' -> Owns (compose_avalue m m') let composable_assoc_r #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k0 k1 /\ composable (compose k0 k1) k2) (ensures composable k1 k2 /\ composable k0 (compose k1 k2) /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2 ) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_r m0 m1 m2 let composable_assoc_l #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k1 k2 /\ composable k0 (compose k1 k2)) (ensures composable k0 k1 /\ composable (compose k0 k1) k2 /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_l m0 m1 m2 /// Now, we can define our PCM /// The core of the PCM let p0 #v #p #s : pcm' (knowledge #v #p s) = { composable; op=compose; one=Nothing }
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: Steel.FractionalAnchoredPreorder.avalue s -> Steel.FractionalAnchoredPreorder.permission v
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "Steel.FractionalAnchoredPreorder.avalue", "FStar.Pervasives.Native.fst", "Steel.FractionalAnchoredPreorder.permission", "Steel.Preorder.vhist" ]
[]
false
false
false
false
false
let avalue_perm (#v: Type) (#p: preorder v) (#s: anchor_rel p) (m: avalue s) =
fst m
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.p0
val p0 (#v #p #s: _) : pcm' (knowledge #v #p s)
val p0 (#v #p #s: _) : pcm' (knowledge #v #p s)
let p0 #v #p #s : pcm' (knowledge #v #p s) = { composable; op=compose; one=Nothing }
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 1, "end_line": 373, "start_col": 0, "start_line": 369 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm) /// Composing them sums the permissions let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1) /// Anchored permissions are composable when at most one of them has the anchor set let permission_composable #v (p0 p1 : permission v) : prop = let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ // some of fracs can't exceed 1 not (Some? s0 && Some? s1) // at most one can have an anchor /// Compose permissions component wise, keeping the anchor if set let compose_permissions (#v:_) (p0:permission v) (p1:permission v{permission_composable p0 p1}) : permission v = compose_perm_opt (fst p0) (fst p1), (match snd p0, snd p1 with | None, a | a, None -> a) /// This is the central definition of the PCM --- defining when two /// values are composable, or equivalently, when one thread's /// knowledge of [av0] is compatible with another thread's knowledge /// of [av1] /// /// This definition shows the interplay between fractions and anchors. /// It wasn't obvious to me how to factor this further, e.g., adding /// anchors to preorders and then separately adding fractions to it. let avalue_composable (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (av0 av1: avalue anchors) : prop = let (p0, v0) = av0 in let (p1, v1) = av1 in permission_composable p0 p1 /\ (if not (has_some_ownership p0) && not (has_some_ownership p1) then p_composable _ v0 v1 //neither has ownership, one history is older than the other else if not (has_some_ownership p0) && has_some_ownership p1 then ( if has_nonzero p1 then v1 `extends` v0 //the one with ownership is more recent else ( //this part is the most subtle assert (has_anchor p1); p_composable _ v0 v1 /\ (v0 `extends` v1 ==> //if v0 is a more recent snapshot than v1 //then v0 must still be anchored by v1's anchor anchor_of p1 `anchors` curval v0) ) ) else if has_some_ownership p0 && not (has_some_ownership p1) then ( //symmetric if has_nonzero p0 then v0 `extends` v1 else ( assert (has_anchor p0); p_composable _ v0 v1 /\ (v1 `extends` v0 ==> anchor_of p0 `anchors` curval v1) ) ) else ( assert (has_some_ownership p0 && has_some_ownership p1); if has_nonzero p0 && has_nonzero p1 then v0 == v1 //if both have non-zero perm, then they must both only have read permission and must agree on the value else if has_nonzero p0 && has_anchor p1 then ( assert (not (has_nonzero p1)); //The key part of the anchor semantics: v0 `extends` v1 /\ //v0 has advanceable ownership, so extends anchor_of p1 `anchors` curval v0 //but not beyond what is allowed by s ) else if has_anchor p0 && has_nonzero p1 //symmetric then ( assert (not (has_nonzero p0)); v1 `extends` v0 /\ anchor_of p0 `anchors` curval v1 //v1 extends, but not beyond what is allowed by s ) else (assert false; False) ) ) //exhaustive /// Lifting avalue comosability to knowledge, including the unit let composable #v (#p:preorder v) (#a:anchor_rel p) : symrel (knowledge a) = fun (k0 k1:knowledge a) -> match k0, k1 with | Nothing, _ | _, Nothing -> True | Owns m, Owns m' -> avalue_composable m m' /// Compose avalues by composing permissions and composing the values /// using the underlying preorder PCM let compose_avalue (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0:avalue anchors) (m1:avalue anchors { avalue_composable m0 m1 } ) : avalue anchors = let p0, v0 = m0 in let p1, v1 = m1 in let p12 = compose_permissions p0 p1 in let v12 = p_op _ v0 v1 in p12, v12 //////////////////////////////////////////////////////////////////////////////// (** avalue composition is associative and commutative **) let compose_avalue_comm (#v:Type) (#p:preorder v) (#anc:anchor_rel p) (m0: avalue anc) (m1: avalue anc{ avalue_composable m0 m1 }) : Lemma (compose_avalue m0 m1 == compose_avalue m1 m0) = () let avalue_composable_assoc_l (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2) = () let compose_avalue_assoc_l (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_l m0 m1 m2 let avalue_composable_assoc_r (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2)) = () let compose_avalue_assoc_r (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s{ avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_r m0 m1 m2 //////////////////////////////////////////////////////////////////////////////// /// lifting avalue composition to knowledge, including unit let compose (#v:Type) (#p:preorder v) (#s:anchor_rel p) (k0:knowledge s) (k1:knowledge s{ composable k0 k1 }) : knowledge s = match k0, k1 with | Nothing, k | k, Nothing -> k | Owns m, Owns m' -> Owns (compose_avalue m m') let composable_assoc_r #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k0 k1 /\ composable (compose k0 k1) k2) (ensures composable k1 k2 /\ composable k0 (compose k1 k2) /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2 ) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_r m0 m1 m2 let composable_assoc_l #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k1 k2 /\ composable k0 (compose k1 k2)) (ensures composable k0 k1 /\ composable (compose k0 k1) k2 /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_l m0 m1 m2 /// Now, we can define our PCM
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
FStar.PCM.pcm' (Steel.FractionalAnchoredPreorder.knowledge s)
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "FStar.PCM.Mkpcm'", "Steel.FractionalAnchoredPreorder.knowledge", "Steel.FractionalAnchoredPreorder.composable", "Steel.FractionalAnchoredPreorder.compose", "Steel.FractionalAnchoredPreorder.Nothing", "FStar.PCM.pcm'" ]
[]
false
false
false
false
false
let p0 #v #p #s : pcm' (knowledge #v #p s) =
{ composable = composable; op = compose; one = Nothing }
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.avalue_owns
val avalue_owns (#v: Type) (#p: preorder v) (#s: anchor_rel p) (m: avalue s) : prop
val avalue_owns (#v: Type) (#p: preorder v) (#s: anchor_rel p) (m: avalue s) : prop
let avalue_owns (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) : prop = fst (avalue_perm m) == Some full_perm /\ Some? (snd (avalue_perm m))
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 31, "end_line": 390, "start_col": 0, "start_line": 384 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm) /// Composing them sums the permissions let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1) /// Anchored permissions are composable when at most one of them has the anchor set let permission_composable #v (p0 p1 : permission v) : prop = let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ // some of fracs can't exceed 1 not (Some? s0 && Some? s1) // at most one can have an anchor /// Compose permissions component wise, keeping the anchor if set let compose_permissions (#v:_) (p0:permission v) (p1:permission v{permission_composable p0 p1}) : permission v = compose_perm_opt (fst p0) (fst p1), (match snd p0, snd p1 with | None, a | a, None -> a) /// This is the central definition of the PCM --- defining when two /// values are composable, or equivalently, when one thread's /// knowledge of [av0] is compatible with another thread's knowledge /// of [av1] /// /// This definition shows the interplay between fractions and anchors. /// It wasn't obvious to me how to factor this further, e.g., adding /// anchors to preorders and then separately adding fractions to it. let avalue_composable (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (av0 av1: avalue anchors) : prop = let (p0, v0) = av0 in let (p1, v1) = av1 in permission_composable p0 p1 /\ (if not (has_some_ownership p0) && not (has_some_ownership p1) then p_composable _ v0 v1 //neither has ownership, one history is older than the other else if not (has_some_ownership p0) && has_some_ownership p1 then ( if has_nonzero p1 then v1 `extends` v0 //the one with ownership is more recent else ( //this part is the most subtle assert (has_anchor p1); p_composable _ v0 v1 /\ (v0 `extends` v1 ==> //if v0 is a more recent snapshot than v1 //then v0 must still be anchored by v1's anchor anchor_of p1 `anchors` curval v0) ) ) else if has_some_ownership p0 && not (has_some_ownership p1) then ( //symmetric if has_nonzero p0 then v0 `extends` v1 else ( assert (has_anchor p0); p_composable _ v0 v1 /\ (v1 `extends` v0 ==> anchor_of p0 `anchors` curval v1) ) ) else ( assert (has_some_ownership p0 && has_some_ownership p1); if has_nonzero p0 && has_nonzero p1 then v0 == v1 //if both have non-zero perm, then they must both only have read permission and must agree on the value else if has_nonzero p0 && has_anchor p1 then ( assert (not (has_nonzero p1)); //The key part of the anchor semantics: v0 `extends` v1 /\ //v0 has advanceable ownership, so extends anchor_of p1 `anchors` curval v0 //but not beyond what is allowed by s ) else if has_anchor p0 && has_nonzero p1 //symmetric then ( assert (not (has_nonzero p0)); v1 `extends` v0 /\ anchor_of p0 `anchors` curval v1 //v1 extends, but not beyond what is allowed by s ) else (assert false; False) ) ) //exhaustive /// Lifting avalue comosability to knowledge, including the unit let composable #v (#p:preorder v) (#a:anchor_rel p) : symrel (knowledge a) = fun (k0 k1:knowledge a) -> match k0, k1 with | Nothing, _ | _, Nothing -> True | Owns m, Owns m' -> avalue_composable m m' /// Compose avalues by composing permissions and composing the values /// using the underlying preorder PCM let compose_avalue (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0:avalue anchors) (m1:avalue anchors { avalue_composable m0 m1 } ) : avalue anchors = let p0, v0 = m0 in let p1, v1 = m1 in let p12 = compose_permissions p0 p1 in let v12 = p_op _ v0 v1 in p12, v12 //////////////////////////////////////////////////////////////////////////////// (** avalue composition is associative and commutative **) let compose_avalue_comm (#v:Type) (#p:preorder v) (#anc:anchor_rel p) (m0: avalue anc) (m1: avalue anc{ avalue_composable m0 m1 }) : Lemma (compose_avalue m0 m1 == compose_avalue m1 m0) = () let avalue_composable_assoc_l (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2) = () let compose_avalue_assoc_l (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_l m0 m1 m2 let avalue_composable_assoc_r (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2)) = () let compose_avalue_assoc_r (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s{ avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_r m0 m1 m2 //////////////////////////////////////////////////////////////////////////////// /// lifting avalue composition to knowledge, including unit let compose (#v:Type) (#p:preorder v) (#s:anchor_rel p) (k0:knowledge s) (k1:knowledge s{ composable k0 k1 }) : knowledge s = match k0, k1 with | Nothing, k | k, Nothing -> k | Owns m, Owns m' -> Owns (compose_avalue m m') let composable_assoc_r #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k0 k1 /\ composable (compose k0 k1) k2) (ensures composable k1 k2 /\ composable k0 (compose k1 k2) /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2 ) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_r m0 m1 m2 let composable_assoc_l #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k1 k2 /\ composable k0 (compose k1 k2)) (ensures composable k0 k1 /\ composable (compose k0 k1) k2 /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_l m0 m1 m2 /// Now, we can define our PCM /// The core of the PCM let p0 #v #p #s : pcm' (knowledge #v #p s) = { composable; op=compose; one=Nothing } let avalue_perm (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) = fst m /// A avalue represents full ownership when the fraction is full AND /// the anchor is set. This means that no knowledge held by any other
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: Steel.FractionalAnchoredPreorder.avalue s -> Prims.prop
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "Steel.FractionalAnchoredPreorder.avalue", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "FStar.Pervasives.Native.fst", "Steel.FractionalAnchoredPreorder.avalue_perm", "FStar.Pervasives.Native.Some", "Steel.FractionalPermission.full_perm", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.snd", "Prims.prop" ]
[]
false
false
false
false
true
let avalue_owns (#v: Type) (#p: preorder v) (#s: anchor_rel p) (m: avalue s) : prop =
fst (avalue_perm m) == Some full_perm /\ Some? (snd (avalue_perm m))
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.perm_opt_composable
val perm_opt_composable (p0 p1: option perm) : prop
val perm_opt_composable (p0 p1: option perm) : prop
let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 76, "end_line": 149, "start_col": 0, "start_line": 143 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p0: FStar.Pervasives.Native.option Steel.FractionalPermission.perm -> p1: FStar.Pervasives.Native.option Steel.FractionalPermission.perm -> Prims.prop
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "FStar.Pervasives.Native.Mktuple2", "Prims.l_True", "Steel.FractionalAnchoredPreorder.b2p", "Steel.FractionalPermission.lesser_equal_perm", "Steel.FractionalPermission.full_perm", "Steel.FractionalPermission.sum_perm", "Prims.prop" ]
[]
false
false
false
true
true
let perm_opt_composable (p0 p1: option perm) : prop =
match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p ((sum_perm p0 p1) `lesser_equal_perm` full_perm)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.compose_perm_opt
val compose_perm_opt : p0: FStar.Pervasives.Native.option Steel.FractionalPermission.perm -> p1: FStar.Pervasives.Native.option Steel.FractionalPermission.perm -> FStar.Pervasives.Native.option Steel.FractionalPermission.perm
let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 45, "end_line": 156, "start_col": 0, "start_line": 152 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm)
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p0: FStar.Pervasives.Native.option Steel.FractionalPermission.perm -> p1: FStar.Pervasives.Native.option Steel.FractionalPermission.perm -> FStar.Pervasives.Native.option Steel.FractionalPermission.perm
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.Some", "Steel.FractionalPermission.sum_perm" ]
[]
false
false
false
true
false
let compose_perm_opt (p0 p1: option perm) =
match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.initial_value
val initial_value (#v: Type) (#p: preorder v) (#anchors: anchor_rel p) (value: v{anchors value value}) : avalue anchors
val initial_value (#v: Type) (#p: preorder v) (#anchors: anchor_rel p) (value: v{anchors value value}) : avalue anchors
let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value]
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 41, "end_line": 128, "start_col": 0, "start_line": 126 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 1, "initial_ifuel": 2, "max_fuel": 1, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
value: v{anchors value value} -> Steel.FractionalAnchoredPreorder.avalue anchors
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "FStar.Pervasives.Native.Mktuple2", "Steel.FractionalAnchoredPreorder.permission", "Steel.Preorder.vhist", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "FStar.Pervasives.Native.Some", "Steel.FractionalPermission.full_perm", "Prims.Cons", "Prims.Nil", "Steel.FractionalAnchoredPreorder.avalue" ]
[]
false
false
false
false
false
let initial_value (#v: Type) (#p: preorder v) (#anchors: anchor_rel p) (value: v{anchors value value}) : avalue anchors =
(Some full_perm, Some value), [value]
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.sealBase
val sealBase: cs:ciphersuite_not_export_only -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> aad:AEAD.ad (aead_alg_of cs) -> pt:AEAD.plain (aead_alg_of cs) -> Tot (option (key_dh_public_s cs & AEAD.encrypted #(aead_alg_of cs) pt))
val sealBase: cs:ciphersuite_not_export_only -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> aad:AEAD.ad (aead_alg_of cs) -> pt:AEAD.plain (aead_alg_of cs) -> Tot (option (key_dh_public_s cs & AEAD.encrypted #(aead_alg_of cs) pt))
let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 468, "start_col": 0, "start_line": 462 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite_not_export_only -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> aad: Spec.Agile.AEAD.ad (Spec.Agile.HPKE.aead_alg_of cs) -> pt: Spec.Agile.AEAD.plain (Spec.Agile.HPKE.aead_alg_of cs) -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Spec.Agile.AEAD.encrypted pt)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite_not_export_only", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.AEAD.ad", "Spec.Agile.HPKE.aead_alg_of", "Spec.Agile.AEAD.plain", "Spec.Agile.HPKE.setupBaseS", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.AEAD.encrypted", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.context_seal", "Spec.Agile.AEAD.cipher", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let sealBase cs skE pkR info aad pt =
match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct)
false
Spec.Agile.HPKE.fst
Spec.Agile.HPKE.sealPSK
val sealPSK: cs:ciphersuite_not_export_only -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> aad:AEAD.ad (aead_alg_of cs) -> pt:AEAD.plain (aead_alg_of cs) -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (key_dh_public_s cs & AEAD.encrypted #(aead_alg_of cs) pt))
val sealPSK: cs:ciphersuite_not_export_only -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> info:info_s cs -> aad:AEAD.ad (aead_alg_of cs) -> pt:AEAD.plain (aead_alg_of cs) -> psk:psk_s cs -> psk_id:psk_id_s cs -> Tot (option (key_dh_public_s cs & AEAD.encrypted #(aead_alg_of cs) pt))
let sealPSK cs skE pkR info aad pt psk psk_id = match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct)
{ "file_name": "specs/Spec.Agile.HPKE.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 516, "start_col": 0, "start_line": 510 }
module Spec.Agile.HPKE open FStar.Mul open Lib.IntTypes open Lib.RawIntTypes open Lib.Sequence open Lib.ByteSequence module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash module HKDF = Spec.Agile.HKDF let pow2_61 : _:unit{pow2 61 == 2305843009213693952} = assert_norm(pow2 61 == 2305843009213693952) let pow2_35_less_than_pow2_61 : _:unit{pow2 32 * pow2 3 <= pow2 61 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 61 - 1) let pow2_35_less_than_pow2_125 : _:unit{pow2 32 * pow2 3 <= pow2 125 - 1} = assert_norm(pow2 32 * pow2 3 <= pow2 125 - 1) #set-options "--z3rlimit 20 --fuel 0 --ifuel 1" /// Types val id_kem: cs:ciphersuite -> Tot (lbytes 2) let id_kem cs = let kem_dh, kem_hash, _, _ = cs in match kem_dh, kem_hash with | DH.DH_P256, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 16) | DH.DH_Curve25519, Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 32) val id_kdf: cs:ciphersuite -> Tot (lbytes 2) let id_kdf cs = let _, _, _, h = cs in match h with | Hash.SHA2_256 -> create 1 (u8 0) @| create 1 (u8 1) | Hash.SHA2_384 -> create 1 (u8 0) @| create 1 (u8 2) | Hash.SHA2_512 -> create 1 (u8 0) @| create 1 (u8 3) val id_aead: cs:ciphersuite -> Tot (lbytes 2) let id_aead cs = let _, _, a, _ = cs in match a with | Seal AEAD.AES128_GCM -> create 1 (u8 0) @| create 1 (u8 1) | Seal AEAD.AES256_GCM -> create 1 (u8 0) @| create 1 (u8 2) | Seal AEAD.CHACHA20_POLY1305 -> create 1 (u8 0) @| create 1 (u8 3) | ExportOnly -> create 1 (u8 255) @| create 1 (u8 255) val suite_id_kem: cs:ciphersuite -> Tot (lbytes size_suite_id_kem) let suite_id_kem cs = Seq.append label_KEM (id_kem cs) val suite_id_hpke: cs:ciphersuite -> Tot (lbytes size_suite_id_hpke) let suite_id_hpke cs = Seq.append label_HPKE (id_kem cs @| id_kdf cs @| id_aead cs) val id_of_mode: m:mode -> Tot (lbytes size_mode_identifier) let id_of_mode m = match m with | Base -> create 1 (u8 0) | PSK -> create 1 (u8 1) | Auth -> create 1 (u8 2) | AuthPSK -> create 1 (u8 3) val labeled_extract: a:hash_algorithm -> suite_id:bytes -> salt:bytes -> label:bytes -> ikm:bytes -> Pure (lbytes (Spec.Hash.Definitions.hash_length a)) (requires Spec.Agile.HMAC.keysized a (Seq.length salt) /\ labeled_extract_ikm_length_pred a (Seq.length suite_id + Seq.length label + Seq.length ikm)) (ensures fun _ -> True) let labeled_extract a suite_id salt label ikm = let labeled_ikm1 = Seq.append label_version suite_id in let labeled_ikm2 = Seq.append labeled_ikm1 label in let labeled_ikm3 = Seq.append labeled_ikm2 ikm in HKDF.extract a salt labeled_ikm3 val labeled_expand: a:hash_algorithm -> suite_id:bytes -> prk:bytes -> label:bytes -> info:bytes -> l:size_nat -> Pure (lbytes l) (requires Spec.Hash.Definitions.hash_length a <= Seq.length prk /\ Spec.Agile.HMAC.keysized a (Seq.length prk) /\ labeled_expand_info_length_pred a (Seq.length suite_id + Seq.length label + Seq.length info) /\ HKDF.expand_output_length_pred a l) (ensures fun _ -> True) let labeled_expand a suite_id prk label info l = let labeled_info1 = nat_to_bytes_be 2 l in let labeled_info2 = Seq.append labeled_info1 label_version in let labeled_info3 = Seq.append labeled_info2 suite_id in let labeled_info4 = Seq.append labeled_info3 label in let labeled_info5 = Seq.append labeled_info4 info in HKDF.expand a prk labeled_info5 l let extract_and_expand_dh_pred (cs:ciphersuite) (dh_length:nat) = labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + dh_length) let extract_and_expand_ctx_pred (cs:ciphersuite) (ctx_length:nat) = labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + ctx_length) val extract_and_expand: cs:ciphersuite -> dh:bytes -> kem_context:bytes -> Pure (key_kem_s cs) (requires extract_and_expand_dh_pred cs (Seq.length dh) /\ extract_and_expand_ctx_pred cs (Seq.length kem_context)) (ensures fun _ -> True) let extract_and_expand cs dh kem_context = let eae_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_eae_prk dh in labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) eae_prk label_shared_secret kem_context (size_kem_key cs) let deserialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Extract the point coordinates by removing the first representation byte | DH.DH_P256 -> sub pk 1 64 let serialize_public_key cs pk = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> pk // Add the first representation byte to the point coordinates | DH.DH_P256 -> create 1 (u8 4) @| pk val dkp_nist_p: cs:ciphersuite -> lbytes (size_kem_kdf cs) -> counter:uint8 -> Tot (option (key_dh_secret_s cs & key_dh_public_s cs)) (decreases 255 - v counter) let rec dkp_nist_p cs dkp_prk counter = let counterbyte = nat_to_intseq_be #U8 #SEC 1 (v counter) in let bytes = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_candidate counterbyte (size_dh_key cs) in let bytes = Lib.Sequence.map2 (logand #U8 #SEC) bytes (Seq.create (size_dh_key cs) (u8 255)) in let sk = nat_from_intseq_be #U8 #SEC bytes in if sk = 0 || sk >= Spec.P256.prime then if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) else match DH.secret_to_public (kem_dh_of_cs cs) bytes with | Some pk -> Some (bytes, serialize_public_key cs pk) | None -> if (v counter) = 255 then None else dkp_nist_p cs dkp_prk (counter +! (u8 1)) let derive_key_pair cs ikm = match kem_dh_of_cs cs with | DH.DH_Curve25519 -> begin let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in let sk = labeled_expand (kem_hash_of_cs cs) (suite_id_kem cs) dkp_prk label_sk lbytes_empty (size_dh_key cs) in match DH.secret_to_public (kem_dh_of_cs cs) sk with | Some pk -> Some (sk, serialize_public_key cs pk) end | DH.DH_P256 -> let dkp_prk = labeled_extract (kem_hash_of_cs cs) (suite_id_kem cs) lbytes_empty label_dkp_prk ikm in dkp_nist_p cs dkp_prk (u8 0) val prepare_dh: cs:ciphersuite -> DH.serialized_point (kem_dh_of_cs cs) -> Tot (lbytes 32) let prepare_dh cs dh = match (kem_dh_of_cs cs) with | DH.DH_Curve25519 -> serialize_public_key cs dh | DH.DH_P256 -> sub dh 0 32 val encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let encap cs skE pkR = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> let enc = serialize_public_key cs pkE in match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some dh -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret:key_kem_s cs = extract_and_expand cs dhm kem_context in Some (shared_secret, enc) val decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> Tot (option (key_kem_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let decap cs enc skR = let _ = allow_inversion Spec.Agile.DH.algorithm in let _ = allow_inversion Spec.Agile.Hash.hash_alg in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some dh -> match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let kem_context = concat enc pkRm in let dhm = prepare_dh cs dh in assert (Seq.length kem_context = 2*size_dh_public cs); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); let shared_secret = extract_and_expand cs dhm kem_context in Some (shared_secret) val auth_encap: cs:ciphersuite -> skE:key_dh_secret_s cs -> pkR:DH.serialized_point (kem_dh_of_cs cs) -> skS:key_dh_secret_s cs -> Tot (option (key_kem_s cs & key_dh_public_s cs)) #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_encap cs skE pkR skS = let _ = allow_inversion Spec.Agile.DH.algorithm in match DH.secret_to_public (kem_dh_of_cs cs) skE with | None -> None | Some pkE -> match DH.dh (kem_dh_of_cs cs) skE pkR with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skS pkR with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in // TODO Do not put 32 literally let dh = concat #uint8 #32 #32 esm ssm in let enc = serialize_public_key cs pkE in match DH.secret_to_public (kem_dh_of_cs cs) skS with | None -> None | Some pkS -> let pkSm = serialize_public_key cs pkS in let pkRm = serialize_public_key cs pkR in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); // TODO Do not put 64 literally assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret, enc) #reset-options val auth_decap: cs: ciphersuite -> enc: key_dh_public_s cs -> skR: key_dh_secret_s cs -> pkS: DH.serialized_point (kem_dh_of_cs cs) -> Tot (option (key_kem_s cs)) #restart-solver #set-options "--z3rlimit 100 --fuel 0 --ifuel 0" let auth_decap cs enc skR pkS = let _ = allow_inversion Spec.Agile.DH.algorithm in let pkE = deserialize_public_key cs enc in match DH.dh (kem_dh_of_cs cs) skR pkE with | None -> None | Some es -> match DH.dh (kem_dh_of_cs cs) skR pkS with | None -> None | Some ss -> let esm = prepare_dh cs es in let ssm = prepare_dh cs ss in let dh = concat #uint8 #32 #32 esm ssm in match DH.secret_to_public (kem_dh_of_cs cs) skR with | None -> None | Some pkR -> let pkRm = serialize_public_key cs pkR in let pkSm = serialize_public_key cs pkS in let kem_context = concat enc (concat pkRm pkSm) in assert (Seq.length kem_context = 3*size_dh_public cs); assert (labeled_expand_info_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_shared_secret + Seq.length kem_context)); assert (extract_and_expand_ctx_pred cs (Seq.length kem_context)); assert (Seq.length dh = 64); assert (labeled_extract_ikm_length_pred (kem_hash_of_cs cs) (size_suite_id_kem + size_label_eae_prk + Seq.length dh)); assert (extract_and_expand_dh_pred cs (Seq.length dh)); let shared_secret = extract_and_expand cs dh kem_context in Some (shared_secret) #reset-options let default_psk = lbytes_empty let default_psk_id = lbytes_empty val build_context: cs:ciphersuite -> m:mode -> psk_id_hash:lbytes (size_kdf cs) -> info_hash:lbytes (size_kdf cs) -> Tot (lbytes (size_ks_ctx cs)) let build_context cs m psk_id_hash info_hash = let context = id_of_mode m in let context = Seq.append context psk_id_hash in let context = Seq.append context info_hash in context let verify_psk_inputs (cs:ciphersuite) (m:mode) (opsk:option(psk_s cs & psk_id_s cs)) : bool = match (m, opsk) with | Base, None -> true | PSK, Some _ -> true | Auth, None -> true | AuthPSK, Some _ -> true | _, _ -> false // key and nonce are zero-length if AEAD is Export-Only let encryption_context (cs:ciphersuite) = key_aead_s cs & nonce_aead_s cs & seq_aead_s cs & exporter_secret_s cs val key_schedule: cs:ciphersuite -> m:mode -> shared_secret:key_kem_s cs -> info:info_s cs -> opsk:option (psk_s cs & psk_id_s cs) -> Pure (encryption_context cs) (requires verify_psk_inputs cs m opsk) (ensures fun _ -> True) #set-options "--z3rlimit 500 --fuel 0 --ifuel 2" let key_schedule_core (cs:ciphersuite) (m:mode) (shared_secret:key_kem_s cs) (info:info_s cs) (opsk:option (psk_s cs & psk_id_s cs)) : (lbytes (size_ks_ctx cs) & exporter_secret_s cs & (lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) = let (psk, psk_id) = match opsk with | None -> (default_psk, default_psk_id) | Some (psk, psk_id) -> (psk, psk_id) in let psk_id_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_psk_id_hash psk_id in let info_hash = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) lbytes_empty label_info_hash info in let context = build_context cs m psk_id_hash info_hash in let secret = labeled_extract (hash_of_cs cs) (suite_id_hpke cs) shared_secret label_secret psk in let exporter_secret = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_exp context (size_kdf cs) in context, exporter_secret, secret let key_schedule_end (cs:ciphersuite) (m:mode) (context:lbytes (size_ks_ctx cs)) (exporter_secret:exporter_secret_s cs) (secret:(lbytes (Spec.Hash.Definitions.hash_length (hash_of_cs cs)))) : encryption_context cs = if is_valid_not_export_only_ciphersuite cs then ( let key = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_key context (size_aead_key cs) in let base_nonce = labeled_expand (hash_of_cs cs) (suite_id_hpke cs) secret label_base_nonce context (size_aead_nonce cs) in (key, base_nonce, 0, exporter_secret) ) else ( (* if AEAD is Export-Only, then skip computation of key and base_nonce *) assert (size_aead_key cs = 0); assert (size_aead_nonce cs = 0); (lbytes_empty, lbytes_empty, 0, exporter_secret)) let key_schedule cs m shared_secret info opsk = let context, exporter_secret, secret = key_schedule_core cs m shared_secret info opsk in key_schedule_end cs m context exporter_secret secret let key_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let key, _, _, _ = ctx in key let base_nonce_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, base_nonce, _, _ = ctx in base_nonce let seq_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, seq, _ = ctx in seq let exp_sec_of_ctx (cs:ciphersuite) (ctx:encryption_context cs) = let _, _, _, exp_sec = ctx in exp_sec let set_seq (cs:ciphersuite) (ctx:encryption_context cs) (seq:seq_aead_s cs) = let key, base_nonce, _, exp_sec = ctx in (key, base_nonce, seq, exp_sec) /// /// Encryption Context /// let context_export cs ctx exp_ctx l = let exp_sec = exp_sec_of_ctx cs ctx in labeled_expand (hash_of_cs cs) (suite_id_hpke cs) exp_sec label_sec exp_ctx l let context_compute_nonce cs ctx seq = let base_nonce = base_nonce_of_ctx cs ctx in let enc_seq = nat_to_bytes_be (size_aead_nonce cs) seq in Spec.Loops.seq_map2 logxor enc_seq base_nonce let context_increment_seq cs ctx = let seq = seq_of_ctx cs ctx in if seq = max_seq cs then None else Some (set_seq cs ctx (seq + 1)) let context_seal cs ctx aad pt = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in let ct = AEAD.encrypt key nonce aad pt in match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, ct) let context_open cs ctx aad ct = let key = key_of_ctx cs ctx in let seq = seq_of_ctx cs ctx in let nonce = context_compute_nonce cs ctx seq in match AEAD.decrypt key nonce aad ct with | None -> None | Some pt -> match context_increment_seq cs ctx with | None -> None | Some new_ctx -> Some (new_ctx, pt) /// /// Base Mode /// let setupBaseS cs skE pkR info = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> let enc_ctx = key_schedule cs Base shared_secret info None in Some (enc, enc_ctx) let setupBaseR cs enc skR info = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs Base shared_secret info None) | _ -> None let sealBase cs skE pkR info aad pt = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct) let openBase cs enc skR info aad ct = match setupBaseR cs enc skR info with | None -> None | Some ctx -> match context_open cs ctx aad ct with | None -> None | Some (_, pt) -> Some pt let sendExportBase cs skE pkR info exp_ctx l = match setupBaseS cs skE pkR info with | None -> None | Some (enc, ctx) -> Some (enc, context_export cs ctx exp_ctx l) let receiveExportBase cs enc skR info exp_ctx l = match setupBaseR cs enc skR info with | None -> None | Some ctx -> Some (context_export cs ctx exp_ctx l) /// /// PSK mode /// let setupPSKS cs skE pkR info psk psk_id = match encap cs skE pkR with | None -> None | Some (shared_secret, enc) -> assert (verify_psk_inputs cs PSK (Some (psk, psk_id))); let enc_ctx = key_schedule cs PSK shared_secret info (Some (psk, psk_id)) in Some (enc, enc_ctx) let setupPSKR cs enc skR info psk psk_id = let pkR = DH.secret_to_public (kem_dh_of_cs cs) skR in let shared_secret = decap cs enc skR in match pkR, shared_secret with | Some pkR, Some shared_secret -> Some (key_schedule cs PSK shared_secret info (Some (psk, psk_id))) | _ -> None
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Loops.fst.checked", "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "Spec.Agile.HKDF.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.Agile.HPKE.fst" }
[ { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.Agile.HKDF", "short_module": "HKDF" }, { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.RawIntTypes", "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": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Spec.Agile.HPKE.ciphersuite_not_export_only -> skE: Spec.Agile.HPKE.key_dh_secret_s cs -> pkR: Spec.Agile.DH.serialized_point (Spec.Agile.HPKE.kem_dh_of_cs cs) -> info: Spec.Agile.HPKE.info_s cs -> aad: Spec.Agile.AEAD.ad (Spec.Agile.HPKE.aead_alg_of cs) -> pt: Spec.Agile.AEAD.plain (Spec.Agile.HPKE.aead_alg_of cs) -> psk: Spec.Agile.HPKE.psk_s cs -> psk_id: Spec.Agile.HPKE.psk_id_s cs -> FStar.Pervasives.Native.option (Spec.Agile.HPKE.key_dh_public_s cs * Spec.Agile.AEAD.encrypted pt)
Prims.Tot
[ "total" ]
[]
[ "Spec.Agile.HPKE.ciphersuite_not_export_only", "Spec.Agile.HPKE.key_dh_secret_s", "Spec.Agile.DH.serialized_point", "Spec.Agile.HPKE.kem_dh_of_cs", "Spec.Agile.HPKE.info_s", "Spec.Agile.AEAD.ad", "Spec.Agile.HPKE.aead_alg_of", "Spec.Agile.AEAD.plain", "Spec.Agile.HPKE.psk_s", "Spec.Agile.HPKE.psk_id_s", "Spec.Agile.HPKE.setupPSKS", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HPKE.key_dh_public_s", "Spec.Agile.AEAD.encrypted", "Spec.Agile.HPKE.encryption_context", "Spec.Agile.HPKE.context_seal", "Spec.Agile.AEAD.cipher", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option" ]
[]
false
false
false
false
false
let sealPSK cs skE pkR info aad pt psk psk_id =
match setupPSKS cs skE pkR info psk psk_id with | None -> None | Some (enc, ctx) -> match context_seal cs ctx aad pt with | None -> None | Some (_, ct) -> Some (enc, ct)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.compose_permissions
val compose_permissions (#v: _) (p0: permission v) (p1: permission v {permission_composable p0 p1}) : permission v
val compose_permissions (#v: _) (p0: permission v) (p1: permission v {permission_composable p0 p1}) : permission v
let compose_permissions (#v:_) (p0:permission v) (p1:permission v{permission_composable p0 p1}) : permission v = compose_perm_opt (fst p0) (fst p1), (match snd p0, snd p1 with | None, a | a, None -> a)
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 20, "end_line": 172, "start_col": 0, "start_line": 167 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm) /// Composing them sums the permissions let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1) /// Anchored permissions are composable when at most one of them has the anchor set let permission_composable #v (p0 p1 : permission v) : prop = let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ // some of fracs can't exceed 1 not (Some? s0 && Some? s1) // at most one can have an anchor
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p0: Steel.FractionalAnchoredPreorder.permission v -> p1: Steel.FractionalAnchoredPreorder.permission v {Steel.FractionalAnchoredPreorder.permission_composable p0 p1} -> Steel.FractionalAnchoredPreorder.permission v
Prims.Tot
[ "total" ]
[]
[ "Steel.FractionalAnchoredPreorder.permission", "Steel.FractionalAnchoredPreorder.permission_composable", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "Steel.FractionalAnchoredPreorder.compose_perm_opt", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd" ]
[]
false
false
false
false
false
let compose_permissions (#v: _) (p0: permission v) (p1: permission v {permission_composable p0 p1}) : permission v =
compose_perm_opt (fst p0) (fst p1), (match snd p0, snd p1 with | None, a | a, None -> a)
false
Steel.FractionalAnchoredPreorder.fst
Steel.FractionalAnchoredPreorder.avalue_owns_anchored
val avalue_owns_anchored : m: Steel.FractionalAnchoredPreorder.avalue s -> Prims.logical
let avalue_owns_anchored (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) = fst (avalue_perm m) == Some full_perm /\ None? (snd (avalue_perm m))
{ "file_name": "lib/steel/Steel.FractionalAnchoredPreorder.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 33, "end_line": 495, "start_col": 0, "start_line": 490 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author: N. Swamy *) module Steel.FractionalAnchoredPreorder (** This module provides a partial commutative monoid (PCM) for use in the ghost state of a concurrent Steel program. Its goal is to facilitate a form of information-sharing between multiple threads including the following features: Fractions, Preorders, Snapshots, and Anchors --- Anchors are the most unusual. 1. Fractions: Ownership of the state is controlled by a fractional permission, so multiple threads may share read-only privileges on the state, but only one can have write permission. 2. Preorders: The ghost state is governed by a preorder p and all updates to the state must respect the preorder (a reflexive, transitive relation on the state) 3. Snapshots: Since the state always evolves by a preorder, it is possible to take a snapshot of the state. A snapshot does not confer any particular ownership on the state, except knowledge of a snapshot ensures that the current state is related to the snapshot by the preorder. 4. Anchors are a kind of refinement of the preorder. If a thread owns an anchored value [v], then no other thread, even a thread owning a full fractional permission, can advance the state "too far" from the anchor. In particular, the PCM is parameterized by a binary relation on values, [anchors:anchor_relation]. If a thread owns the anchor [v], then the current value `cur` of the state must be in relation [v `anchors` cur]. This can be used to ensure that the knowledge of one thread is never becomes too stale. For example, this allows one to model a shared monotonically increasing counter, where if one thread anchors knowledge of the counter at the value 5, no other thread can increase the value of the counter beyond, say, 10, (when the anchor relation is y - x <= 5), until the anchoring thread releases the anchor. Further, at any point, a thread can take a snapshot of the counter and rely on the fact that the value of the counter in the future will be >= the snapshot. *) open FStar.PCM open FStar.Preorder open Steel.Preorder open Steel.FractionalPermission #push-options "--fuel 0 --ifuel 2" /// A permission: is a pair of /// /// * an optional fractional permission; with `Some p` indicating /// some ownership, while `None` is an additional "0" permission /// value, to encode snapshots. /// /// * and an optional anchor, where `Some a` indicates the current /// value can't be too far from `a`. let permission (v:Type) = option perm & option v /// Non-zero permission is either a read or write permission let has_nonzero #v (p:permission v) = Some? (fst p) /// Has an anchor set let has_anchor #v (p:permission v) = Some? (snd p) /// Either an anchor or non-zero let has_some_ownership #v (p:permission v) = has_nonzero p || has_anchor p /// The anchoring value let anchor_of #v (p:permission v { has_anchor p }) : v = Some?.v (snd p) /// The anchoring relation: /// -- A binary relation on values /// -- refining the preorder /// -- and if x `anchors` z then it also anchors any `y` between `x` and `z` /// /// Explaining the third clause a bit: /// Think intuitively of anchoring relations as a kind of distance measure /// /// if `z` is close enough to `x` then if `z` is further than `y` /// (since it is ahead of `y` in the preorder) then `y` is also /// close enough to `x` let anchor_rel (#v:Type) (p:preorder v) = anchors:(v -> v -> prop) { (forall v0 v1. anchors v0 v1 ==> p v0 v1) /\ (forall x z. x `anchors` z ==> (forall y. p x y /\ p y z ==> x `anchors` y)) // } /// An implementation remark: We use Steel.Preorder here to /// generically transform any preorder into a PCM. That works by /// taking the value of the PCM to be a history of values related by /// the preorder. So, throughout, we'll be using the the [vhist p] /// from Steel.Preorder, the type of a non-empty history compatible /// with [p]. The details of the histories don't matter much for our /// purposes---we only care about the most recent value, for which /// we'll use [curval v]. /// If the anchor is set, then the current value is anchored by it let anchored (#v:Type) (#p:preorder v) (anchors:anchor_rel p) (pv:(permission v & vhist p)) = has_anchor (fst pv) ==> anchor_of (fst pv) `anchors` curval (snd pv) /// A value in our fractional anchored PCM is a pair of a permission /// and a p-compatible history, refined to ensure that if the anchor /// is set, then the current value is anchored by it. let avalue (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = pv:(permission v & vhist p) { anchored anchors pv } /// initial_value: A utility to construct a value in the PCM #push-options "--fuel 1" let initial_value (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (value:v { anchors value value }) : avalue anchors = (Some full_perm, Some value), [value] #pop-options /// We add a unit element to [avalue], as needed for a PCM [@@erasable] noeq type knowledge (#v:Type) (#p:preorder v) (anchors:anchor_rel p) = | Owns : avalue anchors -> knowledge anchors | Nothing : knowledge anchors let b2p (b:bool) : prop = b == true /// Fractional permissions are composable when their sum <= 1.0 let perm_opt_composable (p0 p1:option perm) : prop = match p0, p1 with | None, None -> True | Some p, None | None, Some p -> b2p (p `lesser_equal_perm` full_perm) | Some p0, Some p1 -> b2p (sum_perm p0 p1 `lesser_equal_perm` full_perm) /// Composing them sums the permissions let compose_perm_opt (p0 p1:option perm) = match p0, p1 with | None, p | p, None -> p | Some p0, Some p1 -> Some (sum_perm p0 p1) /// Anchored permissions are composable when at most one of them has the anchor set let permission_composable #v (p0 p1 : permission v) : prop = let q0, s0 = p0 in let q1, s1 = p1 in perm_opt_composable q0 q1 /\ // some of fracs can't exceed 1 not (Some? s0 && Some? s1) // at most one can have an anchor /// Compose permissions component wise, keeping the anchor if set let compose_permissions (#v:_) (p0:permission v) (p1:permission v{permission_composable p0 p1}) : permission v = compose_perm_opt (fst p0) (fst p1), (match snd p0, snd p1 with | None, a | a, None -> a) /// This is the central definition of the PCM --- defining when two /// values are composable, or equivalently, when one thread's /// knowledge of [av0] is compatible with another thread's knowledge /// of [av1] /// /// This definition shows the interplay between fractions and anchors. /// It wasn't obvious to me how to factor this further, e.g., adding /// anchors to preorders and then separately adding fractions to it. let avalue_composable (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (av0 av1: avalue anchors) : prop = let (p0, v0) = av0 in let (p1, v1) = av1 in permission_composable p0 p1 /\ (if not (has_some_ownership p0) && not (has_some_ownership p1) then p_composable _ v0 v1 //neither has ownership, one history is older than the other else if not (has_some_ownership p0) && has_some_ownership p1 then ( if has_nonzero p1 then v1 `extends` v0 //the one with ownership is more recent else ( //this part is the most subtle assert (has_anchor p1); p_composable _ v0 v1 /\ (v0 `extends` v1 ==> //if v0 is a more recent snapshot than v1 //then v0 must still be anchored by v1's anchor anchor_of p1 `anchors` curval v0) ) ) else if has_some_ownership p0 && not (has_some_ownership p1) then ( //symmetric if has_nonzero p0 then v0 `extends` v1 else ( assert (has_anchor p0); p_composable _ v0 v1 /\ (v1 `extends` v0 ==> anchor_of p0 `anchors` curval v1) ) ) else ( assert (has_some_ownership p0 && has_some_ownership p1); if has_nonzero p0 && has_nonzero p1 then v0 == v1 //if both have non-zero perm, then they must both only have read permission and must agree on the value else if has_nonzero p0 && has_anchor p1 then ( assert (not (has_nonzero p1)); //The key part of the anchor semantics: v0 `extends` v1 /\ //v0 has advanceable ownership, so extends anchor_of p1 `anchors` curval v0 //but not beyond what is allowed by s ) else if has_anchor p0 && has_nonzero p1 //symmetric then ( assert (not (has_nonzero p0)); v1 `extends` v0 /\ anchor_of p0 `anchors` curval v1 //v1 extends, but not beyond what is allowed by s ) else (assert false; False) ) ) //exhaustive /// Lifting avalue comosability to knowledge, including the unit let composable #v (#p:preorder v) (#a:anchor_rel p) : symrel (knowledge a) = fun (k0 k1:knowledge a) -> match k0, k1 with | Nothing, _ | _, Nothing -> True | Owns m, Owns m' -> avalue_composable m m' /// Compose avalues by composing permissions and composing the values /// using the underlying preorder PCM let compose_avalue (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0:avalue anchors) (m1:avalue anchors { avalue_composable m0 m1 } ) : avalue anchors = let p0, v0 = m0 in let p1, v1 = m1 in let p12 = compose_permissions p0 p1 in let v12 = p_op _ v0 v1 in p12, v12 //////////////////////////////////////////////////////////////////////////////// (** avalue composition is associative and commutative **) let compose_avalue_comm (#v:Type) (#p:preorder v) (#anc:anchor_rel p) (m0: avalue anc) (m1: avalue anc{ avalue_composable m0 m1 }) : Lemma (compose_avalue m0 m1 == compose_avalue m1 m0) = () let avalue_composable_assoc_l (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2) = () let compose_avalue_assoc_l (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s { avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) }) : Lemma (avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_l m0 m1 m2 let avalue_composable_assoc_r (#v:Type) (#p:preorder v) (#anchors:anchor_rel p) (m0: avalue anchors) (m1: avalue anchors) (m2: avalue anchors { avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2)) = () let compose_avalue_assoc_r (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m0: avalue s) (m1: avalue s) (m2: avalue s{ avalue_composable m0 m1 /\ avalue_composable (compose_avalue m0 m1) m2 }) : Lemma (avalue_composable m1 m2 /\ avalue_composable m0 (compose_avalue m1 m2) /\ compose_avalue m0 (compose_avalue m1 m2) == compose_avalue (compose_avalue m0 m1) m2) = avalue_composable_assoc_r m0 m1 m2 //////////////////////////////////////////////////////////////////////////////// /// lifting avalue composition to knowledge, including unit let compose (#v:Type) (#p:preorder v) (#s:anchor_rel p) (k0:knowledge s) (k1:knowledge s{ composable k0 k1 }) : knowledge s = match k0, k1 with | Nothing, k | k, Nothing -> k | Owns m, Owns m' -> Owns (compose_avalue m m') let composable_assoc_r #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k0 k1 /\ composable (compose k0 k1) k2) (ensures composable k1 k2 /\ composable k0 (compose k1 k2) /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2 ) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_r m0 m1 m2 let composable_assoc_l #v (#p:preorder v) (#s:anchor_rel p) (k0 k1 k2: knowledge s) : Lemma (requires composable k1 k2 /\ composable k0 (compose k1 k2)) (ensures composable k0 k1 /\ composable (compose k0 k1) k2 /\ compose k0 (compose k1 k2) == compose (compose k0 k1) k2) = match k0, k1, k2 with | Nothing, _, _ | _, Nothing, _ | _, _, Nothing -> () | Owns m0, Owns m1, Owns m2 -> compose_avalue_assoc_l m0 m1 m2 /// Now, we can define our PCM /// The core of the PCM let p0 #v #p #s : pcm' (knowledge #v #p s) = { composable; op=compose; one=Nothing } let avalue_perm (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) = fst m /// A avalue represents full ownership when the fraction is full AND /// the anchor is set. This means that no knowledge held by any other /// thread can constrain this value meaningfully. let avalue_owns (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) : prop = fst (avalue_perm m) == Some full_perm /\ Some? (snd (avalue_perm m)) let full_knowledge #v #p #s (kn:knowledge #v #p s) : prop = match kn with | Nothing -> False | Owns km -> avalue_owns km /// The PCM itself, together with proofs of its properties let pcm #v #p #s : pcm (knowledge #v #p s) = { p = p0; comm = (fun k0 k1 -> match k0, k1 with | Nothing, _ | _, Nothing -> () | Owns m0, Owns m1 -> compose_avalue_comm m0 m1); assoc = (fun k0 k1 k2 -> composable_assoc_l k0 k1 k2); assoc_r = (fun k0 k1 k2 -> composable_assoc_r k0 k1 k2); is_unit = (fun _ -> ()); refine = full_knowledge; } /// Some utilities: The value of an avalue let avalue_val (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue #v #p s) = snd m /// Updating the value, in a full-ownership situation, also involves /// updating the anchor let avalue_update (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) (value:vhist p { s (curval value) (curval value) }) : avalue s = let p, _ = avalue_perm m in let p' = p, Some (curval value) in (p', value) /// Our core frame-preserving update: /// /// If you fully own a value, you can update it so long as you /// respect the preorder, and prove that the new value is related /// to itself by the anchor (since we'll be setting the anchor to /// the new value) /// /// This is a building block: we'll define a derived version that /// on values rather than histories #push-options "--z3rlimit_factor 2" let update_hist (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) (v1:vhist p { avalue_owns m /\ v1 `extends` avalue_val m /\ s (curval v1) (curval v1) }) : frame_preserving_upd pcm (Owns m) (Owns (avalue_update m v1)) = fun full_v -> let Owns full_m = full_v in let m_res = avalue_update full_m v1 in Owns m_res #pop-options /// Updating with value, rather than a history let avalue_update_value (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) (value:v { curval (avalue_val m) `p` value /\ s value value }) : m':avalue s { curval (avalue_val m') == value /\ avalue_val m' `extends` avalue_val m } = let v = avalue_val m in avalue_update m (extend_history v value) /// A derived frame-preserving update for which one presents only a value let update_value (#v:Type) (#p:preorder v) (#s:anchor_rel p) (m:avalue s) (v1:v { avalue_owns m /\ //if you have full ownership of key curval (avalue_val m) `p` v1 /\ //you can update it wrt the preorder only s v1 v1 //and it is related to itself by the anchor }) : frame_preserving_upd pcm (Owns m) (Owns (avalue_update_value m v1)) = coerce_eq () (update_hist m (extend_history (avalue_val m) v1)) //F* goes nuts and starts swallowing gobs of memory without the coerce_eq: TODO, debug /// Now for anchored updates
{ "checked_file": "/", "dependencies": [ "Steel.Preorder.fst.checked", "Steel.FractionalPermission.fst.checked", "prims.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.PCM.fst.checked" ], "interface_file": false, "source_file": "Steel.FractionalAnchoredPreorder.fst" }
[ { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.Preorder", "short_module": null }, { "abbrev": false, "full_module": "FStar.PCM", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "Steel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 2, "max_fuel": 0, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: Steel.FractionalAnchoredPreorder.avalue s -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "FStar.Preorder.preorder", "Steel.FractionalAnchoredPreorder.anchor_rel", "Steel.FractionalAnchoredPreorder.avalue", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "Steel.FractionalPermission.perm", "FStar.Pervasives.Native.fst", "Steel.FractionalAnchoredPreorder.avalue_perm", "FStar.Pervasives.Native.Some", "Steel.FractionalPermission.full_perm", "Prims.b2t", "FStar.Pervasives.Native.uu___is_None", "FStar.Pervasives.Native.snd", "Prims.logical" ]
[]
false
false
false
false
true
let avalue_owns_anchored (#v: Type) (#p: preorder v) (#s: anchor_rel p) (m: avalue s) =
fst (avalue_perm m) == Some full_perm /\ None? (snd (avalue_perm m))
false