Library SetcodeConcrete.vmnoncesetcode

SETCODEFROM preserves nonces and never refunds extra gas

Both successful adoption and rejected adoption leave the queried nonce unchanged. An exception from the callback leaves the machine gas as it was; an ordinary return installs the callback's smaller gas value.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.hostnonce QedkConcrete.hostframe
  QedkConcrete.vmhostframe SetcodeConcrete.vmprogress SetcodeConcrete.vmgasframe
  SetcodeConcrete.vmnonceops SetcodeConcrete.vmnoncecalls SetcodeConcrete.noncecreate
  SetcodeConcrete.noncebudget SetcodeConcrete.noncegas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section VM.
Context {F : @V.FArgs S.t} {E : @V.Executor.FArgs S.t F}.
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 (Hprogress : @setcodeGasProgress S.t F).

Theorem setcode_opcode_gas_bound : gasBound (@V.Executor.setcodefrom S.t F E).
Proof.
  intro state. unfold V.Executor.setcodefrom. rewrite Hrevision.
  unfold V.M.op_ltdollargt, V.M.op_letdollar, V.pop.
  cbn -[V.M.HostAPI.setcodefrom V.push V.increase_pc_and_continue V.M.op_coloneq
    V.M.op_exclamation V.Executor.self V.Host].
  destruct (MS.t.stack state) as [|source rest]; [apply N.le_refl|].
  unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base, V.M.HostAPI.lift.
  cbn -[V.push V.increase_pc_and_continue V.Executor.self V.Host].
  match goal with |- context [V.Host.(Evmc.HOST.setcodefrom) ?current ?src ?static ?gas ?host] ⇒
    pose proof (Hprogress current src static gas host) as Hspent;
    destruct (V.Host.(Evmc.HOST.setcodefrom) current src static gas host)
      as [[[valid remaining]|error] after] eqn:Hcallback
  end.
  - specialize (Hspent valid remaining after eq_refl).
    cbn -[V.push V.increase_pc_and_continue]. unfold V.push.
    match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
      cbn; lia.
  - apply N.le_refl.
Qed.

Context (query : Addr.t).
Context (Hnonce : ∀ nonce current source static gas,
  preservesCallback (nonceInvariant query nonce)
    (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).

Theorem setcode_opcode_resources state :
  nonceState query (MS.t.host state) →
  let after := snd (V.Executor.setcodefrom state) in
  nonceState query (MS.t.host after) ∧
  nonceAt (S.t.world_state (MS.t.host after)) query =
    nonceAt (S.t.world_state (MS.t.host state)) query ∧
  (MS.t.gas after ≤ MS.t.gas state)%N.
Proof.
  intro Hinitial. unfold V.Executor.setcodefrom. rewrite Hrevision.
  unfold V.M.op_ltdollargt, V.M.op_letdollar, V.pop.
  cbn -[V.M.HostAPI.setcodefrom V.push V.increase_pc_and_continue V.M.op_coloneq
    V.M.op_exclamation V.Executor.self V.Host].
  destruct (MS.t.stack state) as [|source rest].
  - split; [exact Hinitial|]. split; [reflexivity|apply N.le_refl].
  - unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base, V.M.HostAPI.lift.
    cbn -[V.push V.increase_pc_and_continue V.Executor.self V.Host].
    match goal with |- context [V.Host.(Evmc.HOST.setcodefrom) ?current ?src ?static ?gas ?host] ⇒
      pose proof (Hnonce (nonceAt (S.t.world_state host) query) current src static gas
        host (conj (proj1 Hinitial) eq_refl)) as Hframe;
      pose proof (Hprogress current src static gas host) as Hspent;
      destruct (V.Host.(Evmc.HOST.setcodefrom) current src static gas host)
        as [[[valid remaining]|error] after] eqn:Hcallback
    end.
    all: cbn beta iota zeta delta [snd] in Hframe.
    all: destruct Hframe as [Hmap Hsame].
    all: change (nonceAt (S.t.world_state after) query =
      nonceAt (S.t.world_state (MS.t.host state)) query) in Hsame.
    + specialize (Hspent valid remaining after eq_refl).
      cbn -[V.push V.increase_pc_and_continue].
      unfold V.push.
      match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
      all: cbn -[nonceState nonceAt]; split;
        [split; [exact Hmap|rewrite Hsame; exact (proj2 Hinitial)]|].
      all: split; [exact Hsame|lia].
    + cbn -[nonceState nonceAt]. split;
        [split; [exact Hmap|rewrite Hsame; exact (proj2 Hinitial)]|].
      split; [exact Hsame|apply N.le_refl].
Qed.

Corollary setcode_opcode_nonce_bound : boundedNonce query 0 V.Executor.setcodefrom.
Proof.
  intros state Hgas Hinitial.
  destruct (setcode_opcode_resources state Hinitial) as [Hstate [HnonceEq Hremaining]].
  split; [exact Hstate|]. unfold nonceAndGas. rewrite HnonceEq. lia.
Qed.
End VM.

Print Assumptions setcode_opcode_resources.