Library QedkConcrete.hostcomposition

Composing the transaction-context frame through Host code

These are induction steps for the Host/VM recursion. A VM premise here describes the recursive invocation, not an axiom about the final executor. Its gas bound is retained so the concrete VM loop lemma can discharge it.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe.
Set Default Goal Selector "!".

Lemma context_mbind {A B : Set} (action : S.t → A × S.t)
    (next : A → S.t → B × S.t) :
  preservesContext action → (∀ value, preservesContext (next value)) →
  preservesContext (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof. apply context_bind. Qed.

Lemma context_return {A : Set} (value : A) :
  preservesContext (S.M.(Monad.State.State_result._return) value).
Proof. intro s. reflexivity. Qed.

Lemma context_read {A : Set} (lens : Lens.t S.t A) :
  preservesContext (S.M.(Monad.State.State_result.op_exclamation) lens).
Proof. intro s. reflexivity. Qed.

Lemma context_write {A : Set} (lens : Lens.t S.t A) value :
  (∀ s, transactionContext (Lens.t.set lens value s) = transactionContext s) →
  preservesContext (S.M.(Monad.State.State_result.op_coloneq) lens value).
Proof. exact (fun Hframe ⇒ Hframe). Qed.

Lemma context_update {A : Set} (lens : Lens.t S.t A) f :
  (∀ s value, transactionContext (Lens.t.set lens value s) = transactionContext s) →
  preservesContext (S.M.(Monad.State.State_result.update_field) lens f).
Proof. intros Hframe s. apply Hframe. Qed.

Ltac solve_context_frame :=
  lazymatch goal with
  | |- preservesContext (S.M.(Monad.State.State_result.op_letdollar) _ _) ⇒
    apply context_mbind; [solve_context_frame|];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_context_frame
  | |- preservesContext (S.M.(Monad.State.State_result._return) _) ⇒ apply context_return
  | |- preservesContext (S.M.(Monad.State.State_result.op_exclamation) _) ⇒ apply context_read
  | |- preservesContext (S.M.(Monad.State.State_result.op_coloneq) _ _) ⇒
    apply context_write; reflexivity
  | |- preservesContext (S.M.(Monad.State.State_result.update_field) _ _) ⇒
    apply context_update; reflexivity
  | |- preservesContext (if ?test then _ else _) ⇒ destruct test; solve_context_frame
  | |- preservesContext (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct value; solve_context_frame
  | |- preservesContext (match ?status with Evmc.Result.StatusCode.Success ⇒ _ | _ ⇒ _ end) ⇒
    destruct status; solve_context_frame
  | |- preservesContext (@H.transfer_ether _ _ _ _) ⇒ apply transfer_preserves_context
  | Hvm : ∀ msg code, _ → preservesContext (_ msg code)
    |- preservesContext (_ ?msg ?code) ⇒ apply Hvm; assumption
  end.

Section Creation.
Context {F : H.FArgs}.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable Numeric.U256.t}.
Context {Ugas : Basics.Unreachable Gas.t}.
Context {Uaddr : Basics.Unreachable Chain.Ethereum.Address.t}.
Context {Ubytes : Basics.Unreachable Byte_string.Bytes.t}.
Context {Uunit : Basics.Unreachable unit}.
Context {Ustr : Basics.Unreachable string}.
Context {Uint : Basics.Unreachable int}.
Context {Ubyte : Basics.Unreachable ascii}.
Context {Uint64 : Basics.Unreachable int64}.
Context {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma creation_address_read_only msg :
  preservesContext (@H.contract_creation_address F msg).
Proof.
  unfold H.contract_creation_address, H.M. apply context_mbind.
  - apply context_read.
  - intro nonce. apply context_return.
Qed.

Lemma process_create_preserves_context msg :
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesContext ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesContext (@H.process_create F msg).
Proof.
  intros Hvm Hgas. unfold H.process_create, H.M.
  apply context_mbind; [apply creation_address_read_only|].
  intro addr. solve_context_frame.
Qed.
End Creation.

Print Assumptions process_create_preserves_context.