Library QedkConcrete.blakeoutputs

BLAKE2F return-data bound

The compression function emits eight words, independently of round count and input values. Each word's serializer emits eight bytes.
From Stdlib Require Import ZArith Lia List.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles MonadVM.Support.CryptoBlake2
  QedkConcrete.precompileoutputs QedkConcrete.bytewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma blake_output_words {UW : Basics.Unreachable CryptoBlake2.word} remaining index hash state :
  Lists.List.length (@CryptoBlake2.output_nat UW remaining index hash state) = remaining.
Proof.
  revert index. induction remaining; intro index; [reflexivity|].
  cbn [CryptoBlake2.output_nat Lists.List.length]. rewrite IHremaining. reflexivity.
Qed.

Lemma native_concat_length bytes count :
  Forall (fun s ⇒ OCamlString.length s = count) bytes →
  OCamlString.length (Byte_string.Bytes.concat Byte_string.Bytes.empty bytes) =
    Z.of_nat (Lists.List.length bytes) × count.
Proof.
  intro Hall. induction Hall as [|bytes rest Hbytes Hrest IH].
  - reflexivity.
  - destruct rest as [|next rest].
    + cbn [Byte_string.Bytes.concat MonadVM.String.concat OCamlString.concat Lists.List.length].
      rewrite Hbytes. lia.
    + change (OCamlString.length (String.append bytes
        (Byte_string.Bytes.concat Byte_string.Bytes.empty (next :: rest))) =
        Z.of_nat (S (Lists.List.length (next :: rest))) × count).
      rewrite byte_append_length, IH, Hbytes, Nat2Z.inj_succ. nia.
Qed.

Lemma native_list_map {A B : Set} (f : A → B) values :
  PC.List.map f values = Lists.List.map f values.
Proof.
  revert values. fix IH 1. intros [|a [|b rest]]; [reflexivity|reflexivity|].
  change (f a :: f b :: PC.List.map f rest = f a :: f b :: Lists.List.map f rest).
  rewrite IH. reflexivity.
Qed.

Lemma blake2f_output_bounded
  `{_rocq_assumption_0 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}
  `{_rocq_assumption_1 : RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}
  `{_rocq_assumption_2 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U64.Repr.t}
  `{_rocq_assumption_3 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U64.t}
  `{_rocq_assumption_4 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U32.Repr.t}
  `{_rocq_assumption_5 : RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U32.t}
  `{_rocq_assumption_6 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}
  `{_rocq_assumption_7 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}
  `{_rocq_assumption_8 : RocqOfOCaml.Basics.Unreachable unit}
  `{_rocq_assumption_9 : RocqOfOCaml.Basics.Unreachable string}
  `{_rocq_assumption_10 : RocqOfOCaml.Basics.Unreachable int}
  `{_rocq_assumption_11 : RocqOfOCaml.Basics.Unreachable ascii}
  `{_rocq_assumption_12 : RocqOfOCaml.Basics.Unreachable (int × int)}
  `{_rocq_assumption_13 : RocqOfOCaml.Basics.Unreachable int64}
  (msg : MonadVM.Evmc.Message.t) :
  OCamlString.length (Evmc.Result.t.output_data
    (@Precompiles.blake2f msg)) ≤ 64.
Proof.
  unfold Precompiles.blake2f. apply native_run_output; [lia|].
  repeat match goal with
  | |- nativeOutputBound _ (PC.op_letdollar _ _) ⇒
    apply native_output_bind; intro; cbn beta iota zeta
  end.
  apply native_output_return.
  rewrite native_list_map, native_concat_length with (count := 8).
  - unfold OCamlIarray.to_list. rewrite Lists.List.length_map.
    unfold CryptoBlake2.blake2f. rewrite blake_output_words. lia.
  - apply Lists.List.Forall_forall. intros bytes Hbytes.
    apply Lists.List.in_map_iff in Hbytes as [word [<- _]].
    apply init_length. lia.
Qed.

Print Assumptions blake2f_output_bounded.