Library SetcodeConcrete.windowsupply
Initial supply bounds suffice throughout SETCODE block execution
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.precompileoutputs QedkConcrete.blsmapbounds QedkConcrete.credits.
Require Import SetcodeConcrete.blocksupply SetcodeConcrete.nativeoutputs.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module W := State.WorldState.
Module Block := Chain.Ethereum.Block.
Module T := Chain.Ethereum.Transaction.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.supply QedkConcrete.hostaccountformat
QedkConcrete.nativeinputs QedkConcrete.wordrange QedkConcrete.hashwords
QedkConcrete.precompileoutputs QedkConcrete.blsmapbounds QedkConcrete.credits.
Require Import SetcodeConcrete.blocksupply SetcodeConcrete.nativeoutputs.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module W := State.WorldState.
Module Block := Chain.Ethereum.Block.
Module T := Chain.Ethereum.Transaction.
Wei credited by the withdrawals in block; ordinary fees are not credits.
Definition blockCredits (block : Block.t) : Z :=
Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 (Block.t.withdrawals block).
Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 (Block.t.withdrawals block).
Total external credits in blocks, in wei.
Definition windowCredits (blocks : list Block.t) : Z :=
Stdlib.Lists.List.fold_right (fun block total ⇒ blockCredits block + total) 0 blocks.
Stdlib.Lists.List.fold_right (fun block total ⇒ blockCredits block + total) 0 blocks.
Decoded-input bounds for one block, independent of the execution state.
These do not assert that its transactions or header pass validation.
Record BlockSupplyInputs (block : Block.t) : Prop := {
supply_header : headerWords (Block.t.header block);
supply_beacon_root : OCamlString.length
(Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32;
supply_transactions : Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧ Forall
(fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx)) (Block.t.transactions block);
supply_withdrawals : Forall (fun wd ⇒
0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
(Block.t.withdrawals block)
}.
Lemma block_credits_nonnegative block : BlockSupplyInputs block → 0 ≤ blockCredits block.
Proof. intros H. apply withdrawal_sum_nonnegative, (supply_withdrawals _ H). Qed.
Lemma window_credits_nonnegative blocks :
Forall BlockSupplyInputs blocks → 0 ≤ windowCredits blocks.
Proof.
intro H. induction H as [|block rest Hblock Hrest IH]; [change (0 ≤ 0); lia|].
change (0 ≤ blockCredits block + windowCredits rest).
pose proof (block_credits_nonnegative _ Hblock). lia.
Qed.
Lemma window_credits_app prefix rest :
windowCredits (prefix ++ rest) = windowCredits prefix + windowCredits rest.
Proof.
induction prefix as [|block prefix IH]; [reflexivity|].
change (blockCredits block + windowCredits (prefix ++ rest) =
blockCredits block + windowCredits prefix + windowCredits rest).
rewrite IH. lia.
Qed.
Section Execution.
Context {F : E.FArgs}.
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) (Hsha : sha256OutputSize).
Context (Hg1 : blsG1CofactorOrder) (Hg2 : blsG2CofactorOrder).
Context (Hchain : wordRange (Support.UintN.to_z E.Params.(Execution.Make_Params_signature.chain_id))).
supply_header : headerWords (Block.t.header block);
supply_beacon_root : OCamlString.length
(Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32;
supply_transactions : Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧ Forall
(fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx)) (Block.t.transactions block);
supply_withdrawals : Forall (fun wd ⇒
0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
(Block.t.withdrawals block)
}.
Lemma block_credits_nonnegative block : BlockSupplyInputs block → 0 ≤ blockCredits block.
Proof. intros H. apply withdrawal_sum_nonnegative, (supply_withdrawals _ H). Qed.
Lemma window_credits_nonnegative blocks :
Forall BlockSupplyInputs blocks → 0 ≤ windowCredits blocks.
Proof.
intro H. induction H as [|block rest Hblock Hrest IH]; [change (0 ≤ 0); lia|].
change (0 ≤ blockCredits block + windowCredits rest).
pose proof (block_credits_nonnegative _ Hblock). lia.
Qed.
Lemma window_credits_app prefix rest :
windowCredits (prefix ++ rest) = windowCredits prefix + windowCredits rest.
Proof.
induction prefix as [|block prefix IH]; [reflexivity|].
change (blockCredits block + windowCredits (prefix ++ rest) =
blockCredits block + windowCredits prefix + windowCredits rest).
rewrite IH. lia.
Qed.
Section Execution.
Context {F : E.FArgs}.
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) (Hsha : sha256OutputSize).
Context (Hg1 : blsG1CofactorOrder) (Hg2 : blsG2CofactorOrder).
Context (Hchain : wordRange (Support.UintN.to_z E.Params.(Execution.Make_Params_signature.chain_id))).
snapshot is the state before blocks. verify controls comparison of
computed block results with supplied commitments, not transaction checks.
On success, total balance increases by at most the supplied withdrawals;
account/storage/code representation bounds also hold. On error, no state
is returned and this theorem makes no claim about the error.
Theorem execution_window_preserves_supply verify snapshot blocks :
Forall BlockSupplyInputs blocks → balanceInvariant snapshot → worldFormat snapshot →
totalBalance snapshot + windowCredits blocks < 2^256 →
match Result.List.fold_leftM (E.process_block verify) snapshot blocks with
| inl post ⇒ balanceInvariant post ∧
totalBalance post ≤ totalBalance snapshot + windowCredits blocks ∧ worldFormat post
| inr _ ⇒ True
end.
Proof.
intros Hblocks. revert snapshot.
induction Hblocks as [|block rest Hblock Hrest IH];
intros snapshot Hbalance Hformat Hbudget.
- split; [exact Hbalance|]. split; [change (totalBalance snapshot ≤ totalBalance snapshot + 0); lia|exact Hformat].
- change (match Result.op_letdollar (E.process_block verify snapshot block)
(fun next ⇒ Result.List.fold_leftM (E.process_block verify) next rest) with
| inl post ⇒ balanceInvariant post ∧
totalBalance post ≤ totalBalance snapshot + windowCredits (block :: rest) ∧ worldFormat post
| inr _ ⇒ True end).
destruct (E.process_block verify snapshot block) as [next|error] eqn:Hstep; [|exact I].
change (match Result.List.fold_leftM (E.process_block verify) next rest with
| inl post ⇒ balanceInvariant post ∧
totalBalance post ≤ totalBalance snapshot + windowCredits (block :: rest) ∧ worldFormat post
| inr _ ⇒ True end).
pose proof (window_credits_nonnegative _ Hrest) as HrestCredits.
assert (HblockBudget : totalBalance snapshot + blockCredits block < 2^256).
{ change (totalBalance snapshot + (blockCredits block + windowCredits rest) < 2^256) in Hbudget. lia. }
destruct Hblock as [Hheader Hroot Htx Hwd].
pose proof (@setcode_native_output_bounds (@E.Instantiation_fargs F)
(cold,warm,access,write,reserve) Hrevision Hsha Hg1 Hg2) as Hnative.
destruct (process_block_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative verify snapshot block next Hchain
Hheader Hroot Hbalance Hformat Htx Hwd HblockBudget Hstep)
as [Hnext [Htotal HnextFormat]].
assert (Hremaining : totalBalance next + windowCredits rest < 2^256).
{ change (totalBalance snapshot + (blockCredits block + windowCredits rest) < 2^256) in Hbudget.
change (totalBalance next ≤ totalBalance snapshot + blockCredits block) in Htotal. lia. }
specialize (IH next Hnext HnextFormat Hremaining).
destruct (Result.List.fold_leftM (E.process_block verify) next rest) as [post|error]; [|exact I].
destruct IH as [Hpost [HpostTotal HpostFormat]].
split; [exact Hpost|]. split; [|exact HpostFormat].
change (totalBalance post ≤ totalBalance snapshot + (blockCredits block + windowCredits rest)).
change (totalBalance next ≤ totalBalance snapshot + blockCredits block) in Htotal. lia.
Qed.
Forall BlockSupplyInputs blocks → balanceInvariant snapshot → worldFormat snapshot →
totalBalance snapshot + windowCredits blocks < 2^256 →
match Result.List.fold_leftM (E.process_block verify) snapshot blocks with
| inl post ⇒ balanceInvariant post ∧
totalBalance post ≤ totalBalance snapshot + windowCredits blocks ∧ worldFormat post
| inr _ ⇒ True
end.
Proof.
intros Hblocks. revert snapshot.
induction Hblocks as [|block rest Hblock Hrest IH];
intros snapshot Hbalance Hformat Hbudget.
- split; [exact Hbalance|]. split; [change (totalBalance snapshot ≤ totalBalance snapshot + 0); lia|exact Hformat].
- change (match Result.op_letdollar (E.process_block verify snapshot block)
(fun next ⇒ Result.List.fold_leftM (E.process_block verify) next rest) with
| inl post ⇒ balanceInvariant post ∧
totalBalance post ≤ totalBalance snapshot + windowCredits (block :: rest) ∧ worldFormat post
| inr _ ⇒ True end).
destruct (E.process_block verify snapshot block) as [next|error] eqn:Hstep; [|exact I].
change (match Result.List.fold_leftM (E.process_block verify) next rest with
| inl post ⇒ balanceInvariant post ∧
totalBalance post ≤ totalBalance snapshot + windowCredits (block :: rest) ∧ worldFormat post
| inr _ ⇒ True end).
pose proof (window_credits_nonnegative _ Hrest) as HrestCredits.
assert (HblockBudget : totalBalance snapshot + blockCredits block < 2^256).
{ change (totalBalance snapshot + (blockCredits block + windowCredits rest) < 2^256) in Hbudget. lia. }
destruct Hblock as [Hheader Hroot Htx Hwd].
pose proof (@setcode_native_output_bounds (@E.Instantiation_fargs F)
(cold,warm,access,write,reserve) Hrevision Hsha Hg1 Hg2) as Hnative.
destruct (process_block_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative verify snapshot block next Hchain
Hheader Hroot Hbalance Hformat Htx Hwd HblockBudget Hstep)
as [Hnext [Htotal HnextFormat]].
assert (Hremaining : totalBalance next + windowCredits rest < 2^256).
{ change (totalBalance snapshot + (blockCredits block + windowCredits rest) < 2^256) in Hbudget.
change (totalBalance next ≤ totalBalance snapshot + blockCredits block) in Htotal. lia. }
specialize (IH next Hnext HnextFormat Hremaining).
destruct (Result.List.fold_leftM (E.process_block verify) next rest) as [post|error]; [|exact I].
destruct IH as [Hpost [HpostTotal HpostFormat]].
split; [exact Hpost|]. split; [|exact HpostFormat].
change (totalBalance post ≤ totalBalance snapshot + (blockCredits block + windowCredits rest)).
change (totalBalance next ≤ totalBalance snapshot + blockCredits block) in Htotal. lia.
Qed.
Execute only prefix of prefix ++ rest. A returned state has enough
aggregate supply headroom for every external credit in rest. The bound
is derived from the initial one, not required again after execution.
Corollary execution_prefix_retains_supply_capacity verify snapshot prefix rest :
Forall BlockSupplyInputs (prefix ++ rest) →
balanceInvariant snapshot → worldFormat snapshot →
totalBalance snapshot + windowCredits (prefix ++ rest) < 2^256 →
match Result.List.fold_leftM (E.process_block verify) snapshot prefix with
| inl post ⇒ balanceInvariant post ∧ worldFormat post ∧
totalBalance post + windowCredits rest < 2^256
| inr _ ⇒ True
end.
Proof.
intros Hblocks Hbalance Hformat Hbudget.
apply Forall_app in Hblocks as [Hprefix Hrest].
rewrite window_credits_app in Hbudget.
pose proof (window_credits_nonnegative _ Hrest) as HrestCredits.
pose proof (execution_window_preserves_supply verify snapshot prefix Hprefix
Hbalance Hformat ltac:(lia)) as Hrun.
destruct (Result.List.fold_leftM (E.process_block verify) snapshot prefix) as [post|error]; [|exact I].
destruct Hrun as [Hpost [Htotal HpostFormat]]. split; [exact Hpost|]. split; [exact HpostFormat|lia].
Qed.
End Execution.
Print Assumptions execution_window_preserves_supply.
Print Assumptions execution_prefix_retains_supply_capacity.
Forall BlockSupplyInputs (prefix ++ rest) →
balanceInvariant snapshot → worldFormat snapshot →
totalBalance snapshot + windowCredits (prefix ++ rest) < 2^256 →
match Result.List.fold_leftM (E.process_block verify) snapshot prefix with
| inl post ⇒ balanceInvariant post ∧ worldFormat post ∧
totalBalance post + windowCredits rest < 2^256
| inr _ ⇒ True
end.
Proof.
intros Hblocks Hbalance Hformat Hbudget.
apply Forall_app in Hblocks as [Hprefix Hrest].
rewrite window_credits_app in Hbudget.
pose proof (window_credits_nonnegative _ Hrest) as HrestCredits.
pose proof (execution_window_preserves_supply verify snapshot prefix Hprefix
Hbalance Hformat ltac:(lia)) as Hrun.
destruct (Result.List.fold_leftM (E.process_block verify) snapshot prefix) as [post|error]; [|exact I].
destruct Hrun as [Hpost [Htotal HpostFormat]]. split; [exact Hpost|]. split; [exact HpostFormat|lia].
Qed.
End Execution.
Print Assumptions execution_window_preserves_supply.
Print Assumptions execution_prefix_retains_supply_capacity.