Library SetcodeConcrete.executionnondebit
Empty-account balances in the actual recursive SETCODE executor
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.nondebitframe QedkConcrete.nondebithosttracking
QedkConcrete.assemblychecked QedkConcrete.hostchecked QedkConcrete.hostframe
QedkConcrete.vmcallchecked QedkConcrete.hashwords QedkConcrete.vmmemorywords
QedkConcrete.vmcallframe QedkConcrete.recursiveassembly QedkConcrete.vmsupplywords.
Require Import SetcodeConcrete.vmnondebitentry SetcodeConcrete.executionsupply
SetcodeConcrete.executiontracking SetcodeConcrete.creationtracking
SetcodeConcrete.assembly.
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.
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 (cold warm access write reserve : N) (limit : Z).
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) (Hnative : @nativeOutputBounds args).
Context (query : Chain.Ethereum.Address.t) (minimum : Z).
Local Abbreviation invariant := (hostSupplyContext args.(Host.Instantiate.ChainParams) limit).
Local Abbreviation floor := (uncreatedBalanceFloor query minimum).
Definition protectedVm (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs args)))) :=
∀ msg code, vmMessageBounds msg → OCamlString.length code < 2^256 →
∀ initial, invariant initial → floor initial →
(Evmc.Message.t.recipient msg = query → code = Byte_string.Bytes.empty ∨
In query (S.t.accounts_created_in_current_transaction initial)) →
floor (snd (vm.(Evmc.Vm.SIG.execute) msg code initial)).
Lemma host_constructor_protected vm self msg :
(∀ xs, trackingVm xs vm) → protectedVm vm →
vmMessageBounds msg → self ≠ query → childCallFrom self msg →
(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)) →
∀ initial, invariant initial → floor initial →
floor (snd ((HostF vm).(Evmc.HOST.call) msg initial)).
Proof.
intros Htracking Hvm Hmsg Hself Horigin Htarget initial Hcontext Hfloor.
eapply call_preserves_uncreated_balance_bounded_tracking; try eassumption.
- eapply setcode_reserve_precompile_table. exact Hrevision.
- intros nested code Hgas before Hmap a Hin.
exact (Htracking (S.t.accounts_created_in_current_transaction before)
nested code Hgas before (fun a H ⇒ H) a Hin).
Qed.
Lemma vm_constructor_protected vm :
@checkedVm args limit vm → (∀ xs, trackingVm xs vm) → protectedVm vm →
protectedVm (VmF (HostF vm)).
Proof.
intros Hchecked Htracking Hvm msg code Hmsg Hcode initial Hcontext Hfloor Hallowed.
cbn -[Vm.Make.execute uncreatedBalanceFloor].
match goal with |- floor (snd ?run) ⇒
assert (Hrun : ((invariant (snd run) ∧ floor (snd run)) ∧
resultOutputBound (fst run)) ∨ badMemoryInitComparison)
end.
{ eapply (@execute_uncreated_balance args vm _ limit query minimum
cold warm access write reserve); try eassumption.
- reflexivity.
- eapply concrete_setcode_gas_progress; eassumption.
- intro xs. apply (@host_constructor_tracking xs F cold warm access write reserve
Hrevision Hcold Hwarm). apply Htracking.
- intros child Hb before Hbefore. apply (@host_constructor_checked_table args limit vm
(setcode_native_table cold warm access write reserve Hrevision vm) Hnative Hchecked
child Hb before Hbefore).
- intros Hself child Hb Horigin Htarget before [Hbefore HfloorBefore].
destruct (@host_constructor_checked_table args limit vm
(setcode_native_table cold warm access write reserve Hrevision vm) Hnative Hchecked
child Hb before Hbefore) as [[Hafter Houtput]|Hbad]; [|right; exact Hbad].
left. split; [split; [exact Hafter|]|exact Houtput].
exact (host_constructor_protected vm _ child Htracking Hvm Hb Hself Horigin Htarget
before Hbefore HfloorBefore).
- split; assumption. }
destruct Hrun as [[[_ Hafter] _]|Hbad]; [exact Hafter|].
exfalso. exact (no_bad_memory_init_comparison Hbad).
Qed.
Theorem execution_vm_protected :
protectedVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof.
let vmType := type of (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)) in
set (safe := fun vm : vmType ⇒ @checkedVm args limit vm ∧
(∀ xs, trackingVm xs vm) ∧ protectedVm vm).
assert (Hseed : safe (@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args))).
{ split.
- intros msg code Hmsg Hcode initial Hinitial. left. split; [exact Hinitial|].
change (0 < 2^256). lia.
- split.
+ intros xs msg code Hgas initial Hinitial. exact Hinitial.
+ intros msg code Hmsg Hcode initial Hcontext Hfloor Hallowed. exact Hfloor. }
assert (Hstep : ∀ vm, safe vm → safe (VmF (HostF vm))).
{ intros vm [Hchecked [Htracking Hprotected]]. split.
- exact (vm_constructor_supply cold warm access write reserve limit Hrevision Hcold Hwarm
Hhash Hnative vm Hchecked).
- 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).
apply Htracking.
+ apply vm_constructor_protected; 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
safe (snd pair) ∧ ∃ vm, safe 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 (proj2 (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel)))).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Theorem transaction_call_value_floor tx msg initial :
vmMessageBounds msg →
(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)) →
invariant initial →
uncreatedBalanceFloor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) initial →
floor (snd (@E.Host.call_from_eoa F tx msg initial)).
Proof.
intros Hmsg Htarget Hcontext Hfloor. 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].
eapply call_impl_value_floor_tracking; try eassumption.
- eapply setcode_reserve_precompile_table. exact Hrevision.
- intros nested code Hgas before Hmap a Hin.
pose proof (@execution_vm_preserves_tracking
(S.t.accounts_created_in_current_transaction before) F cold warm access write reserve
Hrevision Hcold Hwarm nested code Hgas before (fun a H ⇒ H) a Hin) as Hkeep.
cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hkeep |- ×.
exact Hkeep.
- exact execution_vm_protected.
Qed.
Theorem transaction_call_nondebit tx msg initial :
vmMessageBounds msg → Evmc.Message.t.sender msg ≠ query →
childCallFrom (Evmc.Message.t.sender msg) msg →
(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)) →
invariant initial → floor initial → floor (snd (@E.Host.call_from_eoa F tx msg initial)).
Proof.
intros Hmsg Hsender Horigin Htarget Hcontext Hfloor. 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].
eapply call_impl_preserves_uncreated_balance_bounded_tracking; try eassumption.
- eapply setcode_reserve_precompile_table. exact Hrevision.
- intros nested code Hgas before Hmap a Hin.
pose proof (@execution_vm_preserves_tracking
(S.t.accounts_created_in_current_transaction before) F cold warm access write reserve
Hrevision Hcold Hwarm nested code Hgas before (fun a H ⇒ H) a Hin) as Hkeep.
cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hkeep |- ×.
exact Hkeep.
- exact execution_vm_protected.
Qed.
End Execution.
Print Assumptions transaction_call_value_floor.
Print Assumptions transaction_call_nondebit.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.nondebitframe QedkConcrete.nondebithosttracking
QedkConcrete.assemblychecked QedkConcrete.hostchecked QedkConcrete.hostframe
QedkConcrete.vmcallchecked QedkConcrete.hashwords QedkConcrete.vmmemorywords
QedkConcrete.vmcallframe QedkConcrete.recursiveassembly QedkConcrete.vmsupplywords.
Require Import SetcodeConcrete.vmnondebitentry SetcodeConcrete.executionsupply
SetcodeConcrete.executiontracking SetcodeConcrete.creationtracking
SetcodeConcrete.assembly.
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.
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 (cold warm access write reserve : N) (limit : Z).
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) (Hnative : @nativeOutputBounds args).
Context (query : Chain.Ethereum.Address.t) (minimum : Z).
Local Abbreviation invariant := (hostSupplyContext args.(Host.Instantiate.ChainParams) limit).
Local Abbreviation floor := (uncreatedBalanceFloor query minimum).
Definition protectedVm (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs args)))) :=
∀ msg code, vmMessageBounds msg → OCamlString.length code < 2^256 →
∀ initial, invariant initial → floor initial →
(Evmc.Message.t.recipient msg = query → code = Byte_string.Bytes.empty ∨
In query (S.t.accounts_created_in_current_transaction initial)) →
floor (snd (vm.(Evmc.Vm.SIG.execute) msg code initial)).
Lemma host_constructor_protected vm self msg :
(∀ xs, trackingVm xs vm) → protectedVm vm →
vmMessageBounds msg → self ≠ query → childCallFrom self msg →
(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)) →
∀ initial, invariant initial → floor initial →
floor (snd ((HostF vm).(Evmc.HOST.call) msg initial)).
Proof.
intros Htracking Hvm Hmsg Hself Horigin Htarget initial Hcontext Hfloor.
eapply call_preserves_uncreated_balance_bounded_tracking; try eassumption.
- eapply setcode_reserve_precompile_table. exact Hrevision.
- intros nested code Hgas before Hmap a Hin.
exact (Htracking (S.t.accounts_created_in_current_transaction before)
nested code Hgas before (fun a H ⇒ H) a Hin).
Qed.
Lemma vm_constructor_protected vm :
@checkedVm args limit vm → (∀ xs, trackingVm xs vm) → protectedVm vm →
protectedVm (VmF (HostF vm)).
Proof.
intros Hchecked Htracking Hvm msg code Hmsg Hcode initial Hcontext Hfloor Hallowed.
cbn -[Vm.Make.execute uncreatedBalanceFloor].
match goal with |- floor (snd ?run) ⇒
assert (Hrun : ((invariant (snd run) ∧ floor (snd run)) ∧
resultOutputBound (fst run)) ∨ badMemoryInitComparison)
end.
{ eapply (@execute_uncreated_balance args vm _ limit query minimum
cold warm access write reserve); try eassumption.
- reflexivity.
- eapply concrete_setcode_gas_progress; eassumption.
- intro xs. apply (@host_constructor_tracking xs F cold warm access write reserve
Hrevision Hcold Hwarm). apply Htracking.
- intros child Hb before Hbefore. apply (@host_constructor_checked_table args limit vm
(setcode_native_table cold warm access write reserve Hrevision vm) Hnative Hchecked
child Hb before Hbefore).
- intros Hself child Hb Horigin Htarget before [Hbefore HfloorBefore].
destruct (@host_constructor_checked_table args limit vm
(setcode_native_table cold warm access write reserve Hrevision vm) Hnative Hchecked
child Hb before Hbefore) as [[Hafter Houtput]|Hbad]; [|right; exact Hbad].
left. split; [split; [exact Hafter|]|exact Houtput].
exact (host_constructor_protected vm _ child Htracking Hvm Hb Hself Horigin Htarget
before Hbefore HfloorBefore).
- split; assumption. }
destruct Hrun as [[[_ Hafter] _]|Hbad]; [exact Hafter|].
exfalso. exact (no_bad_memory_init_comparison Hbad).
Qed.
Theorem execution_vm_protected :
protectedVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof.
let vmType := type of (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)) in
set (safe := fun vm : vmType ⇒ @checkedVm args limit vm ∧
(∀ xs, trackingVm xs vm) ∧ protectedVm vm).
assert (Hseed : safe (@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args))).
{ split.
- intros msg code Hmsg Hcode initial Hinitial. left. split; [exact Hinitial|].
change (0 < 2^256). lia.
- split.
+ intros xs msg code Hgas initial Hinitial. exact Hinitial.
+ intros msg code Hmsg Hcode initial Hcontext Hfloor Hallowed. exact Hfloor. }
assert (Hstep : ∀ vm, safe vm → safe (VmF (HostF vm))).
{ intros vm [Hchecked [Htracking Hprotected]]. split.
- exact (vm_constructor_supply cold warm access write reserve limit Hrevision Hcold Hwarm
Hhash Hnative vm Hchecked).
- 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).
apply Htracking.
+ apply vm_constructor_protected; 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
safe (snd pair) ∧ ∃ vm, safe 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 (proj2 (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel)))).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Theorem transaction_call_value_floor tx msg initial :
vmMessageBounds msg →
(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)) →
invariant initial →
uncreatedBalanceFloor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) initial →
floor (snd (@E.Host.call_from_eoa F tx msg initial)).
Proof.
intros Hmsg Htarget Hcontext Hfloor. 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].
eapply call_impl_value_floor_tracking; try eassumption.
- eapply setcode_reserve_precompile_table. exact Hrevision.
- intros nested code Hgas before Hmap a Hin.
pose proof (@execution_vm_preserves_tracking
(S.t.accounts_created_in_current_transaction before) F cold warm access write reserve
Hrevision Hcold Hwarm nested code Hgas before (fun a H ⇒ H) a Hin) as Hkeep.
cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hkeep |- ×.
exact Hkeep.
- exact execution_vm_protected.
Qed.
Theorem transaction_call_nondebit tx msg initial :
vmMessageBounds msg → Evmc.Message.t.sender msg ≠ query →
childCallFrom (Evmc.Message.t.sender msg) msg →
(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)) →
invariant initial → floor initial → floor (snd (@E.Host.call_from_eoa F tx msg initial)).
Proof.
intros Hmsg Hsender Horigin Htarget Hcontext Hfloor. 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].
eapply call_impl_preserves_uncreated_balance_bounded_tracking; try eassumption.
- eapply setcode_reserve_precompile_table. exact Hrevision.
- intros nested code Hgas before Hmap a Hin.
pose proof (@execution_vm_preserves_tracking
(S.t.accounts_created_in_current_transaction before) F cold warm access write reserve
Hrevision Hcold Hwarm nested code Hgas before (fun a H ⇒ H) a Hin) as Hkeep.
cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hkeep |- ×.
exact Hkeep.
- exact execution_vm_protected.
Qed.
End Execution.
Print Assumptions transaction_call_value_floor.
Print Assumptions transaction_call_nondebit.