Library QedkConcrete.vmcreateframe

Context-preserving recursive CREATE instructions

The child receives at most 63/64 of the gas remaining at the call site. Prefix checks cannot increase that gas, so the Host induction hypothesis is used only for a child message within the signed gas bound.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe
  QedkConcrete.vmboundedframe QedkConcrete.vmcallframe.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas
  MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".

Ltac expose_primitive_calls :=
  unfold V.M.HostAPI.get_balance, V.M.HostAPI.get_code_size,
    V.M.HostAPI.copy_code, V.M.HostAPI.access_account, V.M.HostAPI.Base.

Ltac solve_host_invariant_extra ::=
  lazymatch goal with
  | Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
    |- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
    apply host_call_preserves; apply Hcall;
    [cbn [Evmc.Message.t.gas]; unfold Gas.to_uint64, UintN.to_uint64, UintN.to_z;
    rewrite int64_unsigned_small_identity by lia; lia
    |reflexivity|split; reflexivity]
  | Hcall : ∀ msg, _ → _ → preservesCallback _ _
    |- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
    apply host_call_preserves; apply Hcall;
    [cbn [Evmc.Message.t.gas]; unfold Gas.to_uint64, UintN.to_uint64, UintN.to_z;
    rewrite int64_unsigned_small_identity by lia; lia
    |reflexivity]
  | Hcall : ∀ msg, _ → preservesCallback _ _
    |- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
    apply host_call_preserves; apply Hcall; cbn [Evmc.Message.t.gas];
    unfold Gas.to_uint64, UintN.to_uint64, UintN.to_z;
    rewrite int64_unsigned_small_identity by lia; lia
  | |- preservesHost _ (@V.M.HostAPI.lift _ _ _ _) ⇒
    apply preserves_lift; unfold preservesCallback in *; eauto
  | |- preservesHost _ (match ?value with _ ⇒ _ end) ⇒
    destruct value; solve_host_invariant
  | |- ?g ⇒ fail "Unhandled creation frame goal:" g
  end.

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}.

The endowment is copied unchanged into the child message. This lets the supply proof require child preservation only for nonnegative endowments.
Lemma generic_create_preserves_at_context (invariant : Host_t → Prop) kind salt value inputStart inputSize :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    Evmc.Message.t.value msg = value →
    (Evmc.Message.t.kind msg = kind ∧
      Evmc.Message.t.sender msg = @V.Executor.self Host_t F E) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  preservesBoundedHost invariant (@V.Executor.generic_create_impl Host_t F E
    kind salt value inputStart inputSize).
Proof.
  intros Hprimitive Hcall. destruct Hprimitive.
  unfold V.Executor.generic_create_impl.
  apply bounded_frame_bind_prefix.
  - solve_host_invariant.
  - apply stipend_guard_nonincreasing.
  - intros []. apply bounded_frame_bind_prefix.
    + unfold V.Executor.check_write_permissions. solve_host_invariant.
    + apply check_write_permissions_nonincreasing.
    + intros []. apply bounded_frame_bind_prefix.
      × expose_primitive_calls. solve_host_invariant.
      × apply get_balance_nonincreasing.
      × intro selfBalance. apply bounded_frame_bind_prefix.
        { unfold V.Executor.access_delegation. expose_primitive_calls. solve_host_invariant. }
        { first [apply create_delegation_guard_nonincreasing |
            destruct V.Params.(Vm.Make_Params_signature.revision);
            try apply create_delegation_guard_nonincreasing;
            unfold V.M.when_, Monad.Make_Monad.when_;
            match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
            first [apply fail_nonincreasing | apply return_nonincreasing]]. }
        intros [].
        match goal with |- preservesBoundedHost _ (if ?skip then _ else _) ⇒ destruct skip end.
        { apply bounded_from_unbounded. solve_host_invariant. }
        intros state Hgas Hinitial.
        pose proof (@create_forwarded_le (MS.t.gas state)) as Hforwarded.
        change (@UintN.minus_1_64th (MS.t.gas state) ≤ MS.t.gas state)%N in Hforwarded.
        match goal with |- invariant (MS.t.host (snd
          (@V.M.op_letdollar _ _ _ _ _ ?next state))) ⇒
          let forwarded := constr:(@UintN.minus_1_64th (MS.t.gas state)) in
          change (invariant (MS.t.host (snd (next forwarded state))));
          assert (Hnext : preservesHost invariant (next forwarded)) by
            (unfold V.Executor.merge_child_gas_and_refund; solve_host_invariant);
          apply Hnext; exact Hinitial
        end.
Qed.

Lemma generic_create_preserves_at_value (invariant : Host_t → Prop) kind salt value inputStart inputSize :
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    Evmc.Message.t.value msg = value →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  preservesBoundedHost invariant (@V.Executor.generic_create_impl Host_t F E
    kind salt value inputStart inputSize).
Proof.
  intros Hprimitive Hcall. apply generic_create_preserves_at_context.
  - apply primitive_frame_at. exact Hprimitive.
  - intros msg Hgas Hvalue _. apply Hcall; assumption.
Qed.

Lemma generic_create_preserves (invariant : Host_t → Prop) kind salt value inputStart inputSize :
  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)) →
  preservesBoundedHost invariant (@V.Executor.generic_create_impl Host_t F E
    kind salt value inputStart inputSize).
Proof.
  intros Hprimitive Hcall. apply generic_create_preserves_at_value; [exact Hprimitive|].
  intros msg Hgas _. apply Hcall. exact Hgas.
Qed.

Lemma create_preserves (invariant : Host_t → Prop) :
  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)) →
  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; assumption.
      × intros []. solve_host_invariant.
Qed.

Lemma create2_preserves (invariant : Host_t → Prop) :
  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)) →
  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; assumption. }
        intros []. solve_host_invariant.
Qed.

CREATE and CREATE2 take their endowment from the execution account, including when the created address differs from the code address.
Lemma create_preserves_from_self (invariant : Host_t → Prop) :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) msg →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  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.
      × intros []. solve_host_invariant.
Qed.

Lemma create2_preserves_from_self (invariant : Host_t → Prop) :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) msg →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  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. }
        intros []. solve_host_invariant.
Qed.

End Environment.
End Opcodes.

Print Assumptions create_preserves.
Print Assumptions create2_preserves.

Print Assumptions create_preserves_from_self.
Print Assumptions create2_preserves_from_self.