Library SetcodeConcrete.destructionframe

Deletion requires creation in the same transaction

SETCODEFROM changes code but does not record a creation. The property here is independent of code: every account scheduled for deletion was recorded as created. Together with the separate CREATE-exclusion premise, this rules out deletion of protected transaction senders.
From Stdlib Require Import List ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.hostframe QedkConcrete.hostcode
  QedkConcrete.hostaccountformat QedkConcrete.vmhostframe
  QedkConcrete.recursiveassembly QedkConcrete.reserveprecompileeffects.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.

Definition destructionCovered (s : S.t) : Prop :=
  List.incl (S.t.self_destruct s) (S.t.accounts_created_in_current_transaction s).

Lemma covered_bind {A B : Set} (action : S.t → A × S.t) (next : A → S.t → B × S.t) :
  preservesCallback destructionCovered action →
  (∀ value, preservesCallback destructionCovered (next value)) →
  preservesCallback destructionCovered (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
  intros Haction Hnext s Hbefore.
  change (destructionCovered (snd (let '(value, middle) := action s in next value middle))).
  specialize (Haction s Hbefore). destruct (action s) as [value middle].
  apply Hnext. exact Haction.
Qed.
Lemma covered_return {A : Set} (value : A) :
  preservesCallback destructionCovered (S.M.(Monad.State.State_result._return) value).
Proof. intros s H; exact H. Qed.
Lemma covered_read {A : Set} (lens : Lens.t S.t A) :
  preservesCallback destructionCovered (S.M.(Monad.State.State_result.op_exclamation) lens).
Proof. intros s H; exact H. Qed.
Lemma covered_write {A : Set} (lens : Lens.t S.t A) value :
  (∀ s, destructionCovered s → destructionCovered (Lens.t.set lens value s)) →
  preservesCallback destructionCovered (S.M.(Monad.State.State_result.op_coloneq) lens value).
Proof. exact (fun H ⇒ H). Qed.
Lemma covered_update {A : Set} (lens : Lens.t S.t A) f :
  (∀ s, destructionCovered s → destructionCovered (Lens.t.set lens (f (Lens.t.get lens s)) s)) →
  preservesCallback destructionCovered (S.M.(Monad.State.State_result.update_field) lens f).
Proof. exact (fun H ⇒ H). Qed.

Section Host.
Context {F : H.FArgs}.
Lemma transfer_preserves_destruction sender recipient amount :
  preservesCallback destructionCovered (H.transfer_ether sender recipient amount).
Proof.
  intros s Hbefore. unfold H.transfer_ether. cbn -[Numeric.U256.op_gteq].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
    exact Hbefore.
Qed.
Lemma selfdestruct_preserves_destruction addr beneficiary :
  preservesCallback destructionCovered (H.selfdestruct addr beneficiary).
Proof.
  intros s Hbefore. unfold H.selfdestruct.
  cbn -[H.transfer_ether Addr._Set W.account].
  match goal with |- context [H.transfer_ether ?from ?to ?value ?before] ⇒
    pose proof (transfer_preserves_destruction from to value before Hbefore) as Htransfer;
    destruct (H.transfer_ether from to value before) as [ok middle] end.
  cbn beta iota zeta delta [snd] in Htransfer.
  cbn -[Addr._Set W.account].
  destruct (Addr._Set.(OCamlSet.S.mem) addr
    (S.t.accounts_created_in_current_transaction middle)) eqn:Hcreated.
  - intros query Hin. apply address_set_add_members in Hin as [Heq|Hin].
    + subst query. apply address_set_mem_member. exact Hcreated.
    + apply Htransfer. exact Hin.
  - exact Htransfer.
Qed.
Lemma setcode_preserves_destruction current source static gas :
  preservesCallback destructionCovered (H.setcodefrom current source static gas).
Proof.
  intros s Hbefore. unfold H.setcodefrom.
  destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
  match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
    destruct prices as [[[[cold warm] access] write] reserve] end.
  cbn beta iota zeta.
  repeat match goal with |- context [if ?test then _ else _] ⇒
    destruct test; cbn beta iota zeta end.
  all: exact Hbefore.
Qed.
End Host.

Ltac solve_destruction_frame :=
  lazymatch goal with
  | |- preservesCallback destructionCovered (S.M.(Monad.State.State_result.op_letdollar) _ _) ⇒
    apply covered_bind; [solve_destruction_frame|];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_destruction_frame
  | |- preservesCallback destructionCovered (S.M.(Monad.State.State_result._return) _) ⇒ apply covered_return
  | |- preservesCallback destructionCovered (S.M.(Monad.State.State_result.op_exclamation) _) ⇒ apply covered_read
  | |- preservesCallback destructionCovered (S.M.(Monad.State.State_result.op_coloneq) _ _) ⇒
    apply covered_write; intros s Hbefore; exact Hbefore
  | |- preservesCallback destructionCovered (S.M.(Monad.State.State_result.update_field) _ _) ⇒
    apply covered_update; intros s Hbefore;
    first [exact Hbefore | intros query Hin; apply address_set_add_members; right; apply Hbefore; exact Hin]
  | |- preservesCallback destructionCovered (if ?test then _ else _) ⇒ destruct test; solve_destruction_frame
  | |- preservesCallback destructionCovered (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct value; solve_destruction_frame
  | |- preservesCallback destructionCovered (match ?status with Evmc.Result.StatusCode.Success ⇒ _ | _ ⇒ _ end) ⇒
    destruct status; solve_destruction_frame
  | |- preservesCallback destructionCovered (@H.transfer_ether _ _ _ _) ⇒ apply transfer_preserves_destruction
  | Hvm : ∀ msg code, _ → preservesCallback destructionCovered (_ msg code)
    |- preservesCallback destructionCovered (_ ?msg ?code) ⇒ apply Hvm; assumption
  end.

Section Native.
Context {F : Reserve_balance_introspection.Make.FArgs}.
Lemma reserve_precompile_preserves_destruction msg :
  preservesCallback destructionCovered (Reserve_balance_introspection.Make.precompile msg).
Proof.
  intros s Hbefore.
  change (destructionCovered (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
  destruct (@precompile_world_effect F msg s) as [Hsame|[value [world [Hwrite Hafter]]]];
    [rewrite Hsame|rewrite Hafter]; exact Hbefore.
Qed.
End Native.

Section Dispatch.
Context {F : Precompiles.Make.FArgs}.
Local Lemma singleton_value {A : Set} key (value : A) query result :
  Addr.Map.find_opt query (Addr.Map.of_list ((key, value) :: nil)) = Some result → result = value.
Proof.
  change (OCamlMap.Make.find_opt stringmaps.stringOrder query ((key, value) :: nil) = Some result → result = value).
  cbn [OCamlMap.Make.find_opt].
  destruct (OCamlMap.Make.key_before stringmaps.stringOrder query key); [discriminate|].
  destruct (OCamlMap.Make.key_equal stringmaps.stringOrder query key); [|discriminate].
  intro Heq. inversion Heq. reflexivity.
Qed.
Lemma precompile_preserves_destruction cold warm access write reserve msg addr action :
  Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  Precompiles.Make.try_precompile msg addr = Some action →
  preservesCallback destructionCovered action.
Proof.
  intros Hrevision Hselected. unfold Precompiles.Make.try_precompile in Hselected.
  match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
    destruct (@Addr.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 covered_return.
  - unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
    match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
      destruct (@Addr.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
    2: discriminate.
    apply singleton_value in Hnative. subst native.
    match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
    + inversion Hselected; subst action. apply covered_return.
    + inversion Hselected; subst action. apply reserve_precompile_preserves_destruction.
Qed.
End Dispatch.

Section Calls.
Context {F : H.FArgs}.
Context (cold warm access write reserve : N).
Context (Hrevision : H.ChainParams.(Chain.Monad.PARAMS.revision) =
  Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hvm : ∀ msg code, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesCallback destructionCovered (H.Vm.(Evmc.Vm.SIG.execute) msg code)).

Lemma process_create_preserves_destruction msg :
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesCallback destructionCovered (H.process_create msg).
Proof.
  intro Hgas. unfold H.process_create, H.M. apply covered_bind.
  - unfold H.contract_creation_address, H.M. solve_destruction_frame.
  - intro addr. solve_destruction_frame.
Qed.
Lemma process_call_preserves_destruction from_tx msg :
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesCallback destructionCovered (H.process_call from_tx msg).
Proof.
  intro Hgas. unfold H.process_call, H.M. cbn zeta. apply covered_bind.
  - destruct (H.should_transfer msg); [apply transfer_preserves_destruction|apply covered_return].
  - intros ok. destruct ok; [|apply covered_return]. apply covered_bind.
    + apply covered_bind; [apply covered_read|]. intro code.
      destruct from_tx; destruct (Delegation.get_delegated_address code); solve_destruction_frame.
    + intros [addr code].
      match goal with |- preservesCallback _ (match ?selection with Some _ ⇒ _ | None ⇒ _ end) ⇒
        destruct selection as [action|] eqn:Hselected end.
      × exact (@precompile_preserves_destruction (@H.Precompiles_fargs F)
          cold warm access write reserve msg addr action Hrevision Hselected).
      × apply Hvm. exact Hgas.
Qed.
Lemma call_impl_preserves_destruction from_tx msg :
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesCallback destructionCovered (H.call_impl from_tx msg).
Proof.
  intros Hgas s Hbefore.
  pose proof (process_call_preserves_destruction from_tx msg Hgas) as Hcall.
  pose proof (process_create_preserves_destruction msg Hgas) as Hcreate.
  unfold H.call_impl. destruct (Evmc.Message.t.kind msg);
    cbn -[H.process_call H.process_create Addr.of_contract_creation Reserve_balance.dipped_into_reserve].
  all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hcore : destructionCovered (snd run)) by
      (first [apply Hcall | apply Hcreate]; exact Hbefore);
    destruct run as [result intermediate] eqn:Hrun end.
  all: destruct result as [status gasleft refund output address].
  all: destruct from_tx as [tx|]; destruct status;
    cbn -[Reserve_balance.dipped_into_reserve];
    unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
    try exact Hbefore; try exact Hcore.
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
    assert (Hcheck : snd check = intermediate) by reflexivity;
    destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked end.
  all: destruct dipped; cbn; [exact Hbefore|exact Hcore].
Qed.
Lemma call_preserves_destruction msg :
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesCallback destructionCovered (H.call msg).
Proof.
  intro Hgas. unfold H.call, H.M. apply covered_bind.
  - match goal with |- preservesCallback _ (if ?test then _ else _) ⇒ destruct test end.
    + unfold H.try_increment_nonce, H.M. solve_destruction_frame.
    + apply covered_return.
  - intros ok. destruct ok; [apply call_impl_preserves_destruction; exact Hgas|apply covered_return].
Qed.
End Calls.

Section Primitives.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Lemma concrete_primitives_preserve_destruction vm :
  PrimitiveHostFrame (HostF vm) destructionCovered.
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 destructionCovered].
  all: try lazymatch goal with
    | |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_preserves_destruction
    end.
  all: intros s Hbefore.
  all: first [exact Hbefore | rewrite copy_code_read_only; exact Hbefore |
    unfold H.access_account, H.access_storage;
    cbn -[W.account];
    repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
    exact Hbefore].
Qed.
End Primitives.
Print Assumptions call_preserves_destruction.