Library SetcodeConcrete.vmnonceentry

Nonce and unused-gas bounds for the VM entry point

This connects the instruction loop to the real entry point, including frame-entry delegation reads and receipt construction. Its child Host-call premise is the remaining recursive induction obligation, not an assumption on the fully assembled executor.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmhostframe QedkConcrete.hostframe
  QedkConcrete.hostnonce.
Require Import SetcodeConcrete.checkednonce SetcodeConcrete.noncecreate SetcodeConcrete.noncebudget
  SetcodeConcrete.noncegas SetcodeConcrete.vmprogress SetcodeConcrete.vmnonceloop
  SetcodeConcrete.vmterminal SetcodeConcrete.vmerrors.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Entry.
Context {F : @V.FArgs S.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 S.t F).
Context (Herror : @setcodeNoSuccessError S.t F).
Context (query : Chain.Ethereum.Address.t).
Context (Hprimitive : ∀ nonce, PrimitiveHostFrame V.Host (nonceInvariant query nonce)).
Context (Hsetcode : ∀ nonce current source static gas,
  preservesCallback (nonceInvariant query nonce)
    (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).
Context (Hbalance : ∀ addr host, snd (V.Host.(Evmc.HOST.get_balance) addr host) = host).
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).
Context (Hcall : ∀ msg host, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query host →
  let '(result, after) := V.Host.(Evmc.HOST.call) msg host in
  checkedNonceResult query (Evmc.Message.t.gas msg)
    (if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
    host result after).

Theorem execute_nonce_bound msg code host :
  0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query host →
  let '(result, after) := @V.execute S.t F msg code host in
  nonceResult query (Evmc.Message.t.gas msg) 0 host result after.
Proof.
  intros Hgas Hinitial.
  assert (Hstart : Z.of_N (Gas.of_uint64 (Evmc.Message.t.gas msg)) = Evmc.Message.t.gas msg).
  { assert (Hunsigned : OCamlZ.of_int64_unsigned (Evmc.Message.t.gas msg) = Evmc.Message.t.gas msg).
    { unfold OCamlZ.of_int64_unsigned. destruct (Z.ltb (Evmc.Message.t.gas msg) 0) eqn:Hnegative;
        [apply Z.ltb_lt in Hnegative; lia|reflexivity]. }
    rewrite initial_gas_matches_unsigned by (rewrite Hunsigned; lia).
    exact Hunsigned. }
  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:Hcode;
    cbn [snd] in HreadCopy; subst middle;
    change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in Hcode;
    rewrite Hcode;
    cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
  all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
    pose proof (@run_nonce_bound F env cold warm access write reserve Hrevision Hdebug Hprogress
      query Hprimitive Hsetcode Hbalance Hcall state ltac:(change (Z.of_N (Gas.of_uint64 (Evmc.Message.t.gas msg)) < 2^63); lia)
      Hinitial) as Hnonce;
    pose proof (@run_returned_gas_bound F env cold warm access write reserve Hrevision Hdebug Hprogress state
      ltac:(change (Z.of_N (Gas.of_uint64 (Evmc.Message.t.gas msg)) < 2^63); lia)) as Hreturned;
    pose proof (@setcode_run_no_success_error S.t F env cold warm access write reserve state Hrevision Hdebug Herror Hprogress
      ltac:(change (Z.of_N (Gas.of_uint64 (Evmc.Message.t.gas msg)) < 2^63); lia)) as HrealError;
    unfold generated_run in Hnonce, Hreturned, HrealError;
    destruct (@V.Executor.run S.t F env state) as [outcome after] eqn:Hrun
  end.
  all: cbn beta iota zeta delta [fst snd] in Hnonce, Hreturned, HrealError.
  all: destruct Hnonce as [Hstate Htotal].
  all: unfold nonceAndGas in Htotal;
    cbn [MS.initial MS.t.gas MS.t.host] in Htotal;
    rewrite Hstart in Htotal.
  all: destruct outcome as [[]|error].
  all: try (destruct error).
  all: try contradiction.
  all: cbn -[nonceResult nonceState nonceAt Support.UintN.to_uint64].
  all: unfold nonceResult; split; [exact Hstate|].
  all: cbn -[nonceState nonceAt Support.UintN.to_uint64].
  all: try solve [split; lia].
  all: specialize (Hreturned ltac:(auto));
    change (MS.t.gas after ≤ Gas.of_uint64 (Evmc.Message.t.gas msg))%N in Hreturned;
    unfold Support.UintN.to_uint64, Support.UintN.to_z;
    rewrite int64_unsigned_small_identity by lia;
    split; lia.
Qed.
End Entry.

Print Assumptions execute_nonce_bound.