val lemma_blocki_aux2 (a: blake_alg) (s1 s2: bytes) (nb1 nb2: nat) (i: nat{i < nb2})
    : Lemma (requires S.length s1 == nb1 * block_length a /\ S.length s2 == nb2 * block_length a)
      (ensures
        Spec.Blake2.get_blocki (to_blake_alg a) s2 i ==
        Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) (i + nb1))
let lemma_blocki_aux2 (a:blake_alg) (s1 s2:bytes) (nb1 nb2:nat) (i:nat{i < nb2})
  : Lemma
    (requires
      S.length s1 == nb1 * block_length a /\
      S.length s2 == nb2 * block_length a)
    (ensures Spec.Blake2.get_blocki (to_blake_alg a) s2 i ==
           Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) (i + nb1))
  = let s = s1 `S.append` s2 in
    let a' = to_blake_alg a in
    calc (==) {
          Spec.Blake2.get_blocki a' s (i + nb1);
          (==) { }
          S.slice s ((i + nb1) * block_length a) ((i + nb1 + 1) * block_length a);
          (==) { }
          S.slice s (i * block_length a + nb1 * block_length a) ((i + 1) * block_length a + nb1 * block_length a);
          (==) { }
          S.slice s (i * block_length a + S.length s1) ((i + 1) * block_length a + S.length s1);
          (==) { S.slice_slice s (S.length s1) (S.length s) (i * block_length a) ((i+1) * block_length a) }
          S.slice (S.slice s (S.length s1) (S.length s)) (i * block_length a) ((i+1) * block_length a);
          (==) { S.append_slices s1 s2; assert (s2 `S.equal` S.slice s (S.length s1) (S.length s)) }
          S.slice s2 (i * block_length a) ((i+1) * block_length a);
          (==) { }
          Spec.Blake2.get_blocki a' s2 i;
        }
val lemma_blocki_aux2 (a: blake_alg) (s1 s2: bytes) (nb1 nb2: nat) (i: nat{i < nb2})
    : Lemma (requires S.length s1 == nb1 * block_length a /\ S.length s2 == nb2 * block_length a)
      (ensures
        Spec.Blake2.get_blocki (to_blake_alg a) s2 i ==
        Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) (i + nb1))
let lemma_blocki_aux2 (a: blake_alg) (s1 s2: bytes) (nb1 nb2: nat) (i: nat{i < nb2})
    : Lemma (requires S.length s1 == nb1 * block_length a /\ S.length s2 == nb2 * block_length a)
      (ensures
        Spec.Blake2.get_blocki (to_blake_alg a) s2 i ==
        Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) (i + nb1)) =
let s = s1 `S.append` s2 in
let a' = to_blake_alg a in
calc ( == ) {
  Spec.Blake2.get_blocki a' s (i + nb1);
  ( == ) { () }
  S.slice s ((i + nb1) * block_length a) ((i + nb1 + 1) * block_length a);
  ( == ) { () }
  S.slice s
    (i * block_length a + nb1 * block_length a)
    ((i + 1) * block_length a + nb1 * block_length a);
  ( == ) { () }
  S.slice s (i * block_length a + S.length s1) ((i + 1) * block_length a + S.length s1);
  ( == ) { S.slice_slice s
    (S.length s1)
    (S.length s)
    (i * block_length a)
    ((i + 1) * block_length a) }
  S.slice (S.slice s (S.length s1) (S.length s)) (i * block_length a) ((i + 1) * block_length a);
  ( == ) { (S.append_slices s1 s2;
    assert (s2 `S.equal` (S.slice s (S.length s1) (S.length s)))) }
  S.slice s2 (i * block_length a) ((i + 1) * block_length a);
  ( == ) { () }
  Spec.Blake2.get_blocki a' s2 i;
}
Spec.Hash.Lemmas.fst.checked
Spec.SHA3.fst.checked Spec.Hash.Definitions.fst.checked Spec.Blake2.fst.checked Spec.Agile.Hash.fst.checked Spec.Agile.Hash.fst.checked prims.fst.checked Lib.UpdateMulti.fst.checked Lib.Sequence.Lemmas.fsti.checked Lib.Sequence.fsti.checked Lib.LoopCombinators.fsti.checked Lib.IntTypes.fsti.checked FStar.Seq.fst.checked FStar.Pervasives.fsti.checked FStar.Mul.fst.checked FStar.Math.Lemmas.fst.checked FStar.Classical.fsti.checked FStar.Calc.fsti.checked
Spec.Hash.Lemmas.fst
Spec.Hash.Definitions.blake_alg Spec.Hash.Definitions.bytes Prims.nat Prims.b2t Prims.op_LessThan FStar.Calc.calc_finish Spec.Blake2.block_s Prims.eq2 Spec.Blake2.get_blocki Prims.op_Addition Prims.Cons FStar.Preorder.relation Prims.Nil Prims.unit FStar.Calc.calc_step FStar.Seq.Base.slice Lib.IntTypes.uint8 FStar.Mul.op_Star Spec.Hash.Definitions.block_length FStar.Seq.Base.length FStar.Calc.calc_init FStar.Calc.calc_pack Prims.squash FStar.Seq.Properties.slice_slice Prims._assert FStar.Seq.Base.equal FStar.Seq.Properties.append_slices Spec.Blake2.alg Spec.Hash.Definitions.to_blake_alg FStar.Seq.Base.seq Lib.IntTypes.int_t Lib.IntTypes.U8 Lib.IntTypes.SEC FStar.Seq.Base.append Prims.l_and Prims.int FStar.Pervasives.pattern
module Spec.Hash.Lemmas

module S = FStar.Seq
open Lib.IntTypes

open Spec.Agile.Hash
open Spec.Hash.Definitions

friend Spec.Agile.Hash

(** Lemmas about the behavior of update_multi / update_last *)

#push-options "--fuel 0 --ifuel 1 --z3rlimit 50"

let update_multi_zero a h =
  match a with
  | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
    Lib.UpdateMulti.update_multi_zero (block_length a) (Spec.Agile.Hash.update a) h
  | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
    let rateInBytes = rate a / 8 in
    let f = Spec.SHA3.absorb_inner rateInBytes in
    Lib.Sequence.lemma_repeat_blocks_multi rateInBytes S.empty f h;

    let nb = 0 / rateInBytes in
    Lib.LoopCombinators.eq_repeati0 nb (Lib.Sequence.repeat_blocks_f rateInBytes S.empty f nb) h

let update_multi_zero_blake a prevlen h =
  Lib.LoopCombinators.eq_repeati0 (0 / block_length a) (Spec.Blake2.blake2_update1 (to_blake_alg a) prevlen S.empty) h
#pop-options

#push-options "--fuel 1"
let update_multi_update (a: md_alg) (h: words_state a) (input: bytes_block a): Lemma
  (ensures (update_multi a h () input) == (update a h input))
=
  let h1 = update_multi a h () input in
  assert(h1 == Lib.UpdateMulti.mk_update_multi (block_length a) (update a) h input);
  if S.length input = 0 then
    begin
    assert(h1 == h)
    end
  else
    begin
    let block, rem = Lib.UpdateMulti.split_block (block_length a) input 1 in
    let h2 = update a h block in
    assert(rem `Seq.equal` Seq.empty);
    assert(block `Seq.equal` input);
    let h3 = Lib.UpdateMulti.mk_update_multi (block_length a) (update a) h2 rem in
    assert(h1 == h3)
    end
#pop-options

let update_multi_associative (a: hash_alg{not (is_blake a)})
  (h: words_state a)
  (input1: bytes)
  (input2: bytes):
  Lemma
  (requires
    S.length input1 % block_length a == 0 /\
    S.length input2 % block_length a == 0)
  (ensures (
    let input = S.append input1 input2 in
    S.length input % block_length a == 0 /\
    update_multi a (update_multi a h () input1) () input2 == update_multi a h () input))
  = match a with
  | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 ->
    Lib.UpdateMulti.update_multi_associative (block_length a) (Spec.Agile.Hash.update a) h input1 input2
  | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 ->
    let rateInBytes = rate a /8 in
    let f = Spec.SHA3.absorb_inner rateInBytes in
    let input = input1 `S.append` input2 in
    assert (input1 `S.equal` S.slice input 0 (S.length input1));
    assert (input2 `S.equal` S.slice input (S.length input1) (S.length input));
    Lib.Sequence.Lemmas.repeat_blocks_multi_split (block_length a) (S.length input1) input f h

let lemma_blocki_aux1 (a:blake_alg) (s1 s2:bytes) (i:nat{i < S.length s1 / block_length a})
  : Lemma (Spec.Blake2.get_blocki (to_blake_alg a) s1 i == Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) i)
  = assert (Spec.Blake2.get_blocki (to_blake_alg a) s1 i `S.equal` Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) i)

#push-options "--fuel 0 --ifuel 0 --z3rlimit 300"

open FStar.Mul

let lemma_blocki_aux2 (a:blake_alg) (s1 s2:bytes) (nb1 nb2:nat) (i:nat{i < nb2})
  : Lemma
    (requires
      S.length s1 == nb1 * block_length a /\
      S.length s2 == nb2 * block_length a)
    (ensures Spec.Blake2.get_blocki (to_blake_alg a) s2 i ==
val lemma_blocki_aux2 (a: blake_alg) (s1 s2: bytes) (nb1 nb2: nat) (i: nat{i < nb2})
    : Lemma (requires S.length s1 == nb1 * block_length a /\ S.length s2 == nb2 * block_length a)
      (ensures
        Spec.Blake2.get_blocki (to_blake_alg a) s2 i ==
        Spec.Blake2.get_blocki (to_blake_alg a) (S.append s1 s2) (i + nb1))

    a: Spec.Hash.Definitions.blake_alg ->
    s1: Spec.Hash.Definitions.bytes ->
    s2: Spec.Hash.Definitions.bytes ->
    nb1: Prims.nat ->
    nb2: Prims.nat ->
    i: Prims.nat{i < nb2}
  -> FStar.Pervasives.Lemma
      (requires
        FStar.Seq.Base.length s1 == nb1 * Spec.Hash.Definitions.block_length a /\
        FStar.Seq.Base.length s2 == nb2 * Spec.Hash.Definitions.block_length a)
      (ensures
        Spec.Blake2.get_blocki (Spec.Hash.Definitions.to_blake_alg a) s2 i ==
        Spec.Blake2.get_blocki (Spec.Hash.Definitions.to_blake_alg a)
          (FStar.Seq.Base.append s1 s2)
          (i + nb1))