Library SetcodeConcrete.vmcompleteframe

Host invariants through the complete SETCODE VM

Recursive calls use the child's actual gas bound. The Host/VM assembly discharges these callback premises; they are not execution assumptions.
From Stdlib Require Import NArith ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
  QedkConcrete.vmhostframe QedkConcrete.vmboundedframe
  QedkConcrete.vmcallframe QedkConcrete.vmcreateframe.
Require Import SetcodeConcrete.vmprogress SetcodeConcrete.vmframes.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas.
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.
Section VM.
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 (Hprogress : @setcodeGasProgress Host_t F).
Context (property : Host_t → Prop).
Context (Hprimitive : PrimitiveHostFrame V.Host property).
Context (Hsetcode : ∀ current source static gas,
  preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).
Context (Hcall : ∀ msg, 0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback property (V.Host.(Evmc.HOST.call) msg)).

Lemma complete_run_preserves {E : @V.Executor.FArgs Host_t F} state :
  Z.of_N (MS.t.gas state) < 2^63 → property (MS.t.host state) →
  property (MS.t.host (snd (@generated_run Host_t F E state))).
Proof.
  apply (setcode_run_preserves_from_calls cold warm access write reserve property state
    Hrevision Hdebug Hprogress (primitive_frame_at _ _ _ Hprimitive) Hsetcode).
  intros opcode before Hopcode Hbound Hbefore.
  assert (Hframe : preservesBoundedHost property (dispatch opcode)).
  { 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. }
  exact (Hframe before Hbound Hbefore).
Qed.

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

Theorem complete_execute_preserves msg code host :
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  property host → property (snd (@V.execute Host_t F msg code host)).
Proof.
  intros Hgas Hinitial.
  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 _ _ ?env ?state] ⇒
    pose proof (@complete_run_preserves env state
      ltac:(apply initial_gas_fits_signed_int64; exact Hgas) Hinitial) as Hrun;
    unfold generated_run in Hrun;
    destruct (@V.Executor.run Host_t F env state) as [outcome after]
  end.
  all: destruct outcome as [[]|error]; [exact Hrun|].
  all: destruct error; exact Hrun.
Qed.
End VM.
Print Assumptions complete_execute_preserves.