Library QedkConcrete.hostnocalls

Zero-value system-call entry

The Host reads code from the account, not from the message's code field. The zero-value transfer preserves that code and every balance; failure restores the input state. Thus the call-free VM result applies to the complete system call, including accounts with delegation.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.nondebitframe QedkConcrete.hostcode
  QedkConcrete.assemblychecked QedkConcrete.precompilesframe QedkConcrete.reserveprecompile
  QedkConcrete.contexthostinstance QedkConcrete.vmhostframe
  QedkConcrete.supplyhostinstance QedkConcrete.hostsupply QedkConcrete.supply
  QedkConcrete.hostframe QedkConcrete.hostbalance QedkConcrete.systemcode
  QedkConcrete.hostchecked QedkConcrete.hostaccountformat
  QedkConcrete.hostcomposition QedkConcrete.vmsupplywords
  QedkConcrete.stakingaccounting.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
  (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).

Local Abbreviation floor := (@balanceFloor).

Context (limit : Z).
Local Abbreviation invariant := (hostSupplyContext F.(Host.Instantiate.ChainParams) limit).

Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).

Lemma native_preserves_balance_floor query minimum msg addr action :
  addr ≠ query →
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  nativeDispatch vm msg addr = Some action →
  preservesCallback (floor query minimum) action.
Proof.
  intros Hneq 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 Hfloor; exact Hfloor.
  - unfold Precompiles.Make.monad_precompiles in Hselected.
    change (@Precompiles.Make.Revision (@H.Precompiles_fargs
      (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
      with F.(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 [native|] eqn:Hnative in Hselected end.
    2: discriminate.
    apply precompilesframe.find_pair_value in Hnative as [[Haddr ->] | [Haddr ->]];
      match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end;
      inversion Hselected; subst action.
    + intros s Hfloor; exact Hfloor.
    + apply balance_floor_callback.
      × intros s Hinv.
        match goal with |- balanceInvariant ?world ∧ _ ⇒
          assert (Hsafe : balanceInvariant world ∧ totalBalance world = totalBalance (S.t.world_state s))
            by (apply precompile_preserves_supply; exact Hinv)
        end. destruct Hsafe as [Hafter Htotal]. split; [exact Hafter|lia].
      × intros s Hinv. rewrite precompile_preserves_balances by exact (proj1 Hinv). reflexivity.
    + intros s Hfloor; exact Hfloor.
    + apply balance_floor_callback.
      × intros s Hinv.
        match goal with |- context [MonadVM.Staking.staking_contract ?fb0 ?fb1 ?fb2 msg] ⇒
          destruct (staking_contract_preserves_supply fb0 fb1 fb2 msg s Hinv) as [Hafter [Htotal _]]
end.
        split; [exact Hafter|apply Z.eq_le_incl; exact Htotal].
      × intros s Hinv.
        match goal with |- context [MonadVM.Staking.staking_contract ?fb0 ?fb1 ?fb2 msg] ⇒
          destruct (staking_contract_preserves_supply fb0 fb1 fb2 msg s Hinv) as [_ [_ Hbal]]
end.
        exact (Hbal query (@eq_ind _ addr (fun a ⇒ a ≠ query) Hneq _ Haddr)).
Qed.

Lemma zero_transfer_balance_floor sender recipient amount query minimum s :
  Numeric.U256.to_z amount = 0 → floor query minimum s →
  floor query minimum (snd (@H.transfer_ether
    (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) sender recipient amount s)).
Proof.
  intros Hzero [Hinv Hfloor].
  destruct (@transfer_balance_effects (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) sender recipient amount s Hinv ltac:(lia))
    as [Hafter [_ Hbalances]].
  split; [exact Hafter|]. specialize (Hbalances query).
  rewrite Hzero in Hbalances.
  match type of Hbalances with _ = _ + ?effect ⇒
    replace effect with 0 in Hbalances by
      (repeat match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity)
  end.
  rewrite Z.add_0_r in Hbalances.
  eapply Z.le_trans; [exact Hfloor|]. rewrite Hbalances. apply Z.le_refl.
Qed.

Lemma process_system_call_free_preserves (property : S.t → Prop) msg s :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
  S.t.accounts_created_in_current_transaction s = nil →
  callFreeBytes (A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s)
    (@W.account None (Evmc.Message.t.code_address msg)))) = true →
  (∀ code, OCamlString.length code < 2^256 → callFreeBytes code = true →
    ∀ middle, invariant middle → property middle →
      property (snd (vm.(Evmc.Vm.SIG.execute) msg code middle))) →
  (∀ action, nativeDispatch vm msg (Evmc.Message.t.code_address msg) = Some action → preservesCallback property action) →
  (∀ before, invariant before → property before → property (snd
    (@H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
      (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg) before))) →
  invariant s → property s →
  property (snd (processCall None msg s)).
Proof.
  intros Hrevision Hzero Hcreated Hcode Hvm Hnative Htransfer Hcontext Hinitial.
  set (transfer := (if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
    then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
      (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
    else fun st ⇒ (true, st))).
  assert (Hfloor : property (snd (transfer s))).
  { unfold transfer. destruct (H.should_transfer msg); [|exact Hinitial].
    apply Htransfer; assumption. }
  assert (Hcontext' : invariant (snd (transfer s))).
  { unfold transfer. eapply host_supply_context_frame.
    - destruct (H.should_transfer msg); [apply supply_transfer; lia|apply supply_return].
    - destruct (H.should_transfer msg); [intro; apply transfer_preserves_format|apply format_return].
    - destruct (H.should_transfer msg); [apply transfer_preserves_context|apply context_return].
    - exact Hcontext. }
  assert (Hcode' : callFreeBytes (A.t.code (State.op_pointcaretlparenrparen
      (S.t.world_state (snd (transfer s)))
      (@W.account None (Evmc.Message.t.code_address msg)))) = true).
  { unfold transfer. destruct (H.should_transfer msg); [|exact Hcode].
    destruct (@transfer_preserves_creation UF (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) (Evmc.Message.t.sender msg)
      (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg) s
      (proj1 (proj1 (proj1 (proj1 Hcontext))))) as [_ Hframe].
    rewrite (noncreated_code _ _ Hframe); [exact Hcode|].
    pose proof (f_equal fst (@transfer_preserves_creation_tracking (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) (Evmc.Message.t.sender msg)
      (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg) s)) as Hset.
    cbn beta iota zeta delta [fst] in Hset. rewrite Hset, Hcreated. intro Hbad; inversion Hbad. }
  unfold H.process_call, H.M.
  cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile balanceFloor].
  match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    change run with (transfer s); destruct (transfer s) as [ok middle] eqn:Hrun end.
  cbn beta iota zeta delta [snd] in Hfloor, Hcontext', Hcode'.
  destruct ok; [|exact Hfloor].
  cbn -[W.account Precompiles.Make.try_precompile balanceFloor].
  match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct selection as [action|] eqn:Hselected end.
  - eapply Hnative; eassumption.
  - apply Hvm; [|exact Hcode'|exact Hcontext'|exact Hfloor].
    pose proof (@read_account_format (S.t.world_state middle)
      (Evmc.Message.t.code_address msg) (proj1 (proj2 (proj1 Hcontext')))) as [_ Hsize].
    match type of Hsize with ?len < _ ⇒ change (len < 2^256) end. lia.
Qed.

Lemma system_call_free_preserves (property : S.t → Prop) msg s :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
  S.t.accounts_created_in_current_transaction s = nil →
  callFreeBytes (A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s)
    (@W.account None (Evmc.Message.t.code_address msg)))) = true →
  (∀ code, OCamlString.length code < 2^256 → callFreeBytes code = true →
    ∀ middle, invariant middle → property middle →
      property (snd (vm.(Evmc.Vm.SIG.execute) msg code middle))) →
  (∀ action, nativeDispatch vm msg (Evmc.Message.t.code_address msg) = Some action → preservesCallback property action) →
  (∀ before, invariant before → property before → property (snd
    (@H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
      (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg) before))) →
  invariant s → property s →
  property (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
  intros Hrevision Hkind Hzero Hcreated Hcode Hvm Hnative Htransfer Hcontext Hinitial.
  pose proof (process_system_call_free_preserves property msg s Hrevision Hzero
    Hcreated Hcode Hvm Hnative Htransfer Hcontext Hinitial) as Hcore.
  cbn -[H.call balanceFloor]. unfold H.call, H.call_impl. rewrite Hkind.
  cbn -[H.process_call balanceFloor].
  match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hafter : property (snd run)) by exact Hcore;
    destruct run as [result after] end.
  cbn beta iota zeta delta [snd] in Hafter.
  destruct result as [status gasleft refund output address]; destruct status;
    cbn -[balanceFloor]; first [exact Hafter|exact Hinitial].
Qed.

Corollary system_call_free_balance query minimum msg s :
  Evmc.Message.t.code_address msg ≠ query →
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
  S.t.accounts_created_in_current_transaction s = nil →
  callFreeBytes (A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s)
    (@W.account None (Evmc.Message.t.code_address msg)))) = true →
  (∀ code, OCamlString.length code < 2^256 → callFreeBytes code = true →
    ∀ middle, invariant middle → floor query minimum middle →
      floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code middle))) →
  invariant s → floor query minimum s →
  floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
  intros Hneq Hrevision Hkind Hzero Hcreated Hcode Hvm Hcontext Hinitial.
  apply system_call_free_preserves; try assumption.
  - intros action Hselected. eapply native_preserves_balance_floor; eassumption.
  - intros before Hbefore Hfloor. apply zero_transfer_balance_floor; assumption.
Qed.

Lemma native_preserves_creation_set msg addr action :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  nativeDispatch vm msg addr = Some action →
  ∀ s, S.t.accounts_created_in_current_transaction (snd (action s)) =
    S.t.accounts_created_in_current_transaction s.
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;
      intro s; reflexivity.
  - unfold Precompiles.Make.monad_precompiles in Hselected.
    change (@Precompiles.Make.Revision (@H.Precompiles_fargs
      (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
      with F.(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 [native|] 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.
    + intro s; reflexivity.
    + intro s. apply precompile_preserves_creation_set.
    + intro s; reflexivity.
    + intro s.
      match goal with |- context [MonadVM.Staking.staking_contract ?fb0 ?fb1 ?fb2 msg] ⇒
        destruct (staking_contract_uframe fb0 fb1 fb2 msg s)
          as [[_ [_ [_ [Hcreated _]]]] _]
      end.
      exact Hcreated.
Qed.

Corollary system_call_free_creation_set msg s :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
  S.t.accounts_created_in_current_transaction s = nil →
  callFreeBytes (A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s)
    (@W.account None (Evmc.Message.t.code_address msg)))) = true →
  (∀ code, OCamlString.length code < 2^256 → callFreeBytes code = true →
    ∀ middle, invariant middle →
      S.t.accounts_created_in_current_transaction middle = nil →
      S.t.accounts_created_in_current_transaction (snd (vm.(Evmc.Vm.SIG.execute) msg code middle)) = nil) →
  invariant s →
  S.t.accounts_created_in_current_transaction (snd ((HostF vm).(Evmc.HOST.call) msg s)) = nil.
Proof.
  intros Hrevision Hkind Hzero Hcreated Hcode Hvm Hcontext.
  apply system_call_free_preserves with
    (property := fun st ⇒ S.t.accounts_created_in_current_transaction st = nil); try assumption.
  - intros action Hselected before Hbefore.
    etransitivity; [exact (native_preserves_creation_set msg _ action Hrevision Hselected before)|exact Hbefore].
  - intros before Hbefore Hempty.
    pose proof (f_equal fst (@transfer_preserves_creation_tracking
      (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
      (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg) before)) as Hset.
    cbn beta iota zeta delta [fst] in Hset. rewrite Hset. exact Hempty.
Qed.
End Host.