Library QedkConcrete.hostnonce

Nonces through concrete Host operations

Account-map normalization may delete an empty entry, but that entry's nonce was already zero. Balance and storage writes therefore preserve nonces even when they change whether the account is stored explicitly.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.accountnonces
  QedkConcrete.hostframe QedkConcrete.worldaccounts QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Nonce.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun s addr ⇒ State.op_pointcaretlparenrparen
  (S.t.world_state s) (@W.account None addr)).

Definition nonceInvariant query nonce (s : S.t) :=
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
  A.t.nonce (readAccount s query) = nonce.

Lemma nonce_write query nonce s keep addr value :
  nonceInvariant query nonce s →
  (addr = query → A.t.nonce value = nonce) →
  nonceInvariant query nonce (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
Proof.
  intros [Hmap Hnonce] Hvalue. split.
  - apply account_write_wellformed. exact Hmap.
  - cbv beta. cbn [S.t.with_world_state S.t.world_state].
    rewrite nonce_after_account_write by exact Hmap.
    destruct (String.eqb addr query) eqn:Heq.
    + apply Hvalue, String.eqb_eq. exact Heq.
    + exact Hnonce.
Qed.

Lemma nonce_world_frame query nonce before after :
  nonceInvariant query nonce before →
  S.t.world_state after = S.t.world_state before → nonceInvariant query nonce after.
Proof. unfold nonceInvariant. intros Hbefore Hworld. rewrite Hworld. exact Hbefore. Qed.

Context {F : H.FArgs}.
Lemma transfer_preserves_nonce query nonce sender recipient amount :
  preservesCallback (nonceInvariant query nonce) (@H.transfer_ether F sender recipient amount).
Proof.
  intros s Hbefore. unfold H.transfer_ether.
  cbn -[Numeric.U256.op_gteq Numeric.U256.op_plus Numeric.U256.op_minus W.account].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [|exact Hbefore].
  cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
  match goal with |- nonceInvariant _ _ (S.t.with_world_state _ ?middle) ⇒
    set (debited := middle) in × end.
  assert (Hdebit : nonceInvariant query nonce debited).
  { unfold debited. apply nonce_write; [exact Hbefore|].
    intro Heq. subst sender. exact (proj2 Hbefore). }
  apply nonce_write; [exact Hdebit|]. intro Heq. subst recipient. exact (proj2 Hdebit).
Qed.

Context {UI : Basics.Unreachable int}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}
  {UU : Basics.Unreachable unit}.

Lemma storage_preserves_nonce query nonce addr key value :
  preservesCallback (nonceInvariant query nonce) (@H.set_storage F addr key value).
Proof.
  intros s Hbefore. apply nonce_write; [exact Hbefore|].
  intro Heq. subst addr. exact (proj2 Hbefore).
Qed.

Lemma selfdestruct_preserves_nonce query nonce addr beneficiary :
  preservesCallback (nonceInvariant query nonce) (@H.selfdestruct F addr beneficiary).
Proof.
  intros s Hbefore. unfold H.selfdestruct.
  cbn -[H.transfer_ether Addr._Set W.account].
  match goal with |- context [H.transfer_ether ?from ?to ?value ?initial] ⇒
    pose proof (transfer_preserves_nonce query nonce from to value initial Hbefore) as Htransfer;
    destruct (H.transfer_ether from to value initial) as [ok middle]
  end.
  cbn beta iota zeta delta [snd] in Htransfer.
  cbn -[Addr._Set W.account].
  destruct (Addr._Set.(OCamlSet.S.mem) addr
    (S.t.accounts_created_in_current_transaction middle)).
  - apply (nonce_write query nonce
      (S.t.with_self_destruct (Addr._Set.(OCamlSet.S.add) addr (S.t.self_destruct middle)) middle)
      false addr (A.t.with_balance (@Numeric.U256.zero)
        (State.op_pointcaretlparenrparen (S.t.world_state middle) (@W.account None addr))));
      [exact Htransfer|].
    intro Heq. subst addr. exact (proj2 Htransfer).
  - exact Htransfer.
Qed.

Lemma increment_preserves_other_nonce query nonce addr :
  addr ≠ query →
  preservesCallback (nonceInvariant query nonce) (@H.try_increment_nonce F addr).
Proof.
  intros Hdifferent s Hbefore. unfold H.try_increment_nonce, H.M.
  cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
  apply nonce_write; [exact Hbefore|]. intro Heq. contradiction.
Qed.
End Nonce.

A delegation marker is exactly the prefix the CREATE guard reads. Its validity makes the substring operation in bounds; no decoder fallback or assumption about the delegated code is needed.
Lemma string_sub_full {US : Basics.Unreachable string} bytes :
  @OCamlString.sub US bytes 0 (OCamlString.length bytes) = bytes.
Proof.
  unfold OCamlString.sub. rewrite Z.add_0_l, Z.leb_refl.
  assert (Hnonnegative : (0 <=? OCamlString.length bytes) = true)
    by (apply Z.leb_le; unfold OCamlString.length; lia).
  rewrite Hnonnegative, Z.leb_refl. cbn [andb].
  unfold OCamlString.length. rewrite Nat2Z.id.
  clear Hnonnegative. change (substring 0 (Strings.String.length bytes) bytes = bytes).
  induction bytes; cbn; congruence.
Qed.

Section CodeRead.
Context {F : H.FArgs} {U64 : Basics.Unreachable Numeric.U64.t}
  {UF : Basics.Unreachable Numeric.U256.t} {US : Basics.Unreachable string}
  {UI : Basics.Unreachable int}.

Lemma copy_code_delegated query s :
  let code := A.t.code (State.op_pointcaretlparenrparen
    (S.t.world_state s) (@W.account None query)) in
  @Delegation.is_valid_delegation code = true →
  @H.copy_code F query 0 (@Delegation.eoa_delegated_code_length) s = (code, s).
Proof.
  cbn zeta. intro Hdelegated.
  assert (Hlength : Byte_string.Bytes.length (A.t.code (State.op_pointcaretlparenrparen
    (S.t.world_state s) (@W.account None query))) = 23).
  { unfold Delegation.is_valid_delegation in Hdelegated.
    apply Bool.andb_true_iff in Hdelegated as [Hlength _].
    apply Z.eqb_eq in Hlength. exact Hlength. }
  unfold H.copy_code. cbn -[W.account Byte_string.Bytes.length Byte_string.Bytes.sub].
  match goal with |- context [Byte_string.Bytes.length ?code] ⇒
    change (Byte_string.Bytes.length code = 23) in Hlength
  end.
  rewrite Hlength. cbn -[W.account Byte_string.Bytes.sub].
  match type of Hlength with Byte_string.Bytes.length ?code = _ ⇒
    change ((@Byte_string.Bytes.sub (@H._rocq_fallback_copy_code_0 F) code 0 23, s) = (code, s));
    assert (Hsub : @Byte_string.Bytes.sub (@H._rocq_fallback_copy_code_0 F) code 0 23 = code)
  end.
  - rewrite <- Hlength. apply string_sub_full.
  - rewrite Hsub. reflexivity.
Qed.
End CodeRead.