val pad_to_128_bits (p: seq nat8)
    : Pure (seq nat8)
      (requires True)
      (ensures fun q -> length q % 16 == 0 /\ length q <= length p + 15)
let pad_to_128_bits (p:seq nat8) : Pure (seq nat8)
  (requires True)
  (ensures fun q -> length q % 16 == 0 /\ length q <= length p + 15)
  =
  let num_extra_bytes = length p % 16 in
  if num_extra_bytes = 0 then p
  else p @| (create (16 - num_extra_bytes) 0)
val pad_to_128_bits (p: seq nat8)
    : Pure (seq nat8)
      (requires True)
      (ensures fun q -> length q % 16 == 0 /\ length q <= length p + 15)
let pad_to_128_bits (p: seq nat8)
    : Pure (seq nat8)
      (requires True)
      (ensures fun q -> length q % 16 == 0 /\ length q <= length p + 15) =
let num_extra_bytes = length p % 16 in
if num_extra_bytes = 0 then p else p @| (create (16 - num_extra_bytes) 0)
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
FStar.Seq.Base.seq Vale.Def.Types_s.nat8 Prims.op_Equality Prims.int Prims.bool FStar.Seq.Base.op_At_Bar FStar.Seq.Base.create Prims.op_Subtraction Prims.op_Modulus FStar.Seq.Base.length Prims.l_True Prims.l_and Prims.eq2 Prims.b2t Prims.op_LessThanOrEqual Prims.op_Addition
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)
                               (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))

let pad_to_128_bits (p:seq nat8) : Pure (seq nat8)
  (requires True)
val pad_to_128_bits (p: seq nat8)
    : Pure (seq nat8)
      (requires True)
      (ensures fun q -> length q % 16 == 0 /\ length q <= length p + 15)
p: FStar.Seq.Base.seq Vale.Def.Types_s.nat8 -> Prims.Pure (FStar.Seq.Base.seq Vale.Def.Types_s.nat8)