Library QedkConcrete.protectedstoragehost

Protected configuration storage through concrete Host calls

Ordinary calls can transfer value to the protected account, but its empty code cannot write storage. CREATE is excluded by its nonzero nonce. This file supplies the Host induction step, including rollback; recursive VM and native-setter closure are discharged separately.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.protectedstorage
  QedkConcrete.vmcallframe QedkConcrete.hostcode
  QedkConcrete.hostframe QedkConcrete.vmhostframe QedkConcrete.assemblychecked
  QedkConcrete.contexthostinstance.
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 protected := (@protectedStorage).
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)))).

Lemma concrete_primitives_preserve_protected_storage self query P :
  self ≠ query → PrimitiveHostFrameAt (HostF vm) (protected query P) self.
Proof.
  intro Hdifferent. constructor; unfold preservesCallback; 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].
  all: try assumption.
  all: try (rewrite copy_code_read_only; assumption).
  all: try (eapply protected_storage_same_fields; [reflexivity|reflexivity|reflexivity|reflexivity|eassumption]).
  all: lazymatch goal with
  | |- protected _ _ (snd (@H.set_storage _ _ _ _ _)) ⇒
    apply sstore_preserves_protected_storage; assumption
  | |- protected _ _ (snd (@H.selfdestruct _ _ _ _)) ⇒
    apply selfdestruct_preserves_protected_storage; assumption
  | _ ⇒ unfold H.access_account, H.access_storage, H.set_transient_storage, H.emit_log;
    cbn; try match goal with |- context [if ?accessed then _ else _] ⇒ destruct accessed end;
    (eapply (@protected_storage_same_fields _ _ host);
      [reflexivity|reflexivity|reflexivity|reflexivity|assumption])
  end.
Qed.

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

Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).

Local Abbreviation hostCall := (@H.call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).

Lemma process_call_preserves_protected_storage from_tx query P msg :
  (∀ addr action, nativeDispatch vm msg addr = Some action →
    preservesCallback (protected query P) action) →
  (Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
  (∀ code s, protected query P s →
    (Evmc.Message.t.recipient msg = query → code = Byte_string.Bytes.empty) →
    protected query P (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
  preservesCallback (protected query P) (processCall from_tx msg).
Proof.
  intros Hnative HcodeAddress Hvm s Hinitial.
  assert (Htransfer : protected query P (snd
    ((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)) s))).
  { destruct (H.should_transfer msg); [apply transfer_preserves_protected_storage|]; exact Hinitial. }
  unfold H.process_call, H.M.
  cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile].
  match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hmiddle : protected query P (snd run)) by exact Htransfer;
    destruct run as [ok middle] eqn:Hrun end.
  cbn beta iota zeta delta [snd] in Hmiddle.
  destruct ok; [|exact Hmiddle].
  destruct from_tx as [tx|].
  all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address].
  all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
    destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
  all: cbn -[W.account Precompiles.Make.try_precompile].
  all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct selection as [action|] eqn:Hselected end.
  all: try (eapply Hnative; eassumption).
  all: apply Hvm; [exact Hmiddle|].
  all: intro Hrecipient;
    pose proof (protected_storage_code _ _ _ Hmiddle) as Hempty;
    first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
  exfalso. match type of Hdelegation with
  | @Delegation.get_delegated_address ?bytes = Some _ ⇒
    assert (Hbytes : bytes = Byte_string.Bytes.empty) by
      (rewrite (HcodeAddress Hrecipient); exact Hempty);
    pose proof (eq_trans
      (eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
      Hdelegation) as Hfalse;
    discriminate Hfalse
  end.
Qed.
On failure the Host restores the call-entry state. On success it keeps the state protected by the recursive VM/native-call induction steps. The top-level reserve check is read-only and can only cause rollback.
Lemma call_impl_preserves_protected_storage from_tx query P msg :
  (∀ nested addr action, nativeDispatch vm nested addr = Some action →
    preservesCallback (protected query P) action) →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ s, protected query P s →
    (Evmc.Message.t.recipient nested = query → code = Byte_string.Bytes.empty) →
    protected query P (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
  preservesCallback (protected query P) (callImpl from_tx msg).
Proof.
  intros Hnative Hvm Hgas Htarget s Hinitial.
  pose proof (process_create_preserves_protected_storage (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
    query P msg) as Hcreate.
  specialize (Hcreate (fun nested code Hbound Hneq initial Hprotected ⇒
    Hvm nested code Hbound initial Hprotected (fun Heq ⇒ False_rect _ (Hneq Heq))) Hgas).
  assert (Hcall :
    (Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
    preservesCallback (protected query P) (processCall from_tx msg)).
  { intro Hcode. exact (process_call_preserves_protected_storage from_tx query P msg
      (Hnative msg) Hcode (fun code initial ⇒ Hvm msg code Hgas initial)). }
  unfold H.call_impl.
  destruct (Evmc.Message.t.kind msg) eqn:Hkind;
    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 : protected query P (snd run)) by
      (lazymatch run with ?f ?initial ⇒
        assert (Hentry : protected query P initial) by
          (eapply (@protected_storage_same_fields query P s);
            [reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]);
        first [exact (Hcreate initial Hentry)|
          destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate;
          exact (Hcall Htarget initial Hentry)] end);
    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 Hcore; try exact Hinitial.
  all: try (eapply (@protected_storage_same_fields query P s);
    [reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]).
  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; first [exact Hinitial|exact Hcore|
    eapply (@protected_storage_same_fields query P s);
    [reflexivity|reflexivity|reflexivity|reflexivity|exact Hinitial]].
Qed.

Lemma call_preserves_protected_storage self query P msg :
  (∀ nested addr action, nativeDispatch vm nested addr = Some action →
    preservesCallback (protected query P) action) →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ s, protected query P s →
    (Evmc.Message.t.recipient nested = query → code = Byte_string.Bytes.empty) →
    protected query P (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  self ≠ query → childCallFrom self msg →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
  preservesCallback (protected query P) (hostCall msg).
Proof.
  intros Hnative Hvm Hgas Hdifferent Horigin Htarget s Hinitial.
  pose proof (call_impl_preserves_protected_storage None query P msg
    Hnative Hvm Hgas Htarget) as Himpl.
  unfold H.call.
  destruct (Evmc.Message.t.kind msg) eqn:Hkind.
  all: cbn -[H.call_impl H.try_increment_nonce].
  all: try (apply Himpl; exact Hinitial).
  all: assert (Hsender : Evmc.Message.t.sender msg ≠ query) by
    (unfold childCallFrom in Horigin; rewrite Hkind in Horigin; rewrite Horigin; exact Hdifferent).
  all: pose proof (increment_nonce_preserves_protected_storage
    (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) query P
    (Evmc.Message.t.sender msg) Hsender s Hinitial) as Hincrement.
  all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    destruct run as [ok middle] end.
  all: cbn [snd] in Hincrement; destruct ok;
    cbn -[H.call_impl]; [apply Himpl; exact Hincrement|exact Hincrement].
Qed.
End Host.

Print Assumptions concrete_primitives_preserve_protected_storage.
Print Assumptions process_call_preserves_protected_storage.
Print Assumptions call_preserves_protected_storage.