Library QedkConcrete.systemmessages

Prescribed system calls at block entry

These calls bypass transaction reserve checks. Their bytecode cannot call another account, their root value is zero, and they cannot create accounts. Consequently they preserve all code and cannot debit any other account, including a delegated signer. No system-message balance axiom or cryptographic creation exclusion is needed for this step.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.executionnocalls QedkConcrete.systemcode
  QedkConcrete.transactionchecked QedkConcrete.hostcontextwords
  QedkConcrete.supply QedkConcrete.hostaccountformat QedkConcrete.vmsupplywords
  QedkConcrete.vmcallchecked QedkConcrete.hashwords QedkConcrete.wordrange.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).

Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Local Abbreviation readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@W.account None addr)).
Local Abbreviation systemMessage := (@E.process_system_message F).
Local Abbreviation hostCall := (@E.Host.call F).

Theorem system_message_call_free block addr data :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  OCamlString.length data < 2^62 →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  callFreeBytes (A.t.code (readAccount (B.t.world_state block) addr)) = true →
  let post := snd (systemMessage block addr data) in
  balanceInvariant (B.t.world_state post) ∧
  totalBalance (B.t.world_state post) ≤ totalBalance (B.t.world_state block) ∧
  (∀ query, A.t.code (readAccount (B.t.world_state post) query) =
    A.t.code (readAccount (B.t.world_state block) query)) ∧
  (∀ query, addr ≠ query →
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state post) query))).
Proof.
  intros Hrevision Hhash Hchain Hheader Hdata Hbalance Hformat Hcode.
  cbn zeta. unfold E.process_system_message.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  { split; [exact Hbalance|]. split; [apply Z.le_refl|].
    split; intros; [reflexivity|apply Z.le_refl]. }
  cbn beta iota zeta.
  match goal with |- context [E.Host.call ?message ?entry] ⇒
    set (msg := message); set (initial := entry) end.
  assert (Hmsg : @vmMessageBounds msg).
  { split; [change (0 ≤ 30000000 < 2^63); lia|].
    split; [change (0 ≤ 0 < 2^256); lia|]. split; [exact Hdata|].
    apply (encoded_capacity_all_decoders (UI := Opaque.legacy_witness 0%Z)).
    unfold E.vm_max_memory_usage. rewrite Hrevision. vm_compute. reflexivity. }
  assert (Hentry : hostSupplyContext params (totalBalance (B.t.world_state block)) initial).
  { split.
    - split; [split; [exact Hbalance|apply Z.le_refl]|].
      split; [exact Hformat|constructor].
    - destruct Hheader as [Hbase [Hnumbers Hrandao]]. split.
      + constructor; [change (0 ≤ 0 < 2^256); lia|].
        constructor; [exact Hbase|]. constructor; [exact Hchain|constructor].
      + split; assumption. }
  assert (Hcodes : ∀ query,
    A.t.code (readAccount (S.t.world_state (snd (hostCall msg initial))) query) =
    A.t.code (readAccount (B.t.world_state block) query)).
  { intro query. exact (@execution_system_call_code F

      (totalBalance (B.t.world_state block)) msg initial query Hrevision Hhash Hmsg
      eq_refl eq_refl eq_refl Hcode Hentry). }
  assert (Hbalances : ∀ query, addr ≠ query →
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
    Numeric.U256.to_z (A.t.balance (readAccount (S.t.world_state (snd (hostCall msg initial))) query))).
  { intros query Hdifferent. exact (@execution_system_call_nondebit F

      (totalBalance (B.t.world_state block)) msg initial query Hrevision Hhash Hmsg
      eq_refl eq_refl eq_refl Hcode Hdifferent Hdifferent Hentry). }
  pose proof (@execution_system_call_supply F

    (totalBalance (B.t.world_state block)) msg initial Hrevision Hhash Hmsg
    eq_refl eq_refl eq_refl Hcode Hentry) as Hsupply.
  change (balanceInvariant (S.t.world_state (snd (hostCall msg initial))) ∧
    totalBalance (S.t.world_state (snd (hostCall msg initial))) ≤ totalBalance (B.t.world_state block))
    in Hsupply.
  destruct (hostCall msg initial) as [result afterVm].
  split; [exact (proj1 Hsupply)|]. split; [exact (proj2 Hsupply)|].
  split; [exact Hcodes|exact Hbalances].
Qed.

Corollary system_message_prescribed_code block addr data :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  OCamlString.length data < 2^62 →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  (A.t.code (readAccount (B.t.world_state block) addr) = beaconRootsCode ∨
   A.t.code (readAccount (B.t.world_state block) addr) = historyStorageCode) →
  let post := snd (systemMessage block addr data) in
  balanceInvariant (B.t.world_state post) ∧
  totalBalance (B.t.world_state post) ≤ totalBalance (B.t.world_state block) ∧
  (∀ query, A.t.code (readAccount (B.t.world_state post) query) =
    A.t.code (readAccount (B.t.world_state block) query)) ∧
  (∀ query, addr ≠ query →
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state post) query))).
Proof.
  intros Hrevision Hhash Hchain Hheader Hdata Hbalance Hformat Hcode.
  apply system_message_call_free; try assumption.
  destruct Hcode as [Hcode|Hcode]; rewrite Hcode;
    [apply beaconRootsCode_call_free|apply historyStorageCode_call_free].
Qed.
The first system call preserves the second account's code. Its proved supply bound and the ordinary format frame establish the second call's entry conditions, so no intermediate-state premise is needed.
Theorem two_system_messages_prescribed_code block beaconAddr beaconData historyAddr historyData :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  OCamlString.length beaconData < 2^62 → OCamlString.length historyData < 2^62 →
  balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  A.t.code (readAccount (B.t.world_state block) beaconAddr) = beaconRootsCode →
  A.t.code (readAccount (B.t.world_state block) historyAddr) = historyStorageCode →
  let afterBeacon := snd (systemMessage block beaconAddr beaconData) in
  let post := snd (systemMessage afterBeacon historyAddr historyData) in
  balanceInvariant (B.t.world_state post) ∧
  totalBalance (B.t.world_state post) ≤ totalBalance (B.t.world_state block) ∧
  (∀ query, A.t.code (readAccount (B.t.world_state post) query) =
    A.t.code (readAccount (B.t.world_state block) query)) ∧
  (∀ query, beaconAddr ≠ query → historyAddr ≠ query →
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
    Numeric.U256.to_z (A.t.balance (readAccount (B.t.world_state post) query))).
Proof.
  intros Hrevision Hhash Hchain Hheader HbeaconData HhistoryData Hbalance Hformat Hbeacon Hhistory.
  cbn zeta.
  destruct (system_message_prescribed_code block beaconAddr beaconData Hrevision Hhash Hchain
    Hheader HbeaconData Hbalance Hformat (or_introl Hbeacon))
    as [Hbalance1 [Htotal1 [Hcode1 Hdebit1]]].
  set (afterBeacon := snd (systemMessage block beaconAddr beaconData)) in ×.
  assert (Hformat1 : worldFormat (B.t.world_state afterBeacon)).
  { apply transactionformat.system_message_preserves_format; assumption. }
  assert (Hheader1 : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block afterBeacon))).
  { unfold afterBeacon. rewrite system_message_current_block. exact Hheader. }
  assert (Hhistory1 : A.t.code (readAccount (B.t.world_state afterBeacon) historyAddr) = historyStorageCode).
  { rewrite Hcode1. exact Hhistory. }
  destruct (system_message_prescribed_code afterBeacon historyAddr historyData Hrevision Hhash Hchain
    Hheader1 HhistoryData Hbalance1 Hformat1 (or_intror Hhistory1))
    as [Hbalance2 [Htotal2 [Hcode2 Hdebit2]]].
  split; [exact Hbalance2|]. split; [lia|]. split.
  - intro query. rewrite Hcode2, Hcode1. reflexivity.
  - intros query HnotBeacon HnotHistory.
    eapply Z.le_trans; [apply Hdebit1; exact HnotBeacon|apply Hdebit2; exact HnotHistory].
Qed.
End Execution.
Print Assumptions system_message_call_free.
Print Assumptions system_message_prescribed_code.

Print Assumptions two_system_messages_prescribed_code.