Library QedkConcrete.vmsupplyentry

Supply and output bounds through the concrete VM entry point

Initialization supplies the empty-stack and memory invariants; the Host constructs the environment from bounded transaction fields. The opcode proof supplies the loop invariant, including failure exits. Recursive Host calls remain an explicit induction obligation, not a VM axiom.
From Stdlib Require Import 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.
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.

Lemma execute_preserves_supply_checked_from_child
    {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 →
  hostContextWords HF.(Host.Instantiate.ChainParams) host →
  @vmMessageBounds msg → OCamlString.length code < 2^256 →
  (∀ child, @vmMessageBounds child → ∀ before, invariant before →
    let result := (HostF vm).(Evmc.HOST.call) child before in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  invariant host →
  let result := @V.execute S.t F
    msg code host in
  (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Hcontext [Hgas [Hvalue [Hdata Hcapacity]]] Hcode Hchild Hinitial.
  assert (Hctx : contextWords (fst ((HostF vm).(Evmc.HOST.get_tx_context) host))).
  { apply get_tx_context_words. exact Hcontext. }
  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 invariant (snd execution) ∨ badMemoryInitComparison) by
        (apply (run_preserves_supply_checked_from_child vm params limit (E := env) state);
        [exact Hrevision|exact Hdebug|exact Hhash|
         apply environment_make_words; try assumption; lia|exact Hchild|
         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.
Context bounds must accompany the child state: they justify its VM environment rather than being required anew for every possible state.
Local Abbreviation contextInvariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).

Lemma execute_preserves_supply_context_from_child
    {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) →
  contextInvariant host →
  let result := @V.execute S.t F
    msg code host in
  (contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash [Hgas [Hvalue [Hdata Hcapacity]]] Hcode Hchild Hinitial.
  assert (Hctx : contextWords (fst ((HostF vm).(Evmc.HOST.get_tx_context) host))).
  { apply get_tx_context_words. exact (proj2 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 contextInvariant (snd execution) ∨ badMemoryInitComparison) by
        (apply (run_preserves_supply_context_from_child vm params limit (E := env) state);
        [exact Hrevision|exact Hdebug|exact Hhash|
         apply environment_make_words; try assumption; lia|exact Hchild|
         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.
End Entry.
Print Assumptions execute_preserves_supply_checked_from_child.

Print Assumptions execute_preserves_supply_context_from_child.