Library SetcodeConcrete.host

SETCODEFROM does not change account balances

This proof is about the concrete Host implementation, for every choice of gas prices and for both success and failure. Map well-formedness is the ordinary representation invariant, not an assumed VM behavior.
From Stdlib Require Import String Bool ZArith NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host QedkConcrete.worldaccounts.
Set Default Goal Selector "!".
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

Lemma code_write_preserves_balance s current code query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  A.t.balance (readAccount (S.t.world_state
    (State.op_pointcaretlparenrparenltminus s
      (Lens.Infix.op_pipeminusminus (@S.account None current) A.TLens.code) code)) query) =
  A.t.balance (readAccount (S.t.world_state s) query).
Proof.
  intro Hwf.
  change (Numeric.U256.to_z (A.t.balance (readAccount
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s) (@W.account (Some false) current)
      (A.t.with_code code (readAccount (S.t.world_state s) current))) query)) =
    Numeric.U256.to_z (A.t.balance (readAccount (S.t.world_state s) query))).
  pose proof (balance_after_account_write false (S.t.world_state s) current
    (A.t.with_code code (readAccount (S.t.world_state s) current)) query Hwf) as Hbalance.
  destruct (String.eqb current query) eqn:Heq.
  - apply String.eqb_eq in Heq. subst query. exact Hbalance.
  - exact Hbalance.
Qed.

Theorem setcodefrom_preserves_balances {F : H.FArgs} current source static gas s query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  A.t.balance (readAccount (S.t.world_state
    (snd (H.setcodefrom current source static gas s))) query) =
  A.t.balance (readAccount (S.t.world_state s) query).
Proof.
  intro Hwf. unfold H.setcodefrom.
  destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try reflexivity.
  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 reflexivity.
  all: match goal with
  | |- context [State.op_pointcaretlparenrparenltminus ?state _ ?code] ⇒
      exact (code_write_preserves_balance state current code query Hwf)
  end.
Qed.

Positive source-access prices are sufficient for strict gas progress. MIP-19 does not assign their values; the experiment makes them explicit.
Theorem setcodefrom_success_spends_gas {F : H.FArgs}
    cold warm currentCost writeCost reserveCost current source static gas s valid remaining after :
  H.ChainParams.(Chain.Monad.PARAMS.revision) =
    Support.Variants.Revision.Setcode (cold, warm, currentCost, writeCost, reserveCost) →
  (0 < cold)%N → (0 < warm)%N →
  H.setcodefrom current source static gas s = (inl (valid, remaining), after) →
  (remaining < gas)%N.
Proof.
  intros Hrevision Hcold Hwarm Hrun. unfold H.setcodefrom in Hrun.
  rewrite Hrevision in Hrun. cbn beta iota zeta in Hrun.
  repeat match type of Hrun with context [if ?condition then _ else _] ⇒
    destruct condition eqn:?; cbn beta iota zeta in Hrun; try discriminate
  end.
  all: inversion Hrun; subst; clear Hrun.
  all: unfold Support.UintN.op_lt, Support.UintN.op_minus in ×.
  all: repeat match goal with
  | H : context [if N.leb ?a ?b then _ else _] |- _ ⇒
      destruct (N.leb a b) eqn:?; cbn in H
  | |- context [if N.leb ?a ?b then _ else _] ⇒
      destruct (N.leb a b) eqn:?; cbn
  end.
  all: repeat match goal with
  | H : N.leb _ _ = true |- _ ⇒ apply N.leb_le in H
  | H : N.leb _ _ = false |- _ ⇒ apply N.leb_gt in H
  | H : N.ltb _ _ = true |- _ ⇒ apply N.ltb_lt in H
  | H : N.ltb _ _ = false |- _ ⇒ apply N.ltb_ge in H
  end.
  all: lia.
Qed.

Print Assumptions setcodefrom_preserves_balances.
Print Assumptions setcodefrom_success_spends_gas.