Library QedkConcrete.vmnondebitentry

Checked VM entry preserves an unrelated empty account

The entry builds the word and memory invariants from concrete message fields. This theorem still has recursive Host premises; the module assembly must discharge them before it applies to transaction execution.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.vmsupplywords QedkConcrete.vmchecked
  QedkConcrete.vmcallchecked QedkConcrete.vmsizes QedkConcrete.vmmachinewords
  QedkConcrete.vmenvironmentwords QedkConcrete.vmerrors QedkConcrete.hostcontextwords
  QedkConcrete.contexthostinstance QedkConcrete.hashwords QedkConcrete.memorywords QedkConcrete.vmmemorywords.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas.
Require Import QedkConcrete.vmnondebitchecked QedkConcrete.vmcallframe QedkConcrete.vmframe
  QedkConcrete.creationassembly QedkConcrete.nondebitframe QedkConcrete.hostcode
  QedkConcrete.vmentryframe QedkConcrete.vmsupplyentry QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Entry.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation invariant := (hostSupplyWords limit).
  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 S.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 S.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 S.t F →
        @MonadVM.Vm.Make.M.Option.t S.t F
          (@MonadVM.Vm.Make.Memory.t S.t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t S.t F →
        MonadVM.Support.UintN.t)}.

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

Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation contextInvariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).
Local Abbreviation floor := (@uncreatedBalanceFloor).
Context (query : Chain.Ethereum.Address.t) (minimum : Z).
Local Abbreviation protected := (fun s ⇒ contextInvariant s ∧ floor query minimum s).

Lemma execute_other_preserves_uncreated_checked
    {Uint64 : Basics.Unreachable MonadVM.Int64.t}
    {Uresult : Basics.Unreachable Evmc.Result.t}
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)} msg code host :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  @vmMessageBounds msg → OCamlString.length code < 2^256 →
  (∀ child, @vmMessageBounds child → ∀ before, contextInvariant before →
    let result := (HostF vm).(Evmc.HOST.call) child before in
    (contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  creationHost (HostF vm) →
  (∀ child, @vmMessageBounds child →
    childCallFrom (Evmc.Message.t.recipient msg) child →
    (Evmc.Message.t.kind child = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind child = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient child = query → Evmc.Message.t.code_address child = query)) →
    ∀ before, protected before →
    let result := (HostF vm).(Evmc.HOST.call) child before in
    (protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  Evmc.Message.t.recipient msg ≠ query → protected host →
  let result := @V.execute S.t F
    msg code host in
  (protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash [Hgas [Hvalue [Hdata Hcapacity]]] Hcode Hsupply Hcreation Hchild Hself Hinitial.
  assert (Hctx : contextWords (fst ((HostF vm).(Evmc.HOST.get_tx_context) host))).
  { apply get_tx_context_words. exact (proj2 (proj1 Hinitial)). }
  unfold V.execute.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial].
  match goal with |- context [match ?execution with (_, _) ⇒ _ end] ⇒
    lazymatch execution with
    | @V.Executor.run _ _ ?env ?state ⇒
      assert (Hrun : checkedMachine protected (snd execution) ∨ badMemoryInitComparison) by
        (apply (@run_preserves_uncreated_checked HF vm params limit query minimum env U9 U15 U17 U18 U19 U20 U22 U23 U24 U25 U28 U31 U32 U33 Urun state);
        [exact Hrevision|exact Hdebug|exact Hhash|
         apply environment_make_words; try assumption; lia|exact Hcreation|exact Hsupply|exact Hchild|exact Hself|
         apply initial_gas_fits_signed_int64; exact Hgas|
         split; [apply initial_machine_words; exact Hinitial|apply initial_machine_sizes; exact (Hcapacity U4)]]);
      assert (Herror : fst execution ≠ inr Evmc.Result.StatusCode.Success) by
        (apply (@run_no_success_error S.t F env Urun state);
         [exact Hrevision|exact Hdebug|apply initial_gas_fits_signed_int64; exact Hgas]);
      destruct execution as [result finalState]
    end
  end.
  destruct Hrun as [[Hwords Hsizes]|Hbad]; [|right; exact Hbad].
  left. destruct result as [[]|error].
  - split; [exact (proj1 (proj2 (proj2 Hwords)))|exact (proj2 Hsizes)].
  - destruct error; try (exfalso; apply Herror; reflexivity).
    all: split; [exact (proj1 (proj2 (proj2 Hwords)))|].
    all: first [exact (proj2 Hsizes)|cbn; lia].
Qed.
Lemma execute_preserves_uncreated_checked
    {Uint64 : Basics.Unreachable MonadVM.Int64.t}
    {Uresult : Basics.Unreachable Evmc.Result.t}
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)} msg code host :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  @vmMessageBounds msg → OCamlString.length code < 2^256 →
  (∀ child, @vmMessageBounds child → ∀ before, contextInvariant before →
    let result := (HostF vm).(Evmc.HOST.call) child before in
    (contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  creationHost (HostF vm) →
  (Evmc.Message.t.recipient msg ≠ query →
    ∀ child, @vmMessageBounds child →
    childCallFrom (Evmc.Message.t.recipient msg) child →
    (Evmc.Message.t.kind child = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind child = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient child = query → Evmc.Message.t.code_address child = query)) →
    ∀ before, protected before →
    let result := (HostF vm).(Evmc.HOST.call) child before in
    (protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  (Evmc.Message.t.recipient msg = query →
    code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction host)) →
  protected host →
  let result := @V.execute S.t F
    msg code host in
  (protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Hbounds Hcode Hsupply Hcreation Hchild Hallowed Hinitial.
  destruct (String.string_dec (Evmc.Message.t.recipient msg) query) as [Hsame|Hdifferent].
  2: { apply execute_other_preserves_uncreated_checked; try assumption.
       apply Hchild. exact Hdifferent. }
  cbn zeta. match goal with |- context [snd ?run] ⇒ set (result := run) end.
  assert (Hchecked : (contextInvariant (snd result) ∧
    OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨ badMemoryInitComparison).
  { unfold result. apply execute_preserves_supply_context_from_child; try assumption.
    exact (proj1 Hinitial). }
  destruct Hchecked as [[Hpost Houtput]|Hbad]; [|right; exact Hbad].
  left. split; [split; [exact Hpost|]|exact Houtput].
  destruct (Hallowed Hsame) as [Hempty|Hcreated].
  - subst code. assert (Hstate : snd result = host).
    { unfold result. apply execute_empty_preserves_host; [exact Hdebug|].
      apply concrete_context_read_only. }
    rewrite Hstate. exact (proj2 Hinitial).
  - assert (Hframe : @creationInvariant host (snd result)).
    { destruct Hcreation as [Hprimitives [Hread Hcall]]. unfold result.
      apply execute_preserves; [exact Hrevision|exact Hdebug|apply Hprimitives| | |exact (proj1 Hbounds)|].
      + intros before Hbefore. rewrite Hread. exact Hbefore.
      + intros nested Hgas. apply creation_callback. apply Hcall. exact Hgas.
      + split; [exact (proj1 (proj1 (proj2 Hinitial)))|apply creation_frame_refl]. }
    split; [exact (proj1 (proj1 (proj1 Hpost)))|].
    left. exact (creations_retained _ _ (proj2 Hframe) query Hcreated).
Qed.
End Entry.

Print Assumptions execute_other_preserves_uncreated_checked.

Print Assumptions execute_preserves_uncreated_checked.