Library QedkConcrete.executionnondebit

Nonsender balance protection in the generated recursive executor

Supply/representation bounds, creation tracking and the balance floor are proved simultaneously through the actual Host/VM module unrolling. No recursive VM balance contract remains in the resulting execution theorem. Creation at a protected signer must still be excluded cryptographically.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution MonadVM.Vm QedkConcrete.executionchecked
  QedkConcrete.executioncreation QedkConcrete.nondebitassembly
  QedkConcrete.vmnondebitentry QedkConcrete.creationassembly
  QedkConcrete.assemblychecked QedkConcrete.nondebitframe
  QedkConcrete.hostchecked QedkConcrete.nondebithostchecked QedkConcrete.vmcallframe
  QedkConcrete.hostframe QedkConcrete.hostcode
  QedkConcrete.contexthostinstance QedkConcrete.vmsupplywords
  QedkConcrete.vmcallchecked QedkConcrete.vmmemorywords QedkConcrete.hashwords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module E := Execution.Make.
Module V := Vm.Make.

Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).

Context (limit : Z).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).

Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation invariant := (hostSupplyContext args.(Host.Instantiate.ChainParams) limit).
Local Abbreviation floor := (@uncreatedBalanceFloor).

Lemma execution_vm_constructor_protected vm query minimum :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @checkedVm args limit vm → creationVm vm →
  @protectedVm args limit query minimum vm →
  @protectedVm args limit query minimum (VmF (HostF vm)).
Proof.
  intros Hneq Hrevision Hhash Hnative Hchecked Hcreation Hprotected
    msg code Hmsg Hcode initial Hinitial Hfloor Hallowed.
  cbn -[V.execute uncreatedBalanceFloor].
  match goal with |- context [snd ?run] ⇒
    assert (Hrun : ((invariant (snd run) ∧ floor query minimum (snd run)) ∧
      resultOutputBound (fst run)) ∨ badMemoryInitComparison)
  end.
  { eapply (@execute_preserves_uncreated_checked args vm _ limit);
      try solve [lazymatch goal with |- Basics.Unreachable _ ⇒
        apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end].
    - exact Hrevision.
    - reflexivity.
    - exact Hhash.
    - exact Hmsg.
    - exact Hcode.
    - intros child Hchild before Hbefore.
      exact (@host_constructor_checked args limit vm Hrevision Hnative Hchecked child Hchild before Hbefore).
    - apply host_constructor_preserves_creation; assumption.
    - intros Hself child Hchild Horigin Htarget before Hbefore.
      exact (@host_constructor_protected args limit query minimum vm
        (Evmc.Message.t.recipient msg) child Hneq Hrevision Hnative Hchecked Hcreation Hprotected
        Hchild Hself Horigin Htarget before Hbefore).
    - exact Hallowed.
    - split; assumption. }
  destruct Hrun as [[[_ Hpost] _]|Hbad]; [exact Hpost|].
  exfalso. exact (no_bad_memory_init_comparison Hbad).
Qed.

Theorem execution_vm_protected query minimum :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @protectedVm args limit query minimum
    (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof.
  intros Hneq Hrevision Hhash Hnative.
  apply (proj2 (proj2 (@recursive_assembly_protected args limit query minimum
    (fun vm ⇒ execution_vm_constructor_checked limit vm Hrevision Hhash Hnative)
    (fun vm Hcreation ⇒ execution_vm_constructor_preserves_creation (HostF vm)
      Hrevision (host_constructor_preserves_creation vm Hrevision Hcreation))
    (fun vm ⇒ execution_vm_constructor_protected vm query minimum Hneq Hrevision Hhash Hnative)))).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).

Theorem execution_call_from_eoa_protected tx self query minimum msg :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @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 (callFromEoa tx msg s)).
Proof.
  intros Hneq Hrevision Hhash Hnative Hmsg Hself Horigin Htarget s 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 args
    (@H.Vm (@Host.Instantiate.Host_fargs args)) limit (Some tx) self query minimum msg).
  - exact Hneq.
  - exact Hrevision.
  - intros nested code Hbound.
    pose proof (execution_vm_preserves_creation nested code Hrevision Hbound) as Hvm.
    cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
    exact Hvm.
  - exact (execution_vm_protected query minimum Hneq Hrevision Hhash Hnative).
  - exact Hmsg.
  - exact Hself.
  - exact Horigin.
  - exact Htarget.
  - exact Hcontext.
  - exact Hfloor.
Qed.

An unrelated empty-code account cannot lose balance through the actual transaction Host entry, including nested calls, native calls, creation, and reserve-check rollback. Excluding the account from the final creation set is essential: initcode may execute and selfdestruct without leaving runtime code. The supply invariant prevents incoming credits from wrapping.
Theorem execution_call_from_eoa_nondebit tx query msg initial :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @vmMessageBounds msg →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create) →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
    Evmc.Message.t.code_address msg = Evmc.Message.t.recipient msg) →
  Evmc.Message.t.sender msg ≠ query → invariant initial →
  A.t.code (State.op_pointcaretlparenrparen (S.t.world_state initial)
    (@W.account None query)) = Byte_string.Bytes.empty →
  let final := snd (callFromEoa tx msg initial) in
  ¬ List.In query (S.t.accounts_created_in_current_transaction final) →
  Numeric.U256.to_z (A.t.balance (State.op_pointcaretlparenrparen
    (S.t.world_state initial) (@W.account None query))) ≤
  Numeric.U256.to_z (A.t.balance (State.op_pointcaretlparenrparen
    (S.t.world_state final) (@W.account None query))).
Proof.
  intros Hneq Hrevision Hhash Hnative Hmsg Hkind HcodeAddress Hsender Hcontext Hempty
    final HnotCreated.
  pose (minimum := Numeric.U256.to_z (A.t.balance (State.op_pointcaretlparenrparen
    (S.t.world_state initial) (@W.account None query)))).
  assert (Hfloor : floor query minimum initial).
  { split; [exact (proj1 (proj1 (proj1 Hcontext)))|]. right. split; [exact Hempty|reflexivity]. }
  assert (Horigin : childCallFrom (Evmc.Message.t.sender msg) msg).
  { unfold childCallFrom. destruct Hkind as [Hkind|Hkind]; rewrite Hkind; reflexivity. }
  assert (Htarget : 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)).
  { destruct Hkind as [Hkind|Hkind]; [right; right; intro Hrecipient; rewrite (HcodeAddress Hkind); exact Hrecipient|left; exact Hkind]. }
  pose proof (execution_call_from_eoa_protected tx (Evmc.Message.t.sender msg) query minimum msg
    Hneq Hrevision Hhash Hnative Hmsg Hsender Horigin Htarget initial Hcontext Hfloor) as Hafter.
  destruct Hafter as [_ [Hcreated|[_ Hbalance]]]; [contradiction|exact Hbalance].
Qed.
An empty-code account loses at most the root message value. This is the sender bound needed by an emptying transaction, not a reserve floor. Nested VM behavior is discharged by the existing recursive proof.
Theorem execution_call_from_eoa_value_floor tx query minimum msg :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @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 (callFromEoa tx msg s)).
Proof.
  intros Hneq Hrevision Hhash Hnative Hmsg Htarget s 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 args
    (@H.Vm (@Host.Instantiate.Host_fargs args)) limit (Some tx) query minimum msg).
  - exact Hneq.
  - exact Hrevision.
  - intros nested code Hbound.
    pose proof (execution_vm_preserves_creation nested code Hrevision Hbound) as Hvm.
    cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
    exact Hvm.
  - exact (execution_vm_protected query minimum Hneq Hrevision Hhash Hnative).
  - exact Hmsg.
  - exact Htarget.
  - exact Hcontext.
  - exact Hfloor.
Qed.

Theorem execution_call_from_eoa_value_bound tx query msg initial :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @vmMessageBounds msg →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create) →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
    Evmc.Message.t.code_address msg = Evmc.Message.t.recipient msg) →
  invariant initial →
  A.t.code (State.op_pointcaretlparenrparen (S.t.world_state initial)
    (@W.account None query)) = Byte_string.Bytes.empty →
  let final := snd (callFromEoa tx msg initial) in
  ¬ List.In query (S.t.accounts_created_in_current_transaction final) →
  Numeric.U256.to_z (A.t.balance (State.op_pointcaretlparenrparen
    (S.t.world_state initial) (@W.account None query))) -
    Numeric.U256.to_z (Evmc.Message.t.value msg) ≤
  Numeric.U256.to_z (A.t.balance (State.op_pointcaretlparenrparen
    (S.t.world_state final) (@W.account None query))).
Proof.
  intros Hneq Hrevision Hhash Hnative Hmsg Hkind HcodeAddress Hcontext Hempty
    final HnotCreated.
  pose (minimum := Numeric.U256.to_z (A.t.balance (State.op_pointcaretlparenrparen
    (S.t.world_state initial) (@W.account None query))) -
    Numeric.U256.to_z (Evmc.Message.t.value msg)).
  assert (Hfloor : floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) initial).
  { split; [exact (proj1 (proj1 (proj1 Hcontext)))|]. right. split; [exact Hempty|unfold minimum; lia]. }
  assert (Htarget : 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)).
  { destruct Hkind as [Hkind|Hkind]; [right; right; intro Hrecipient; rewrite (HcodeAddress Hkind); exact Hrecipient|left; exact Hkind]. }
  pose proof (execution_call_from_eoa_value_floor tx query minimum msg
    Hneq Hrevision Hhash Hnative Hmsg Htarget initial Hcontext Hfloor) as Hafter.
  destruct Hafter as [_ [Hcreated|[_ Hbalance]]]; [contradiction|exact Hbalance].
Qed.
A system CALL can spend only its explicit root value from an empty-code account; in the block executor that value is zero. This entry point does not run the transaction reserve check, so the conclusion must follow from concrete VM behavior rather than from that check.
Theorem execution_host_call_value_floor query minimum msg :
  MonadVM.Staking.staking_address ≠ query →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @nativeOutputBounds args →
  @vmMessageBounds msg →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  (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 (@E.Host.call F
      msg s)).
Proof.
  intros Hneq Hrevision Hhash Hnative Hmsg Hkind Htarget s Hcontext Hfloor.
  unfold E.Host.call.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    H.functor H.Make_result.call H.call].
  rewrite Hkind.
  eapply (@call_impl_value_floor args
    (@H.Vm (@Host.Instantiate.Host_fargs args)) limit None query minimum msg).
  - exact Hneq.
  - exact Hrevision.
  - intros nested code Hbound.
    pose proof (execution_vm_preserves_creation nested code Hrevision Hbound) as Hvm.
    cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
    exact Hvm.
  - exact (execution_vm_protected query minimum Hneq Hrevision Hhash Hnative).
  - exact Hmsg.
  - right. right. exact Htarget.
  - exact Hcontext.
  - exact Hfloor.
Qed.
End Execution.

Print Assumptions execution_vm_protected.

Print Assumptions execution_call_from_eoa_nondebit.

Print Assumptions execution_call_from_eoa_value_bound.
Print Assumptions execution_host_call_value_floor.