Library QedkConcrete.nondebithostchecked
Guarded balance preservation with concrete VM-entry bounds
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.nondebitframe QedkConcrete.nondebitcreate QedkConcrete.creationhost
QedkConcrete.creationassembly QedkConcrete.hostcode QedkConcrete.assemblychecked
QedkConcrete.precompilesframe QedkConcrete.reserveprecompile
QedkConcrete.contexthostinstance QedkConcrete.vmhostframe
QedkConcrete.supplyhostinstance QedkConcrete.hostsupply QedkConcrete.supply
QedkConcrete.hostframe QedkConcrete.vmcallframe.
Require Import QedkConcrete.nondebithost QedkConcrete.nondebitcreatechecked
QedkConcrete.vmcallchecked QedkConcrete.hostchecked QedkConcrete.hostcallchecked
QedkConcrete.hostaccountformat QedkConcrete.hostcomposition QedkConcrete.vmsupplywords.
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.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation frame := (@creationFrame).
Local Abbreviation preserves := preservesCreation.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation floor := (@uncreatedBalanceFloor).
Context (limit : Z).
Local Abbreviation invariant := (hostSupplyContext F.(Host.Instantiate.ChainParams) limit).
Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.nondebitframe QedkConcrete.nondebitcreate QedkConcrete.creationhost
QedkConcrete.creationassembly QedkConcrete.hostcode QedkConcrete.assemblychecked
QedkConcrete.precompilesframe QedkConcrete.reserveprecompile
QedkConcrete.contexthostinstance QedkConcrete.vmhostframe
QedkConcrete.supplyhostinstance QedkConcrete.hostsupply QedkConcrete.supply
QedkConcrete.hostframe QedkConcrete.vmcallframe.
Require Import QedkConcrete.nondebithost QedkConcrete.nondebitcreatechecked
QedkConcrete.vmcallchecked QedkConcrete.hostchecked QedkConcrete.hostcallchecked
QedkConcrete.hostaccountformat QedkConcrete.hostcomposition QedkConcrete.vmsupplywords.
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.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation frame := (@creationFrame).
Local Abbreviation preserves := preservesCreation.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation floor := (@uncreatedBalanceFloor).
Context (limit : Z).
Local Abbreviation invariant := (hostSupplyContext F.(Host.Instantiate.ChainParams) limit).
Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).
If the recipient is the protected address, its code is empty unless it
was recorded as created. This also rules out delegated-code resolution
at that address on the external transaction path.
Lemma process_call_preserves_uncreated_balance_bounded from_tx self query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
childCallFrom self msg → self ≠ query →
@vmMessageBounds msg →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code, OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd (processCall from_tx msg s)).
Proof.
intros Hneq Hrevision Horigin Hdifferent Hmsg HcodeAddress Hvm s Hcontext Hinitial.
pose proof (proj1 (proj1 (proj2 Hmsg))) as Hvalue.
pose proof (child_transfer_preserves_uncreated_balance vm self query minimum msg
Horigin Hdifferent Hvalue s Hinitial) as Htransfer.
assert (HtransferContext : invariant (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ eapply host_supply_context_frame.
- destruct (H.should_transfer msg); [apply supply_transfer; exact Hvalue|apply supply_return].
- destruct (H.should_transfer msg); [intro; apply transfer_preserves_format|apply format_return].
- destruct (H.should_transfer msg); [apply transfer_preserves_context|apply context_return].
- exact Hcontext. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile
uncreatedBalanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : floor query minimum (snd run)) by exact Htransfer;
assert (HmiddleContext : invariant (snd run)) by exact HtransferContext;
destruct run as [ok middle] eqn:Hrun
end.
cbn beta iota zeta delta [snd] in Hmiddle, HmiddleContext.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address
uncreatedBalanceFloor].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile uncreatedBalanceFloor].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply native_preserves_uncreated_balance; eassumption).
all: apply Hvm; [|exact HmiddleContext|exact Hmiddle|].
all: try (match goal with |- OCamlString.length ?bytes < _ ⇒
lazymatch bytes with context [@W.account _ ?addr] ⇒
pose proof (@read_account_format (S.t.world_state middle) addr
(proj1 (proj2 (proj1 HmiddleContext)))) as [_ Hsize];
match type of Hsize with ?len < _ ⇒ change (len < 2^256) end; lia end end).
all: intro Hrecipient;
destruct Hmiddle as [Hinv [Hcreated|[Hempty Hfloor]]]; [right; exact Hcreated|].
all: left; first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
childCallFrom self msg → self ≠ query →
@vmMessageBounds msg →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code, OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd (processCall from_tx msg s)).
Proof.
intros Hneq Hrevision Horigin Hdifferent Hmsg HcodeAddress Hvm s Hcontext Hinitial.
pose proof (proj1 (proj1 (proj2 Hmsg))) as Hvalue.
pose proof (child_transfer_preserves_uncreated_balance vm self query minimum msg
Horigin Hdifferent Hvalue s Hinitial) as Htransfer.
assert (HtransferContext : invariant (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ eapply host_supply_context_frame.
- destruct (H.should_transfer msg); [apply supply_transfer; exact Hvalue|apply supply_return].
- destruct (H.should_transfer msg); [intro; apply transfer_preserves_format|apply format_return].
- destruct (H.should_transfer msg); [apply transfer_preserves_context|apply context_return].
- exact Hcontext. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile
uncreatedBalanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : floor query minimum (snd run)) by exact Htransfer;
assert (HmiddleContext : invariant (snd run)) by exact HtransferContext;
destruct run as [ok middle] eqn:Hrun
end.
cbn beta iota zeta delta [snd] in Hmiddle, HmiddleContext.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address
uncreatedBalanceFloor].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile uncreatedBalanceFloor].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply native_preserves_uncreated_balance; eassumption).
all: apply Hvm; [|exact HmiddleContext|exact Hmiddle|].
all: try (match goal with |- OCamlString.length ?bytes < _ ⇒
lazymatch bytes with context [@W.account _ ?addr] ⇒
pose proof (@read_account_format (S.t.world_state middle) addr
(proj1 (proj2 (proj1 HmiddleContext)))) as [_ Hsize];
match type of Hsize with ?len < _ ⇒ change (len < 2^256) end; lia end end).
all: intro Hrecipient;
destruct Hmiddle as [Hinv [Hcreated|[Hempty Hfloor]]]; [right; exact Hcreated|].
all: left; first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
The enclosing Host callback also covers failure: rollback restores the
input balance, while success keeps the bound proved for process_call.
Lemma noncreation_call_preserves_uncreated_balance_bounded self query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create →
Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create2 →
childCallFrom self msg → self ≠ query →
@vmMessageBounds msg →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code, OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
intros Hneq Hrevision Hcreate Hcreate2 Horigin Hdifferent Hmsg HcodeAddress Hvm s Hcontext Hinitial.
pose proof (process_call_preserves_uncreated_balance_bounded None self query minimum msg
Hneq Hrevision Horigin Hdifferent Hmsg HcodeAddress Hvm s Hcontext Hinitial) as Hcore.
cbn -[H.call uncreatedBalanceFloor]. unfold H.call, H.call_impl.
Local Opaque H.process_call uncreatedBalanceFloor Addr.of_contract_creation W.account.
destruct (Evmc.Message.t.kind msg); try contradiction; cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hafter : floor query minimum (snd run)) by exact Hcore;
destruct run as [result after]
end.
all: cbn beta iota zeta delta [snd] in Hafter.
all: destruct result as [status gasleft refund output address]; destruct status;
cbn; first [exact Hafter|exact Hinitial].
Qed.
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create →
Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create2 →
childCallFrom self msg → self ≠ query →
@vmMessageBounds msg →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code, OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
intros Hneq Hrevision Hcreate Hcreate2 Horigin Hdifferent Hmsg HcodeAddress Hvm s Hcontext Hinitial.
pose proof (process_call_preserves_uncreated_balance_bounded None self query minimum msg
Hneq Hrevision Horigin Hdifferent Hmsg HcodeAddress Hvm s Hcontext Hinitial) as Hcore.
cbn -[H.call uncreatedBalanceFloor]. unfold H.call, H.call_impl.
Local Opaque H.process_call uncreatedBalanceFloor Addr.of_contract_creation W.account.
destruct (Evmc.Message.t.kind msg); try contradiction; cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hafter : floor query minimum (snd run)) by exact Hcore;
destruct run as [result after]
end.
all: cbn beta iota zeta delta [snd] in Hafter.
all: destruct result as [status gasleft refund output address]; destruct status;
cbn; first [exact Hafter|exact Hinitial].
Qed.
Warming a creation address and retaining the creator's nonce on failure
do not spend the unrelated account's balance. Initcode may run at the
protected address only after CREATE has recorded that address.
Lemma creation_call_impl_preserves_uncreated_balance_bounded query minimum msg :
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@vmMessageBounds msg →
Evmc.Message.t.sender msg ≠ query →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd (callImpl H.Evm_call msg s)).
Proof.
intros Hkind Hcreation Hvm Hmsg Hsender s Hcontext Hinitial.
pose proof (process_create_preserves_uncreated_balance_bounded (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
limit query minimum msg Hcreation Hvm Hmsg Hsender) as Hcreate.
unfold H.call_impl.
Local Opaque H.process_create Addr.of_contract_creation uncreatedBalanceFloor W.account.
destruct (Evmc.Message.t.kind msg); try (destruct Hkind; discriminate); cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hafter : floor query minimum (snd run)) by (apply Hcreate; assumption);
destruct run as [result after]
end.
all: cbn beta iota zeta delta [snd] in Hafter.
all: destruct result as [status gasleft refund output address]; destruct status;
cbn; first [exact Hafter|exact Hinitial].
Qed.
Lemma creation_call_preserves_uncreated_balance_bounded query minimum msg :
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@vmMessageBounds msg →
Evmc.Message.t.sender msg ≠ query →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
intros Hkind Hcreation Hvm Hmsg Hsender s Hcontext Hinitial.
pose proof (creation_call_impl_preserves_uncreated_balance_bounded query minimum msg
Hkind Hcreation Hvm Hmsg Hsender) as Himpl.
pose proof (increment_nonce_preserves_uncreated_balance
(F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query minimum (Evmc.Message.t.sender msg) s Hinitial) as Hnonce.
assert (HnonceContext : invariant (snd (@H.try_increment_nonce
(H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) (Evmc.Message.t.sender msg) s))).
{ eapply host_supply_context_frame; [| | |exact Hcontext].
- unfold H.try_increment_nonce, H.M. solve_supply_frame.
- unfold H.try_increment_nonce, H.M. solve_format.
- unfold H.try_increment_nonce, H.M. solve_context_frame. }
cbn -[H.call uncreatedBalanceFloor]. unfold H.call.
destruct Hkind as [Hkind|Hkind]; rewrite Hkind.
all: cbn -[H.try_increment_nonce H.call_impl uncreatedBalanceFloor].
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : floor query minimum (snd run)) by exact Hnonce;
destruct run as [ok middle]
end.
all: cbn beta iota zeta delta [snd] in Hmiddle, HnonceContext.
all: destruct ok; [apply Himpl; assumption|exact Hmiddle].
Qed.
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@vmMessageBounds msg →
Evmc.Message.t.sender msg ≠ query →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd (callImpl H.Evm_call msg s)).
Proof.
intros Hkind Hcreation Hvm Hmsg Hsender s Hcontext Hinitial.
pose proof (process_create_preserves_uncreated_balance_bounded (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
limit query minimum msg Hcreation Hvm Hmsg Hsender) as Hcreate.
unfold H.call_impl.
Local Opaque H.process_create Addr.of_contract_creation uncreatedBalanceFloor W.account.
destruct (Evmc.Message.t.kind msg); try (destruct Hkind; discriminate); cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hafter : floor query minimum (snd run)) by (apply Hcreate; assumption);
destruct run as [result after]
end.
all: cbn beta iota zeta delta [snd] in Hafter.
all: destruct result as [status gasleft refund output address]; destruct status;
cbn; first [exact Hafter|exact Hinitial].
Qed.
Lemma creation_call_preserves_uncreated_balance_bounded query minimum msg :
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@vmMessageBounds msg →
Evmc.Message.t.sender msg ≠ query →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
intros Hkind Hcreation Hvm Hmsg Hsender s Hcontext Hinitial.
pose proof (creation_call_impl_preserves_uncreated_balance_bounded query minimum msg
Hkind Hcreation Hvm Hmsg Hsender) as Himpl.
pose proof (increment_nonce_preserves_uncreated_balance
(F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query minimum (Evmc.Message.t.sender msg) s Hinitial) as Hnonce.
assert (HnonceContext : invariant (snd (@H.try_increment_nonce
(H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) (Evmc.Message.t.sender msg) s))).
{ eapply host_supply_context_frame; [| | |exact Hcontext].
- unfold H.try_increment_nonce, H.M. solve_supply_frame.
- unfold H.try_increment_nonce, H.M. solve_format.
- unfold H.try_increment_nonce, H.M. solve_context_frame. }
cbn -[H.call uncreatedBalanceFloor]. unfold H.call.
destruct Hkind as [Hkind|Hkind]; rewrite Hkind.
all: cbn -[H.try_increment_nonce H.call_impl uncreatedBalanceFloor].
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : floor query minimum (snd run)) by exact Hnonce;
destruct run as [ok middle]
end.
all: cbn beta iota zeta delta [snd] in Hmiddle, HnonceContext.
all: destruct ok; [apply Himpl; assumption|exact Hmiddle].
Qed.
All message kinds of the concrete Host constructor. The remaining VM
premise is restricted to the actual execution context: it need not
protect an address against arbitrary code executed at that address.
Theorem call_preserves_uncreated_balance_bounded self query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@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)) →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
intros Hneq Hrevision Hcreation Hvm Hmsg Hdifferent Horigin Htarget.
destruct (Evmc.Message.t.kind msg) eqn:Hkind.
all: first [eapply noncreation_call_preserves_uncreated_balance_bounded with (self := self);
[exact Hneq|exact Hrevision|rewrite Hkind; discriminate|rewrite Hkind; discriminate|
exact Horigin|exact Hdifferent|exact Hmsg|
destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate; exact Htarget|
intros code Hcode s Hcontext Hfloor Hallowed; apply Hvm; assumption] |
apply creation_call_preserves_uncreated_balance_bounded;
[first [left; exact Hkind|right; exact Hkind]|exact Hcreation|exact Hvm|
exact Hmsg|unfold childCallFrom in Horigin; rewrite Hkind in Horigin;
rewrite Horigin; exact Hdifferent]].
Qed.
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@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)) →
∀ s, invariant s → floor query minimum s →
floor query minimum (snd ((HostF vm).(Evmc.HOST.call) msg s)).
Proof.
intros Hneq Hrevision Hcreation Hvm Hmsg Hdifferent Horigin Htarget.
destruct (Evmc.Message.t.kind msg) eqn:Hkind.
all: first [eapply noncreation_call_preserves_uncreated_balance_bounded with (self := self);
[exact Hneq|exact Hrevision|rewrite Hkind; discriminate|rewrite Hkind; discriminate|
exact Horigin|exact Hdifferent|exact Hmsg|
destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate; exact Htarget|
intros code Hcode s Hcontext Hfloor Hallowed; apply Hvm; assumption] |
apply creation_call_preserves_uncreated_balance_bounded;
[first [left; exact Hkind|right; exact Hkind]|exact Hcreation|exact Hvm|
exact Hmsg|unfold childCallFrom in Horigin; rewrite Hkind in Horigin;
rewrite Horigin; exact Hdifferent]].
Qed.
The transaction entry additionally resolves delegation and may revert on
the final reserve check. Both are covered here, without treating a failed
reserve check as a premise about the VM's balances.
Theorem call_impl_preserves_uncreated_balance_bounded from_tx self query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@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)) →
∀ s, invariant s → floor query minimum s →
floor query minimum
(snd (callImpl (match from_tx with Some tx ⇒ H.Eoa_tx tx | None ⇒ H.Evm_call end) msg s)).
Proof.
intros Hneq Hrevision Hcreation Hvm Hmsg Hself Horigin Htarget s Hcontext Hinitial.
pose proof (process_call_preserves_uncreated_balance_bounded from_tx self query minimum msg
Hneq Hrevision Horigin Hself Hmsg) as Hcall.
pose proof (process_create_preserves_uncreated_balance_bounded (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
limit query minimum msg Hcreation Hvm Hmsg) as Hcreate.
unfold H.call_impl.
Local Opaque H.process_call H.process_syscall H.process_create Addr.of_contract_creation
Reserve_balance.dipped_into_reserve uncreatedBalanceFloor W.account.
destruct (Evmc.Message.t.kind msg) eqn:Hkind; destruct from_tx as [tx|]; cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : floor query minimum (snd run)) by
(lazymatch type of Hkind with
| _ = Evmc.Message.CallKind.Create ⇒ apply Hcreate
| _ = Evmc.Message.CallKind.Create2 ⇒ apply Hcreate
| _ ⇒ eapply Hcall
end;
try exact Hcontext; try exact Hinitial;
try (unfold childCallFrom in Horigin; rewrite Hkind in Horigin;
rewrite Horigin; exact Hself);
try (destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate; exact Htarget);
try (intros code Hcode before Hbefore Hfloor Hallowed; apply Hvm; assumption));
destruct run as [result intermediate] eqn:Hrun
end.
all: cbn beta iota zeta delta [snd] in Hcore.
all: destruct result as [status gasleft refund output address].
all: destruct status;
cbn; unfold Monad.Make_Monad.when_; cbn;
try exact Hcore; try exact Hinitial.
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn; first [exact Hinitial|exact Hcore].
Qed.
Local Transparent H.process_call H.process_syscall H.process_create Addr.of_contract_creation
Reserve_balance.dipped_into_reserve uncreatedBalanceFloor W.account.
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@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)) →
∀ s, invariant s → floor query minimum s →
floor query minimum
(snd (callImpl (match from_tx with Some tx ⇒ H.Eoa_tx tx | None ⇒ H.Evm_call end) msg s)).
Proof.
intros Hneq Hrevision Hcreation Hvm Hmsg Hself Horigin Htarget s Hcontext Hinitial.
pose proof (process_call_preserves_uncreated_balance_bounded from_tx self query minimum msg
Hneq Hrevision Horigin Hself Hmsg) as Hcall.
pose proof (process_create_preserves_uncreated_balance_bounded (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
limit query minimum msg Hcreation Hvm Hmsg) as Hcreate.
unfold H.call_impl.
Local Opaque H.process_call H.process_syscall H.process_create Addr.of_contract_creation
Reserve_balance.dipped_into_reserve uncreatedBalanceFloor W.account.
destruct (Evmc.Message.t.kind msg) eqn:Hkind; destruct from_tx as [tx|]; cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : floor query minimum (snd run)) by
(lazymatch type of Hkind with
| _ = Evmc.Message.CallKind.Create ⇒ apply Hcreate
| _ = Evmc.Message.CallKind.Create2 ⇒ apply Hcreate
| _ ⇒ eapply Hcall
end;
try exact Hcontext; try exact Hinitial;
try (unfold childCallFrom in Horigin; rewrite Hkind in Horigin;
rewrite Horigin; exact Hself);
try (destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate; exact Htarget);
try (intros code Hcode before Hbefore Hfloor Hallowed; apply Hvm; assumption));
destruct run as [result intermediate] eqn:Hrun
end.
all: cbn beta iota zeta delta [snd] in Hcore.
all: destruct result as [status gasleft refund output address].
all: destruct status;
cbn; unfold Monad.Make_Monad.when_; cbn;
try exact Hcore; try exact Hinitial.
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn; first [exact Hinitial|exact Hcore].
Qed.
Local Transparent H.process_call H.process_syscall H.process_create Addr.of_contract_creation
Reserve_balance.dipped_into_reserve uncreatedBalanceFloor W.account.
The root message may spend its explicit value. Recursive calls retain
the resulting floor; rollback restores the larger input balance.
Lemma process_call_value_floor from_tx query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
@vmMessageBounds msg →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code, OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
∀ s, invariant s → floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) s →
floor query minimum (snd (processCall from_tx msg s)).
Proof.
intros Hneq Hrevision Hmsg HcodeAddress Hvm s Hcontext Hinitial.
pose proof (proj1 (proj1 (proj2 Hmsg))) as Hvalue.
assert (Htransfer : floor query minimum (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ destruct (H.should_transfer msg).
- apply transfer_value_floor; assumption.
- cbn [snd]. eapply uncreated_floor_weaken; [|exact Hinitial]; lia. }
assert (HtransferContext : invariant (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ eapply host_supply_context_frame.
- destruct (H.should_transfer msg); [apply supply_transfer; exact Hvalue|apply supply_return].
- destruct (H.should_transfer msg); [intro; apply transfer_preserves_format|apply format_return].
- destruct (H.should_transfer msg); [apply transfer_preserves_context|apply context_return].
- exact Hcontext. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile
uncreatedBalanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : floor query minimum (snd run)) by exact Htransfer;
assert (HmiddleContext : invariant (snd run)) by exact HtransferContext;
destruct run as [ok middle] eqn:Hrun
end.
cbn beta iota zeta delta [snd] in Hmiddle, HmiddleContext.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address
uncreatedBalanceFloor].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile uncreatedBalanceFloor].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply native_preserves_uncreated_balance; eassumption).
all: apply Hvm; [|exact HmiddleContext|exact Hmiddle|].
all: try (match goal with |- OCamlString.length ?bytes < _ ⇒
lazymatch bytes with context [@W.account _ ?addr] ⇒
pose proof (@read_account_format (S.t.world_state middle) addr
(proj1 (proj2 (proj1 HmiddleContext)))) as [_ Hsize];
match type of Hsize with ?len < _ ⇒ change (len < 2^256) end; lia end end).
all: intro Hrecipient;
destruct Hmiddle as [Hinv [Hcreated|[Hempty Hfloor]]]; [right; exact Hcreated|].
all: left; first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
Theorem call_impl_value_floor from_tx query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@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)) →
∀ s, invariant s → floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) s →
floor query minimum
(snd (callImpl (match from_tx with Some tx ⇒ H.Eoa_tx tx | None ⇒ H.Evm_call end) msg s)).
Proof.
intros Hneq Hrevision Hcreation Hvm Hmsg Htarget s Hcontext Hinitial.
assert (Hlow : floor query minimum s).
{ eapply uncreated_floor_weaken; [|exact Hinitial]. pose proof (proj1 (proj1 (proj2 Hmsg))); lia. }
pose proof (process_call_value_floor from_tx query minimum msg
Hneq Hrevision Hmsg) as Hcall.
pose proof (process_create_value_floor (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
limit query minimum msg Hcreation Hvm Hmsg) as Hcreate.
unfold H.call_impl.
Local Opaque H.process_call H.process_syscall H.process_create Addr.of_contract_creation
Reserve_balance.dipped_into_reserve uncreatedBalanceFloor W.account.
destruct (Evmc.Message.t.kind msg) eqn:Hkind; destruct from_tx as [tx|]; cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : floor query minimum (snd run)) by
(lazymatch type of Hkind with
| _ = Evmc.Message.CallKind.Create ⇒ apply Hcreate
| _ = Evmc.Message.CallKind.Create2 ⇒ apply Hcreate
| _ ⇒ eapply Hcall
end;
try exact Hcontext; try exact Hinitial;
try (destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate; exact Htarget);
try (intros code Hcode before Hbefore Hfloor Hallowed; apply Hvm; assumption));
destruct run as [result intermediate] eqn:Hrun
end.
all: cbn beta iota zeta delta [snd] in Hcore.
all: destruct result as [status gasleft refund output address].
all: destruct status;
cbn; unfold Monad.Make_Monad.when_; cbn;
try exact Hcore; try exact Hlow.
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn; first [exact Hlow|exact Hcore].
Qed.
End Host.
Print Assumptions call_preserves_uncreated_balance_bounded.
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
@vmMessageBounds msg →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code, OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
∀ s, invariant s → floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) s →
floor query minimum (snd (processCall from_tx msg s)).
Proof.
intros Hneq Hrevision Hmsg HcodeAddress Hvm s Hcontext Hinitial.
pose proof (proj1 (proj1 (proj2 Hmsg))) as Hvalue.
assert (Htransfer : floor query minimum (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ destruct (H.should_transfer msg).
- apply transfer_value_floor; assumption.
- cbn [snd]. eapply uncreated_floor_weaken; [|exact Hinitial]; lia. }
assert (HtransferContext : invariant (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ eapply host_supply_context_frame.
- destruct (H.should_transfer msg); [apply supply_transfer; exact Hvalue|apply supply_return].
- destruct (H.should_transfer msg); [intro; apply transfer_preserves_format|apply format_return].
- destruct (H.should_transfer msg); [apply transfer_preserves_context|apply context_return].
- exact Hcontext. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile
uncreatedBalanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : floor query minimum (snd run)) by exact Htransfer;
assert (HmiddleContext : invariant (snd run)) by exact HtransferContext;
destruct run as [ok middle] eqn:Hrun
end.
cbn beta iota zeta delta [snd] in Hmiddle, HmiddleContext.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address
uncreatedBalanceFloor].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile uncreatedBalanceFloor].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply native_preserves_uncreated_balance; eassumption).
all: apply Hvm; [|exact HmiddleContext|exact Hmiddle|].
all: try (match goal with |- OCamlString.length ?bytes < _ ⇒
lazymatch bytes with context [@W.account _ ?addr] ⇒
pose proof (@read_account_format (S.t.world_state middle) addr
(proj1 (proj2 (proj1 HmiddleContext)))) as [_ Hsize];
match type of Hsize with ?len < _ ⇒ change (len < 2^256) end; lia end end).
all: intro Hrecipient;
destruct Hmiddle as [Hinv [Hcreated|[Hempty Hfloor]]]; [right; exact Hcreated|].
all: left; first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
Theorem call_impl_value_floor from_tx query minimum msg :
MonadVM.Staking.staking_address ≠ query →
F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
∀ s, invariant s → floor query minimum s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
floor query minimum (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
@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)) →
∀ s, invariant s → floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) s →
floor query minimum
(snd (callImpl (match from_tx with Some tx ⇒ H.Eoa_tx tx | None ⇒ H.Evm_call end) msg s)).
Proof.
intros Hneq Hrevision Hcreation Hvm Hmsg Htarget s Hcontext Hinitial.
assert (Hlow : floor query minimum s).
{ eapply uncreated_floor_weaken; [|exact Hinitial]. pose proof (proj1 (proj1 (proj2 Hmsg))); lia. }
pose proof (process_call_value_floor from_tx query minimum msg
Hneq Hrevision Hmsg) as Hcall.
pose proof (process_create_value_floor (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
limit query minimum msg Hcreation Hvm Hmsg) as Hcreate.
unfold H.call_impl.
Local Opaque H.process_call H.process_syscall H.process_create Addr.of_contract_creation
Reserve_balance.dipped_into_reserve uncreatedBalanceFloor W.account.
destruct (Evmc.Message.t.kind msg) eqn:Hkind; destruct from_tx as [tx|]; cbn.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : floor query minimum (snd run)) by
(lazymatch type of Hkind with
| _ = Evmc.Message.CallKind.Create ⇒ apply Hcreate
| _ = Evmc.Message.CallKind.Create2 ⇒ apply Hcreate
| _ ⇒ eapply Hcall
end;
try exact Hcontext; try exact Hinitial;
try (destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate; exact Htarget);
try (intros code Hcode before Hbefore Hfloor Hallowed; apply Hvm; assumption));
destruct run as [result intermediate] eqn:Hrun
end.
all: cbn beta iota zeta delta [snd] in Hcore.
all: destruct result as [status gasleft refund output address].
all: destruct status;
cbn; unfold Monad.Make_Monad.when_; cbn;
try exact Hcore; try exact Hlow.
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn; first [exact Hlow|exact Hcore].
Qed.
End Host.
Print Assumptions call_preserves_uncreated_balance_bounded.