Library QedkConcrete.vmhashwords

Hash-producing instructions

Hashing arbitrary bytes returns a word under the approved primitive output-size contract. In particular SHA3 does not need to assume that reading memory returns any particular contents.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmmachinewords QedkConcrete.wordrange
  QedkConcrete.bytewords QedkConcrete.vmbytewords QedkConcrete.hashwords
  QedkConcrete.vmhostwords QedkConcrete.hostframe QedkConcrete.contexthostinstance
  QedkConcrete.vmhostframe QedkConcrete.supplyhostinstance QedkConcrete.supply.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}
  {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}.

Lemma keccak_preserves_words

     :
  keccak256OutputSize →
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  machineSafe invariant (fun _ ⇒ True)
    (@V.Executor.keccak Host_t F E).
Proof.
  intros Hsize Hrevision. unfold V.Executor.keccak.
  eapply safe_bind; [apply safe_pop2|]. intros [start count] _.
  cbn zeta. eapply safe_bind.
  - apply (extend_memory_preserves_words invariant
      (UN := Opaque.legacy_witness 0%N)
      (Ucost := Opaque.legacy_witness (fun _ ⇒ 0%N))
      (UE := Opaque.legacy_witness (fun _ _ _ ⇒ None))); exact Hrevision.
  - intros cost _. eapply safe_bind; [apply safe_spend|]. intros [] _.
    eapply safe_bind; [apply safe_fmap, safe_memory_read|]. intros bytes _.
    eapply safe_bind; [apply safe_push, keccak_word; exact Hsize|]. intros [] _. apply safe_increase_pc.
Qed.

Lemma extcodehash_words_from_host

     :
  (∀ addr, machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.access_account Host_t F addr)) →
  (∀ addr, machineSafe invariant
    (fun result ⇒ ∀ hash, result = Some hash → OCamlString.length hash = 32)
    (@V.M.HostAPI.get_code_hash Host_t F addr)) →
  machineSafe invariant (fun _ ⇒ True)
    (@V.Executor.extcodehash Host_t F E).
Proof.
  intros Haccess Hhash. unfold V.Executor.extcodehash.
  eapply safe_bind; [apply safe_fmap, safe_pop|]. intros address _.
  eapply safe_bind; [apply safe_fmap, Haccess|]. intros cost _.
  eapply safe_bind; [apply safe_spend|]. intros [] _.
  eapply safe_bind; [apply safe_fmap, Hhash|]. intros hash [result [Hresult ->]].
  eapply safe_bind.
  - apply safe_push, word_repr. destruct result as [value|].
    + specialize (Hresult value eq_refl). cbn. lia.
    + change (OCamlString.length (@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32) ≤ 32).
      rewrite b32_zeros_length. lia.
  - intros [] _. apply safe_increase_pc.
Qed.

Lemma blockhash_words_from_host
     :
  (∀ number, machineSafe invariant
    (fun result ⇒ ∀ hash, result = Some hash → OCamlString.length hash = 32)
    (@V.M.HostAPI.get_block_hash Host_t F number)) →
  machineSafe invariant (fun _ ⇒ True) (@V.Executor.blockhash Host_t F E).
Proof.
  intro Hhash. unfold V.Executor.blockhash.
  eapply safe_bind; [apply safe_fmap, safe_pop|]. intros number _.
  eapply safe_bind; [apply safe_spend|]. intros [] _.
  cbn zeta. eapply safe_bind with (P := wordRange).
  - match goal with |- machineSafe _ _ (if ?outside then _ else _) ⇒ destruct outside end.
    + apply safe_return, word_zero.
    + eapply safe_bind; [apply Hhash|]. intros [hash|] Hresult; [|apply safe_fail].
      apply safe_return, word_repr. specialize (Hresult hash eq_refl). lia.
  - intros hash Hword. eapply safe_bind; [apply safe_push; exact Hword|]. intros [] _. apply safe_increase_pc.
Qed.
End Instructions.

Section Concrete.
Context {F : Host.Instantiate.FArgs} (Hsize : keccak256OutputSize).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation VF := (V.Build_FArgs params (HostF vm)).
Local Abbreviation invariant := (fun s ⇒ balanceInvariant (S.t.world_state s) ∧
  totalBalance (S.t.world_state s) ≤ limit).

Lemma concrete_get_code_hash_words addr :
  machineSafe invariant
    (fun result ⇒ ∀ hash, result = Some hash → OCamlString.length hash = 32)
    (@V.M.HostAPI.get_code_hash S.t VF addr).
Proof.
  apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_code_hash]. intros hash Hhash. eapply get_code_hash_length; eassumption.
Qed.

Lemma concrete_get_block_hash_words number :
  machineSafe invariant
    (fun result ⇒ ∀ hash, result = Some hash → OCamlString.length hash = 32)
    (@V.M.HostAPI.get_block_hash S.t VF number).
Proof.
  apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_block_hash]. intros hash Hhash. eapply get_block_hash_length; eassumption.
Qed.

Context {E : @V.Executor.FArgs S.t VF}.
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}
  {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}.

Lemma concrete_extcodehash_words

     :
  machineSafe invariant (fun _ ⇒ True)
    (@V.Executor.extcodehash S.t VF E).
Proof.
  apply extcodehash_words_from_host; [apply concrete_access_account_words|apply concrete_get_code_hash_words].
Qed.

Lemma concrete_blockhash_words
     :
  machineSafe invariant (fun _ ⇒ True) (@V.Executor.blockhash S.t VF E).
Proof. apply blockhash_words_from_host, concrete_get_block_hash_words. Qed.
End Concrete.

Print Assumptions keccak_preserves_words.
Print Assumptions concrete_extcodehash_words.
Print Assumptions concrete_blockhash_words.