Library SetcodeConcrete.vmnondebit
Empty-account balances through the SETCODE instruction loop
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vmsupplywords QedkConcrete.memorymodel.
Require Import QedkConcrete.nondebitframe QedkConcrete.vmcallprotected
QedkConcrete.vmcreateprotected QedkConcrete.vmcallframe QedkConcrete.creationassembly
QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.vmboundedframe
QedkConcrete.vmchecked QedkConcrete.vmcallchecked QedkConcrete.vmmachinewords
QedkConcrete.vmmemorywords QedkConcrete.vmsizes QedkConcrete.vmrun
QedkConcrete.vmenvironmentwords QedkConcrete.vmdispatchwords
QedkConcrete.vmopcodeframe QedkConcrete.contexthostinstance
QedkConcrete.hostframe QedkConcrete.supply QedkConcrete.worldaccounts
QedkConcrete.wordrange QedkConcrete.hashwords QedkConcrete.vminvariant.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.nondebithosttracking
SetcodeConcrete.vmsupply SetcodeConcrete.vmcontextframe SetcodeConcrete.vmframes
SetcodeConcrete.vmprogress SetcodeConcrete.executiontracking SetcodeConcrete.creationtracking
SetcodeConcrete.emptyaccount SetcodeConcrete.accountframe SetcodeConcrete.host
SetcodeConcrete.adoptionsupply SetcodeConcrete.vmemptyframe.
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.
Lemma primitive_frame_at_conj {Host_t : Set} (host : Evmc.HOST (t := Host_t)) P Q 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; unfold preservesCallback in *;
intros; split; intuition eauto.
Qed.
Section Concrete.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation contextInvariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).
Local Abbreviation floor := (@uncreatedBalanceFloor).
Context (query : Chain.Ethereum.Address.t) (minimum : Z).
Local Abbreviation protected := (fun s ⇒ contextInvariant s ∧ floor query minimum s).
Local Abbreviation stillUncreated := (fun s ⇒ contextInvariant s ∧
(floor query minimum s ∧ ¬ List.In query (S.t.accounts_created_in_current_transaction s))).
Lemma protected_primitives self : self ≠ query →
PrimitiveHostFrameAt (HostF vm) protected self.
Proof.
intro Hself. apply primitive_frame_at_conj.
- apply primitive_frame_at, supply_context_primitives.
- apply concrete_primitives_preserve_uncreated_balance. exact Hself.
Qed.
Lemma still_uncreated_primitives self : self ≠ query →
PrimitiveHostFrameAt (HostF vm) stillUncreated self.
Proof.
intro Hself. apply primitive_frame_at_conj.
- apply primitive_frame_at, supply_context_primitives.
- apply concrete_primitives_preserve_still_uncreated_balance. exact Hself.
Qed.
Context (cold warm access write reserve : N).
Context {E : @V.Executor.FArgs S.t F}.
Local Abbreviation executeOpcode := (@dispatch S.t F E).
Lemma adoption_preserves_other_floor current source static gas :
current ≠ query →
preservesCallback (floor query minimum) ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof.
intros Hneq initial [Hbalance Haccount].
pose proof (@setcodefrom_preserves_supply
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas initial Hbalance) as [Hafter Htotal].
split; [exact Hafter|].
destruct Haccount as [Hcreated|[Hempty Hfloor]].
- left. exact (@setcode_preserves_tracking (S.t.accounts_created_in_current_transaction initial)
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas initial (fun a H ⇒ H) query Hcreated).
- pose proof (@setcode_other_account_frame
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
query (fun code _ ⇒ code = Byte_string.Bytes.empty) current source static gas
Hneq initial (conj (proj1 Hbalance) (or_intror Hempty))) as [_ Hcode].
destruct Hcode as [Hcreated|HemptyAfter]; [left; exact Hcreated|].
right. split; [exact HemptyAfter|].
pose proof (@setcodefrom_preserves_balances
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas initial query (proj1 Hbalance)) as Hsame.
exact (eq_rect _ (fun value ⇒ minimum ≤ U.to_z value) Hfloor _ (eq_sym Hsame)).
Qed.
Lemma instruction_retains_creation opcode state :
params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
params.(Vm.Make_Params_signature.debug_tstore) = false →
(∀ xs, trackingHost xs (HostF vm)) →
Z.of_N (MS.t.gas state) < 2^63 →
creationsRetained (S.t.accounts_created_in_current_transaction (MS.t.host state))
(MS.t.host (snd (executeOpcode opcode state))).
Proof.
intros Hrevision Hdebug Htracking Hgas.
destruct (Htracking (S.t.accounts_created_in_current_transaction (MS.t.host state)))
as [Hprimitive [Hsetcode [Hprogress [Hsize [Hcopy [Hcontext Hcall]]]]]].
assert (Hframe : preservesBoundedHost
(creationsRetained (S.t.accounts_created_in_current_transaction (MS.t.host state)))
(executeOpcode opcode)).
{ destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
- apply recursive_opcode_preserves; [exact Hprimitive|exact Hcall|exact Hrecursive].
- apply bounded_from_unbounded. destruct (machineOnlyOpcode opcode) eqn:Hlocal.
+ eapply machine_only_preserves_host_core; [| |exact Hdebug|exact Hlocal].
× eapply setcode_clz_preserves_host; exact Hrevision.
× intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
-- eapply setcode_opcode_preserves_host; eassumption.
-- unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
+ apply primitive_host_preserves_at_any_revision; try assumption.
× apply primitive_frame_at; exact Hprimitive.
× destruct opcode; cbn [recursiveCallOpcode machineOnlyOpcode primitiveHostOpcode] in *; congruence. }
exact (Hframe state Hgas (fun a H ⇒ H)).
Qed.
Lemma dispatch_preserves_uncreated_checked opcode state :
params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
params.(Vm.Make_Params_signature.debug_tstore) = false →
keccak256OutputSize →
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
opcodeWordParameters opcode →
(∀ xs, trackingHost xs (HostF vm)) →
(∀ msg, @vmMessageBounds msg → ∀ host, contextInvariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
(∀ msg, @vmMessageBounds msg →
childCallFrom (@V.Executor.self S.t F E) 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)) →
∀ host, protected host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
@V.Executor.self S.t F E ≠ query →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine protected state →
checkedMachine protected (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
intros Hrevision Hdebug Hhash Henv Hparams Hcreation Hsupply Hchild Hself Hgas Hinitial.
assert (Hbounded : BoundedMemory F) by (eapply setcode_bounded_memory; exact Hrevision).
assert (Hcontext : checkedMachine contextInvariant state).
{ eapply checked_machine_weaken with (pre := protected); [intros h Hh; exact (proj1 Hh)|exact Hinitial]. }
destruct (dispatch_supply_context vm params limit cold warm access write reserve Hrevision Hdebug Hhash (E := E)
opcode state
Henv Hparams Hsupply Hgas Hcontext) as [Hafter|Hbad];
[|right; exact Hbad].
pose proof (proj2 (proj1 (proj2 (proj2 (proj1 Hinitial))))) as Hfloor.
destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction (MS.t.host state))) as [Hcreated|Habsent].
- pose proof (instruction_retains_creation opcode state Hrevision Hdebug Hcreation Hgas) as Htracking.
left. unfold checkedMachine, machineWords in Hafter |- ×.
destruct Hafter as [[Hstack [Hpc [Hhost Hmemory]]] Hsizes].
split; [|exact Hsizes]. split; [exact Hstack|]. split; [exact Hpc|]. split; [|exact Hmemory].
split; [exact Hhost|]. split; [exact (proj1 (proj1 (proj1 Hhost)))|].
left. exact (Htracking query Hcreated).
- destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
+ assert (Hvalue : wordRange (Env.t.value
((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)))).
{ exact (List.Forall_inv (List.Forall_inv_tail (proj1 Henv))). }
assert (Hpre : checkedMachine stillUncreated state).
{ unfold checkedMachine, machineWords in Hinitial |- ×. tauto. }
assert (Hempty : ∀ host, stillUncreated host →
(HostF vm).(Evmc.HOST.copy_code) query 0 (@Delegation.eoa_delegated_code_length) host =
(Byte_string.Bytes.empty, host)).
{ intros host [_ [Hprotected Hnot]]. apply concrete_copy_empty_code.
exact (proj2 (uncreated_balance_floor_not_created query minimum host Hprotected Hnot)). }
assert (Hweaken : ∀ host, stillUncreated host → protected host) by (intros; tauto).
assert (Hprimitive : PrimitiveHostFrameAt (HostF vm) protected (@V.Executor.self S.t F E))
by (apply protected_primitives; exact Hself).
assert (HprimitivePre : PrimitiveHostFrameAt (HostF vm) stillUncreated (@V.Executor.self S.t F E))
by (apply still_uncreated_primitives; exact Hself).
assert (Hlift : ∀ action : @V.M.t S.t F bool,
boundedCheckedSafe protected (fun _ ⇒ True) action →
checkedMachine protected (snd (action state)) ∨ badMemoryInitComparison).
{ intros action Hsafe. destruct (Hsafe state Hgas Hinitial) as [[H _]|Hbad];
[left; exact H|right; exact Hbad]. }
assert (HliftPre : ∀ action : @V.M.t S.t F bool,
(∀ before, Z.of_N (MS.t.gas before) < 2^63 → checkedMachine stillUncreated before →
(checkedMachine protected (snd (action before)) ∧
match fst (action before) with inl _ ⇒ True | inr _ ⇒ True end) ∨ badMemoryInitComparison) →
checkedMachine protected (snd (action state)) ∨ badMemoryInitComparison).
{ intros action Hsafe. destruct (Hsafe state Hgas Hpre) as [[H _]|Hbad];
[left; exact H|right; exact Hbad]. }
destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate.
all: cbn [dispatch V.Executor.execute_opcode].
× apply Hlift, create_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|tauto].
× apply HliftPre. apply call_checked_empty_recipient_bounded with (query := query); try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Horigin Hcode. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exact (proj1 (Hcode Heq)).
× apply Hlift, callcode_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exfalso. unfold childCallFrom in Horigin. rewrite Hkind in Horigin.
apply Hself. destruct Horigin as [_ Hrecipient]. congruence.
× apply Hlift, delegatecall_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exfalso. unfold childCallFrom in Horigin. rewrite Hkind in Horigin.
apply Hself. congruence.
× apply Hlift, create2_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|tauto].
× apply HliftPre. apply staticcall_checked_empty_recipient_bounded with (query := query); try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Horigin Hcode. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exact (proj1 (Hcode Heq)).
+ assert (Hframe : preservesHost (floor query minimum) (executeOpcode opcode)).
{ destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_host_core; [| |exact Hdebug|exact Hlocal].
+ eapply setcode_clz_preserves_host; exact Hrevision.
+ intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
× eapply setcode_opcode_preserves_at_context; [exact Hrevision|].
intros; apply adoption_preserves_other_floor; exact Hself.
× unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
- apply primitive_host_preserves_at_any_revision; try assumption.
+ apply concrete_primitives_preserve_uncreated_balance. exact Hself.
+ destruct opcode; cbn [recursiveCallOpcode machineOnlyOpcode primitiveHostOpcode] in *;
congruence. }
specialize (Hframe state Hfloor). left.
unfold checkedMachine, machineWords in ×. tauto.
Qed.
Lemma run_preserves_uncreated_checked
state :
@setcodeGasProgress S.t F →
params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
params.(Vm.Make_Params_signature.debug_tstore) = false →
keccak256OutputSize →
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
(∀ xs, trackingHost xs (HostF vm)) →
(∀ msg, @vmMessageBounds msg → ∀ host, contextInvariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
(∀ msg, @vmMessageBounds msg →
childCallFrom (@V.Executor.self S.t F E) 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)) →
∀ host, protected host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
@V.Executor.self S.t F E ≠ query →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine protected state →
checkedMachine protected (snd (@generated_run S.t F E state)) ∨ badMemoryInitComparison.
Proof.
intros Hprogress Hrevision Hdebug Hhash Henv Hcreation Hsupply Hchild Hself Hgas Hinitial.
apply (@setcode_run_preserves S.t F E cold warm access write reserve
(fun st ⇒ checkedMachine protected st ∨ badMemoryInitComparison) state Hrevision Hprogress).
- intros before Hbound [Hbefore|Hbad]; [|right; exact Hbad].
apply dispatch_preserves_uncreated_checked; try assumption.
apply selected_opcode_word_parameters.
- exact Hgas.
- left; exact Hinitial.
Qed.
End Concrete.
Print Assumptions run_preserves_uncreated_checked.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vmsupplywords QedkConcrete.memorymodel.
Require Import QedkConcrete.nondebitframe QedkConcrete.vmcallprotected
QedkConcrete.vmcreateprotected QedkConcrete.vmcallframe QedkConcrete.creationassembly
QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.vmboundedframe
QedkConcrete.vmchecked QedkConcrete.vmcallchecked QedkConcrete.vmmachinewords
QedkConcrete.vmmemorywords QedkConcrete.vmsizes QedkConcrete.vmrun
QedkConcrete.vmenvironmentwords QedkConcrete.vmdispatchwords
QedkConcrete.vmopcodeframe QedkConcrete.contexthostinstance
QedkConcrete.hostframe QedkConcrete.supply QedkConcrete.worldaccounts
QedkConcrete.wordrange QedkConcrete.hashwords QedkConcrete.vminvariant.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.nondebithosttracking
SetcodeConcrete.vmsupply SetcodeConcrete.vmcontextframe SetcodeConcrete.vmframes
SetcodeConcrete.vmprogress SetcodeConcrete.executiontracking SetcodeConcrete.creationtracking
SetcodeConcrete.emptyaccount SetcodeConcrete.accountframe SetcodeConcrete.host
SetcodeConcrete.adoptionsupply SetcodeConcrete.vmemptyframe.
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.
Lemma primitive_frame_at_conj {Host_t : Set} (host : Evmc.HOST (t := Host_t)) P Q 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; unfold preservesCallback in *;
intros; split; intuition eauto.
Qed.
Section Concrete.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation contextInvariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).
Local Abbreviation floor := (@uncreatedBalanceFloor).
Context (query : Chain.Ethereum.Address.t) (minimum : Z).
Local Abbreviation protected := (fun s ⇒ contextInvariant s ∧ floor query minimum s).
Local Abbreviation stillUncreated := (fun s ⇒ contextInvariant s ∧
(floor query minimum s ∧ ¬ List.In query (S.t.accounts_created_in_current_transaction s))).
Lemma protected_primitives self : self ≠ query →
PrimitiveHostFrameAt (HostF vm) protected self.
Proof.
intro Hself. apply primitive_frame_at_conj.
- apply primitive_frame_at, supply_context_primitives.
- apply concrete_primitives_preserve_uncreated_balance. exact Hself.
Qed.
Lemma still_uncreated_primitives self : self ≠ query →
PrimitiveHostFrameAt (HostF vm) stillUncreated self.
Proof.
intro Hself. apply primitive_frame_at_conj.
- apply primitive_frame_at, supply_context_primitives.
- apply concrete_primitives_preserve_still_uncreated_balance. exact Hself.
Qed.
Context (cold warm access write reserve : N).
Context {E : @V.Executor.FArgs S.t F}.
Local Abbreviation executeOpcode := (@dispatch S.t F E).
Lemma adoption_preserves_other_floor current source static gas :
current ≠ query →
preservesCallback (floor query minimum) ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof.
intros Hneq initial [Hbalance Haccount].
pose proof (@setcodefrom_preserves_supply
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas initial Hbalance) as [Hafter Htotal].
split; [exact Hafter|].
destruct Haccount as [Hcreated|[Hempty Hfloor]].
- left. exact (@setcode_preserves_tracking (S.t.accounts_created_in_current_transaction initial)
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas initial (fun a H ⇒ H) query Hcreated).
- pose proof (@setcode_other_account_frame
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
query (fun code _ ⇒ code = Byte_string.Bytes.empty) current source static gas
Hneq initial (conj (proj1 Hbalance) (or_intror Hempty))) as [_ Hcode].
destruct Hcode as [Hcreated|HemptyAfter]; [left; exact Hcreated|].
right. split; [exact HemptyAfter|].
pose proof (@setcodefrom_preserves_balances
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas initial query (proj1 Hbalance)) as Hsame.
exact (eq_rect _ (fun value ⇒ minimum ≤ U.to_z value) Hfloor _ (eq_sym Hsame)).
Qed.
Lemma instruction_retains_creation opcode state :
params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
params.(Vm.Make_Params_signature.debug_tstore) = false →
(∀ xs, trackingHost xs (HostF vm)) →
Z.of_N (MS.t.gas state) < 2^63 →
creationsRetained (S.t.accounts_created_in_current_transaction (MS.t.host state))
(MS.t.host (snd (executeOpcode opcode state))).
Proof.
intros Hrevision Hdebug Htracking Hgas.
destruct (Htracking (S.t.accounts_created_in_current_transaction (MS.t.host state)))
as [Hprimitive [Hsetcode [Hprogress [Hsize [Hcopy [Hcontext Hcall]]]]]].
assert (Hframe : preservesBoundedHost
(creationsRetained (S.t.accounts_created_in_current_transaction (MS.t.host state)))
(executeOpcode opcode)).
{ destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
- apply recursive_opcode_preserves; [exact Hprimitive|exact Hcall|exact Hrecursive].
- apply bounded_from_unbounded. destruct (machineOnlyOpcode opcode) eqn:Hlocal.
+ eapply machine_only_preserves_host_core; [| |exact Hdebug|exact Hlocal].
× eapply setcode_clz_preserves_host; exact Hrevision.
× intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
-- eapply setcode_opcode_preserves_host; eassumption.
-- unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
+ apply primitive_host_preserves_at_any_revision; try assumption.
× apply primitive_frame_at; exact Hprimitive.
× destruct opcode; cbn [recursiveCallOpcode machineOnlyOpcode primitiveHostOpcode] in *; congruence. }
exact (Hframe state Hgas (fun a H ⇒ H)).
Qed.
Lemma dispatch_preserves_uncreated_checked opcode state :
params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
params.(Vm.Make_Params_signature.debug_tstore) = false →
keccak256OutputSize →
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
opcodeWordParameters opcode →
(∀ xs, trackingHost xs (HostF vm)) →
(∀ msg, @vmMessageBounds msg → ∀ host, contextInvariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
(∀ msg, @vmMessageBounds msg →
childCallFrom (@V.Executor.self S.t F E) 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)) →
∀ host, protected host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
@V.Executor.self S.t F E ≠ query →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine protected state →
checkedMachine protected (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
intros Hrevision Hdebug Hhash Henv Hparams Hcreation Hsupply Hchild Hself Hgas Hinitial.
assert (Hbounded : BoundedMemory F) by (eapply setcode_bounded_memory; exact Hrevision).
assert (Hcontext : checkedMachine contextInvariant state).
{ eapply checked_machine_weaken with (pre := protected); [intros h Hh; exact (proj1 Hh)|exact Hinitial]. }
destruct (dispatch_supply_context vm params limit cold warm access write reserve Hrevision Hdebug Hhash (E := E)
opcode state
Henv Hparams Hsupply Hgas Hcontext) as [Hafter|Hbad];
[|right; exact Hbad].
pose proof (proj2 (proj1 (proj2 (proj2 (proj1 Hinitial))))) as Hfloor.
destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction (MS.t.host state))) as [Hcreated|Habsent].
- pose proof (instruction_retains_creation opcode state Hrevision Hdebug Hcreation Hgas) as Htracking.
left. unfold checkedMachine, machineWords in Hafter |- ×.
destruct Hafter as [[Hstack [Hpc [Hhost Hmemory]]] Hsizes].
split; [|exact Hsizes]. split; [exact Hstack|]. split; [exact Hpc|]. split; [|exact Hmemory].
split; [exact Hhost|]. split; [exact (proj1 (proj1 (proj1 Hhost)))|].
left. exact (Htracking query Hcreated).
- destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
+ assert (Hvalue : wordRange (Env.t.value
((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)))).
{ exact (List.Forall_inv (List.Forall_inv_tail (proj1 Henv))). }
assert (Hpre : checkedMachine stillUncreated state).
{ unfold checkedMachine, machineWords in Hinitial |- ×. tauto. }
assert (Hempty : ∀ host, stillUncreated host →
(HostF vm).(Evmc.HOST.copy_code) query 0 (@Delegation.eoa_delegated_code_length) host =
(Byte_string.Bytes.empty, host)).
{ intros host [_ [Hprotected Hnot]]. apply concrete_copy_empty_code.
exact (proj2 (uncreated_balance_floor_not_created query minimum host Hprotected Hnot)). }
assert (Hweaken : ∀ host, stillUncreated host → protected host) by (intros; tauto).
assert (Hprimitive : PrimitiveHostFrameAt (HostF vm) protected (@V.Executor.self S.t F E))
by (apply protected_primitives; exact Hself).
assert (HprimitivePre : PrimitiveHostFrameAt (HostF vm) stillUncreated (@V.Executor.self S.t F E))
by (apply still_uncreated_primitives; exact Hself).
assert (Hlift : ∀ action : @V.M.t S.t F bool,
boundedCheckedSafe protected (fun _ ⇒ True) action →
checkedMachine protected (snd (action state)) ∨ badMemoryInitComparison).
{ intros action Hsafe. destruct (Hsafe state Hgas Hinitial) as [[H _]|Hbad];
[left; exact H|right; exact Hbad]. }
assert (HliftPre : ∀ action : @V.M.t S.t F bool,
(∀ before, Z.of_N (MS.t.gas before) < 2^63 → checkedMachine stillUncreated before →
(checkedMachine protected (snd (action before)) ∧
match fst (action before) with inl _ ⇒ True | inr _ ⇒ True end) ∨ badMemoryInitComparison) →
checkedMachine protected (snd (action state)) ∨ badMemoryInitComparison).
{ intros action Hsafe. destruct (Hsafe state Hgas Hpre) as [[H _]|Hbad];
[left; exact H|right; exact Hbad]. }
destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate.
all: cbn [dispatch V.Executor.execute_opcode].
× apply Hlift, create_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|tauto].
× apply HliftPre. apply call_checked_empty_recipient_bounded with (query := query); try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Horigin Hcode. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exact (proj1 (Hcode Heq)).
× apply Hlift, callcode_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exfalso. unfold childCallFrom in Horigin. rewrite Hkind in Horigin.
apply Hself. destruct Horigin as [_ Hrecipient]. congruence.
× apply Hlift, delegatecall_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exfalso. unfold childCallFrom in Horigin. rewrite Hkind in Horigin.
apply Hself. congruence.
× apply Hlift, create2_checked_from_self_bounded; try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Hkind Horigin. apply Hchild; [exact Hbounds|exact Horigin|tauto].
× apply HliftPre. apply staticcall_checked_empty_recipient_bounded with (query := query); try assumption;
try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
intros msg Hbounds Horigin Hcode. apply Hchild; [exact Hbounds|exact Horigin|].
right; right. intro Heq. exact (proj1 (Hcode Heq)).
+ assert (Hframe : preservesHost (floor query minimum) (executeOpcode opcode)).
{ destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_host_core; [| |exact Hdebug|exact Hlocal].
+ eapply setcode_clz_preserves_host; exact Hrevision.
+ intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
× eapply setcode_opcode_preserves_at_context; [exact Hrevision|].
intros; apply adoption_preserves_other_floor; exact Hself.
× unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
- apply primitive_host_preserves_at_any_revision; try assumption.
+ apply concrete_primitives_preserve_uncreated_balance. exact Hself.
+ destruct opcode; cbn [recursiveCallOpcode machineOnlyOpcode primitiveHostOpcode] in *;
congruence. }
specialize (Hframe state Hfloor). left.
unfold checkedMachine, machineWords in ×. tauto.
Qed.
Lemma run_preserves_uncreated_checked
state :
@setcodeGasProgress S.t F →
params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
params.(Vm.Make_Params_signature.debug_tstore) = false →
keccak256OutputSize →
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
(∀ xs, trackingHost xs (HostF vm)) →
(∀ msg, @vmMessageBounds msg → ∀ host, contextInvariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
(∀ msg, @vmMessageBounds msg →
childCallFrom (@V.Executor.self S.t F E) 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)) →
∀ host, protected host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(protected (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
@V.Executor.self S.t F E ≠ query →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine protected state →
checkedMachine protected (snd (@generated_run S.t F E state)) ∨ badMemoryInitComparison.
Proof.
intros Hprogress Hrevision Hdebug Hhash Henv Hcreation Hsupply Hchild Hself Hgas Hinitial.
apply (@setcode_run_preserves S.t F E cold warm access write reserve
(fun st ⇒ checkedMachine protected st ∨ badMemoryInitComparison) state Hrevision Hprogress).
- intros before Hbound [Hbefore|Hbad]; [|right; exact Hbad].
apply dispatch_preserves_uncreated_checked; try assumption.
apply selected_opcode_word_parameters.
- exact Hgas.
- left; exact Hinitial.
Qed.
End Concrete.
Print Assumptions run_preserves_uncreated_checked.