Library QedkConcrete.persistentnonce

A delegated account's code and nonce during VM execution

Keeping the code alongside the nonce lets the CREATE guard rule out nonce increments in this account's execution context. Other creators cannot create at this address because its code is nonempty.
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostnonce QedkConcrete.persistentcode
  QedkConcrete.vmhostframe QedkConcrete.worldaccounts.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.

Section Persistent.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.

Definition persistentNonce query code nonce s :=
  @persistentCode query code s ∧ @nonceInvariant query nonce s.

Lemma persistent_nonce_write query code nonce s keep addr value :
  persistentNonce query code nonce s →
  (addr = query → A.t.code value = code ∧ A.t.nonce value = nonce) →
  persistentNonce query code nonce (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
Proof.
  intros [Hcode Hnonce] Hvalue. split.
  - apply persistent_code_write; [exact Hcode|]. intro Heq. exact (proj1 (Hvalue Heq)).
  - apply nonce_write; [exact Hnonce|]. intro Heq. exact (proj2 (Hvalue Heq)).
Qed.

Lemma persistent_nonce_mark_creation query code nonce s addr :
  persistentNonce query code nonce s → addr ≠ query →
  persistentNonce query code nonce (S.t.with_accounts_created_in_current_transaction
    (Addr._Set.(OCamlSet.S.add) addr (S.t.accounts_created_in_current_transaction s)) s).
Proof.
  intros [Hcode Hnonce] Hdifferent. split.
  - apply persistent_code_mark_creation; assumption.
  - exact Hnonce.
Qed.

Context {F : H.FArgs}.
Lemma transfer_preserves_persistent_nonce query code nonce sender recipient amount :
  preservesCallback (persistentNonce query code nonce) (@H.transfer_ether F sender recipient amount).
Proof.
  intros s [Hcode Hnonce]. split.
  - apply transfer_preserves_persistent_code. exact Hcode.
  - apply transfer_preserves_nonce. exact Hnonce.
Qed.

Lemma increment_preserves_other_persistent_nonce query code nonce addr :
  addr ≠ query →
  preservesCallback (persistentNonce query code nonce) (@H.try_increment_nonce F addr).
Proof.
  intros Hdifferent s [Hcode Hnonce]. split.
  - apply increment_nonce_preserves_persistent_code. exact Hcode.
  - apply increment_preserves_other_nonce; assumption.
Qed.
End Persistent.

Section Creation.
Context {F : H.FArgs}
  {UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
  {UF : Basics.Unreachable Numeric.U256.t} {UG : Basics.Unreachable Gas.t}
  {UA : Basics.Unreachable Addr.t} {UB : Basics.Unreachable Byte_string.Bytes.t}
  {UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
  {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
  {U64i : Basics.Unreachable int64}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation protected := (@persistentNonce).

Lemma process_create_preserves_existing_nonce query fixedCode nonce msg :
  fixedCode ≠ Byte_string.Bytes.empty →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    preservesCallback (protected query fixedCode nonce) ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback (protected query fixedCode nonce)
    (@H.process_create F msg).
Proof.
  intros Hnonempty Hvm Hgas s Hinitial.
  unfold H.process_create, H.contract_creation_address, H.M.
  cbn -[Addr.of_contract_creation W.account Numeric.U64.op_ltgt
    Byte_string.Bytes.op_ltgt H.transfer_ether].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
  { exact Hinitial. }
  match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
  assert (Htarget : target ≠ query).
  { exact (@create_guard_excludes_existing_code query fixedCode s target _ (proj1 Hinitial) Hnonempty Hcollision). }
  match goal with |- context [H.transfer_ether _ _ _
    (S.t.with_world_state ?nonceWorld (S.t.with_world_state ?storageWorld ?created))] ⇒
    set (marked := created) in *;
    set (stored := S.t.with_world_state storageWorld marked) in *;
    set (prepared := S.t.with_world_state nonceWorld stored) in ×
  end.
  assert (Hmarked : protected query fixedCode nonce marked).
  { exact (@persistent_nonce_mark_creation query fixedCode nonce s target Hinitial Htarget). }
  assert (Hstored : protected query fixedCode nonce stored).
  { unfold stored. eapply persistent_nonce_write with (s := marked) (keep := false) (addr := target);
      [exact Hmarked|intro Heq; contradiction]. }
  assert (Hprepared : protected query fixedCode nonce prepared).
  { unfold prepared. eapply persistent_nonce_write with (s := stored) (keep := false) (addr := target);
      [exact Hstored|intro Heq; contradiction]. }
  pose proof (@transfer_preserves_persistent_nonce UF F query fixedCode nonce
    (Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared Hprepared) as Htransfer.
  destruct (@H.transfer_ether F (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared) as [ok middle] eqn:HtransferRun.
  cbn beta iota zeta delta [fst snd] in Htransfer.
  cbn -[W.account Addr.of_contract_creation target stored prepared marked].
  destruct ok.
  2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked].
    exact Htransfer. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked].
  match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
    pose proof (Hvm nested code Hgas middle Htransfer) as Hrecursive;
    destruct ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
  end.
  cbn beta iota zeta delta [fst snd] in Hrecursive.
  destruct result as [status gasleft refund output address]. destruct status;
    cbn -[W.account Addr.of_contract_creation target stored prepared marked];
    try exact Hrecursive.
  match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct failure as [error|] end.
  - exact Hrecursive.
  - eapply persistent_nonce_write with (s := afterVm) (keep := false) (addr := target);
      [exact Hrecursive|intro Heq; contradiction].
Qed.

End Creation.