val quic_coerce (#i:unsafe_id) (u:info i)
  (ts:traffic_secret u.halg)
  : ST (aead_writer i)
  (requires fun h0 -> True)
  (ensures fun h0 w h1 ->
    winvariant w h1 /\
    B.modifies B.loc_none h0 h1 /\
    B.fresh_loc (wfootprint w) h0 h1 /\
    B.(loc_includes (loc_ae_region ()) (wfootprint w)) /\
    wgetinfo w == u /\
    wkey w ==
      QUIC.Spec.derive_secret u.halg ts
        QUIC.Spec.label_key (Spec.key_length u.alg)
  )
let quic_coerce #i u ts =
  coerce u
    (QUIC.Spec.derive_secret u.halg ts
      QUIC.Spec.label_key (Spec.key_length u.alg))
val quic_coerce (#i:unsafe_id) (u:info i)
  (ts:traffic_secret u.halg)
  : ST (aead_writer i)
  (requires fun h0 -> True)
  (ensures fun h0 w h1 ->
    winvariant w h1 /\
    B.modifies B.loc_none h0 h1 /\
    B.fresh_loc (wfootprint w) h0 h1 /\
    B.(loc_includes (loc_ae_region ()) (wfootprint w)) /\
    wgetinfo w == u /\
    wkey w ==
      QUIC.Spec.derive_secret u.halg ts
        QUIC.Spec.label_key (Spec.key_length u.alg)
  )
let quic_coerce #i u ts =
coerce u (QUIC.Spec.derive_secret u.halg ts QUIC.Spec.label_key (Spec.key_length u.alg))
Model.AEAD.fst.checked
Spec.Agile.AEAD.fsti.checked QUIC.Spec.fsti.checked prims.fst.checked Model.Indexing.fsti.checked Model.Helpers.fsti.checked Mem.fst.checked LowStar.BufferOps.fst.checked LowStar.Buffer.fst.checked FStar.UInt32.fsti.checked FStar.UInt128.fsti.checked FStar.Seq.fst.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked FStar.HyperStack.fst.checked
Model.AEAD.fst
Model.AEAD.unsafe_id Model.AEAD.info Model.AEAD.traffic_secret Model.AEAD.__proj__Mkinfo'__item__halg Model.AEAD.coerce QUIC.Spec.Crypto.derive_secret QUIC.Spec.Crypto.label_key Spec.Agile.AEAD.key_length Model.AEAD.__proj__Mkinfo'__item__alg Model.AEAD.aead_writer
module Model.AEAD

module HS = FStar.HyperStack
module I = Model.Indexing
module U32 = FStar.UInt32
module U128 = FStar.UInt128
module B = LowStar.Buffer
module Spec = Spec.Agile.AEAD

#set-options "--fuel 0 --ifuel 0"

open Mem
open LowStar.BufferOps
open Model.Helpers

let log #i (u: info i): Type0 =
  Seq.seq (entry i u)

let model_writer i: Type u#1 =
  u:info i & B.pointer (log #i u)

let unsafe_writer i =
  info i & Spec.kv (I.ae_id_ginfo i)

let aead_writer (i: id): Type u#1 =
  if is_safe i then
    model_writer i
  else
    unsafe_writer i

let aead_reader #i (w: aead_writer i) =
  w':aead_writer i { w' == w }

let wgetinfo #i (u: aead_writer i) =
  if is_safe i then
    dfst (u <: model_writer i)
  else
    fst (u <: unsafe_writer i)

let rgetinfo #_ #w _ =
  wgetinfo w

let wlog #i w h =
  B.deref h (dsnd (w <: model_writer i))

let wkey #i w =
  snd (w <: unsafe_writer i)

let wfootprint #i w =
  if is_safe i then
    B.loc_addr_of_buffer (dsnd (w <: model_writer i))
  else
    B.loc_none

let winvariant #i w h =
  if is_safe i then
    B.live h (dsnd (w <: model_writer i))
  else
    True

let wframe_invariant #_ w h0 l h1 =
  ()

let frame_log #_ w h0 l h1 =
  ()

let gen i u =
  if is_safe i then
    let l: log #i u = Seq.empty #(entry i u) in
    ((| u, B.malloc q_ae_region l 1ul |) <: model_writer i)
  else
    let key_l = Spec.key_length u.alg in
    (u, random key_l) <: unsafe_writer i

let gen_reader #i w =
  w

let coerce #i u kv =
  (u, kv) <: unsafe_writer i

// With QUIC-specific derivation of key from transport secret
val quic_coerce (#i:unsafe_id) (u:info i)
  (ts:traffic_secret u.halg)
  : ST (aead_writer i)
  (requires fun h0 -> True)
  (ensures fun h0 w h1 ->
    winvariant w h1 /\
    B.modifies B.loc_none h0 h1 /\
    B.fresh_loc (wfootprint w) h0 h1 /\
    B.(loc_includes (loc_ae_region ()) (wfootprint w)) /\
    wgetinfo w == u /\
    wkey w ==
      QUIC.Spec.derive_secret u.halg ts
        QUIC.Spec.label_key (Spec.key_length u.alg)
  )
u141: Model.AEAD.info i -> ts: Model.AEAD.traffic_secret (Mkinfo'?.halg u141)
  -> FStar.HyperStack.ST.ST (Model.AEAD.aead_writer i)