Library SetcodeConcrete.transactioncode
From Stdlib Require Import List String ZArith NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.accountcode
QedkConcrete.worldaccounts QedkConcrete.stringmaps.
Require Import SetcodeConcrete.executionpersistent SetcodeConcrete.systempersistent
SetcodeConcrete.nonceeffects SetcodeConcrete.transactionnonce.
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 S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Definition regularCode code :=
orb (Byte_string.Bytes.op_eq code Byte_string.Bytes.empty)
(Delegation.is_valid_delegation code) = false.
Definition worldCode query code world :=
stringmaps.WellFormed (W.t.accounts world) ∧
A.t.code (State.op_pointcaretlparenrparen world (W.account None query)) = code.
Lemma world_code_write world query code keep addr value :
worldCode query code world →
(addr = query → A.t.code value = code) →
worldCode query code (State.op_pointcaretlparenrparenltminus world
(W.account (Some keep) addr) value).
Proof.
intros [Hmap Hcode] Hvalue. split; [apply account_write_wellformed; exact Hmap|].
rewrite code_after_account_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq; [apply Hvalue, String.eqb_eq; exact Heq|exact Hcode].
Qed.
Section Authorizations.
Context {F : E.FArgs}.
Lemma authorization_preserves_system_code query code auth s :
regularCode code → persistentCode query code s →
persistentCode query code (snd (E.process_authorization s auth)).
Proof.
intros Hregular Hbefore. unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
destruct (T.Authorization.authority auth) as [authority|]; [|exact Hbefore].
cbn beta iota zeta.
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hvalid end.
2: destruct Hbefore; constructor; assumption.
assert (Hneq : authority ≠ query).
{ intro Heq; subst authority. apply Bool.andb_true_iff in Hvalid as [Heligible _].
change (orb (Byte_string.Bytes.op_eq
(A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query))) Byte_string.Bytes.empty)
(Delegation.is_valid_delegation
(A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query)))) = true) in Heligible.
rewrite (persistent_code_value _ _ _ Hbefore) in Heligible. unfold regularCode in Hregular. congruence. }
match goal with |- context [State.op_pointcaretlparenrparenltminus _
(S.account None authority) ?value] ⇒ set (updated := value) end.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: constructor; try exact (persistent_code_uncreated _ _ _ Hbefore);
try exact (persistent_code_undestroyed _ _ _ Hbefore).
all: try (apply account_write_wellformed; exact (persistent_code_map _ _ _ Hbefore)).
all: change (A.t.code (State.op_pointcaretlparenrparen
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) authority) updated) (W.account None query)) = code).
all: rewrite code_after_account_write by exact (persistent_code_map _ _ _ Hbefore).
all: destruct (String.eqb authority query) eqn:Heq;
[apply String.eqb_eq in Heq; contradiction|exact (persistent_code_value _ _ _ Hbefore)].
Qed.
Lemma authorizations_preserve_system_code query code auths s authorities :
regularCode code → persistentCode query code s →
persistentCode query code (snd (processAuthorizations s authorities auths)).
Proof.
intros Hregular. revert s authorities. induction auths as [|auth rest IH]; intros s authorities Hbefore.
- exact Hbefore.
- pose proof (authorization_preserves_system_code query code auth s Hregular Hbefore) as Hstep.
unfold processAuthorizations. change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
destruct (E.process_authorization s auth) as [[authority|] after].
all: apply IH; exact Hstep.
Qed.
End Authorizations.
Lemma delete_accounts_preserves_code block addresses query code :
worldCode query code (B.t.world_state block) → ¬ In query addresses →
worldCode query code (B.t.world_state (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s (B.account_opt None addr) None)
block (OCamlSeq.of_list addresses))).
Proof.
revert block. induction addresses as [|addr rest IH]; intros block [Hmap Hcode] Habsent.
- split; assumption.
- cbn [OCamlSeq.of_list OCamlSeq.fold_left]. apply IH.
+ split; [apply remove_wellformed; exact Hmap|].
change (A.t.code (match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.remove stringOrder addr (W.t.accounts (B.t.world_state block))) with
| Some value ⇒ value | None ⇒ A.empty end) = code).
rewrite find_remove_other by (first [exact Hmap|intro Heq; apply Habsent; left; exact Heq]).
exact Hcode.
+ intro Hin. apply Habsent. right. exact Hin.
Qed.
Section Transaction.
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).
Lemma bump_counters_persistent query code s addresses :
persistentCode query code s →
persistentCode query code (E.bump_emptying_transaction_counters addresses s).
Proof.
intros [Hmap Hcode Hcreated Hdestroyed]. constructor.
- rewrite bump_counters_accounts. exact Hmap.
- unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[Addr.Map.find_opt E.bump_emptying_transaction_counters].
rewrite bump_counters_accounts. exact Hcode.
- exact Hcreated.
- exact Hdestroyed.
Qed.
Theorem transaction_preserves_system_code state tx validation query code post :
Setcode_rules.system_account query = true → regularCode code →
code ≠ Byte_string.Bytes.empty →
E.validate_transaction state tx = inl validation →
E.process_transaction state tx = inl post →
worldCode query code (B.t.world_state state) →
worldCode query code (B.t.world_state post).
Proof.
intros Hsystem Hregular Hnonempty Hvalidation Hrun Hinitial.
pose proof (prepared_message_gas state tx validation) as Hmessage.
unfold E.process_transaction in Hrun. rewrite Hvalidation in Hrun.
destruct validation as [sender fee price intrinsic].
change (@Result.op_letdollar) with
(fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
match first with inl value ⇒ next value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta in Hrun.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [Result.List.fold_left ?step (nil, ?initial) ?auths] ⇒
set (prepared := initial) in × end.
assert (Hprepared : persistentCode query code prepared).
{ assert (Hworld : worldCode query code (S.t.world_state prepared)).
{ unfold prepared. apply world_code_write with
(world := B.t.world_state state) (keep := false) (addr := sender);
[exact Hinitial|]. intro Heq; subst sender. exact (proj2 Hinitial). }
constructor; try exact (proj1 Hworld); try exact (proj2 Hworld);
intro Hin; inversion Hin. }
pose proof (authorizations_preserve_system_code query code (T.authorization_list tx)
prepared nil Hregular Hprepared) as Hauth.
match type of Hrun with context [Result.List.fold_left ?step (nil, prepared) ?auths] ⇒
change (Result.List.fold_left step (nil, prepared) auths)
with (processAuthorizations prepared nil auths) in Hrun end.
destruct (processAuthorizations prepared nil (T.authorization_list tx))
as [authorities authorized] eqn:HauthRun.
cbn beta iota zeta delta [fst snd] in Hauth, Hrun.
match type of Hrun with context [S.initialize_access_sets tx ?entry ?precompiles] ⇒
set (beforeVm := S.initialize_access_sets tx entry precompiles) in × end.
assert (HbeforeAccounts : W.t.accounts (S.t.world_state beforeVm) =
W.t.accounts (S.t.world_state authorized)) by
(unfold beforeVm; apply bump_counters_accounts).
assert (Hbefore : persistentCode query code beforeVm).
{ destruct Hauth as [Hmap Hcode Hcreated Hdestroyed]. constructor.
- rewrite HbeforeAccounts. exact Hmap.
- unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[Addr.Map.find_opt beforeVm]. rewrite HbeforeAccounts. exact Hcode.
- exact Hcreated.
- exact Hdestroyed. }
match type of Hrun with context [E.Host.call_from_eoa tx ?message beforeVm] ⇒
set (msg := message) in × end.
specialize (Hmessage (S.t.world_state beforeVm) Hvalidation).
change (0 ≤ Evmc.Message.t.gas msg < 2^63 ∧
Evmc.Message.t.gas msg + 1 ≤ Z.of_N (T.gas_limit tx)) in Hmessage.
pose proof (execution_call_from_eoa_preserves_existing_code query code Hsystem
cold warm access write reserve Hcold Hwarm tx msg Hnonempty Hrevision
(proj1 Hmessage) beforeVm Hbefore) as Hvm.
destruct (E.Host.call_from_eoa tx msg beforeVm) as [result afterVm] eqn:HvmRun.
destruct migrated; cbn beta iota zeta in Hrun.
all: try (destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg)); cbn beta iota zeta in Hrun).
all: match type of Hrun with Result._return ?computed = inl ?target ⇒
assert (Hpost : computed = target) by
(exact (f_equal (fun r ⇒ match r with inl value ⇒ value | inr _ ⇒ target end) Hrun));
rewrite <- Hpost; clear Hpost Hrun end.
all: cbn beta iota zeta delta [B.t.with_transactions_processed B.t.with_transaction_frames
B.t.with_gas_used B.t.world_state E.transaction_validation.sender].
all: match goal with |- context [E.bump_emptying_transaction_counters (?sender :: nil) ?entry] ⇒
set (final := E.bump_emptying_transaction_counters (sender :: nil) entry) in × end.
all: assert (Hfinal : persistentCode query code final) by
(unfold final; apply bump_counters_persistent; first [exact Hbefore|exact Hvm]).
all: match goal with |- context [Result.Seq.fold_left ?step ?credited ?addresses] ⇒
set (paid := credited) in *;
assert (Hpaid : worldCode query code (B.t.world_state paid)) by
(unfold paid; apply world_code_write with
(world := S.t.world_state final) (keep := false)
(addr := Chain.Ethereum.Block.Header.t.beneficiary
(Chain.Ethereum.Block.t.header (B.t.current_block state)));
[split; [exact (persistent_code_map _ _ _ Hfinal)|exact (persistent_code_value _ _ _ Hfinal)]|
intro Heq; rewrite Heq; exact (persistent_code_value _ _ _ Hfinal)]);
pose proof (delete_accounts_preserves_code paid (S.t.self_destruct final) query code
Hpaid (persistent_code_undestroyed _ _ _ Hfinal)) as Hdelete
end.
all: exact Hdelete.
Qed.
End Transaction.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.accountcode
QedkConcrete.worldaccounts QedkConcrete.stringmaps.
Require Import SetcodeConcrete.executionpersistent SetcodeConcrete.systempersistent
SetcodeConcrete.nonceeffects SetcodeConcrete.transactionnonce.
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 S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Definition regularCode code :=
orb (Byte_string.Bytes.op_eq code Byte_string.Bytes.empty)
(Delegation.is_valid_delegation code) = false.
Definition worldCode query code world :=
stringmaps.WellFormed (W.t.accounts world) ∧
A.t.code (State.op_pointcaretlparenrparen world (W.account None query)) = code.
Lemma world_code_write world query code keep addr value :
worldCode query code world →
(addr = query → A.t.code value = code) →
worldCode query code (State.op_pointcaretlparenrparenltminus world
(W.account (Some keep) addr) value).
Proof.
intros [Hmap Hcode] Hvalue. split; [apply account_write_wellformed; exact Hmap|].
rewrite code_after_account_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq; [apply Hvalue, String.eqb_eq; exact Heq|exact Hcode].
Qed.
Section Authorizations.
Context {F : E.FArgs}.
Lemma authorization_preserves_system_code query code auth s :
regularCode code → persistentCode query code s →
persistentCode query code (snd (E.process_authorization s auth)).
Proof.
intros Hregular Hbefore. unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
destruct (T.Authorization.authority auth) as [authority|]; [|exact Hbefore].
cbn beta iota zeta.
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hvalid end.
2: destruct Hbefore; constructor; assumption.
assert (Hneq : authority ≠ query).
{ intro Heq; subst authority. apply Bool.andb_true_iff in Hvalid as [Heligible _].
change (orb (Byte_string.Bytes.op_eq
(A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query))) Byte_string.Bytes.empty)
(Delegation.is_valid_delegation
(A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query)))) = true) in Heligible.
rewrite (persistent_code_value _ _ _ Hbefore) in Heligible. unfold regularCode in Hregular. congruence. }
match goal with |- context [State.op_pointcaretlparenrparenltminus _
(S.account None authority) ?value] ⇒ set (updated := value) end.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
all: constructor; try exact (persistent_code_uncreated _ _ _ Hbefore);
try exact (persistent_code_undestroyed _ _ _ Hbefore).
all: try (apply account_write_wellformed; exact (persistent_code_map _ _ _ Hbefore)).
all: change (A.t.code (State.op_pointcaretlparenrparen
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some false) authority) updated) (W.account None query)) = code).
all: rewrite code_after_account_write by exact (persistent_code_map _ _ _ Hbefore).
all: destruct (String.eqb authority query) eqn:Heq;
[apply String.eqb_eq in Heq; contradiction|exact (persistent_code_value _ _ _ Hbefore)].
Qed.
Lemma authorizations_preserve_system_code query code auths s authorities :
regularCode code → persistentCode query code s →
persistentCode query code (snd (processAuthorizations s authorities auths)).
Proof.
intros Hregular. revert s authorities. induction auths as [|auth rest IH]; intros s authorities Hbefore.
- exact Hbefore.
- pose proof (authorization_preserves_system_code query code auth s Hregular Hbefore) as Hstep.
unfold processAuthorizations. change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
destruct (E.process_authorization s auth) as [[authority|] after].
all: apply IH; exact Hstep.
Qed.
End Authorizations.
Lemma delete_accounts_preserves_code block addresses query code :
worldCode query code (B.t.world_state block) → ¬ In query addresses →
worldCode query code (B.t.world_state (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s (B.account_opt None addr) None)
block (OCamlSeq.of_list addresses))).
Proof.
revert block. induction addresses as [|addr rest IH]; intros block [Hmap Hcode] Habsent.
- split; assumption.
- cbn [OCamlSeq.of_list OCamlSeq.fold_left]. apply IH.
+ split; [apply remove_wellformed; exact Hmap|].
change (A.t.code (match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.remove stringOrder addr (W.t.accounts (B.t.world_state block))) with
| Some value ⇒ value | None ⇒ A.empty end) = code).
rewrite find_remove_other by (first [exact Hmap|intro Heq; apply Habsent; left; exact Heq]).
exact Hcode.
+ intro Hin. apply Habsent. right. exact Hin.
Qed.
Section Transaction.
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).
Lemma bump_counters_persistent query code s addresses :
persistentCode query code s →
persistentCode query code (E.bump_emptying_transaction_counters addresses s).
Proof.
intros [Hmap Hcode Hcreated Hdestroyed]. constructor.
- rewrite bump_counters_accounts. exact Hmap.
- unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[Addr.Map.find_opt E.bump_emptying_transaction_counters].
rewrite bump_counters_accounts. exact Hcode.
- exact Hcreated.
- exact Hdestroyed.
Qed.
Theorem transaction_preserves_system_code state tx validation query code post :
Setcode_rules.system_account query = true → regularCode code →
code ≠ Byte_string.Bytes.empty →
E.validate_transaction state tx = inl validation →
E.process_transaction state tx = inl post →
worldCode query code (B.t.world_state state) →
worldCode query code (B.t.world_state post).
Proof.
intros Hsystem Hregular Hnonempty Hvalidation Hrun Hinitial.
pose proof (prepared_message_gas state tx validation) as Hmessage.
unfold E.process_transaction in Hrun. rewrite Hvalidation in Hrun.
destruct validation as [sender fee price intrinsic].
change (@Result.op_letdollar) with
(fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
match first with inl value ⇒ next value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta in Hrun.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [Result.List.fold_left ?step (nil, ?initial) ?auths] ⇒
set (prepared := initial) in × end.
assert (Hprepared : persistentCode query code prepared).
{ assert (Hworld : worldCode query code (S.t.world_state prepared)).
{ unfold prepared. apply world_code_write with
(world := B.t.world_state state) (keep := false) (addr := sender);
[exact Hinitial|]. intro Heq; subst sender. exact (proj2 Hinitial). }
constructor; try exact (proj1 Hworld); try exact (proj2 Hworld);
intro Hin; inversion Hin. }
pose proof (authorizations_preserve_system_code query code (T.authorization_list tx)
prepared nil Hregular Hprepared) as Hauth.
match type of Hrun with context [Result.List.fold_left ?step (nil, prepared) ?auths] ⇒
change (Result.List.fold_left step (nil, prepared) auths)
with (processAuthorizations prepared nil auths) in Hrun end.
destruct (processAuthorizations prepared nil (T.authorization_list tx))
as [authorities authorized] eqn:HauthRun.
cbn beta iota zeta delta [fst snd] in Hauth, Hrun.
match type of Hrun with context [S.initialize_access_sets tx ?entry ?precompiles] ⇒
set (beforeVm := S.initialize_access_sets tx entry precompiles) in × end.
assert (HbeforeAccounts : W.t.accounts (S.t.world_state beforeVm) =
W.t.accounts (S.t.world_state authorized)) by
(unfold beforeVm; apply bump_counters_accounts).
assert (Hbefore : persistentCode query code beforeVm).
{ destruct Hauth as [Hmap Hcode Hcreated Hdestroyed]. constructor.
- rewrite HbeforeAccounts. exact Hmap.
- unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[Addr.Map.find_opt beforeVm]. rewrite HbeforeAccounts. exact Hcode.
- exact Hcreated.
- exact Hdestroyed. }
match type of Hrun with context [E.Host.call_from_eoa tx ?message beforeVm] ⇒
set (msg := message) in × end.
specialize (Hmessage (S.t.world_state beforeVm) Hvalidation).
change (0 ≤ Evmc.Message.t.gas msg < 2^63 ∧
Evmc.Message.t.gas msg + 1 ≤ Z.of_N (T.gas_limit tx)) in Hmessage.
pose proof (execution_call_from_eoa_preserves_existing_code query code Hsystem
cold warm access write reserve Hcold Hwarm tx msg Hnonempty Hrevision
(proj1 Hmessage) beforeVm Hbefore) as Hvm.
destruct (E.Host.call_from_eoa tx msg beforeVm) as [result afterVm] eqn:HvmRun.
destruct migrated; cbn beta iota zeta in Hrun.
all: try (destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg)); cbn beta iota zeta in Hrun).
all: match type of Hrun with Result._return ?computed = inl ?target ⇒
assert (Hpost : computed = target) by
(exact (f_equal (fun r ⇒ match r with inl value ⇒ value | inr _ ⇒ target end) Hrun));
rewrite <- Hpost; clear Hpost Hrun end.
all: cbn beta iota zeta delta [B.t.with_transactions_processed B.t.with_transaction_frames
B.t.with_gas_used B.t.world_state E.transaction_validation.sender].
all: match goal with |- context [E.bump_emptying_transaction_counters (?sender :: nil) ?entry] ⇒
set (final := E.bump_emptying_transaction_counters (sender :: nil) entry) in × end.
all: assert (Hfinal : persistentCode query code final) by
(unfold final; apply bump_counters_persistent; first [exact Hbefore|exact Hvm]).
all: match goal with |- context [Result.Seq.fold_left ?step ?credited ?addresses] ⇒
set (paid := credited) in *;
assert (Hpaid : worldCode query code (B.t.world_state paid)) by
(unfold paid; apply world_code_write with
(world := S.t.world_state final) (keep := false)
(addr := Chain.Ethereum.Block.Header.t.beneficiary
(Chain.Ethereum.Block.t.header (B.t.current_block state)));
[split; [exact (persistent_code_map _ _ _ Hfinal)|exact (persistent_code_value _ _ _ Hfinal)]|
intro Heq; rewrite Heq; exact (persistent_code_value _ _ _ Hfinal)]);
pose proof (delete_accounts_preserves_code paid (S.t.self_destruct final) query code
Hpaid (persistent_code_undestroyed _ _ _ Hfinal)) as Hdelete
end.
all: exact Hdelete.
Qed.
End Transaction.