Library QedkConcrete.vmemptycall

Empty-recipient CALL with a weaker postcondition

Before code selection the account must still be uncreated and empty. A child call may then create it, so the instruction's postcondition must permit the created-address exception rather than demand empty code.
From Stdlib Require Import ZArith String.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe
  QedkConcrete.vmboundedframe QedkConcrete.vmcreateframe QedkConcrete.vmcallframe
  QedkConcrete.vmentryframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmCallHandlerProgress.
Set Default Goal Selector "!".
Section Opcodes.
  Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
  Context `{U0 : RocqOfOCaml.Basics.Unreachable Z.t}.
  Context `{U1 :
    RocqOfOCaml.Basics.Unreachable (RocqOfOCaml.OCamlSeq.t int)}.
  Context `{U2 : RocqOfOCaml.Basics.Unreachable RocqOfOCaml.OCamlInt32.t}.
  Context `{U3 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}.
  Context `{U4 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}.
  Context `{U5 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}.
  Context `{U7 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.Integer.t}.
  Context `{U8 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.I256.t}.
  Context `{U9 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}.
  Context `{U10 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Delegation.Address.t}.
  Context `{U11 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}.
  Context `{U12 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}.
  Context `{U13 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.M.Option.t Host_t F MonadVM.Numeric.U256.t)}.
  Context `{U15 : RocqOfOCaml.Basics.Unreachable unit}.
  Context `{U16 : RocqOfOCaml.Basics.Unreachable string}.
  Context `{U17 :
    RocqOfOCaml.Basics.Unreachable (option MonadVM.Numeric.U256.t)}.
  Context `{U18 : RocqOfOCaml.Basics.Unreachable (option string)}.
  Context `{U19 :
    RocqOfOCaml.Basics.Unreachable
      (list MonadVM.Byte_string.Bytes.t)}.
  Context `{U20 : RocqOfOCaml.Basics.Unreachable (list int)}.
  Context `{U21 : RocqOfOCaml.Basics.Unreachable int}.
  Context `{U22 : RocqOfOCaml.Basics.Unreachable ascii}.
  Context `{U23 : RocqOfOCaml.Basics.Unreachable (int × int)}.
  Context `{U24 :
    RocqOfOCaml.Basics.Unreachable
      (Z.t → MonadVM.Numeric.U256.t)}.
  Context `{U25 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Support.UintN.t → MonadVM.Support.UintN.t)}.
  Context `{U26 :
    RocqOfOCaml.Basics.Unreachable
      (unit → RocqOfOCaml.OCamlSeq.node int)}.
  Context `{U27 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.opcode_impl Host_t F)}.
  Context `{U28 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.t)}.
  Context `{U31 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}.
  Context `{U32 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Numeric.U256.t → MonadVM.Numeric.U256.t →
        @MonadVM.Vm.Make.Memory.t Host_t F →
        @MonadVM.Vm.Make.M.Option.t Host_t F
          (@MonadVM.Vm.Make.Memory.t Host_t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t Host_t F →
        MonadVM.Support.UintN.t)}.

  Let State := @MonadVM.Vm.Make.MachineState.t Host_t F.

Section Environment.
Context {E : @V.Executor.FArgs Host_t F}.
Context (pre post : Host_t → Prop) (query : Chain.Ethereum.Address.t).

Ltac protected_pop_gas :=
  lazymatch goal with
  | |- gas_nonincreasing (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply map_nonincreasing; protected_pop_gas
  | |- gas_nonincreasing (@V.pop _ _) ⇒ apply pop_nonincreasing
  | |- gas_nonincreasing (@V.pop2 _ _) ⇒ apply pop2_nonincreasing
  | |- gas_nonincreasing (@V.pop3 _ _) ⇒ apply pop3_nonincreasing
  end.

Ltac solve_empty_call_between Hweaken HprimitivePre HprimitivePost Hread Hcall :=
  first [apply bounded_frame_bind_between with (pre := pre);
    [exact Hweaken | solve_host_invariant | protected_pop_gas | ];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_empty_call_between Hweaken HprimitivePre HprimitivePost Hread Hcall |
    intros before Hbound Hinitial;
    eapply call_opcode_protects_empty_recipient with (pre := pre) (post := post) (query := query);
    [exact Hweaken|exact HprimitivePre|exact HprimitivePost|apply Hread| |exact Hbound|exact Hinitial];
    intros nested Hgas Hvalue [Hkind [Hsender Hrecipient]] Hcode;
    apply Hcall; [exact Hgas|unfold childCallFrom; rewrite Hkind; exact Hsender|];
    right; right; intro;
    match goal with Hcode : ?premise → _ ∧ _, Heq : ?premise |- _ ⇒
      exact (proj1 (Hcode Heq)) end].

Lemma call_preserves_empty_recipient_between :
  (∀ h, pre h → post h) →
  PrimitiveHostFrameAt (@V.Host Host_t F) pre (@V.Executor.self Host_t F E) →
  PrimitiveHostFrameAt (@V.Host Host_t F) post (@V.Executor.self Host_t F E) →
  (∀ size h, pre h → (@V.Host Host_t F).(Evmc.HOST.copy_code)
    query 0%Z size h = (Byte_string.Bytes.empty, h)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback post ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
  post (MS.t.host (snd (@V.Executor.call Host_t F E
    state))).
Proof.
  intros Hweaken HprimitivePre HprimitivePost Hread Hcall. unfold V.Executor.call.
  solve_empty_call_between Hweaken HprimitivePre HprimitivePost Hread Hcall.
Qed.

Lemma staticcall_preserves_empty_recipient_between :
  (∀ h, pre h → post h) →
  PrimitiveHostFrameAt (@V.Host Host_t F) pre (@V.Executor.self Host_t F E) →
  PrimitiveHostFrameAt (@V.Host Host_t F) post (@V.Executor.self Host_t F E) →
  (∀ size h, pre h → (@V.Host Host_t F).(Evmc.HOST.copy_code)
    query 0%Z size h = (Byte_string.Bytes.empty, h)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback post ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
  post (MS.t.host (snd (@V.Executor.staticcall Host_t F E
    state))).
Proof.
  intros Hweaken HprimitivePre HprimitivePost Hread Hcall. unfold V.Executor.staticcall.
  solve_empty_call_between Hweaken HprimitivePre HprimitivePost Hread Hcall.
Qed.

End Environment.
End Opcodes.