Library SetcodeConcrete.vmemptyframe

Empty-account calls in the SETCODE VM

The VM executes no instructions for empty bytecode. Ordinary CALL and STATICCALL select the empty account's own code; CALLCODE and DELEGATECALL retain the calling account.
From Stdlib Require Import ZArith NArith String.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vminvariant QedkConcrete.vmhostframe
  QedkConcrete.vmcallframe QedkConcrete.vmcreateframe QedkConcrete.vmboundedframe.
Require Import SetcodeConcrete.vmcontextframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmCallHandlerProgress.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
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.

Section Entry.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
  Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hcontext : ∀ host, snd (V.Host.(Evmc.HOST.get_tx_context) host) = host).
Context (Hsize : ∀ addr host, snd (V.Host.(Evmc.HOST.get_code_size) addr host) = host).
Context (Hcopy : ∀ addr offset size host, snd (V.Host.(Evmc.HOST.copy_code) addr offset size host) = host).
Lemma execute_empty_host msg host :
  snd (@V.execute Host_t F msg Byte_string.Bytes.empty host) = host.
Proof.
  unfold V.execute.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  pose proof (Hcontext host) as Hread.
  destruct (V.Host.(Evmc.HOST.get_tx_context) host) as [context middle] eqn:Hctx.
  cbn [snd] in Hread. subst middle.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  rewrite Hrevision.
  pose proof (Hsize (Evmc.Message.t.recipient msg) host) as HreadSize.
  destruct (V.Host.(Evmc.HOST.get_code_size) (Evmc.Message.t.recipient msg) host)
    as [size middle] eqn:Hsz. cbn [snd] in HreadSize. subst middle.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hlength end.
  2: cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  1: (pose proof (Hcopy (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) as HreadCopy;
    destruct (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0
      Delegation.eoa_delegated_code_length host) as [ownCode middle] eqn:HownCode;
    cbn [snd] in HreadCopy; subst middle;
    change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in HownCode;
    rewrite HownCode;
    cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
  all: match goal with |- context [@V.Executor.run ?ht ?f ?env ?state] ⇒
    pose proof (@generated_run_stop_exact ht f env state Hdebug eq_refl) as Hrun;
    unfold generated_run in Hrun;
    destruct (@V.Executor.run ht f env state) as [result finalState] eqn:Hresult;
    inversion Hrun; subst
  end.
  all: reflexivity.
Qed.
End Entry.
Section RecursiveOpcodes.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Lemma recursive_opcode_preserves (env : @V.Executor.FArgs Host_t F)
    (invariant : Host_t → Prop) opcode :
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  recursiveCallOpcode opcode = true →
  preservesBoundedHost invariant (@dispatch Host_t F env
    opcode).
Proof.
  intros Hprimitive Hcall Hopcode.
  destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
  all: cbn [dispatch V.Executor.execute_opcode].
  - apply create_preserves; assumption.
  - apply call_preserves; assumption.
  - apply callcode_preserves; assumption.
  - apply delegatecall_preserves; assumption.
  - apply create2_preserves; assumption.
  - apply staticcall_preserves; assumption.
Qed.
End RecursiveOpcodes.

Section Calls.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop) (query : Chain.Ethereum.Address.t).
Ltac solve_protected_call Hprimitive Hread Hcall :=
  lazymatch goal with
  | |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply bounded_frame_bind_prefix;
    [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_protected_call Hprimitive Hread Hcall
  | _ ⇒ unfold preservesBoundedHost; intros before Hbound Hinitial;
    eapply call_opcode_protects_empty_recipient with (pre := invariant) (post := invariant) (query := query);
    [intros; assumption|exact Hprimitive|exact Hprimitive|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 |- _ ⇒
      let Hpair := fresh "Hpair" in pose proof (Hcode Heq) as Hpair;
      exact (proj1 Hpair)
    end
  end.

Ltac solve_retained_context Hprimitive Hcall Hdifferent :=
  lazymatch goal with
  | |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply bounded_frame_bind_prefix;
    [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_retained_context Hprimitive Hcall Hdifferent
  | _ ⇒ apply call_opcode_preserves_at_context; [exact Hprimitive|];
    intros nested Hgas Hvalue [Hkind [Hsender Hrecipient]];
    apply Hcall;
    [exact Hgas|unfold childCallFrom; rewrite Hkind; first [exact Hrecipient|split; assumption]|];
    right; right; intro Heq; exfalso; apply Hdifferent; congruence
  end.

Lemma call_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ size h, invariant 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 invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.call Host_t F E).
Proof.
  intros Hprimitive Hread Hcall. unfold V.Executor.call.
  solve_protected_call Hprimitive Hread Hcall.
Qed.

Lemma staticcall_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ size h, invariant 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 invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.staticcall Host_t F E).
Proof.
  intros Hprimitive Hread Hcall. unfold V.Executor.staticcall.
  solve_protected_call Hprimitive Hread Hcall.
Qed.

Lemma callcode_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  @V.Executor.self Host_t F E ≠ query →
  (∀ 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 invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.callcode Host_t F E).
Proof.
  intros Hprimitive Hdifferent Hcall. unfold V.Executor.callcode.
  solve_retained_context Hprimitive Hcall Hdifferent.
Qed.

Lemma delegatecall_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  @V.Executor.self Host_t F E ≠ query →
  (∀ 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 invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.delegatecall Host_t F E).
Proof.
  intros Hprimitive Hdifferent Hcall. unfold V.Executor.delegatecall.
  solve_retained_context Hprimitive Hcall Hdifferent.
Qed.

Lemma create_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ 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 invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.create Host_t F E).
Proof.
  intros Hprimitive Hcall. unfold V.Executor.create.
  apply bounded_frame_bind_prefix; [solve_host_invariant|apply pop3_nonincreasing|].
  intros [[value inputStart] inputSize].
  apply bounded_frame_bind_prefix.
  - unfold V.Executor.extend_memory_to. solve_host_invariant.
  - apply extend_memory_to_nonincreasing.
  - intro memoryGas. apply bounded_frame_bind_prefix.
    + solve_host_invariant.
    + apply spend_nonincreasing.
    + intros []. apply bounded_frame_bind_suffix.
      × apply generic_create_preserves_at_context; [exact Hprimitive|].
        intros msg Hgas Hvalue [Hkind Hsender].
        apply Hcall; [exact Hgas|unfold childCallFrom; rewrite Hkind; exact Hsender|].
        left. exact Hkind.
      × intros []. solve_host_invariant.
Qed.
Lemma create2_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ 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 invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.create2 Host_t F E).
Proof.
  intros Hprimitive Hcall. unfold V.Executor.create2.
  apply bounded_frame_bind_prefix; [solve_host_invariant|apply pop3_nonincreasing|].
  intros [[value inputStart] inputSize].
  apply bounded_frame_bind_prefix; [solve_host_invariant| |].
  - apply map_nonincreasing. apply pop_nonincreasing.
  - intro salt. apply bounded_frame_bind_prefix.
    + unfold V.Executor.extend_memory_to. solve_host_invariant.
    + apply extend_memory_to_nonincreasing.
    + intro memoryGas. apply bounded_frame_bind_prefix.
      × solve_host_invariant.
      × apply spend_nonincreasing.
      × intros []. apply bounded_frame_bind_suffix.
        { apply generic_create_preserves_at_context; [exact Hprimitive|].
        intros msg Hgas Hvalue [Hkind Hsender].
        apply Hcall; [exact Hgas|unfold childCallFrom; rewrite Hkind; exact Hsender|].
        right; left. exact Hkind. }
        intros []. solve_host_invariant.
Qed.
End Calls.
Section Between.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (pre post : Host_t → Prop) (query : Chain.Ethereum.Address.t).
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 Between.