Library SetcodeConcrete.systemnonce

System messages preserve protected nonces

The bytecode is checked to contain no CALL/CREATE. The proof executes the actual VM and Host wrappers, including transfer and rollback. It does not assume a bound on the number of system-message instructions.
From Stdlib Require Import List String NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.recursiveassembly QedkConcrete.hostnonce
  QedkConcrete.hostframe QedkConcrete.vmhostframe QedkConcrete.systemcode
  QedkConcrete.accountcode QedkConcrete.worldaccounts.
Require Import SetcodeConcrete.systemvm SetcodeConcrete.noncehost
  SetcodeConcrete.nonceprecompile SetcodeConcrete.assembly SetcodeConcrete.noncebudget.
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.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module H := Host.Make.
Local Abbreviation codeAt := (fun world a ⇒
  A.t.code (State.op_pointcaretlparenrparen world (W.account None a))).

Lemma transfer_preserves_code {F : H.FArgs} sender recipient amount state query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) →
  codeAt (S.t.world_state (snd (H.transfer_ether sender recipient amount state))) query =
    codeAt (S.t.world_state state) query.
Proof.
  intro Hmap. unfold H.transfer_ether.
  cbn -[Numeric.U256.op_gteq Numeric.U256.op_plus Numeric.U256.op_minus W.account].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [|reflexivity].
  cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
  match goal with |- context [S.t.with_world_state _ ?middle] ⇒
    set (debited := middle) end.
  assert (HdebitMap : stringmaps.WellFormed (W.t.accounts (S.t.world_state debited)))
    by (apply account_write_wellformed; exact Hmap).
  assert (HdebitCode : ∀ q, codeAt (S.t.world_state debited) q = codeAt (S.t.world_state state) q).
  { intro q. unfold debited. cbn beta iota zeta delta [S.t.world_state S.t.with_world_state].
    match goal with |- context [(W.account (Some false) sender).(Lens.t.set) ?value ?world] ⇒
      etransitivity; [exact (code_after_account_write false world sender value q Hmap)|]
    end.
    destruct (String.eqb sender q) eqn:Heq; [apply String.eqb_eq in Heq; subst q|]; reflexivity. }
  cbn beta iota zeta delta [S.t.world_state S.t.with_world_state].
  match goal with |- context [(W.account (Some false) recipient).(Lens.t.set) ?value ?world] ⇒
    etransitivity; [exact (code_after_account_write false world recipient value query HdebitMap)|]
  end.
  destruct (String.eqb recipient query) eqn:Heq.
  - apply String.eqb_eq in Heq. subst query. apply HdebitCode.
  - apply HdebitCode.
Qed.

Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
  Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).

Lemma call_free_vm_constructor vm query nonce msg code state :
  0 ≤ Evmc.Message.t.gas msg < 2^63 → callFreeBytes code = true →
  nonceInvariant query nonce state →
  nonceInvariant query nonce (snd ((VmF (HostF vm)).(Evmc.Vm.SIG.execute) msg code state)).
Proof.
  intros Hgas Hcode Hinitial. cbn -[Vm.Make.execute nonceInvariant].
  eapply call_free_execute_preserves_guarded with (property := nonceInvariant query nonce); try exact Hrevision; try reflexivity.
  - exact (@concrete_setcode_gas_progress args vm _ cold warm access write reserve Hrevision Hcold Hwarm).
  - exact (@concrete_primitives_preserve_nonce args vm query nonce).
  - intros. apply copy_code_read_only.
  - intros _. exact (@concrete_setcode_preserves_nonce args vm query nonce).
  - exact Hgas.
  - exact Hcode.
  - exact Hinitial.
Qed.

Theorem execution_call_free_nonce query nonce msg code state :
  0 ≤ Evmc.Message.t.gas msg < 2^63 → callFreeBytes code = true →
  nonceInvariant query nonce state →
  nonceInvariant query nonce (snd (@Host.Instantiate.Vm.execute args msg code state)).
Proof.
  intros Hgas Hcode Hinitial.
  change (nonceInvariant query nonce (snd
    ((@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).(Evmc.Vm.SIG.execute) msg code state))).
  unfold Evmc.Instantiate.Vm, Evmc.Instantiate._recursive_modules_Host_Vm.
  generalize Evmc.Instantiate.recursive_module_fuel. intros [|fuel].
  - exact Hinitial.
  - destruct fuel; cbn [recursive_module_unroll fst snd];
      apply call_free_vm_constructor; assumption.
Qed.

Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.

Lemma system_process_call_nonce query nonce msg state :
  query ≠ Reserve_balance.precompile_address →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  callFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
  nonceInvariant query nonce state →
  nonceInvariant query nonce (snd (@H.process_call (@Host.Instantiate.Host_fargs args) None msg state)).
Proof.
  intros Hnative Hgas Hcode Hinitial.
  set (transfer := if @H.should_transfer (@Host.Instantiate.Host_fargs args) msg
    then @H.transfer_ether (@Host.Instantiate.Host_fargs args)
      (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
    else fun st ⇒ (true, st)).
  assert (Hnonce : nonceInvariant query nonce (snd (transfer state))).
  { unfold transfer. destruct (H.should_transfer msg); [apply transfer_preserves_nonce|]; exact Hinitial. }
  assert (Hbody : callFreeBytes (codeAt (S.t.world_state (snd (transfer state)))
    (Evmc.Message.t.code_address msg)) = true).
  { unfold transfer. destruct (H.should_transfer msg); [|exact Hcode].
    rewrite transfer_preserves_code by exact (proj1 Hinitial). exact Hcode. }
  unfold H.process_call, H.M.
  cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile nonceInvariant].
  match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    change run with (transfer state); destruct (transfer state) as [ok middle] eqn:Htransfer end.
  cbn beta iota zeta delta [snd] in Hnonce, Hbody.
  destruct ok; [|exact Hnonce].
  cbn -[W.account Precompiles.Make.try_precompile nonceInvariant].
  match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct selection as [action|] eqn:Hselected end.
  - eapply precompile_preserves_nonce.
    3: exact Hselected.
    + exact Hrevision.
    + exact Hnative.
    + exact Hnonce.
  - apply execution_call_free_nonce; assumption.
Qed.

Unlike user transactions, system messages do not consume sender nonces. Their CALL has no CREATE increment, and a failed call restores its input.
Theorem execution_system_call_nonce query nonce msg state :
  query ≠ Reserve_balance.precompile_address →
  0 ≤ Evmc.Message.t.gas msg < 2^63 → Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  callFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
  nonceInvariant query nonce state →
  nonceInvariant query nonce (snd (@E.Host.call F msg state)).
Proof.
  intros Hnative Hgas Hkind Hcode Hinitial.
  pose proof (system_process_call_nonce query nonce msg state Hnative Hgas Hcode Hinitial) as Hbody.
  change (nonceInvariant query nonce (snd (@H.call (@Host.Instantiate.Host_fargs args) msg state))).
  unfold H.call, H.call_impl. rewrite Hkind.
  cbn -[H.process_call nonceInvariant].
  match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    destruct run as [result after] end.
  cbn beta iota zeta delta [snd] in Hbody.
  destruct result as [status gasleft refund output address]; destruct status;
    cbn -[nonceInvariant]; first [exact Hbody|exact Hinitial].
Qed.

Theorem system_message_nonce block addr data query nonce :
  query ≠ Reserve_balance.precompile_address →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
  nonceAt (B.t.world_state block) query = nonce →
  callFreeBytes (codeAt (B.t.world_state block) addr) = true →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state (snd (E.process_system_message block addr data)))) ∧
  nonceAt (B.t.world_state (snd (E.process_system_message block addr data))) query = nonce.
Proof.
  intros Hnative Hmap Hnonce Hcode.
  unfold E.process_system_message.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  - split; assumption.
  - cbn beta iota zeta.
    match goal with |- context [E.Host.call ?msg ?entry] ⇒
      pose proof (execution_system_call_nonce query nonce msg entry Hnative
        ltac:(change (0 ≤ 30000000 < 2^63); lia) eq_refl Hcode (conj Hmap Hnonce)) as Hrun;
      destruct (E.Host.call msg entry) as [result after]
    end.
    exact Hrun.
Qed.
End Execution.

Print Assumptions execution_call_free_nonce.
Print Assumptions system_message_nonce.