Library SetcodeConcrete.blocksupply
Whole-block supply and account bounds for SETCODE
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.supply QedkConcrete.hostaccountformat
QedkConcrete.nativeinputs QedkConcrete.wordrange QedkConcrete.hashwords
QedkConcrete.assemblychecked QedkConcrete.credits QedkConcrete.reservewrites.
Require Import SetcodeConcrete.transactionsupply SetcodeConcrete.supplysequence.
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.
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 (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (Hhash : keccak256OutputSize) (Hnative : @nativeOutputBounds args).
Theorem process_block_preserves_supply verify world block post :
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))
(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 ∧ worldFormat post.
Proof.
intros 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 cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative initial addr data Hchain Hheader
Hdata (proj1 HinitialSupply) HinitialFormat) as Hbeacon;
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, HbeaconBlock, Hrun.
destruct Hbeacon as [Hbeacon [HbeaconTotal HbeaconFormat]].
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 cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative afterBeacon addr data Hchain HbeaconHeader
HdataHash Hbeacon HbeaconFormat) as Hhistory;
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, HhistoryBlock, Hrun.
destruct Hhistory as [Hhistory [HhistoryTotal HhistoryFormat]].
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 (transactions_preserve_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative afterHistory _ afterTransactions Hchain HhistoryHeader Htxs Hhistory HhistoryFormat Htransactions)
as [HtxFinal [HtxTotal HtxFormat]].
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].
pose proof (withdrawals_preserve_format afterTransactions
(Chain.Ethereum.Block.t.withdrawals block) HtxFormat) as HpaidFormat.
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|]; split; [|exact HpaidFormat].
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.
End Execution.
Print Assumptions process_block_preserves_supply.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.supply QedkConcrete.hostaccountformat
QedkConcrete.nativeinputs QedkConcrete.wordrange QedkConcrete.hashwords
QedkConcrete.assemblychecked QedkConcrete.credits QedkConcrete.reservewrites.
Require Import SetcodeConcrete.transactionsupply SetcodeConcrete.supplysequence.
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.
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 (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (Hhash : keccak256OutputSize) (Hnative : @nativeOutputBounds args).
Theorem process_block_preserves_supply verify world block post :
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))
(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 ∧ worldFormat post.
Proof.
intros 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 cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative initial addr data Hchain Hheader
Hdata (proj1 HinitialSupply) HinitialFormat) as Hbeacon;
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, HbeaconBlock, Hrun.
destruct Hbeacon as [Hbeacon [HbeaconTotal HbeaconFormat]].
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 cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative afterBeacon addr data Hchain HbeaconHeader
HdataHash Hbeacon HbeaconFormat) as Hhistory;
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, HhistoryBlock, Hrun.
destruct Hhistory as [Hhistory [HhistoryTotal HhistoryFormat]].
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 (transactions_preserve_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative afterHistory _ afterTransactions Hchain HhistoryHeader Htxs Hhistory HhistoryFormat Htransactions)
as [HtxFinal [HtxTotal HtxFormat]].
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].
pose proof (withdrawals_preserve_format afterTransactions
(Chain.Ethereum.Block.t.withdrawals block) HtxFormat) as HpaidFormat.
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|]; split; [|exact HpaidFormat].
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.
End Execution.
Print Assumptions process_block_preserves_supply.