val gctr_encrypt_recursive
      (icb_BE: quad32)
      (plain: gctr_plain_internal_LE)
      (alg: algorithm)
      (key: aes_key_LE alg)
      (i: int)
    : Tot (seq quad32) (decreases %[length plain])
let rec gctr_encrypt_recursive (icb_BE:quad32) (plain:gctr_plain_internal_LE)
                               (alg:algorithm) (key:aes_key_LE alg) (i:int) : Tot (seq quad32) (decreases %[length plain]) =
  if length plain = 0 then empty
  else
    cons (gctr_encrypt_block icb_BE (head plain) alg key i) (gctr_encrypt_recursive icb_BE (tail plain) alg key (i + 1))
val gctr_encrypt_recursive
      (icb_BE: quad32)
      (plain: gctr_plain_internal_LE)
      (alg: algorithm)
      (key: aes_key_LE alg)
      (i: int)
    : Tot (seq quad32) (decreases %[length plain])
let rec gctr_encrypt_recursive
      (icb_BE: quad32)
      (plain: gctr_plain_internal_LE)
      (alg: algorithm)
      (key: aes_key_LE alg)
      (i: int)
    : Tot (seq quad32) (decreases %[length plain]) =
if length plain = 0
then empty
else
  cons (gctr_encrypt_block icb_BE (head plain) alg key i)
    (gctr_encrypt_recursive icb_BE (tail plain) alg key (i + 1))
Vale.AES.GCTR_s.fst.checked
Vale.Def.Words_s.fsti.checked Vale.Def.Types_s.fst.checked Vale.Def.Prop_s.fst.checked Vale.Def.Opaque_s.fsti.checked Vale.AES.AES_s.fst.checked prims.fst.checked FStar.Seq.fst.checked FStar.Pervasives.fsti.checked FStar.Mul.fst.checked
Vale.AES.GCTR_s.fst
Vale.Def.Types_s.quad32 Vale.AES.GCTR_s.gctr_plain_internal_LE Vale.AES.AES_common_s.algorithm Vale.AES.AES_s.aes_key_LE Prims.int Prims.op_Equality FStar.Seq.Base.length FStar.Seq.Base.empty Prims.bool FStar.Seq.Properties.cons Vale.AES.GCTR_s.gctr_encrypt_block FStar.Seq.Properties.head Vale.AES.GCTR_s.gctr_encrypt_recursive FStar.Seq.Properties.tail Prims.op_Addition FStar.Seq.Base.seq
module Vale.AES.GCTR_s

// IMPORTANT: Following NIST's specification, this spec is written assuming a big-endian mapping from bytes to quad32s
//            Since the AES spec (AES_s) is in little-endian, we need to byteswap each time we call AES

open Vale.Def.Prop_s
open Vale.Def.Opaque_s
open Vale.Def.Words_s
open Vale.Def.Types_s
open FStar.Mul
open Vale.AES.AES_s
open FStar.Seq

// The max length of pow2_32 corresponds to the max length of buffers in Low*
// length plain < pow2_32 <= spec max of 2**39 - 256;
let is_gctr_plain_LE (p:seq nat8) : prop0 = length p < pow2_32
type gctr_plain_LE:eqtype = p:seq nat8 { is_gctr_plain_LE p }
type gctr_plain_internal_LE:eqtype = seq quad32

let inc32 (cb:quad32) (i:int) : quad32 =
  Mkfour ((cb.lo0 + i) % pow2_32) cb.lo1 cb.hi2 cb.hi3

let gctr_encrypt_block (icb_BE:quad32) (plain_LE:quad32) (alg:algorithm) (key:seq nat32) (i:int) : Pure quad32
  (requires is_aes_key_LE alg key)
  (ensures fun _ -> True)
  =
  let icb_LE = reverse_bytes_quad32 (inc32 icb_BE i) in
  quad32_xor plain_LE (aes_encrypt_LE alg key icb_LE)


let rec gctr_encrypt_recursive (icb_BE:quad32) (plain:gctr_plain_internal_LE)
val gctr_encrypt_recursive
      (icb_BE: quad32)
      (plain: gctr_plain_internal_LE)
      (alg: algorithm)
      (key: aes_key_LE alg)
      (i: int)
    : Tot (seq quad32) (decreases %[length plain])

    icb_BE: Vale.Def.Types_s.quad32 ->
    plain: Vale.AES.GCTR_s.gctr_plain_internal_LE ->
    alg: Vale.AES.AES_common_s.algorithm ->
    key: Vale.AES.AES_s.aes_key_LE alg ->
    i: Prims.int
  -> Prims.Tot (FStar.Seq.Base.seq Vale.Def.Types_s.quad32)