Library SetcodeConcrete.executionsenderreserve
An ordinary sender remains protected when its code migrates
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.hostrollback QedkConcrete.hostframe QedkConcrete.reservecheck
QedkConcrete.hostcode.
Require Import SetcodeConcrete.reserve SetcodeConcrete.codeevolution
SetcodeConcrete.transactioncode SetcodeConcrete.contextframe
SetcodeConcrete.executioncontext SetcodeConcrete.executiondestruction
SetcodeConcrete.executionreserve.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Lemma emptying_requires_empty_code s :
mayEmpty s =
(Support.UintN.op_lteq (State.op_pointcaretlparenrparen (S.t.world_state s)
(W.next_emptying_transaction_block_for (S.t.tx_origin s)))
(Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))) &&
Byte_string.Bytes.op_eq (A.t.code (accountAt s (S.t.tx_origin s))) Byte_string.Bytes.empty).
Proof.
unfold mayEmpty.
change (fst (Reserve_balance.is_emptying_tx s)) with
(negb (Delegation.is_valid_delegation (A.t.code (accountAt s (S.t.tx_origin s)))) &&
Support.UintN.op_lteq (State.op_pointcaretlparenrparen (S.t.world_state s)
(W.next_emptying_transaction_block_for (S.t.tx_origin s)))
(Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s)))).
destruct (Byte_string.Bytes.op_eq (A.t.code (accountAt s (S.t.tx_origin s))) Byte_string.Bytes.empty)
eqn:Hempty; [|rewrite !Bool.andb_false_r; reflexivity].
change (Z.eqb (OCamlString.compare (A.t.code (accountAt s (S.t.tx_origin s)))
Byte_string.Bytes.empty) 0 = true) in Hempty.
apply Z.eqb_eq, address_compare_eq in Hempty. rewrite Hempty. reflexivity.
Qed.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
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 transaction_call_cannot_enable_emptying tx msg initial :
S.t.tx_origin initial ≠ Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
mayEmpty initial = false →
let post := snd (E.Host.call_from_eoa tx msg initial) in
¬ In (S.t.tx_origin initial) (S.t.accounts_created_in_current_transaction post) →
mayEmpty post = false.
Proof.
intros Hnative Hmap Hgas Hempty post Hcreated.
pose proof (transaction_call_preserves_migration_context cold warm access write reserve
Hrevision Hcold Hwarm tx msg Hgas initial) as Hcontext.
change (migrationContext post = migrationContext initial) in Hcontext.
unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hsnapshot Hblock Hsender Hlimit Hprice Hhistory Hcounters Hprior Hchain.
rewrite emptying_requires_empty_code in Hempty |- ×.
rewrite Hsender, Hblock.
destruct (transaction_call_code_evolution cold warm access write reserve Hrevision Hcold Hwarm
(S.t.tx_origin initial) tx msg initial Hnative Hmap Hgas Hcreated) as [Hregular|Hsame].
- unfold regularCode in Hregular. apply Bool.orb_false_iff in Hregular as [Hnonempty _].
change (Byte_string.Bytes.op_eq (A.t.code (accountAt post (S.t.tx_origin initial)))
Byte_string.Bytes.empty = false) in Hnonempty.
rewrite Hnonempty, Bool.andb_false_r. reflexivity.
- change (A.t.code (accountAt post (S.t.tx_origin initial)) =
A.t.code (accountAt initial (S.t.tx_origin initial))) in Hsame.
rewrite Hsame. unfold State.op_pointcaretlparenrparen, W.next_emptying_transaction_block_for.
cbn. rewrite Hcounters. exact Hempty.
Qed.
Theorem transaction_call_protects_sender tx msg initial original :
S.t.tx_origin initial ≠ Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
S.t.self_destruct initial = nil →
In (S.t.tx_origin initial, original) (W.t.accounts (S.t.initial_world_state initial)) →
mayEmpty initial = false →
let amount := U.to_z (protected initial (S.t.tx_origin initial) original) in
let fee := Z.of_N (S.t.tx_gas_limit initial × S.t.tx_gas_price initial)%N in
0 ≤ amount < 2^256 →
amount - fee ≤ U.to_z (A.t.balance (accountAt initial (S.t.tx_origin initial))) →
let post := snd (E.Host.call_from_eoa tx msg initial) in
¬ In (S.t.tx_origin initial) (S.t.accounts_created_in_current_transaction post) →
amount - fee ≤ U.to_z (A.t.balance (accountAt post (S.t.tx_origin initial))).
Proof.
intros Hnative Hmap Hgas Hdestruct Hin Hempty amount fee Hrange Hentry post Hcreated.
pose proof (transaction_call_preserves_migration_context cold warm access write reserve
Hrevision Hcold Hwarm tx msg Hgas initial) as Hcontext.
change (migrationContext post = migrationContext initial) in Hcontext.
pose proof (uncreated_sender_not_destroyed cold warm access write reserve Hrevision Hcold Hwarm
tx msg initial (S.t.tx_origin initial) Hgas Hdestruct Hcreated) as Hnot.
assert (Hdes : Addr._Set.(OCamlSet.S.mem) (S.t.tx_origin initial) (S.t.self_destruct post) = false).
{ destruct (Addr._Set.(OCamlSet.S.mem) (S.t.tx_origin initial) (S.t.self_destruct post)) eqn:Hmem;
[exfalso; apply Hnot, address_set_mem_member, Hmem|reflexivity]. }
pose proof (@transaction_world_rollback_or_reserve (@Host.Instantiate.Host_fargs args)
tx msg initial) as Hresult.
change (S.t.world_state post = S.t.world_state initial ∨
fst (Reserve_balance.dipped_into_reserve E.Params.(Execution.Make_Params_signature.revision) post) = false)
in Hresult.
destruct Hresult as [Hrollback|Hcheck].
- unfold accountAt. rewrite Hrollback. exact Hentry.
- rewrite Hrevision in Hcheck.
pose proof (transaction_call_cannot_enable_emptying tx msg initial Hnative Hmap Hgas Hempty Hcreated) as HafterEmpty.
pose proof (context_preserves_protected initial post (S.t.tx_origin initial) original Hcontext) as Hprotected.
unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hsnapshot Hblock Hsender Hlimit Hprice Hhistory Hcounters Hprior Hchain.
pose proof (sender_retains_reserve_minus_fee (cold,warm,access,write,reserve) post original Hcheck
ltac:(rewrite Hsender, Hsnapshot; exact Hin) ltac:(rewrite Hsender; exact Hdes)
HafterEmpty ltac:(rewrite Hsender, Hprotected; exact Hrange)) as [_ Hbound].
rewrite Hsender, Hprotected, Hlimit, Hprice in Hbound. exact Hbound.
Qed.
End Execution.
Print Assumptions transaction_call_protects_sender.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.hostrollback QedkConcrete.hostframe QedkConcrete.reservecheck
QedkConcrete.hostcode.
Require Import SetcodeConcrete.reserve SetcodeConcrete.codeevolution
SetcodeConcrete.transactioncode SetcodeConcrete.contextframe
SetcodeConcrete.executioncontext SetcodeConcrete.executiondestruction
SetcodeConcrete.executionreserve.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Lemma emptying_requires_empty_code s :
mayEmpty s =
(Support.UintN.op_lteq (State.op_pointcaretlparenrparen (S.t.world_state s)
(W.next_emptying_transaction_block_for (S.t.tx_origin s)))
(Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))) &&
Byte_string.Bytes.op_eq (A.t.code (accountAt s (S.t.tx_origin s))) Byte_string.Bytes.empty).
Proof.
unfold mayEmpty.
change (fst (Reserve_balance.is_emptying_tx s)) with
(negb (Delegation.is_valid_delegation (A.t.code (accountAt s (S.t.tx_origin s)))) &&
Support.UintN.op_lteq (State.op_pointcaretlparenrparen (S.t.world_state s)
(W.next_emptying_transaction_block_for (S.t.tx_origin s)))
(Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s)))).
destruct (Byte_string.Bytes.op_eq (A.t.code (accountAt s (S.t.tx_origin s))) Byte_string.Bytes.empty)
eqn:Hempty; [|rewrite !Bool.andb_false_r; reflexivity].
change (Z.eqb (OCamlString.compare (A.t.code (accountAt s (S.t.tx_origin s)))
Byte_string.Bytes.empty) 0 = true) in Hempty.
apply Z.eqb_eq, address_compare_eq in Hempty. rewrite Hempty. reflexivity.
Qed.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
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 transaction_call_cannot_enable_emptying tx msg initial :
S.t.tx_origin initial ≠ Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
mayEmpty initial = false →
let post := snd (E.Host.call_from_eoa tx msg initial) in
¬ In (S.t.tx_origin initial) (S.t.accounts_created_in_current_transaction post) →
mayEmpty post = false.
Proof.
intros Hnative Hmap Hgas Hempty post Hcreated.
pose proof (transaction_call_preserves_migration_context cold warm access write reserve
Hrevision Hcold Hwarm tx msg Hgas initial) as Hcontext.
change (migrationContext post = migrationContext initial) in Hcontext.
unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hsnapshot Hblock Hsender Hlimit Hprice Hhistory Hcounters Hprior Hchain.
rewrite emptying_requires_empty_code in Hempty |- ×.
rewrite Hsender, Hblock.
destruct (transaction_call_code_evolution cold warm access write reserve Hrevision Hcold Hwarm
(S.t.tx_origin initial) tx msg initial Hnative Hmap Hgas Hcreated) as [Hregular|Hsame].
- unfold regularCode in Hregular. apply Bool.orb_false_iff in Hregular as [Hnonempty _].
change (Byte_string.Bytes.op_eq (A.t.code (accountAt post (S.t.tx_origin initial)))
Byte_string.Bytes.empty = false) in Hnonempty.
rewrite Hnonempty, Bool.andb_false_r. reflexivity.
- change (A.t.code (accountAt post (S.t.tx_origin initial)) =
A.t.code (accountAt initial (S.t.tx_origin initial))) in Hsame.
rewrite Hsame. unfold State.op_pointcaretlparenrparen, W.next_emptying_transaction_block_for.
cbn. rewrite Hcounters. exact Hempty.
Qed.
Theorem transaction_call_protects_sender tx msg initial original :
S.t.tx_origin initial ≠ Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
S.t.self_destruct initial = nil →
In (S.t.tx_origin initial, original) (W.t.accounts (S.t.initial_world_state initial)) →
mayEmpty initial = false →
let amount := U.to_z (protected initial (S.t.tx_origin initial) original) in
let fee := Z.of_N (S.t.tx_gas_limit initial × S.t.tx_gas_price initial)%N in
0 ≤ amount < 2^256 →
amount - fee ≤ U.to_z (A.t.balance (accountAt initial (S.t.tx_origin initial))) →
let post := snd (E.Host.call_from_eoa tx msg initial) in
¬ In (S.t.tx_origin initial) (S.t.accounts_created_in_current_transaction post) →
amount - fee ≤ U.to_z (A.t.balance (accountAt post (S.t.tx_origin initial))).
Proof.
intros Hnative Hmap Hgas Hdestruct Hin Hempty amount fee Hrange Hentry post Hcreated.
pose proof (transaction_call_preserves_migration_context cold warm access write reserve
Hrevision Hcold Hwarm tx msg Hgas initial) as Hcontext.
change (migrationContext post = migrationContext initial) in Hcontext.
pose proof (uncreated_sender_not_destroyed cold warm access write reserve Hrevision Hcold Hwarm
tx msg initial (S.t.tx_origin initial) Hgas Hdestruct Hcreated) as Hnot.
assert (Hdes : Addr._Set.(OCamlSet.S.mem) (S.t.tx_origin initial) (S.t.self_destruct post) = false).
{ destruct (Addr._Set.(OCamlSet.S.mem) (S.t.tx_origin initial) (S.t.self_destruct post)) eqn:Hmem;
[exfalso; apply Hnot, address_set_mem_member, Hmem|reflexivity]. }
pose proof (@transaction_world_rollback_or_reserve (@Host.Instantiate.Host_fargs args)
tx msg initial) as Hresult.
change (S.t.world_state post = S.t.world_state initial ∨
fst (Reserve_balance.dipped_into_reserve E.Params.(Execution.Make_Params_signature.revision) post) = false)
in Hresult.
destruct Hresult as [Hrollback|Hcheck].
- unfold accountAt. rewrite Hrollback. exact Hentry.
- rewrite Hrevision in Hcheck.
pose proof (transaction_call_cannot_enable_emptying tx msg initial Hnative Hmap Hgas Hempty Hcreated) as HafterEmpty.
pose proof (context_preserves_protected initial post (S.t.tx_origin initial) original Hcontext) as Hprotected.
unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hsnapshot Hblock Hsender Hlimit Hprice Hhistory Hcounters Hprior Hchain.
pose proof (sender_retains_reserve_minus_fee (cold,warm,access,write,reserve) post original Hcheck
ltac:(rewrite Hsender, Hsnapshot; exact Hin) ltac:(rewrite Hsender; exact Hdes)
HafterEmpty ltac:(rewrite Hsender, Hprotected; exact Hrange)) as [_ Hbound].
rewrite Hsender, Hprotected, Hlimit, Hprice in Hbound. exact Hbound.
Qed.
End Execution.
Print Assumptions transaction_call_protects_sender.