Library QedkConcrete.precompilesframe

Context frame of the concrete precompile dispatcher

Ethereum precompiles return data without writing account state. The native reserve precompile has the separately proved configuration-only effect. The table lookup cannot introduce any other stateful callback.
From Stdlib Require Import String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostsupply QedkConcrete.supply.
Require Import MonadVM.Precompiles QedkConcrete.hostcomposition QedkConcrete.reserveprecompile.
Require Import QedkConcrete.hostframe QedkConcrete.stringmaps.
Require Import QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Require Import QedkConcrete.stakingaccounting.
Set Default Goal Selector "!".
Module P := Precompiles.Make.

Lemma find_singleton_value {A : Set} k (v : A) query result :
  Chain.Ethereum.Address.Map.find_opt query
    (Chain.Ethereum.Address.Map.of_list ((k, v) :: nil)) = Some result →
  query = k ∧ result = v.
Proof.
  change (OCamlMap.Make.find_opt stringOrder query
    (OCamlMap.Make.add stringOrder k v nil) = Some result →
    query = k ∧ result = v).
  destruct (String.eqb_spec k query) as [<-|Hne].
  - rewrite (find_add_same nil k v empty_wellformed). intros [= <-]. split; reflexivity.
  - rewrite (find_add_other nil k query v empty_wellformed Hne). discriminate.
Qed.

Lemma find_pair_value {A : Set} k1 (v1 : A) k2 v2 query result :
  Chain.Ethereum.Address.Map.find_opt query
    (Chain.Ethereum.Address.Map.of_list ((k1, v1) :: (k2, v2) :: nil)) = Some result →
  (query = k2 ∧ result = v2) ∨ (query = k1 ∧ result = v1).
Proof.
  change (OCamlMap.Make.find_opt stringOrder query
    (OCamlMap.Make.add stringOrder k2 v2 (OCamlMap.Make.add stringOrder k1 v1 nil)) = Some result →
    (query = k2 ∧ result = v2) ∨ (query = k1 ∧ result = v1)).
  assert (Hwf1 : WellFormed (OCamlMap.Make.add stringOrder k1 v1 nil))
    by (apply add_wellformed, empty_wellformed).
  destruct (String.eqb_spec k2 query) as [<-|Hne2].
  - rewrite (find_add_same _ k2 v2 Hwf1). intros [= <-]. left; split; reflexivity.
  - rewrite (find_add_other _ k2 query v2 Hwf1 Hne2).
    destruct (String.eqb_spec k1 query) as [<-|Hne1].
    + rewrite (find_add_same nil k1 v1 empty_wellformed). intros [= <-]. right; split; reflexivity.
    + rewrite (find_add_other nil k1 query v1 empty_wellformed Hne1). discriminate.
Qed.

Lemma staking_contract_preserves_context fb0 fb1 fb2 msg :
  preservesContext (MonadVM.Staking.staking_contract fb0 fb1 fb2 msg).
Proof.
  intro s.
  pose proof (proj1 (staking_contract_uframe fb0 fb1 fb2 msg s))
    as [Hinit [Hblk [_ [_ [Horig [Hgprice [Hglim [_ [_ [_ [_ [Hhist Hnext]]]]]]]]]]]].
  unfold transactionContext.
  repeat f_equal; assumption.
Qed.

Lemma staking_contract_preserves_format fb0 fb1 fb2 msg :
  preservesCallback hostFormat (MonadVM.Staking.staking_contract fb0 fb1 fb2 msg).
Proof.
  intros s [Hworld Htransient].
  pose proof (stakingStateWords_of_world s Hworld) as Hwords.
  pose proof (staking_contract_preserves_words fb0 fb1 fb2 msg s Hwords)
    as [[_ Hworld'] _].
  pose proof (proj1 (staking_contract_uframe fb0 fb1 fb2 msg s))
    as [_ [_ [Htrans_eq _]]].
  split.
  - exact Hworld'.
  - exact (eq_ind_r (fun l ⇒ Forall (fun entry ⇒ hoststoragewords.storageWords (snd entry)) l)
      Htransient Htrans_eq).
Qed.

Lemma staking_syscalls_preserves_context fb0 msg :
  preservesContext (MonadVM.Staking.staking_syscalls fb0 msg).
Proof.
  intro s.
  destruct (staking_syscalls_uframe fb0 msg s)
    as [[[Hinit [Hblk [_ [_ [Horig [Hgprice [Hglim [_ [_ [_ [_ [Hhist Hnext]]]]]]]]]]]] _] |
        [[Hinit [Hblk [_ [_ [Horig [Hgprice [Hglim [_ [_ [_ [_ [Hhist Hnext]]]]]]]]]]]] _]];
    unfold transactionContext; repeat f_equal; assumption.
Qed.

Lemma staking_syscalls_preserves_format fb0 msg :
  preservesCallback hostFormat (MonadVM.Staking.staking_syscalls fb0 msg).
Proof.
  intros s [Hworld Htransient].
  pose proof (stakingStateWords_of_world s Hworld) as Hwords.
  pose proof (staking_syscalls_preserves_words fb0 msg s Hwords)
    as [[_ Hworld'] _].
  destruct (staking_syscalls_uframe fb0 msg s)
    as [[[_ [_ [Htrans_eq _]]] _] | [[_ [_ [Htrans_eq _]]] _]];
    (split; [exact Hworld' |
      exact (eq_ind_r (fun l ⇒ Forall (fun entry ⇒ hoststoragewords.storageWords (snd entry)) l)
        Htransient Htrans_eq)]).
Qed.

Section Dispatch.
Context {F : P.FArgs}.

Local Abbreviation lookup := (@P.try_precompile F).

Lemma try_precompile_preserves_context msg addr action :
  (@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
  lookup msg addr = Some action → preservesContext action.
Proof.
  intros Hrevision Hselected. unfold P.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;
      apply context_return.
  - unfold P.monad_precompiles 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 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.
    + apply context_return.
    + apply precompile_preserves_context.
    + apply context_return.
    + apply staking_contract_preserves_context.
Qed.
Lemma try_precompile_preserves_supply msg addr action :
  (@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
  lookup msg addr = Some action → preservesSupply action.
Proof.
  intros Hrevision Hselected. unfold P.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;
      apply supply_return.
  - unfold P.monad_precompiles 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 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.
    + apply supply_return.
    + 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|apply Z.eq_le_incl; exact Htotal].
    + apply supply_return.
    + intros s Hinv.
      destruct (staking_contract_preserves_supply
        P._rocq_fallback_monad_precompiles_1
        P._rocq_fallback_monad_precompiles_2
        P._rocq_fallback_monad_precompiles_3
        msg s Hinv) as [Hafter [Htotal _]].
      split; [exact Hafter|apply Z.eq_le_incl; exact Htotal].
Qed.
Lemma try_precompile_preserves_format msg addr action :
  (@P.Revision F).(Precompiles.Make_Revision_signature.revision) = Support.Variants.Revision.Eleven →
  lookup msg addr = Some action → preservesCallback hostFormat action.
Proof.
  intros Hrevision Hselected. unfold P.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;
      apply format_return.
  - unfold P.monad_precompiles 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 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.
    + apply format_return.
    + exact (@precompile_preserves_format _
        (Opaque.legacy_witness tt) (Opaque.legacy_witness (Ascii.ascii_of_nat 0))
        (Opaque.legacy_witness (0%Z,0%Z)) (Opaque.legacy_witness String.EmptyString)
        (Opaque.legacy_witness 0%Z) msg).
    + apply format_return.
    + apply staking_contract_preserves_format.
Qed.

Lemma syscall_endpoint_preserves_context msg addr endpoint :
  Chain.Ethereum.Address.Map.find_opt addr (@P.syscall_endpoints F) = Some endpoint →
  preservesContext (endpoint msg).
Proof.
  intro Hfind. unfold P.syscall_endpoints in Hfind.
  apply find_singleton_value in Hfind as [_ ->].
  apply staking_syscalls_preserves_context.
Qed.

Lemma syscall_endpoint_preserves_format msg addr endpoint :
  Chain.Ethereum.Address.Map.find_opt addr (@P.syscall_endpoints F) = Some endpoint →
  preservesCallback hostFormat (endpoint msg).
Proof.
  intro Hfind. unfold P.syscall_endpoints in Hfind.
  apply find_singleton_value in Hfind as [_ ->].
  apply staking_syscalls_preserves_format.
Qed.

End Dispatch.