Library QedkConcrete.executionreservestorage

Only native setters can change reserve configuration during VM execution

This theorem is about Execution's generated recursive Host/VM. The post storage is reachable by finitely many successful concrete setters, not arbitrary writes. It requires no native-call or recursive-VM contract. Slot separation is needed only later, for the accounts in this finite write sequence, when deriving delayed-threshold stability.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.executionprotectedstorage
  QedkConcrete.protectedstoragenative QedkConcrete.storagehost
  QedkConcrete.protectedstorage QedkConcrete.hostframe QedkConcrete.vmhostframe
  QedkConcrete.assemblychecked QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".

Section Dispatch.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation native := (@Reserve_balance.precompile_address).

Definition reserveStorageInvariant block initial :=
  @protectedStorage native
    (@reachableReserveStorage block initial).

Lemma native_dispatch_only_reserve_settings vm block initial msg addr action :
  HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  @nativeDispatch HF vm msg addr = Some action →
  preservesCallback (reserveStorageInvariant block initial) action.
Proof.
  intros Hrevision Hselected. unfold nativeDispatch, Precompiles.Make.try_precompile in Hselected.
  match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
    destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
  - destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
      intros s Hs; exact Hs.
  - unfold Precompiles.Make.monad_precompiles in Hselected.
    change (@Precompiles.Make.Revision (@H.Precompiles_fargs
      (H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
      with HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) in Hselected.
    rewrite Hrevision in Hselected.
    match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
      destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [action'|] eqn:Hnative in Hselected end.
    2: discriminate.
    apply precompilesframe.find_pair_value in Hnative as [[_ ->] | [_ ->]];
      match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end;
      inversion Hselected; subst action.
    + intros s Hs. exact Hs.
    + apply native_preserves_reachable_reserve_storage.
    + intros s Hs. exact Hs.
    + apply protectedstoragehost.staking_contract_preserves_protected_storage. discriminate.
Qed.
End Dispatch.

Module E := Execution.Make.
Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).

Local Abbreviation native := (@Reserve_balance.precompile_address).

Theorem execution_call_from_eoa_only_reserve_settings block initial tx msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
  preservesCallback (@reserveStorageInvariant block initial)
    (@E.Host.call_from_eoa F
      tx msg).
Proof.
  intros Hrevision Hgas Htarget.
  apply (@execution_call_from_eoa_protects_storage F
    native).
  - exact Hrevision.
  - intros vm nested addr action Haction.
    exact (@native_dispatch_only_reserve_settings args vm block initial nested addr action
      Hrevision Haction).
  - exact Hgas.
  - exact Htarget.
Qed.

Theorem execution_call_from_monad_system_transaction_only_reserve_settings block initial msg :
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  preservesCallback (@reserveStorageInvariant block initial)
    (@E.Host.call_from_monad_system_transaction F
      msg).
Proof.
  intro Hkind.
  apply (@execution_call_from_monad_system_transaction_protects_storage F
    native (@reachableReserveStorage block initial) msg).
  - discriminate.
  - exact Hkind.
Qed.

Theorem execution_system_call_only_reserve_settings block initial msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  Evmc.Message.t.sender msg ≠ native →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  (Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native) →
  preservesCallback (@reserveStorageInvariant block initial)
    (@E.Host.call F
      msg).
Proof.
  intros Hrevision Hgas Hsender Hkind Htarget.
  apply (@execution_call_protects_storage F
    native).
  - exact Hrevision.
  - intros vm nested addr action Haction.
    exact (@native_dispatch_only_reserve_settings args vm block initial nested addr action
      Hrevision Haction).
  - exact Hgas.
  - exact Hsender.
  - exact Hkind.
  - right; right. exact Htarget.
Qed.
End Execution.

Print Assumptions execution_call_from_eoa_only_reserve_settings.
Print Assumptions execution_system_call_only_reserve_settings.