Library QedkConcrete.nativenonce

Native reserve settings do not change user nonces

From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostnonce QedkConcrete.authorizationnonces
  QedkConcrete.reserveprecompile QedkConcrete.hostcode 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 ReserveSetter.
Context {UZ : Basics.Unreachable Z.t} {Uarr : Basics.Unreachable (OCamlIarray.t int)}
  {UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
  {UR : Basics.Unreachable Numeric.U256.Repr.t} {UF : Basics.Unreachable Numeric.U256.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}
  {UP : Basics.Unreachable (int × int)}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation activate := (@Reserve_balance.activate_precompile).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).

Lemma account_write_other_nonce keep world addr value query :
  stringmaps.WellFormed (W.t.accounts world) → addr ≠ query →
  A.t.nonce (readAccount (State.op_pointcaretlparenrparenltminus world
    (@W.account (Some keep) addr) value) query) = A.t.nonce (readAccount world query).
Proof.
  intros Hwf Hdifferent. cbv beta.
  rewrite nonce_after_account_write by exact Hwf.
  destruct (String.eqb addr query) eqn:Heq; [|reflexivity].
  apply String.eqb_eq in Heq. contradiction.
Qed.

Activation touches the reserve precompile's nonce; all other accounts' nonces survive setting or replacing their configured threshold.
Lemma setter_other_nonce world block addr value after query :
  query ≠ @Reserve_balance.precompile_address →
  stringmaps.WellFormed (W.t.accounts world) → setter world block addr value = Some after →
  stringmaps.WellFormed (W.t.accounts after) ∧
  A.t.nonce (readAccount after query) = A.t.nonce (readAccount world query).
Proof.
  intros Hdifferent Hwf Hset. unfold Reserve_balance.set_reserve_balance in Hset.
  match type of Hset with context [match ?configResult with None ⇒ _ | Some _ ⇒ _ end] ⇒
    destruct configResult as [record|] end; [|discriminate].
  injection Hset as <-.
  assert (Hactive : stringmaps.WellFormed (W.t.accounts (activate world))).
  { apply account_write_wellformed. exact Hwf. }
  split.
  - apply (@account_write_wellformed false (activate world)
      (@Reserve_balance.precompile_address)). exact Hactive.
  - transitivity (A.t.nonce (readAccount (activate world) query)).
    + eapply (account_write_other_nonce false (activate world)
        (@Reserve_balance.precompile_address));
        [exact Hactive|congruence].
    + eapply account_write_other_nonce; [exact Hwf|congruence].
Qed.

Context {F : Reserve_balance_introspection.Make.FArgs}
  {Useq : Basics.Unreachable (OCamlSeq.t (Addr.Map.key × A.t))}
  {U64i : Basics.Unreachable MonadVM.Int64.t} {UG : Basics.Unreachable Gas.t}.
Local Abbreviation precompile := (@Reserve_balance_introspection.Make.precompile
  F ).

Lemma reserve_precompile_preserves_other_nonce query nonce msg :
  query ≠ @Reserve_balance.precompile_address →
  preservesCallback (@nonceInvariant query nonce) (precompile msg).
Proof.
  intros Hdifferent s [Hwf Hnonce].
  change (@nonceInvariant query nonce (snd (precompile msg s))).
  destruct (@reserveprecompile.precompile_world_effect F msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
  - rewrite Hsame. split; assumption.
  - rewrite Hafter. unfold nonceInvariant.
    destruct (setter_other_nonce _ _ _ _ _ query Hdifferent Hwf Hwrite) as [Hmap Hvalue].
    split; [exact Hmap|].
    etransitivity; [exact Hvalue|exact Hnonce].
Qed.
End ReserveSetter.