Library SetcodeConcrete.executionconfigstorage
Configuration storage survives the actual SETCODE VM
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.hostframe QedkConcrete.hostcode QedkConcrete.vmhostframe
QedkConcrete.recursiveassembly QedkConcrete.assemblychecked QedkConcrete.vmcallframe.
Require Import SetcodeConcrete.vmcontextframe SetcodeConcrete.vmemptyframe
SetcodeConcrete.assembly SetcodeConcrete.vmprogress SetcodeConcrete.emptyaccount.
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 setcode_other_storage {F : H.FArgs} query P current source static gas :
current ≠ query →
preservesCallback (protectedStorage query P) (H.setcodefrom current source static gas).
Proof.
intros Hneq s Hbefore. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] access] write] reserve] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact Hbefore.
all: try solve [eapply (@protected_storage_same_fields query P s);
[reflexivity|reflexivity|reflexivity|reflexivity|exact Hbefore]].
all: apply protected_storage_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current).
all: try contradiction.
all: eapply (@protected_storage_same_fields query P s);
[reflexivity|reflexivity|reflexivity|reflexivity|exact Hbefore].
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) (P : N → list (string × string) → Prop).
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 protected := (protectedStorage query P).
Definition configStorageVm {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, protected s →
(Evmc.Message.t.recipient msg = query → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) msg code s)).
Context (Hnative : ∀ vm msg addr action,
@nativeDispatch args vm msg addr = Some action → preservesCallback protected action).
Lemma config_storage_vm_constructor vm :
configStorageVm vm → configStorageVm (VmF (HostF vm)).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve warm write.
intros Hvm msg code Hgas initial Hinitial Hcode.
destruct (String.string_dec (Evmc.Message.t.recipient msg) query) as [Heq|Hneq].
{ rewrite (Hcode Heq). cbn -[Vm.Make.execute].
match goal with |- protected (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 protected Hinitial Hsame). }
cbn -[Vm.Make.execute].
eapply execute_preserves_at_context with (P := protected).
- 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_preserve_protected_storage args vm
(Evmc.Message.t.recipient msg) query P Hneq).
- intros. apply setcode_other_storage. exact Hneq.
- intros env opcode before Hself Hflag Hopcode Hbound HP.
assert (HselfDifferent : @V.Executor.self S.t _ env ≠ query).
{ intro Hequal. apply Hneq. exact (eq_trans (eq_sym Hself) Hequal). }
assert (Hprimitive : PrimitiveHostFrameAt (HostF vm) protected (@V.Executor.self S.t _ env))
by (apply concrete_primitives_preserve_protected_storage; exact HselfDifferent).
assert (HcopyEmpty : ∀ size s, protected s →
(HostF vm).(Evmc.HOST.copy_code) query 0 size s = (Byte_string.Bytes.empty, s)).
{ intros size s Hs. apply emptyaccount.concrete_copy_empty_code.
exact (protected_storage_code _ _ _ Hs). }
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 = query → Evmc.Message.t.code_address nested = query)) →
preservesCallback protected ((HostF vm).(Evmc.HOST.call) nested)).
{ intros nested Hb Horigin Htarget.
exact (@call_preserves_protected_storage args vm (@V.Executor.self S.t _ env)
query P nested (Hnative 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 protected query Hprimitive Hnested before Hbound HP).
+ exact (@call_preserves_protected_recipient S.t _ env protected query Hprimitive HcopyEmpty Hnested before Hbound HP).
+ exact (@callcode_preserves_protected_recipient S.t _ env protected query Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@delegatecall_preserves_protected_recipient S.t _ env protected query Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@create2_preserves_protected_recipient S.t _ env protected query Hprimitive Hnested before Hbound HP).
+ exact (@staticcall_preserves_protected_recipient S.t _ env protected query Hprimitive HcopyEmpty Hnested before Hbound HP).
- exact Hgas.
- exact Hinitial.
Qed.
Theorem execution_vm_protects_config_storage :
configStorageVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve warm write.
assert (Hseed : configStorageVm (@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args))).
{ intros msg code Hgas s Hinitial Hempty. exact Hinitial. }
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
configStorageVm (snd pair) ∧ ∃ vm, configStorageVm 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 config_storage_vm_constructor. exact Hinner.
+ eexists; split; [exact Hvm|reflexivity]. }
exact (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel)).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Theorem transaction_call_protects_config_storage 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 = query → Evmc.Message.t.code_address msg = query)) →
preservesCallback protected (@E.Host.call_from_eoa F tx msg).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve 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_preserves_protected_storage args
(@H.Vm (@Host.Instantiate.Host_fargs args))).
- apply Hnative.
- exact execution_vm_protects_config_storage.
- exact Hgas.
- exact Htarget.
Qed.
Theorem system_call_protects_config_storage msg :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
Evmc.Message.t.sender msg ≠ query →
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
preservesCallback protected (@E.Host.call F msg).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve warm write.
intros Hgas Hsender Hkind Htarget. unfold E.Host.call.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
H.functor H.Make_result.call].
apply (@call_preserves_protected_storage args
(@H.Vm (@Host.Instantiate.Host_fargs args)) (Evmc.Message.t.sender msg)).
- apply Hnative.
- exact execution_vm_protects_config_storage.
- exact Hgas.
- exact Hsender.
- unfold childCallFrom. rewrite Hkind. reflexivity.
- right; right; exact Htarget.
Qed.
End Execution.
Print Assumptions execution_vm_protects_config_storage.
Print Assumptions transaction_call_protects_config_storage.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.hostframe QedkConcrete.hostcode QedkConcrete.vmhostframe
QedkConcrete.recursiveassembly QedkConcrete.assemblychecked QedkConcrete.vmcallframe.
Require Import SetcodeConcrete.vmcontextframe SetcodeConcrete.vmemptyframe
SetcodeConcrete.assembly SetcodeConcrete.vmprogress SetcodeConcrete.emptyaccount.
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 setcode_other_storage {F : H.FArgs} query P current source static gas :
current ≠ query →
preservesCallback (protectedStorage query P) (H.setcodefrom current source static gas).
Proof.
intros Hneq s Hbefore. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] access] write] reserve] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact Hbefore.
all: try solve [eapply (@protected_storage_same_fields query P s);
[reflexivity|reflexivity|reflexivity|reflexivity|exact Hbefore]].
all: apply protected_storage_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current).
all: try contradiction.
all: eapply (@protected_storage_same_fields query P s);
[reflexivity|reflexivity|reflexivity|reflexivity|exact Hbefore].
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) (P : N → list (string × string) → Prop).
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 protected := (protectedStorage query P).
Definition configStorageVm {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, protected s →
(Evmc.Message.t.recipient msg = query → code = Byte_string.Bytes.empty) →
protected (snd (vm.(Evmc.Vm.SIG.execute) msg code s)).
Context (Hnative : ∀ vm msg addr action,
@nativeDispatch args vm msg addr = Some action → preservesCallback protected action).
Lemma config_storage_vm_constructor vm :
configStorageVm vm → configStorageVm (VmF (HostF vm)).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve warm write.
intros Hvm msg code Hgas initial Hinitial Hcode.
destruct (String.string_dec (Evmc.Message.t.recipient msg) query) as [Heq|Hneq].
{ rewrite (Hcode Heq). cbn -[Vm.Make.execute].
match goal with |- protected (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 protected Hinitial Hsame). }
cbn -[Vm.Make.execute].
eapply execute_preserves_at_context with (P := protected).
- 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_preserve_protected_storage args vm
(Evmc.Message.t.recipient msg) query P Hneq).
- intros. apply setcode_other_storage. exact Hneq.
- intros env opcode before Hself Hflag Hopcode Hbound HP.
assert (HselfDifferent : @V.Executor.self S.t _ env ≠ query).
{ intro Hequal. apply Hneq. exact (eq_trans (eq_sym Hself) Hequal). }
assert (Hprimitive : PrimitiveHostFrameAt (HostF vm) protected (@V.Executor.self S.t _ env))
by (apply concrete_primitives_preserve_protected_storage; exact HselfDifferent).
assert (HcopyEmpty : ∀ size s, protected s →
(HostF vm).(Evmc.HOST.copy_code) query 0 size s = (Byte_string.Bytes.empty, s)).
{ intros size s Hs. apply emptyaccount.concrete_copy_empty_code.
exact (protected_storage_code _ _ _ Hs). }
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 = query → Evmc.Message.t.code_address nested = query)) →
preservesCallback protected ((HostF vm).(Evmc.HOST.call) nested)).
{ intros nested Hb Horigin Htarget.
exact (@call_preserves_protected_storage args vm (@V.Executor.self S.t _ env)
query P nested (Hnative 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 protected query Hprimitive Hnested before Hbound HP).
+ exact (@call_preserves_protected_recipient S.t _ env protected query Hprimitive HcopyEmpty Hnested before Hbound HP).
+ exact (@callcode_preserves_protected_recipient S.t _ env protected query Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@delegatecall_preserves_protected_recipient S.t _ env protected query Hprimitive HselfDifferent Hnested before Hbound HP).
+ exact (@create2_preserves_protected_recipient S.t _ env protected query Hprimitive Hnested before Hbound HP).
+ exact (@staticcall_preserves_protected_recipient S.t _ env protected query Hprimitive HcopyEmpty Hnested before Hbound HP).
- exact Hgas.
- exact Hinitial.
Qed.
Theorem execution_vm_protects_config_storage :
configStorageVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve warm write.
assert (Hseed : configStorageVm (@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args))).
{ intros msg code Hgas s Hinitial Hempty. exact Hinitial. }
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
configStorageVm (snd pair) ∧ ∃ vm, configStorageVm 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 config_storage_vm_constructor. exact Hinner.
+ eexists; split; [exact Hvm|reflexivity]. }
exact (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel)).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Theorem transaction_call_protects_config_storage 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 = query → Evmc.Message.t.code_address msg = query)) →
preservesCallback protected (@E.Host.call_from_eoa F tx msg).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve 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_preserves_protected_storage args
(@H.Vm (@Host.Instantiate.Host_fargs args))).
- apply Hnative.
- exact execution_vm_protects_config_storage.
- exact Hgas.
- exact Htarget.
Qed.
Theorem system_call_protects_config_storage msg :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
Evmc.Message.t.sender msg ≠ query →
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
preservesCallback protected (@E.Host.call F msg).
Proof using F Hcold Hnative Hrevision Hwarm P access cold query reserve warm write.
intros Hgas Hsender Hkind Htarget. unfold E.Host.call.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
H.functor H.Make_result.call].
apply (@call_preserves_protected_storage args
(@H.Vm (@Host.Instantiate.Host_fargs args)) (Evmc.Message.t.sender msg)).
- apply Hnative.
- exact execution_vm_protects_config_storage.
- exact Hgas.
- exact Hsender.
- unfold childCallFrom. rewrite Hkind. reflexivity.
- right; right; exact Htarget.
Qed.
End Execution.
Print Assumptions execution_vm_protects_config_storage.
Print Assumptions transaction_call_protects_config_storage.