Library SetcodeConcrete.executionguardedaccount
Guarded delegated nonsenders survive the actual SETCODE VM
From Stdlib Require Import List String ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.protectedstoragenative QedkConcrete.hostframe QedkConcrete.hostcode
QedkConcrete.vmhostframe QedkConcrete.recursiveassembly QedkConcrete.assemblychecked
QedkConcrete.vmcallframe QedkConcrete.hoststoragewords QedkConcrete.hostrollback
QedkConcrete.reservecheck.
Require Import SetcodeConcrete.vmcontextframe SetcodeConcrete.vmemptyframe
SetcodeConcrete.assembly SetcodeConcrete.vmprogress SetcodeConcrete.emptyaccount
SetcodeConcrete.accountframe SetcodeConcrete.contextframe
SetcodeConcrete.executionconfig SetcodeConcrete.executiontracking
SetcodeConcrete.creationtracking SetcodeConcrete.guardedaccountframe
SetcodeConcrete.guardedaccounthost SetcodeConcrete.reserve
SetcodeConcrete.executioncontext SetcodeConcrete.executiondestruction
SetcodeConcrete.executionreserve.
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.
Lemma primitive_frame_at_conj {Host_t : Set} (host : Evmc.HOST (t := Host_t))
(P Q : Host_t → Prop) self :
PrimitiveHostFrameAt host P self →
PrimitiveHostFrameAt host Q self →
PrimitiveHostFrameAt host (fun s ⇒ P s ∧ Q s) self.
Proof.
intros HP HQ; destruct HP, HQ; constructor; intros;
unfold preservesCallback in *; intros s [HsP HsQ]; split; eauto.
Qed.
Lemma noncontract_nonsender_retains_reserve prices s addr original :
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Addr.op_eq addr (S.t.tx_origin s) = false →
A.is_smart_contract (accountAt s addr) = false →
protected s addr original ≤ A.t.balance (accountAt s addr).
Proof.
intros Hcheck Hin Hdes Hsender Hnoncontract.
assert (Hexempt : Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s addr))
(Addr.op_eq addr (S.t.tx_origin s)) (Reserve_balance.sent_in_window s addr) = false).
{ unfold Setcode_rules.contract_exempt. rewrite Hnoncontract. reflexivity. }
pose proof (checked_account prices s addr original Hcheck Hin Hdes Hexempt) as Hbound.
unfold reserveViolated in Hbound. rewrite Hsender, u256_lt in Hbound.
apply Z.ltb_ge in Hbound. exact Hbound.
Qed.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
Context (query : Addr.t) (delegated : string) (storage : list (string × string))
(block : N) (sender : Addr.t).
Context (Hdifferent : query ≠ sender).
Context (HnotNative : query ≠ Reserve_balance.precompile_address).
Context (Hdelegated : Delegation.is_valid_delegation delegated = true).
Context (Hmap : stringmaps.WellFormed storage) (Hwords : storageWords storage).
Context (Hpositive : initialThreshold storage block query ≠ 0).
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).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation guarded := (guardedAccount query delegated storage block sender).
Definition guardedAccountVm {T : Evmc.Vm_H_signature (t := S.t)}
(vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs T)) :=
∀ msg code, 0 ≤ Evmc.Message.t.gas msg < 2^63 →
∀ s, guarded s →
(Evmc.Message.t.recipient msg = native → code = Byte_string.Bytes.empty) →
guarded (snd (vm.(Evmc.Vm.SIG.execute) msg code s)).
Lemma guarded_native_dispatch vm msg addr action :
@nativeDispatch args vm msg addr = Some action →
preservesCallback guarded action.
Proof using F HnotNative Hrevision access block cold delegated query reserve sender storage warm write.
intros Hselected s [Hconfig [Hsender Haccount]]. split.
- exact (@native_config_storage args vm block storage msg addr action
cold warm access write reserve Hrevision Hselected s Hconfig).
- split.
+ pose proof (@precompile_preserves_migration_context
(@H.Precompiles_fargs (H.Build_FArgs args.(Host.Instantiate.ChainParams) vm))
cold warm access write reserve msg addr action Hrevision Hselected s) as Hctx.
rewrite (migration_context_origin _ _ Hctx). exact Hsender.
+ exact (@precompile_account_frame
(@H.Precompiles_fargs (H.Build_FArgs args.(Host.Instantiate.ChainParams) vm))
cold warm access write reserve query (fun code _ ⇒ code = delegated)
msg addr action Hrevision HnotNative Hselected s Haccount).
Qed.
Lemma concrete_primitives_origin vm :
PrimitiveHostFrame (HostF vm) (fun s ⇒ S.t.tx_origin s = sender).
Proof using F sender.
constructor; intros; unfold preservesCallback in *; intros s Hsender;
destruct (@concrete_primitives_preserve_migration_context args vm s);
unfold preservesCallback in ×.
all: eapply eq_trans; [apply (migration_context_origin s); eauto|exact Hsender].
Qed.
Lemma concrete_primitives_guarded_account vm self :
self ≠ native →
PrimitiveHostFrameAt (HostF vm) guarded self.
Proof using F block delegated query sender storage.
intro Hself. unfold guardedAccount.
apply primitive_frame_at_conj.
- exact (@concrete_primitives_preserve_protected_storage args vm
self native (reachableReserveStorage block storage) Hself).
- apply primitive_frame_at_conj.
+ apply primitive_frame_at. apply concrete_primitives_origin.
+ apply primitive_frame_at. apply concrete_primitives_account_frame.
Qed.
Lemma guarded_vm_constructor vm :
(∀ xs, trackingVm xs vm) →
guardedAccountVm vm →
guardedAccountVm (VmF (HostF vm)).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
intros Htracking Hvm msg code Hgas initial Hinitial Hcode.
destruct (String.string_dec (Evmc.Message.t.recipient msg) native) as [Heq|Hneq].
{ rewrite (Hcode Heq). cbn -[Vm.Make.execute].
match goal with |- guarded (snd (@V.execute ?ht ?vf ?message ?body ?state)) ⇒
pose proof (@execute_empty_host ht vf cold warm access write reserve
Hrevision eq_refl (fun s ⇒ eq_refl) (fun a s ⇒ eq_refl)
(fun a o n s ⇒ copy_code_read_only a o n s) message state) as Hsame
end.
exact (eq_ind_r guarded Hinitial Hsame). }
cbn -[Vm.Make.execute].
eapply execute_preserves_at_context with (P := guarded).
- exact Hrevision.
- reflexivity.
- exact (@concrete_setcode_gas_progress args vm _ cold warm access write reserve Hrevision Hcold Hwarm).
- intros; reflexivity.
- intros; reflexivity.
- intros; apply copy_code_read_only.
- exact (concrete_primitives_guarded_account vm (Evmc.Message.t.recipient msg) Hneq).
- intros. eapply setcode_guarded_account; eassumption.
- intros env opcode before Hself Hflag Hopcode Hbound HP.
assert (HselfDifferent : @V.Executor.self S.t _ env ≠ native).
{ intro Hequal. apply Hneq. exact (eq_trans (eq_sym Hself) Hequal). }
assert (Hprimitive : PrimitiveHostFrameAt (HostF vm) guarded (@V.Executor.self S.t _ env))
by (apply concrete_primitives_guarded_account; exact HselfDifferent).
assert (HcopyEmpty : ∀ size s, guarded s →
(HostF vm).(Evmc.HOST.copy_code) native 0 size s = (Byte_string.Bytes.empty, s)).
{ intros size s [Hconfig _]. apply emptyaccount.concrete_copy_empty_code.
exact (protected_storage_code _ _ _ Hconfig). }
assert (Hnested : ∀ nested, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
childCallFrom (@V.Executor.self S.t _ env) nested →
(Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient nested = native → Evmc.Message.t.code_address nested = native)) →
preservesCallback guarded ((HostF vm).(Evmc.HOST.call) nested)).
{ intros nested Hb Horigin Htarget.
exact (@call_guarded_account args query delegated storage block sender vm
(fun child body Hc xs ⇒ Htracking xs child body Hc)
(@V.Executor.self S.t _ env) nested (guarded_native_dispatch vm)
Hvm Hb HselfDifferent Horigin Htarget). }
destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
all: cbn [dispatch V.Executor.execute_opcode].
+ exact (@create_preserves_protected_recipient S.t _ env guarded native Hprimitive Hnested before Hbound HP).
+ exact (@call_preserves_protected_recipient S.t _ env guarded native Hprimitive HcopyEmpty Hnested before Hbound HP).
+ exact (@callcode_preserves_protected_recipient S.t _ env guarded native Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@delegatecall_preserves_protected_recipient S.t _ env guarded native Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@create2_preserves_protected_recipient S.t _ env guarded native Hprimitive Hnested before Hbound HP).
+ exact (@staticcall_preserves_protected_recipient S.t _ env guarded native Hprimitive HcopyEmpty Hnested before Hbound HP).
- exact Hgas.
- exact Hinitial.
Qed.
Theorem execution_vm_guarded_account :
guardedAccountVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
let vm := constr:(@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args)) in
assert (Hseed : (∀ xs, trackingVm xs vm) ∧ guardedAccountVm vm).
{ split.
- intros xs msg body Hb s HP; exact HP.
- intros msg body Hb s HP Htarget; exact HP. }
assert (Hstep : ∀ vm,
(∀ xs, trackingVm xs vm) ∧ guardedAccountVm vm →
(∀ xs, trackingVm xs (VmF (HostF vm))) ∧ guardedAccountVm (VmF (HostF vm))).
{ intros vm [Ht Hg]. split.
- intro xs. apply (@vm_constructor_tracking xs F cold warm access write reserve Hrevision).
apply (@host_constructor_tracking xs F cold warm access write reserve Hrevision Hcold Hwarm), Ht.
- apply guarded_vm_constructor; assumption. }
assert (Hunroll : ∀ fuel,
let pair := recursive_module_unroll fuel
(@Evmc.Instantiate.recursive_module_seed S.t (@recursiveArgs args))
(fun pair ⇒ (HostF (snd pair), VmF (fst pair))) in
((∀ xs, trackingVm xs (snd pair)) ∧ guardedAccountVm (snd pair)) ∧
∃ vm, ((∀ xs, trackingVm xs vm) ∧ guardedAccountVm vm) ∧ fst pair = HostF vm).
{ intro fuel. induction fuel as [|fuel IH].
- split; [exact Hseed|]. eexists; split; [exact Hseed|reflexivity].
- cbn [recursive_module_unroll fst snd].
destruct IH as [Hvm [inner [Hinner Hhost]]]. split.
+ rewrite Hhost. apply Hstep. exact Hinner.
+ eexists; split; [exact Hvm|reflexivity]. }
exact (proj2 (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel))).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Theorem transaction_call_guarded_account tx msg :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
preservesCallback guarded (@E.Host.call_from_eoa F tx msg).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
intros Hgas Htarget. unfold E.Host.call_from_eoa.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
H.functor H.Make_result.call_from_eoa H.call_from_eoa].
apply (@call_impl_guarded_account args query delegated storage block sender
(@H.Vm (@Host.Instantiate.Host_fargs args))).
- intros nested body Hc xs.
exact (@execution_vm_preserves_tracking xs F cold warm access write reserve
Hrevision Hcold Hwarm nested body Hc).
- apply guarded_native_dispatch.
- exact execution_vm_guarded_account.
- exact Hgas.
- exact Htarget.
Qed.
Theorem transaction_call_protects_guarded_nonsender tx msg initial original :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
S.t.self_destruct initial = nil →
In (query, original) (W.t.accounts (S.t.initial_world_state initial)) →
guarded initial →
protected initial query original ≤ A.t.balance (accountAt initial query) →
let post := snd (@E.Host.call_from_eoa F tx msg initial) in
¬ In query (S.t.accounts_created_in_current_transaction post) →
A.t.code (accountAt post query) = delegated ∧
A.is_smart_contract (accountAt post query) = false ∧
protected initial query original ≤ A.t.balance (accountAt post query).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
intros Hgas Htarget Hempty Hin Hguarded Hentry post HnotCreated.
pose proof (transaction_call_guarded_account tx msg Hgas Htarget initial Hguarded)
as [_ [HpostOrigin [_ [Hcreated|Hcode]]]]; [contradiction|].
change (A.t.code (accountAt post query) = delegated) in Hcode.
assert (Hnoncontract : A.is_smart_contract (accountAt post query) = false).
{ change (andb
(negb (Byte_string.Bytes.op_eq (A.t.code (accountAt post query)) Byte_string.Bytes.empty))
(negb (Delegation.is_valid_delegation (A.t.code (accountAt post query)))) = false).
rewrite Hcode, Hdelegated, Bool.andb_false_r. reflexivity. }
split; [exact Hcode|]. split; [exact Hnoncontract|].
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 query Hgas Hempty HnotCreated) as HnotDestroyed.
assert (Hdes : Addr._Set.(OCamlSet.S.mem) query (S.t.self_destruct post) = false).
{ destruct (Addr._Set.(OCamlSet.S.mem) query (S.t.self_destruct post)) eqn:Hmem; [|reflexivity].
exfalso. apply HnotDestroyed, address_set_mem_member, Hmem. }
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.
rewrite <- (context_preserves_protected initial post query original Hcontext).
apply (noncontract_nonsender_retains_reserve (cold,warm,access,write,reserve)
post query original Hcheck).
+ unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hsnapshot _ _ _ _ _ _ _ _.
rewrite Hsnapshot. exact Hin.
+ exact Hdes.
+ change (S.t.tx_origin post = sender) in HpostOrigin.
rewrite HpostOrigin.
change (Z.eqb (OCamlString.compare query sender) 0 = false).
apply Z.eqb_neq. intro Heq. apply Hdifferent, address_compare_eq, Heq.
+ exact Hnoncontract.
Qed.
End Execution.
Print Assumptions execution_vm_guarded_account.
Print Assumptions transaction_call_guarded_account.
Print Assumptions transaction_call_protects_guarded_nonsender.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.protectedstoragenative QedkConcrete.hostframe QedkConcrete.hostcode
QedkConcrete.vmhostframe QedkConcrete.recursiveassembly QedkConcrete.assemblychecked
QedkConcrete.vmcallframe QedkConcrete.hoststoragewords QedkConcrete.hostrollback
QedkConcrete.reservecheck.
Require Import SetcodeConcrete.vmcontextframe SetcodeConcrete.vmemptyframe
SetcodeConcrete.assembly SetcodeConcrete.vmprogress SetcodeConcrete.emptyaccount
SetcodeConcrete.accountframe SetcodeConcrete.contextframe
SetcodeConcrete.executionconfig SetcodeConcrete.executiontracking
SetcodeConcrete.creationtracking SetcodeConcrete.guardedaccountframe
SetcodeConcrete.guardedaccounthost SetcodeConcrete.reserve
SetcodeConcrete.executioncontext SetcodeConcrete.executiondestruction
SetcodeConcrete.executionreserve.
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.
Lemma primitive_frame_at_conj {Host_t : Set} (host : Evmc.HOST (t := Host_t))
(P Q : Host_t → Prop) self :
PrimitiveHostFrameAt host P self →
PrimitiveHostFrameAt host Q self →
PrimitiveHostFrameAt host (fun s ⇒ P s ∧ Q s) self.
Proof.
intros HP HQ; destruct HP, HQ; constructor; intros;
unfold preservesCallback in *; intros s [HsP HsQ]; split; eauto.
Qed.
Lemma noncontract_nonsender_retains_reserve prices s addr original :
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Addr.op_eq addr (S.t.tx_origin s) = false →
A.is_smart_contract (accountAt s addr) = false →
protected s addr original ≤ A.t.balance (accountAt s addr).
Proof.
intros Hcheck Hin Hdes Hsender Hnoncontract.
assert (Hexempt : Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s addr))
(Addr.op_eq addr (S.t.tx_origin s)) (Reserve_balance.sent_in_window s addr) = false).
{ unfold Setcode_rules.contract_exempt. rewrite Hnoncontract. reflexivity. }
pose proof (checked_account prices s addr original Hcheck Hin Hdes Hexempt) as Hbound.
unfold reserveViolated in Hbound. rewrite Hsender, u256_lt in Hbound.
apply Z.ltb_ge in Hbound. exact Hbound.
Qed.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
Context (query : Addr.t) (delegated : string) (storage : list (string × string))
(block : N) (sender : Addr.t).
Context (Hdifferent : query ≠ sender).
Context (HnotNative : query ≠ Reserve_balance.precompile_address).
Context (Hdelegated : Delegation.is_valid_delegation delegated = true).
Context (Hmap : stringmaps.WellFormed storage) (Hwords : storageWords storage).
Context (Hpositive : initialThreshold storage block query ≠ 0).
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).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation guarded := (guardedAccount query delegated storage block sender).
Definition guardedAccountVm {T : Evmc.Vm_H_signature (t := S.t)}
(vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs T)) :=
∀ msg code, 0 ≤ Evmc.Message.t.gas msg < 2^63 →
∀ s, guarded s →
(Evmc.Message.t.recipient msg = native → code = Byte_string.Bytes.empty) →
guarded (snd (vm.(Evmc.Vm.SIG.execute) msg code s)).
Lemma guarded_native_dispatch vm msg addr action :
@nativeDispatch args vm msg addr = Some action →
preservesCallback guarded action.
Proof using F HnotNative Hrevision access block cold delegated query reserve sender storage warm write.
intros Hselected s [Hconfig [Hsender Haccount]]. split.
- exact (@native_config_storage args vm block storage msg addr action
cold warm access write reserve Hrevision Hselected s Hconfig).
- split.
+ pose proof (@precompile_preserves_migration_context
(@H.Precompiles_fargs (H.Build_FArgs args.(Host.Instantiate.ChainParams) vm))
cold warm access write reserve msg addr action Hrevision Hselected s) as Hctx.
rewrite (migration_context_origin _ _ Hctx). exact Hsender.
+ exact (@precompile_account_frame
(@H.Precompiles_fargs (H.Build_FArgs args.(Host.Instantiate.ChainParams) vm))
cold warm access write reserve query (fun code _ ⇒ code = delegated)
msg addr action Hrevision HnotNative Hselected s Haccount).
Qed.
Lemma concrete_primitives_origin vm :
PrimitiveHostFrame (HostF vm) (fun s ⇒ S.t.tx_origin s = sender).
Proof using F sender.
constructor; intros; unfold preservesCallback in *; intros s Hsender;
destruct (@concrete_primitives_preserve_migration_context args vm s);
unfold preservesCallback in ×.
all: eapply eq_trans; [apply (migration_context_origin s); eauto|exact Hsender].
Qed.
Lemma concrete_primitives_guarded_account vm self :
self ≠ native →
PrimitiveHostFrameAt (HostF vm) guarded self.
Proof using F block delegated query sender storage.
intro Hself. unfold guardedAccount.
apply primitive_frame_at_conj.
- exact (@concrete_primitives_preserve_protected_storage args vm
self native (reachableReserveStorage block storage) Hself).
- apply primitive_frame_at_conj.
+ apply primitive_frame_at. apply concrete_primitives_origin.
+ apply primitive_frame_at. apply concrete_primitives_account_frame.
Qed.
Lemma guarded_vm_constructor vm :
(∀ xs, trackingVm xs vm) →
guardedAccountVm vm →
guardedAccountVm (VmF (HostF vm)).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
intros Htracking Hvm msg code Hgas initial Hinitial Hcode.
destruct (String.string_dec (Evmc.Message.t.recipient msg) native) as [Heq|Hneq].
{ rewrite (Hcode Heq). cbn -[Vm.Make.execute].
match goal with |- guarded (snd (@V.execute ?ht ?vf ?message ?body ?state)) ⇒
pose proof (@execute_empty_host ht vf cold warm access write reserve
Hrevision eq_refl (fun s ⇒ eq_refl) (fun a s ⇒ eq_refl)
(fun a o n s ⇒ copy_code_read_only a o n s) message state) as Hsame
end.
exact (eq_ind_r guarded Hinitial Hsame). }
cbn -[Vm.Make.execute].
eapply execute_preserves_at_context with (P := guarded).
- exact Hrevision.
- reflexivity.
- exact (@concrete_setcode_gas_progress args vm _ cold warm access write reserve Hrevision Hcold Hwarm).
- intros; reflexivity.
- intros; reflexivity.
- intros; apply copy_code_read_only.
- exact (concrete_primitives_guarded_account vm (Evmc.Message.t.recipient msg) Hneq).
- intros. eapply setcode_guarded_account; eassumption.
- intros env opcode before Hself Hflag Hopcode Hbound HP.
assert (HselfDifferent : @V.Executor.self S.t _ env ≠ native).
{ intro Hequal. apply Hneq. exact (eq_trans (eq_sym Hself) Hequal). }
assert (Hprimitive : PrimitiveHostFrameAt (HostF vm) guarded (@V.Executor.self S.t _ env))
by (apply concrete_primitives_guarded_account; exact HselfDifferent).
assert (HcopyEmpty : ∀ size s, guarded s →
(HostF vm).(Evmc.HOST.copy_code) native 0 size s = (Byte_string.Bytes.empty, s)).
{ intros size s [Hconfig _]. apply emptyaccount.concrete_copy_empty_code.
exact (protected_storage_code _ _ _ Hconfig). }
assert (Hnested : ∀ nested, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
childCallFrom (@V.Executor.self S.t _ env) nested →
(Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient nested = native → Evmc.Message.t.code_address nested = native)) →
preservesCallback guarded ((HostF vm).(Evmc.HOST.call) nested)).
{ intros nested Hb Horigin Htarget.
exact (@call_guarded_account args query delegated storage block sender vm
(fun child body Hc xs ⇒ Htracking xs child body Hc)
(@V.Executor.self S.t _ env) nested (guarded_native_dispatch vm)
Hvm Hb HselfDifferent Horigin Htarget). }
destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
all: cbn [dispatch V.Executor.execute_opcode].
+ exact (@create_preserves_protected_recipient S.t _ env guarded native Hprimitive Hnested before Hbound HP).
+ exact (@call_preserves_protected_recipient S.t _ env guarded native Hprimitive HcopyEmpty Hnested before Hbound HP).
+ exact (@callcode_preserves_protected_recipient S.t _ env guarded native Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@delegatecall_preserves_protected_recipient S.t _ env guarded native Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@create2_preserves_protected_recipient S.t _ env guarded native Hprimitive Hnested before Hbound HP).
+ exact (@staticcall_preserves_protected_recipient S.t _ env guarded native Hprimitive HcopyEmpty Hnested before Hbound HP).
- exact Hgas.
- exact Hinitial.
Qed.
Theorem execution_vm_guarded_account :
guardedAccountVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
let vm := constr:(@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args)) in
assert (Hseed : (∀ xs, trackingVm xs vm) ∧ guardedAccountVm vm).
{ split.
- intros xs msg body Hb s HP; exact HP.
- intros msg body Hb s HP Htarget; exact HP. }
assert (Hstep : ∀ vm,
(∀ xs, trackingVm xs vm) ∧ guardedAccountVm vm →
(∀ xs, trackingVm xs (VmF (HostF vm))) ∧ guardedAccountVm (VmF (HostF vm))).
{ intros vm [Ht Hg]. split.
- intro xs. apply (@vm_constructor_tracking xs F cold warm access write reserve Hrevision).
apply (@host_constructor_tracking xs F cold warm access write reserve Hrevision Hcold Hwarm), Ht.
- apply guarded_vm_constructor; assumption. }
assert (Hunroll : ∀ fuel,
let pair := recursive_module_unroll fuel
(@Evmc.Instantiate.recursive_module_seed S.t (@recursiveArgs args))
(fun pair ⇒ (HostF (snd pair), VmF (fst pair))) in
((∀ xs, trackingVm xs (snd pair)) ∧ guardedAccountVm (snd pair)) ∧
∃ vm, ((∀ xs, trackingVm xs vm) ∧ guardedAccountVm vm) ∧ fst pair = HostF vm).
{ intro fuel. induction fuel as [|fuel IH].
- split; [exact Hseed|]. eexists; split; [exact Hseed|reflexivity].
- cbn [recursive_module_unroll fst snd].
destruct IH as [Hvm [inner [Hinner Hhost]]]. split.
+ rewrite Hhost. apply Hstep. exact Hinner.
+ eexists; split; [exact Hvm|reflexivity]. }
exact (proj2 (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel))).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Theorem transaction_call_guarded_account tx msg :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
preservesCallback guarded (@E.Host.call_from_eoa F tx msg).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
intros Hgas Htarget. unfold E.Host.call_from_eoa.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
H.functor H.Make_result.call_from_eoa H.call_from_eoa].
apply (@call_impl_guarded_account args query delegated storage block sender
(@H.Vm (@Host.Instantiate.Host_fargs args))).
- intros nested body Hc xs.
exact (@execution_vm_preserves_tracking xs F cold warm access write reserve
Hrevision Hcold Hwarm nested body Hc).
- apply guarded_native_dispatch.
- exact execution_vm_guarded_account.
- exact Hgas.
- exact Htarget.
Qed.
Theorem transaction_call_protects_guarded_nonsender tx msg initial original :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = native → Evmc.Message.t.code_address msg = native)) →
S.t.self_destruct initial = nil →
In (query, original) (W.t.accounts (S.t.initial_world_state initial)) →
guarded initial →
protected initial query original ≤ A.t.balance (accountAt initial query) →
let post := snd (@E.Host.call_from_eoa F tx msg initial) in
¬ In query (S.t.accounts_created_in_current_transaction post) →
A.t.code (accountAt post query) = delegated ∧
A.is_smart_contract (accountAt post query) = false ∧
protected initial query original ≤ A.t.balance (accountAt post query).
Proof using F Hcold Hdelegated Hdifferent Hmap HnotNative Hpositive Hrevision Hwarm Hwords
access block cold delegated query reserve sender storage warm write.
intros Hgas Htarget Hempty Hin Hguarded Hentry post HnotCreated.
pose proof (transaction_call_guarded_account tx msg Hgas Htarget initial Hguarded)
as [_ [HpostOrigin [_ [Hcreated|Hcode]]]]; [contradiction|].
change (A.t.code (accountAt post query) = delegated) in Hcode.
assert (Hnoncontract : A.is_smart_contract (accountAt post query) = false).
{ change (andb
(negb (Byte_string.Bytes.op_eq (A.t.code (accountAt post query)) Byte_string.Bytes.empty))
(negb (Delegation.is_valid_delegation (A.t.code (accountAt post query)))) = false).
rewrite Hcode, Hdelegated, Bool.andb_false_r. reflexivity. }
split; [exact Hcode|]. split; [exact Hnoncontract|].
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 query Hgas Hempty HnotCreated) as HnotDestroyed.
assert (Hdes : Addr._Set.(OCamlSet.S.mem) query (S.t.self_destruct post) = false).
{ destruct (Addr._Set.(OCamlSet.S.mem) query (S.t.self_destruct post)) eqn:Hmem; [|reflexivity].
exfalso. apply HnotDestroyed, address_set_mem_member, Hmem. }
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.
rewrite <- (context_preserves_protected initial post query original Hcontext).
apply (noncontract_nonsender_retains_reserve (cold,warm,access,write,reserve)
post query original Hcheck).
+ unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hsnapshot _ _ _ _ _ _ _ _.
rewrite Hsnapshot. exact Hin.
+ exact Hdes.
+ change (S.t.tx_origin post = sender) in HpostOrigin.
rewrite HpostOrigin.
change (Z.eqb (OCamlString.compare query sender) 0 = false).
apply Z.eqb_neq. intro Heq. apply Hdifferent, address_compare_eq, Heq.
+ exact Hnoncontract.
Qed.
End Execution.
Print Assumptions execution_vm_guarded_account.
Print Assumptions transaction_call_guarded_account.
Print Assumptions transaction_call_protects_guarded_nonsender.