val decrypt:
  a: ea ->
  k: AEAD.kv a ->
  static_iv: iv_t a ->
  hpk: Cipher.key (AEAD.cipher_alg_of_supported_alg a) ->
  last: nat{last+1 < pow2 62} ->
  cid_len: nat { cid_len <= 20 } ->
  packet: packet ->
  GTot (r: result {
    match r with
    | Failure -> True
    | Success h _ rem ->
      is_valid_header h cid_len last /\
      Seq.length rem <= Seq.length packet /\
      rem `Seq.equal` Seq.slice packet (Seq.length packet - Seq.length rem) (Seq.length packet)
  })
let decrypt
  a k siv hpk last cid_len packet
=
  let open FStar.Math.Lemmas in
  let open FStar.Endianness in
  match H.header_decrypt a hpk cid_len last packet with
  | H.H_Failure -> Failure
  | H.H_Success h c rem ->
    if is_retry h
    then Success h c rem
    else
      match payload_decrypt a k siv h (Seq.seq_hide c) with
      | None -> Failure
      | Some plain -> Success h (Seq.seq_reveal plain) rem
val decrypt:
  a: ea ->
  k: AEAD.kv a ->
  static_iv: iv_t a ->
  hpk: Cipher.key (AEAD.cipher_alg_of_supported_alg a) ->
  last: nat{last+1 < pow2 62} ->
  cid_len: nat { cid_len <= 20 } ->
  packet: packet ->
  GTot (r: result {
    match r with
    | Failure -> True
    | Success h _ rem ->
      is_valid_header h cid_len last /\
      Seq.length rem <= Seq.length packet /\
      rem `Seq.equal` Seq.slice packet (Seq.length packet - Seq.length rem) (Seq.length packet)
  })
let decrypt a k siv hpk last cid_len packet =
let open FStar.Math.Lemmas in
let open FStar.Endianness in
match H.header_decrypt a hpk cid_len last packet with
| H.H_Failure -> Failure
| H.H_Success h c rem ->
  if is_retry h
  then Success h c rem
  else
    match payload_decrypt a k siv h (Seq.seq_hide c) with
    | None -> Failure
    | Some plain -> Success h (Seq.seq_reveal plain) rem
QUIC.Spec.fst.checked
Spec.Hash.Definitions.fst.checked Spec.Agile.HKDF.fsti.checked Spec.Agile.Hash.fsti.checked Spec.Agile.Cipher.fsti.checked Spec.Agile.AEAD.fsti.checked QUIC.Spec.Lemmas.fst.checked QUIC.Spec.Header.Parse.fsti.checked QUIC.Spec.Header.Base.fst.checked QUIC.Spec.Header.fsti.checked QUIC.Secret.Seq.fsti.checked QUIC.Secret.Int.fsti.checked prims.fst.checked FStar.UInt8.fsti.checked FStar.UInt32.fsti.checked FStar.Pervasives.fsti.checked FStar.Math.Lemmas.fst.checked FStar.Endianness.fsti.checked
QUIC.Spec.fst
QUIC.Spec.Crypto.ea Spec.Agile.AEAD.kv QUIC.Spec.iv_t Spec.Agile.Cipher.key Spec.Agile.AEAD.cipher_alg_of_supported_alg Prims.nat Prims.b2t Prims.op_LessThan Prims.op_Addition Prims.pow2 Prims.op_LessThanOrEqual QUIC.Spec.Crypto.packet QUIC.Spec.Header.header_decrypt QUIC.Spec.Failure QUIC.Spec.Header.Base.header QUIC.Spec.Base.bytes QUIC.Spec.Header.Base.is_retry Prims.eq2 Prims.int Prims.bool Prims.l_and QUIC.Spec.Crypto.max_cipher_length FStar.Seq.Base.length QUIC.Spec.Base.byte QUIC.Spec.Success QUIC.Spec.payload_decrypt QUIC.Secret.Seq.seq_hide Lib.IntTypes.U8 Spec.Agile.AEAD.decrypted QUIC.Secret.Seq.seq_reveal Lib.IntTypes.SEC QUIC.Spec.result Prims.l_True QUIC.Spec.Header.Base.is_valid_header FStar.Seq.Base.equal FStar.Seq.Base.slice Prims.op_Subtraction
module QUIC.Spec

open QUIC.Spec.Lemmas
open QUIC.Spec.Header.Base

module Seq = QUIC.Secret.Seq
module U32 = FStar.UInt32
module U8 = FStar.UInt8
module H = Spec.Agile.Hash
module HD = Spec.Hash.Definitions
module Cipher = Spec.Agile.Cipher
module AEAD = Spec.Agile.AEAD
module HKDF = Spec.Agile.HKDF
module Parse = QUIC.Spec.Header.Parse
module H = QUIC.Spec.Header
module Secret = QUIC.Secret.Int

/// encryption of a packet

let iv_for_encrypt_decrypt
  (a: ea)
  (siv: iv_t a)
  (h: header { ~ (is_retry h) })
: GTot (iv_t a)
=
  let pn_len = Secret.v (pn_length h) - 1 in
  let seqn = packet_number h in
  let _ = assert_norm(pow2 62 < pow2 (8 `op_Multiply` 12)) in
  let pnb = FStar.Endianness.n_to_be 12 (Secret.v seqn) in
  Seq.seq_hide #Secret.U8 (xor_inplace pnb (Seq.seq_reveal siv) 0)

let payload_encrypt
  (a: ea)
  (k: AEAD.kv a)
  (siv: iv_t a)
  (h: header { ~ (is_retry h) })
  (plain: pbytes)
: GTot (cbytes)
=
  let aad = Parse.format_header h in
  let iv = iv_for_encrypt_decrypt a siv h in
  Seq.seq_reveal (AEAD.encrypt #a k iv (Seq.seq_hide aad) (Seq.seq_hide plain))

let encrypt
  a k siv hpk h plain
=
  let cipher =
    if is_retry h
    then plain
    else payload_encrypt a k siv h plain
  in
  H.header_encrypt a hpk h cipher

let encrypt_length
  a k siv hpk h plain
=
  let c =
    if is_retry h
    then plain
    else payload_encrypt a k siv h plain
  in
  H.header_encrypt_length a hpk h c

#restart-solver

let payload_decrypt
  (a: ea)
  (k: AEAD.kv a)
  (siv: iv_t a)
  (h: header { ~ (is_retry h) })
  (c: Seq.seq Secret.uint8 { 16 <= Seq.length c /\ Seq.length c < max_cipher_length })
: GTot (option (AEAD.decrypted c))
= 
  let iv = iv_for_encrypt_decrypt a siv h in
  let aad = Parse.format_header h in
  AEAD.decrypt #a k iv (Seq.seq_hide aad) c

let decrypt
  a k siv hpk last cid_len packet
val decrypt:
  a: ea ->
  k: AEAD.kv a ->
  static_iv: iv_t a ->
  hpk: Cipher.key (AEAD.cipher_alg_of_supported_alg a) ->
  last: nat{last+1 < pow2 62} ->
  cid_len: nat { cid_len <= 20 } ->
  packet: packet ->
  GTot (r: result {
    match r with
    | Failure -> True
    | Success h _ rem ->
      is_valid_header h cid_len last /\
      Seq.length rem <= Seq.length packet /\
      rem `Seq.equal` Seq.slice packet (Seq.length packet - Seq.length rem) (Seq.length packet)
  })

    a: QUIC.Spec.Crypto.ea ->
    k: Spec.Agile.AEAD.kv a ->
    static_iv: QUIC.Spec.iv_t a ->
    hpk: Spec.Agile.Cipher.key (Spec.Agile.AEAD.cipher_alg_of_supported_alg a) ->
    last: Prims.nat{last + 1 < Prims.pow2 62} ->
    cid_len: Prims.nat{cid_len <= 20} ->
    packet: QUIC.Spec.Crypto.packet
  -> Prims.GTot
    (r:
      QUIC.Spec.result
        { (match r with
            | QUIC.Spec.Failure -> Prims.l_True
            | QUIC.Spec.Success h _ rem ->
              QUIC.Spec.Header.Base.is_valid_header h cid_len last /\
              FStar.Seq.Base.length rem <= FStar.Seq.Base.length packet /\
              FStar.Seq.Base.equal rem
                (FStar.Seq.Base.slice packet
                    (FStar.Seq.Base.length packet - FStar.Seq.Base.length rem)
                    (FStar.Seq.Base.length packet)))
          <:
          Type0 })