Library QedkConcrete.hostcallchecked

Bounded concrete Host calls

Code is read from the account-format invariant, including delegation lookup. The recursive VM premise therefore applies only to actual bounded messages and code. Native output lengths remain a separate obligation.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.hostcomposition
  QedkConcrete.hostsupply QedkConcrete.hostaccountformat QedkConcrete.hostchecked
  QedkConcrete.hostcreatechecked QedkConcrete.vmsupplywords QedkConcrete.vmcallchecked
  QedkConcrete.wordrange QedkConcrete.worldaccounts QedkConcrete.vmhostframe
  QedkConcrete.precompilesframe QedkConcrete.hostcontextwords
  QedkConcrete.vmmemorywords QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Calls.
Context {HF : Host.Instantiate.FArgs}.
Context (vm : Evmc.Vm.SIG (_fargs :=
  Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (limit : Z).
Local Abbreviation F := (H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm).
Local Abbreviation invariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).

Local Abbreviation processCall := (@H.process_call F).

Local Abbreviation callImpl := (@H.call_impl F).

Local Abbreviation hostCall := (@H.call F).

Local Abbreviation tryNative := (@Precompiles.Make.try_precompile (@H.Precompiles_fargs F)).

Lemma read_code_checked addr :
  hostChecked invariant (fun code ⇒ OCamlString.length code < 2^63)
    (S.M.(Monad.State.State_result.op_exclamation)
      (Lens.Infix.op_pipeminusminus (@S.account None addr) A.TLens.code)).
Proof.
  intros s Hinitial. left. split; [exact Hinitial|].
  pose proof (proj2 (@read_account_format (S.t.world_state s) addr
    (proj1 (proj2 (proj1 Hinitial))))) as Hcode.
  eapply Z.lt_trans; [exact Hcode|change (2^62 < 2^63); lia].
Qed.

Ltac selected_native_frame lemma :=
  match goal with
  | Hselected : ?lookup = Some ?action, Hrevision : @reservePrecompileTable _ |- _ ⇒
    let rec replace_head f :=
      lazymatch f with
      | ?g ?arg ⇒ let g' := replace_head g in constr:(g' arg)
      | _ ⇒ lemma
      end in
    let proof := replace_head lookup in
    exact (proof action Hrevision Hselected)
  end.

Lemma process_call_preserves_checked_table from_tx msg :
  @reservePrecompileTable (@H.Precompiles_fargs F) →
  (∀ code, OCamlString.length code < 2^256 →
    hostChecked invariant resultOutputBound (vm.(Evmc.Vm.SIG.execute) msg code)) →
  (∀ addr action, tryNative msg addr = Some action →
    ∀ s, invariant s → resultOutputBound (fst (action s))) →
  @vmMessageBounds msg →
  hostChecked invariant resultOutputBound (processCall from_tx msg).
Proof.
  intros Hrevision Hvm Hnative [Hgas [Hvalue [Hdata Hcapacity]]].
  assert (Hnonnegative : 0 ≤ U.to_z (Evmc.Message.t.value msg))
    by exact (proj1 Hvalue).
  unfold H.process_call, H.M. cbn zeta.
  eapply host_checked_bind with (P := fun _ ⇒ True).
  - apply host_checked_frame, host_supply_context_frame.
    + destruct (H.should_transfer msg); [apply supply_transfer; exact Hnonnegative|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].
  - intros transfer_ok _. destruct transfer_ok.
    2: { apply host_checked_return. change (0 < 2^256). lia. }
    eapply host_checked_bind with (P := fun pair ⇒ OCamlString.length (snd pair) < 2^63).
    + eapply host_checked_bind with (P := fun code ⇒ OCamlString.length code < 2^63).
      × apply read_code_checked.
      × intros code Hcode. destruct from_tx.
        2: { apply host_checked_return. exact Hcode. }
        match goal with |- context [@Delegation.get_delegated_address code] ⇒
          destruct (@Delegation.get_delegated_address code) end.
        all: try (apply host_checked_return; exact Hcode).
        eapply host_checked_bind with (P := fun bytes ⇒ OCamlString.length bytes < 2^63).
        -- apply read_code_checked.
        -- intros bytes Hbytes. apply host_checked_return. exact Hbytes.
    + intros [addr code] Hcode. cbn [snd] in Hcode.
      change (hostChecked invariant resultOutputBound
        (match tryNative msg addr with Some action ⇒ action
         | None ⇒ vm.(Evmc.Vm.SIG.execute) msg code end)).
      destruct (tryNative msg addr) as [action|] eqn:Hselected.
      × intros s Hinitial. left. split.
        -- apply host_supply_context_frame; try exact Hinitial.
           ++ selected_native_frame constr:(@try_precompile_preserves_supply_table).
           ++ selected_native_frame constr:(@try_precompile_preserves_format_table).
           ++ selected_native_frame constr:(@try_precompile_preserves_context_table).
        -- exact (Hnative addr action Hselected s Hinitial).
      × apply Hvm. lia.
Qed.

Lemma process_call_preserves_checked from_tx msg :
  HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ code, OCamlString.length code < 2^256 →
    hostChecked invariant resultOutputBound (vm.(Evmc.Vm.SIG.execute) msg code)) →
  (∀ addr action, tryNative msg addr = Some action →
    ∀ s, invariant s → resultOutputBound (fst (action s))) →
  @vmMessageBounds msg →
  hostChecked invariant resultOutputBound (processCall from_tx msg).
Proof. intros; eapply process_call_preserves_checked_table with (from_tx := from_tx) (msg := msg);
  eauto using eleven_reserve_precompile_table. Qed.

Rollback restores the entry invariant, including the original aggregate bound. It does not require comparing a restored balance with the depleted balance of the failed child. Returned bytes remain bounded on both paths.
Lemma call_impl_preserves_checked_table from_tx msg :
  @reservePrecompileTable (@H.Precompiles_fargs F) →
  (∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
    hostChecked invariant resultOutputBound (vm.(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested addr action, @vmMessageBounds nested → tryNative nested addr = Some action →
    ∀ s, invariant s → resultOutputBound (fst (action s))) →
  @vmMessageBounds msg →
  hostChecked invariant resultOutputBound (callImpl from_tx msg).
Proof.
  intros Hrevision Hvm Hnative Hmsg s Hinitial.
  pose proof (process_call_preserves_checked_table from_tx msg Hrevision
    (fun code ⇒ Hvm msg code Hmsg) (fun addr action ⇒ Hnative msg addr action Hmsg) Hmsg) as Hcall.
  pose proof (@process_create_preserves_checked F limit msg Hvm Hmsg) as Hcreate.
  unfold H.call_impl.
  destruct (Evmc.Message.t.kind msg);
    cbn -[H.process_call H.process_create Chain.Ethereum.Address.of_contract_creation
      Reserve_balance.dipped_into_reserve hostSupplyContext resultOutputBound].
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hcore : (invariant (snd run) ∧ resultOutputBound (fst run)) ∨ badMemoryInitComparison) by
      (lazymatch run with ?f ?st ⇒
        first [exact (Hcall st Hinitial)|exact (Hcreate st Hinitial)] end);
    destruct run as [result intermediate] eqn:Hrun;
    destruct Hcore as [[Hmiddle Houtput]|Hbad]; [|right; exact Hbad]
  end.
  all: destruct result as [status gasleft refund output address].
  all: destruct from_tx as [tx|]; destruct status;
    cbn -[Reserve_balance.dipped_into_reserve hostSupplyContext resultOutputBound];
    unfold Monad.Make_Monad.when_;
    cbn -[Reserve_balance.dipped_into_reserve hostSupplyContext resultOutputBound];
    try (left; split; [exact Hmiddle|exact Houtput]);
    try (left; split; [exact Hinitial|exact Houtput]).
  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 -[hostSupplyContext resultOutputBound];
    (left; split; [first [exact Hinitial|exact Hmiddle]|exact Houtput]).
Qed.

Lemma call_impl_preserves_checked from_tx msg :
  HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
    hostChecked invariant resultOutputBound (vm.(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested addr action, @vmMessageBounds nested → tryNative nested addr = Some action →
    ∀ s, invariant s → resultOutputBound (fst (action s))) →
  @vmMessageBounds msg →
  hostChecked invariant resultOutputBound (callImpl from_tx msg).
Proof. intros; eapply call_impl_preserves_checked_table with (from_tx := from_tx) (msg := msg);
  eauto using eleven_reserve_precompile_table. Qed.

Lemma call_preserves_checked_table msg :
  @reservePrecompileTable (@H.Precompiles_fargs F) →
  (∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
    hostChecked invariant resultOutputBound (vm.(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested addr action, @vmMessageBounds nested → tryNative nested addr = Some action →
    ∀ s, invariant s → resultOutputBound (fst (action s))) →
  @vmMessageBounds msg →
  hostChecked invariant resultOutputBound (hostCall msg).
Proof.
  intros Hrevision Hvm Hnative Hmsg. unfold H.call, H.M.
  eapply host_checked_bind with (P := fun _ ⇒ True).
  - apply host_checked_frame, host_supply_context_frame.
    + match goal with |- preservesSupply (if ?test then _ else _) ⇒ destruct test end.
      × unfold H.try_increment_nonce, H.M. solve_supply_frame.
      × apply supply_return.
    + match goal with |- preservesCallback _ (if ?test then _ else _) ⇒ destruct test end.
      × unfold H.try_increment_nonce, H.M. solve_format.
      × apply format_return.
    + match goal with |- preservesContext (if ?test then _ else _) ⇒ destruct test end.
      × unfold H.try_increment_nonce, H.M. solve_context_frame.
      × apply context_return.
  - intros noOverflow _. destruct noOverflow.
    + apply call_impl_preserves_checked_table; assumption.
    + apply host_checked_return. change (0 < 2^256). lia.
Qed.

Lemma call_preserves_checked msg :
  HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
    hostChecked invariant resultOutputBound (vm.(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested addr action, @vmMessageBounds nested → tryNative nested addr = Some action →
    ∀ s, invariant s → resultOutputBound (fst (action s))) →
  @vmMessageBounds msg →
  hostChecked invariant resultOutputBound (hostCall msg).
Proof. intros; eapply call_preserves_checked_table with (msg := msg);
  eauto using eleven_reserve_precompile_table. Qed.

End Calls.

Print Assumptions call_preserves_checked.