Library QedkConcrete.blockchecked

Supply bound through the generated block executor

System calls and transactions reuse the concrete recursive execution proof. System-transaction rewards and withdrawals supply the external credit budget; fees are transfers or burns, and native-output bounds are explicit premises discharged in precompileoutputs.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.transactionchecked
  QedkConcrete.transactionsupply QedkConcrete.transactionformat
  QedkConcrete.executionchecked QedkConcrete.assemblychecked
  QedkConcrete.hashwords QedkConcrete.wordrange QedkConcrete.credits
  QedkConcrete.reservewrites QedkConcrete.hostaccountformat QedkConcrete.supply
  QedkConcrete.stakingsupply.
Require Import QedkConcrete.nativeinputs.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.

Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
  {UB : Basics.Unreachable Bloom.t}
  {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.
Context {UTries : Basics.Unreachable (OCamlIarray.t Mpt.Trie.t)}
  {UChar : Basics.Unreachable OCamlChar.t} {UWorld : Basics.Unreachable W.t}
  {UTrie : Basics.Unreachable Mpt.Trie.t} {UNibbles : Basics.Unreachable Mpt.Nibbles.t}
  {UBlock : Basics.Unreachable Chain.Ethereum.Block.t}.

Theorem process_block_preserves_supply verify world block post :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world → worldFormat world →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx) ∧
    (@E.sender_kind_value F tx ≠ Some E.System ∨ U.to_z (T.value tx) = 0))
    (Chain.Ethereum.Block.t.transactions block) →
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
    (Chain.Ethereum.Block.t.withdrawals block) →
  let credits := Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0
    (Chain.Ethereum.Block.t.withdrawals block) in
  totalBalance world + credits < 2^256 →
  @E.process_block F verify world block = inl post →
  balanceInvariant post ∧ totalBalance post ≤ totalBalance world + credits.
Proof.
  intros Hrevision Hhash Hnative Hchain Hheader Hroot Hinitial Hformat Htxs
    Hwithdrawals credits Hbudget Hrun.
  unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
  change (@Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
  destruct (@E.validate_block F world block) as [[]|error] eqn:Hvalidation;
    [|discriminate Hrun].
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [B.make ?w block] ⇒ set (initial := B.make w block) in × end.
  assert (HinitialFormat : worldFormat (B.t.world_state initial)) by
    (apply activate_preserves_format; exact Hformat).
  assert (HinitialSupply : balanceInvariant (B.t.world_state initial) ∧
    totalBalance (B.t.world_state initial) = totalBalance world).
  { apply activate_preserves_supply. exact Hinitial. }
  match type of Hrun with context [E.process_system_message initial ?addr ?data] ⇒
    assert (Hdata : OCamlString.length data < 2^62) by
      (change (OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
        (Chain.Ethereum.Block.t.header block)) < 2^62); rewrite Hroot; lia);
    pose proof (@system_message_preserves_supply F
      initial addr data Hrevision Hhash Hnative Hchain Hheader
      Hdata (proj1 HinitialSupply) HinitialFormat) as Hbeacon;
    pose proof (system_message_preserves_format initial addr data Hrevision HinitialFormat) as HbeaconFormat;
    pose proof (@system_message_current_block F
      initial addr data) as HbeaconBlock;
    destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
  end.
  cbn beta iota zeta delta [snd] in Hbeacon, HbeaconFormat, HbeaconBlock, Hrun.
  destruct Hbeacon as [Hbeacon HbeaconTotal].
  assert (HbeaconHeader : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block afterBeacon))).
  { rewrite HbeaconBlock. exact Hheader. }
  match type of Hrun with context [E.process_system_message afterBeacon ?addr ?data] ⇒
    assert (HdataHash : OCamlString.length data < 2^62) by
      (lazymatch data with ?toBytes ?hash ⇒ change (OCamlString.length hash < 2^62) end;
       rewrite (block_hash_length Hhash); lia);
    pose proof (@system_message_preserves_supply F
      afterBeacon addr data Hrevision Hhash Hnative Hchain HbeaconHeader
      HdataHash Hbeacon HbeaconFormat) as Hhistory;
    pose proof (system_message_preserves_format afterBeacon addr data Hrevision HbeaconFormat) as HhistoryFormat;
    pose proof (@system_message_current_block F
      afterBeacon addr data) as HhistoryBlock;
    destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
  end.
  cbn beta iota zeta delta [snd] in Hhistory, HhistoryFormat, HhistoryBlock, Hrun.
  destruct Hhistory as [Hhistory HhistoryTotal].
  assert (HhistoryHeader : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block afterHistory))).
  { rewrite HhistoryBlock. exact HbeaconHeader. }
  match type of Hrun with context [Result.List.fold_leftM ?step afterHistory ?txs] ⇒
    destruct (Result.List.fold_leftM step afterHistory txs) as [afterTransactions|error] eqn:Htransactions
  end.
  2: discriminate Hrun.
  cbn beta iota zeta in Hrun.
  pose proof (@dispatched_transactions_preserve_supply F UI0 UB U8
    afterHistory _ afterTransactions
    Hrevision Hhash Hnative Hchain HhistoryHeader Htxs Hhistory HhistoryFormat Htransactions)
    as [HtxFinal HtxTotal].
  assert (HremainingBudget : totalBalance (B.t.world_state afterTransactions) + credits < 2^256).
  { pose proof (proj2 HinitialSupply). lia. }
  pose proof (@withdrawals_preserve_supply_bound F afterTransactions
    (Chain.Ethereum.Block.t.withdrawals block) HtxFinal Hwithdrawals HremainingBudget)
    as [Hpaid HpaidTotal].
  match type of Hrun with context [E.validate_block ?world ?block] ⇒
    destruct (E.validate_block world block) as [[]|error]
  end.
  2: discriminate Hrun.
  match type of Hrun with context [if verify then ?check else _] ⇒
    destruct verify; [destruct check as [[]|error]; [|discriminate Hrun]|]
  end.
  all: injection Hrun as <-; split; [exact Hpaid|].
  all: change (totalBalance (B.t.world_state (OCamlList.fold_left (@E.process_withdrawal F)
    afterTransactions (Chain.Ethereum.Block.t.withdrawals block))) ≤ totalBalance world + credits).
  all: destruct HpaidTotal as [HpaidTotal _]; rewrite HpaidTotal; fold credits;
    pose proof (proj2 HinitialSupply); lia.
Qed.

Theorem process_block_supply_with_credits
    (txAllowance : T.t → Z) verify world block post :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header block) →
  OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
    (Chain.Ethereum.Block.t.header block)) = 32 →
  balanceInvariant world → worldFormat world →
  Forall (fun tx ⇒ 0 ≤ txAllowance tx) (Chain.Ethereum.Block.t.transactions block) →
  Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
    Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx) ∧
    (@E.sender_kind_value F tx ≠ Some E.System ∨ U.to_z (T.value tx) ≤ txAllowance tx))
    (Chain.Ethereum.Block.t.transactions block) →
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
    (Chain.Ethereum.Block.t.withdrawals block) →
  let rewardCredits := externalCreditTotal
    (Stdlib.Lists.List.map txAllowance (Chain.Ethereum.Block.t.transactions block)) in
  let withdrawalCredits := Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0
    (Chain.Ethereum.Block.t.withdrawals block) in
  totalBalance world + withdrawalCredits + rewardCredits < 2^256 →
  @E.process_block F verify world block = inl post →
  balanceInvariant post ∧
    totalBalance post ≤ totalBalance world + withdrawalCredits + rewardCredits.
Proof.
  intros Hrevision Hhash Hnative Hchain Hheader Hroot Hinitial Hformat HallowNonneg Htxs
    Hwithdrawals rewardCredits withdrawalCredits Hbudget Hrun.
  unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
  change (@Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
  destruct (@E.validate_block F world block) as [[]|error] eqn:Hvalidation;
    [|discriminate Hrun].
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [B.make ?w block] ⇒ set (initial := B.make w block) in × end.
  assert (HinitialFormat : worldFormat (B.t.world_state initial)) by
    (apply activate_preserves_format; exact Hformat).
  assert (HinitialSupply : balanceInvariant (B.t.world_state initial) ∧
    totalBalance (B.t.world_state initial) = totalBalance world).
  { apply activate_preserves_supply. exact Hinitial. }
  match type of Hrun with context [E.process_system_message initial ?addr ?data] ⇒
    assert (Hdata : OCamlString.length data < 2^62) by
      (change (OCamlString.length (Chain.Ethereum.Block.Header.t.parent_beacon_block_root
        (Chain.Ethereum.Block.t.header block)) < 2^62); rewrite Hroot; lia);
    pose proof (@system_message_preserves_supply F
      initial addr data Hrevision Hhash Hnative Hchain Hheader
      Hdata (proj1 HinitialSupply) HinitialFormat) as Hbeacon;
    pose proof (system_message_preserves_format initial addr data Hrevision HinitialFormat) as HbeaconFormat;
    pose proof (@system_message_current_block F
      initial addr data) as HbeaconBlock;
    destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
  end.
  cbn beta iota zeta delta [snd] in Hbeacon, HbeaconFormat, HbeaconBlock, Hrun.
  destruct Hbeacon as [Hbeacon HbeaconTotal].
  assert (HbeaconHeader : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block afterBeacon))).
  { rewrite HbeaconBlock. exact Hheader. }
  match type of Hrun with context [E.process_system_message afterBeacon ?addr ?data] ⇒
    assert (HdataHash : OCamlString.length data < 2^62) by
      (lazymatch data with ?toBytes ?hash ⇒ change (OCamlString.length hash < 2^62) end;
       rewrite (block_hash_length Hhash); lia);
    pose proof (@system_message_preserves_supply F
      afterBeacon addr data Hrevision Hhash Hnative Hchain HbeaconHeader
      HdataHash Hbeacon HbeaconFormat) as Hhistory;
    pose proof (system_message_preserves_format afterBeacon addr data Hrevision HbeaconFormat) as HhistoryFormat;
    pose proof (@system_message_current_block F
      afterBeacon addr data) as HhistoryBlock;
    destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
  end.
  cbn beta iota zeta delta [snd] in Hhistory, HhistoryFormat, HhistoryBlock, Hrun.
  destruct Hhistory as [Hhistory HhistoryTotal].
  assert (HhistoryHeader : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block afterHistory))).
  { rewrite HhistoryBlock. exact HbeaconHeader. }
  match type of Hrun with context [Result.List.fold_leftM ?step afterHistory ?txs] ⇒
    destruct (Result.List.fold_leftM step afterHistory txs) as [afterTransactions|error] eqn:Htransactions
  end.
  2: discriminate Hrun.
  cbn beta iota zeta in Hrun.
  assert (HrewardsNonneg : Forall (fun r ⇒ 0 ≤ r)
    (Stdlib.Lists.List.map txAllowance (Chain.Ethereum.Block.t.transactions block))).
  { rewrite Forall_map. exact HallowNonneg. }
  assert (HhistoryBudget : supply_with_credits (B.t.world_state afterHistory)
    (Stdlib.Lists.List.map txAllowance (Chain.Ethereum.Block.t.transactions block) ++
      Stdlib.Lists.List.map withdrawalWei (Chain.Ethereum.Block.t.withdrawals block))).
  { apply withdrawals_and_rewards_budget; [exact Hhistory | exact Hwithdrawals | exact HrewardsNonneg |].
    fold rewardCredits withdrawalCredits.
    pose proof (proj2 HinitialSupply). lia. }
  pose proof (@dispatched_transactions_supply_with_credits F UI0 UB U8
    txAllowance afterHistory (Chain.Ethereum.Block.t.transactions block)
    (Stdlib.Lists.List.map withdrawalWei (Chain.Ethereum.Block.t.withdrawals block))
    afterTransactions Hrevision Hhash Hnative Hchain HhistoryHeader Htxs
    HhistoryBudget HhistoryFormat Htransactions)
    as [HtxFinalSup HtxTotal].
  pose proof (proj1 HtxFinalSup) as HtxFinal.
  assert (HremainingBudget : totalBalance (B.t.world_state afterTransactions) + withdrawalCredits < 2^256).
  { destruct HtxFinalSup as [_ [_ Hbound]].
    rewrite external_credit_total_withdrawals in Hbound.
    exact Hbound. }
  pose proof (@withdrawals_preserve_supply_bound F afterTransactions
    (Chain.Ethereum.Block.t.withdrawals block) HtxFinal Hwithdrawals HremainingBudget)
    as [Hpaid HpaidTotal].
  match type of Hrun with context [E.validate_block ?world ?block] ⇒
    destruct (E.validate_block world block) as [[]|error]
  end.
  2: discriminate Hrun.
  match type of Hrun with context [if verify then ?check else _] ⇒
    destruct verify; [destruct check as [[]|error]; [|discriminate Hrun]|]
  end.
  all: injection Hrun as <-; split; [exact Hpaid|].
  all: change (totalBalance (B.t.world_state (OCamlList.fold_left (@E.process_withdrawal F)
    afterTransactions (Chain.Ethereum.Block.t.withdrawals block))) ≤
    totalBalance world + withdrawalCredits + rewardCredits).
  all: destruct HpaidTotal as [HpaidTotal _]; rewrite HpaidTotal;
    fold withdrawalCredits; fold rewardCredits in HtxTotal;
    pose proof (proj2 HinitialSupply); lia.
Qed.
End Execution.

Print Assumptions process_block_preserves_supply.