Library SetcodeConcrete.noncehost

Code adoption preserves the nonce and account-map invariant

This is a property of every outcome of the generated Host callback, including rejection. It is therefore usable while proving opcode invariants, before deciding whether the surrounding call succeeds.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.hostnonce QedkConcrete.hostframe
  QedkConcrete.worldaccounts QedkConcrete.vmhostframe
  QedkConcrete.recursiveassembly SetcodeConcrete.nonceeffects.
Set Default Goal Selector "!".

Theorem setcodefrom_preserves_nonce_invariant {F : H.FArgs}
    query nonce current source static gas :
  preservesCallback (nonceInvariant query nonce)
    (H.setcodefrom current source static gas).
Proof.
  intros s Hinitial. unfold H.setcodefrom.
  destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hinitial.
  match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
    destruct prices as [[[[cold warm] currentCost] writeCost] reserveCost] end.
  cbn beta iota zeta.
  repeat match goal with |- context [if ?condition then _ else _] ⇒
    destruct condition eqn:?; cbn beta iota zeta end.
  all: try exact Hinitial.
  all: apply nonce_write; [exact Hinitial|].
  all: intro Heq; subst current; exact (proj2 Hinitial).
Qed.

Section Assembly.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF :=
  (@Evmc.Instantiate.HostF State.TransactionState.t (@recursiveArgs HF)).

Theorem concrete_setcode_preserves_nonce vm query nonce current source static gas :
  preservesCallback (nonceInvariant query nonce)
    ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof. apply setcodefrom_preserves_nonce_invariant. Qed.

Theorem concrete_primitives_preserve_nonce vm query nonce :
  PrimitiveHostFrame (HostF vm) (nonceInvariant query nonce).
Proof.
  constructor; intros.
  all: cbn -[H.account_exists H.get_balance H.access_account H.get_storage
    H.set_storage H.access_storage H.get_transient_storage H.set_transient_storage
    H.get_code_size H.get_code_hash H.copy_code H.get_block_hash H.emit_log
    H.selfdestruct nonceInvariant].
  all: try lazymatch goal with
    | |- preservesCallback _ (H.set_storage _ _ _) ⇒ apply storage_preserves_nonce
    | |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_preserves_nonce
    end.
  all: intros s Hinitial; eapply nonce_world_frame; [exact Hinitial|].
  all: first [reflexivity | rewrite copy_code_read_only; reflexivity |
    unfold H.access_account, H.access_storage;
    cbn -[W.account];
    repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
    reflexivity].
Qed.
End Assembly.

Print Assumptions concrete_setcode_preserves_nonce.
Print Assumptions concrete_primitives_preserve_nonce.