val iv_for_encrypt_decrypt
      (a: ea)
      (siv dst: B.buffer Secret.uint8)
      (h: G.erased Spec.header {~(Spec.is_retry h)})
      (pn_len: PN.packet_number_length_t)
      (pn: PN.packet_number_t)
    : HST.Stack unit
      (requires (fun m -> iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m))
      (ensures (fun m _ m' -> iv_for_encrypt_decrypt_post a siv dst h pn_len pn m m'))
let iv_for_encrypt_decrypt
  (a: ea)
  (siv: B.buffer Secret.uint8)
  (dst: B.buffer Secret.uint8)
  (h: G.erased Spec.header { ~ (Spec.is_retry h) })
  (pn_len: PN.packet_number_length_t)
  (pn: PN.packet_number_t)
: HST.Stack unit
  (requires (fun m ->
    iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m
  ))
  (ensures (fun m _ m' ->
    iv_for_encrypt_decrypt_post a siv dst h pn_len pn m m'
  ))
= let m0 = HST.get () in
  B.fill dst (Secret.to_u8 0uy) 12ul;
  let pnb_store = B.sub dst 4ul 8ul in
  SecretBuffer.store64_be pnb_store pn;
  let m2 = HST.get () in
  assert (Seq.seq_reveal (B.as_seq m2 (B.gsub dst 0ul 4ul)) `Seq.equal` Seq.create 4 0uy);
  assert (Seq.seq_reveal (B.as_seq m2 dst) `Seq.equal` (Seq.create 4 0uy `Seq.append` FStar.Endianness.n_to_be 8 (Secret.v pn)));
  QUIC.Impl.Lemmas.n_to_be_lower' 8 12 (Secret.v pn);
  assert (Seq.seq_reveal (B.as_seq m2 dst) == FStar.Endianness.n_to_be 12 (Secret.v pn));
  QUIC.Impl.Lemmas.op_inplace dst siv 12ul 0ul (Secret.logxor #Secret.U8 #Secret.SEC);
  QUIC.Impl.Lemmas.secret_xor_inplace_eq (B.as_seq m2 dst) (B.as_seq m2 siv) 0
val iv_for_encrypt_decrypt
      (a: ea)
      (siv dst: B.buffer Secret.uint8)
      (h: G.erased Spec.header {~(Spec.is_retry h)})
      (pn_len: PN.packet_number_length_t)
      (pn: PN.packet_number_t)
    : HST.Stack unit
      (requires (fun m -> iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m))
      (ensures (fun m _ m' -> iv_for_encrypt_decrypt_post a siv dst h pn_len pn m m'))
let iv_for_encrypt_decrypt
      (a: ea)
      (siv dst: B.buffer Secret.uint8)
      (h: G.erased Spec.header {~(Spec.is_retry h)})
      (pn_len: PN.packet_number_length_t)
      (pn: PN.packet_number_t)
    : HST.Stack unit
      (requires (fun m -> iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m))
      (ensures (fun m _ m' -> iv_for_encrypt_decrypt_post a siv dst h pn_len pn m m')) =
let m0 = HST.get () in
B.fill dst (Secret.to_u8 0uy) 12ul;
let pnb_store = B.sub dst 4ul 8ul in
SecretBuffer.store64_be pnb_store pn;
let m2 = HST.get () in
assert ((Seq.seq_reveal (B.as_seq m2 (B.gsub dst 0ul 4ul))) `Seq.equal` (Seq.create 4 0uy));
assert ((Seq.seq_reveal (B.as_seq m2 dst))
    `Seq.equal`
    ((Seq.create 4 0uy) `Seq.append` (FStar.Endianness.n_to_be 8 (Secret.v pn))));
QUIC.Impl.Lemmas.n_to_be_lower' 8 12 (Secret.v pn);
assert (Seq.seq_reveal (B.as_seq m2 dst) == FStar.Endianness.n_to_be 12 (Secret.v pn));
QUIC.Impl.Lemmas.op_inplace dst siv 12ul 0ul (Secret.logxor #Secret.U8 #Secret.SEC);
QUIC.Impl.Lemmas.secret_xor_inplace_eq (B.as_seq m2 dst) (B.as_seq m2 siv) 0
QUIC.Impl.fst.checked
Spec.Agile.HKDF.fsti.checked Spec.Agile.Cipher.fsti.checked Spec.Agile.AEAD.fsti.checked QUIC.Spec.Header.Parse.fsti.checked QUIC.Spec.Header.fsti.checked QUIC.Spec.fst.checked QUIC.Secret.Seq.fsti.checked QUIC.Secret.Buffer.fsti.checked QUIC.Impl.Lemmas.fsti.checked QUIC.Impl.Header.Parse.fsti.checked QUIC.Impl.Header.Base.fst.checked QUIC.Impl.Header.fsti.checked prims.fst.checked LowStar.ImmutableBuffer.fst.checked LowStar.BufferOps.fst.checked LowStar.Buffer.fst.checked Lib.RawIntTypes.fsti.checked FStar.UInt8.fsti.checked FStar.UInt64.fsti.checked FStar.UInt32.fsti.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked FStar.HyperStack.ST.fsti.checked FStar.HyperStack.fst.checked FStar.Ghost.fsti.checked FStar.Endianness.fsti.checked EverCrypt.HKDF.fsti.checked EverCrypt.Helpers.fsti.checked EverCrypt.Error.fsti.checked EverCrypt.CTR.fsti.checked EverCrypt.Cipher.fsti.checked EverCrypt.AEAD.fsti.checked
QUIC.Impl.fst
QUIC.Spec.Crypto.ea LowStar.Buffer.buffer Lib.IntTypes.uint8 FStar.Ghost.erased QUIC.Spec.Header.Base.header Prims.l_not Prims.b2t QUIC.Spec.Header.Base.is_retry FStar.Ghost.reveal QUIC.Spec.PacketNumber.Base.packet_number_length_t QUIC.Spec.PacketNumber.Base.packet_number_t QUIC.Impl.Lemmas.secret_xor_inplace_eq LowStar.Monotonic.Buffer.as_seq LowStar.Buffer.trivial_preorder Prims.unit QUIC.Impl.Lemmas.op_inplace FStar.UInt32.__uint_to_t Lib.IntTypes.logxor Lib.IntTypes.U8 Lib.IntTypes.SEC Prims._assert Prims.eq2 FStar.Seq.Base.seq FStar.UInt8.t QUIC.Secret.Seq.seq_reveal FStar.Endianness.n_to_be QUIC.Secret.Int.Base.v Lib.IntTypes.U64 QUIC.Impl.Lemmas.n_to_be_lower' FStar.Seq.Base.equal Lib.IntTypes.uint_t Lib.IntTypes.PUB FStar.Seq.Base.append FStar.Seq.Base.create FStar.UInt8.__uint_to_t LowStar.Buffer.gsub FStar.Monotonic.HyperStack.mem FStar.HyperStack.ST.get QUIC.Secret.Buffer.store64_be LowStar.Monotonic.Buffer.mbuffer Lib.IntTypes.int_t LowStar.Buffer.sub FStar.Ghost.hide FStar.UInt32.t LowStar.Monotonic.Buffer.fill Lib.IntTypes.to_u8 Lib.IntTypes.U1 QUIC.Impl.iv_for_encrypt_decrypt_pre QUIC.Impl.iv_for_encrypt_decrypt_post
module QUIC.Impl

// This MUST be kept in sync with QUIC.Impl.fsti...
module G = FStar.Ghost
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
module Seq = QUIC.Secret.Seq
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module HST = FStar.HyperStack.ST

module U64 = FStar.UInt64
module U32 = FStar.UInt32
module U8 = FStar.UInt8

open FStar.HyperStack
open FStar.HyperStack.ST

open EverCrypt.Helpers
open EverCrypt.Error

module Cipher = EverCrypt.Cipher
module AEAD = EverCrypt.AEAD
module HKDF = EverCrypt.HKDF
module CTR = EverCrypt.CTR

friend QUIC.Spec

open QUIC.Impl.Lemmas

open LowStar.BufferOps

//module HeaderS = QUIC.Spec.Header
//module HeaderI = QUIC.Impl.Header

#set-options "--z3rlimit 64 --query_stats"

#restart-solver

module SAEAD = Spec.Agile.AEAD
module SCipher = Spec.Agile.Cipher
module SHKDF = Spec.Agile.HKDF

module SecretBuffer = QUIC.Secret.Buffer


(* There are a few places where EverCrypt needs public data whereas it
could/should be secret. Thus, we may need some declassification
locally using Lib.RawIntTypes, but we definitely don't want to make
secret integers globally transparent using friend *)

module ADMITDeclassify = Lib.RawIntTypes

unfold
let iv_for_encrypt_decrypt_pre
  (a: ea)
  (siv: B.buffer Secret.uint8)
  (dst: B.buffer Secret.uint8)
  (h: G.erased Spec.header { ~ (Spec.is_retry h) })
  (pn_len: PN.packet_number_length_t)
  (pn: PN.packet_number_t)
  (m: HS.mem)
: GTot Type0
=
  let a' = SAEAD.cipher_alg_of_supported_alg a in
  B.all_disjoint [
    B.loc_buffer siv;
    B.loc_buffer dst;
  ] /\
  B.live m siv /\ B.length siv == 12 /\
  B.live m dst /\ B.length dst == 12 /\
  pn_len == Spec.pn_length h /\
  pn == Spec.packet_number h

unfold
let iv_for_encrypt_decrypt_post
  (a: ea)
  (siv: B.buffer Secret.uint8)
  (dst: B.buffer Secret.uint8)
  (h: G.erased Spec.header { ~ (Spec.is_retry h) })
  (pn_len: PN.packet_number_length_t)
  (pn: PN.packet_number_t)
  (m: HS.mem)
  (m' : HS.mem)
: GTot Type0
=
  iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m /\
  begin
    B.modifies (B.loc_buffer dst) m m' /\
    B.as_seq m' dst `Seq.equal`
      Spec.iv_for_encrypt_decrypt a (B.as_seq m siv) h
  end

let iv_for_encrypt_decrypt
  (a: ea)
  (siv: B.buffer Secret.uint8)
  (dst: B.buffer Secret.uint8)
  (h: G.erased Spec.header { ~ (Spec.is_retry h) })
  (pn_len: PN.packet_number_length_t)
  (pn: PN.packet_number_t)
: HST.Stack unit
  (requires (fun m ->
    iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m
  ))
  (ensures (fun m _ m' ->
    iv_for_encrypt_decrypt_post a siv dst h pn_len pn m m'
val iv_for_encrypt_decrypt
      (a: ea)
      (siv dst: B.buffer Secret.uint8)
      (h: G.erased Spec.header {~(Spec.is_retry h)})
      (pn_len: PN.packet_number_length_t)
      (pn: PN.packet_number_t)
    : HST.Stack unit
      (requires (fun m -> iv_for_encrypt_decrypt_pre a siv dst h pn_len pn m))
      (ensures (fun m _ m' -> iv_for_encrypt_decrypt_post a siv dst h pn_len pn m m'))

    a: QUIC.Spec.Crypto.ea ->
    siv: LowStar.Buffer.buffer Lib.IntTypes.uint8 ->
    dst: LowStar.Buffer.buffer Lib.IntTypes.uint8 ->
    h:
      FStar.Ghost.erased QUIC.Spec.Header.Base.header
        {~(QUIC.Spec.Header.Base.is_retry (FStar.Ghost.reveal h))} ->
    pn_len: QUIC.Spec.PacketNumber.Base.packet_number_length_t ->
    pn: QUIC.Spec.PacketNumber.Base.packet_number_t
  -> FStar.HyperStack.ST.Stack Prims.unit