Library QedkConcrete.reservestorage

The native setter stores the configuration it computed

EIP-161 removes empty accounts, including their storage. The setter first gives the native account a nonzero nonce; this is essential to its storage readback law, even when nobody has sent funds to the precompile.
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Reserve_balance QedkConcrete.worldaccounts
  QedkConcrete.reserveencoding QedkConcrete.stringmaps QedkConcrete.bytewords.
Require Import QedkConcrete.reservehash.
Require QedkConcrete.reserveconfig QedkConcrete.memorywords QedkConcrete.reservecheck.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module C := Reserve_config.

Section Storage.
Context {UZ : Basics.Unreachable Z.t}.
Context {Uarr : Basics.Unreachable (OCamlIarray.t int)}.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}.
Context {UA : Basics.Unreachable Addr.t} {UB : Basics.Unreachable Byte_string.Bytes.t}.
Context {UU : Basics.Unreachable unit} {US : Basics.Unreachable string}.
Context {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}.
Context {UP : Basics.Unreachable (int × int)}.
Context {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Local Abbreviation native := (@Reserve_balance.precompile_address).
Local Abbreviation activate := (@Reserve_balance.activate_precompile).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
Local Abbreviation readConfig := (@Reserve_balance.stored_config).
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation store := (@storeConfigField).
Local Abbreviation read := (@readConfigField).

Lemma nonzero_nonce_not_empty account : A.t.nonce account ≠ 0 → @A.is_empty account = false.
Proof.
  intro Hnonce. unfold A.is_empty. apply Bool.andb_false_iff. right.
  apply Bool.andb_false_iff. left.
  change (Z.eqb (OCamlZ.compare (A.t.nonce account) 0) 0 = false).
  unfold OCamlZ.compare. destruct (Z.compare_spec (A.t.nonce account) 0);
    [contradiction|reflexivity|reflexivity].
Qed.

Lemma active_account world : WellFormed (W.t.accounts world) →
  A.t.storage (readAccount (activate world) native) = A.t.storage (readAccount world native) ∧
  A.t.nonce (readAccount (activate world) native) ≠ 0.
Proof.
  intro Hwf.
  assert (Hnonce : Numeric.U64.max (A.t.nonce (readAccount world native)) (@Numeric.U64.one) ≠ 0).
  { change ((if Z.ltb (OCamlZ.compare (A.t.nonce (readAccount world native)) 1) 0
      then 1 else A.t.nonce (readAccount world native)) ≠ 0).
    generalize (A.t.nonce (readAccount world native)) as nonce. intro nonce.
    unfold OCamlZ.compare. destruct (Z.compare_spec nonce 1); cbn; lia. }
  unfold Reserve_balance.activate_precompile.
  change (let account := readAccount world native in
    let changed := A.nonce.(Lens.t.set) (Numeric.U64.max (A.t.nonce account) (@Numeric.U64.one)) account in
    let after := State.op_pointcaretlparenrparenltminus world (@W.account (Some false) native) changed in
    A.t.storage (readAccount after native) = A.t.storage account ∧
    A.t.nonce (readAccount after native) ≠ 0).
  cbn beta zeta. rewrite account_read_after_write by exact Hwf.
  rewrite Strings.String.eqb_refl.
  rewrite nonzero_nonce_not_empty by exact Hnonce.
  split; [reflexivity|exact Hnonce].
Qed.

Lemma setter_reads_record world block addr value record after :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  (C.t.settled record < 2^256)%N →
  (C.t.pending record < 2^256)%N →
  (C.t.valid_from record < 2^256)%N →
  @C.set (@Reserve_balance.execution_consensus_delay) block
    (@U.to_uint value) (readConfig world addr) = Some record →
  setter world block addr value = Some after → readConfig after addr = record.
Proof.
  intros Hworld Hstorage Hsettled Hpending Hvalid Hrecord Hset.
  unfold Reserve_balance.set_reserve_balance in Hset. rewrite Hrecord in Hset.
  injection Hset as <-.
  pose proof (active_account world Hworld) as [Hstored Hnonce].
  assert (Hactive : WellFormed (W.t.accounts (activate world))) by
    (apply (@account_write_wellformed false); exact Hworld).
  set (storage := store (store (store (A.t.storage (readAccount (activate world) native))
    addr 0 (C.t.settled record)) addr 1 (C.t.pending record)) addr 2 (C.t.valid_from record)).
  change (let after := State.op_pointcaretlparenrparenltminus (activate world)
    (@W.account (Some false) native)
    (A.storage.(Lens.t.set) storage (readAccount (activate world) native)) in
    {| C.t.settled := read (A.t.storage (readAccount after native)) addr 0;
       C.t.pending := read (A.t.storage (readAccount after native)) addr 1;
       C.t.valid_from := read (A.t.storage (readAccount after native)) addr 2 |} = record).
  cbn beta zeta. rewrite account_read_after_write by exact Hactive.
  rewrite Strings.String.eqb_refl, nonzero_nonce_not_empty by exact Hnonce.
  change ({| C.t.settled := read storage addr 0; C.t.pending := read storage addr 1;
             C.t.valid_from := read storage addr 2 |} = record).
  unfold storage. rewrite Hstored.
  repeat first
    [ rewrite read_store_config_same by (try (repeat apply store_config_wellformed; exact Hstorage); assumption)
    | rewrite read_store_config_other by (try (repeat apply store_config_wellformed; exact Hstorage); lia) ].
  destruct record. reflexivity.
Qed.

Lemma read_config_bounded storage addr offset :
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32) storage →
  (read storage addr offset < 2^256)%N.
Proof.
  intro Hbytes. unfold readConfigField.
  match goal with |- context [Option.value ?found ?zero] ⇒
    assert (Hlength : OCamlString.length (Option.value found zero) = 32) end.
  { match goal with |- context [Option.value ?found _] ⇒ destruct found as [bytes|] eqn:Hfind end.
    - cbn [Option.value]. eapply (memorywords.map_find_value_property _
        (fun bytes ⇒ OCamlString.length bytes = 32)); [exact Hbytes|exact Hfind].
    - apply b32_zeros_length. }
  match type of Hlength with OCamlString.length ?bytes = 32 ⇒
    pose proof (@word_repr bytes ltac:(lia)) as Hrange end.
  rewrite reservecheck.u256_to_uint by exact (proj1 Hrange).
  unfold wordrange.wordRange in Hrange. lia.
Qed.

Both success and failure of the storage-backed setter agree with the generated pure record setter. The premises are finite-map and byte-width invariants, not hypotheses about what EVM execution ought to do.
Theorem setter_storage_refines world block addr value :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32)
    (A.t.storage (readAccount world native)) →
  option_map (fun after ⇒ readConfig after addr) (setter world block addr value) =
    @C.set (@Reserve_balance.execution_consensus_delay) block
      (@U.to_uint value) (readConfig world addr).
Proof.
  intros Hworld Hstorage Hbytes.
  destruct (@C.set (@Reserve_balance.execution_consensus_delay) block
    (@U.to_uint value) (readConfig world addr)) as [record|] eqn:Hrecord.
  - assert (Hpending : (C.t.pending record < 2^256)%N).
    { eapply (proj2 (reserveconfig.setter_output_valid _ _ _ _ _ Hrecord)). }
    assert (Hvalid : (C.t.valid_from record < 2^256)%N).
    { exact (proj2 (proj1 (reserveconfig.setter_output_valid _ _ _ _ _ Hrecord))). }
    assert (Hsettled : (C.t.settled record < 2^256)%N).
    { pose proof Hrecord as Hset. rewrite reserveconfig.set_equation in Hset.
      destruct (N.eqb _ _ || (N.leb _ _ || N.leb _ _))%bool; [discriminate|].
      destruct (N.ltb _ _ && negb (N.eqb _ _))%bool; [discriminate|].
      injection Hset as <-.
      change (@C.active (readConfig world addr) block < 2^256)%N.
      rewrite reserveconfig.active_equation.
      destruct (N.eqb _ _); [vm_compute; reflexivity|].
      destruct (N.ltb _ _).
      - change (read (A.t.storage (readAccount world native)) addr 0%Z < 2^256)%N.
        apply read_config_bounded. exact Hbytes.
      - change (read (A.t.storage (readAccount world native)) addr 1%Z < 2^256)%N.
        apply read_config_bounded. exact Hbytes. }
    destruct (setter world block addr value) as [after|] eqn:Hset.
    + cbn [option_map]. f_equal. eapply setter_reads_record; eassumption.
    + unfold Reserve_balance.set_reserve_balance in Hset. rewrite Hrecord in Hset. discriminate.
  - unfold Reserve_balance.set_reserve_balance. rewrite Hrecord. reflexivity.
Qed.

Compose byte/storage correctness with the existing QEDK record proof. This is a setter refinement, not a theorem about arbitrary VM executions.
Theorem native_setter_refines_qedk world block addr value :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32)
    (A.t.storage (readAccount world native)) →
  (C.t.valid_from (readConfig world addr) = 0%N ∨
    (3 ≤ C.t.valid_from (readConfig world addr))%N) →
  option_map (fun after ⇒ reserveconfig.abstractRecord 3 (readConfig after addr))
    (setter world block addr value) =
    reservebaldelayed_qedk.setReserveBalance 3
      (reserveconfig.abstractRecord 3 (readConfig world addr)) block (@U.to_uint value).
Proof.
  intros Hworld Hstorage Hbytes Hvalid.
  transitivity (option_map (reserveconfig.abstractRecord 3)
    (@C.set (@Reserve_balance.execution_consensus_delay) block
      (@U.to_uint value) (readConfig world addr))).
  - rewrite <- setter_storage_refines by assumption.
    destruct (setter world block addr value); reflexivity.
  - apply reserveconfig.setter_refines; [lia|exact Hvalid].
Qed.

Lemma native_setter_activation world block addr value after :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32)
    (A.t.storage (readAccount world native)) →
  setter world block addr value = Some after →
  @C.active (readConfig after addr) (block + 3)%N = @U.to_uint value ∧
  (∀ n, (block ≤ n < block + 3)%N →
    @C.active (readConfig after addr) n = @C.active (readConfig world addr) n).
Proof.
  intros Hworld Hstorage Hbytes Hset.
  pose proof (setter_storage_refines world block addr value Hworld Hstorage Hbytes) as Hrecord.
  rewrite Hset in Hrecord. cbn [option_map] in Hrecord. symmetry in Hrecord.
  exact (proj2 (proj2 (reserveconfig.set_activation _ _ _ _ _ Hrecord))).
Qed.

A write to another account's record cannot change this account's configuration. Hash separation is an explicit obligation, not an assumed property of the abstract Digestif implementation.
Lemma setter_preserves_other_record world block addr other value after :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  @configSlotsDisjoint addr other →
  setter world block addr value = Some after →
  readConfig after other = readConfig world other.
Proof.
  intros Hworld Hstorage Hslots Hset.
  unfold Reserve_balance.set_reserve_balance in Hset.
  match type of Hset with context [match ?setting with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct setting as [record|] eqn:Hrecord end; [|discriminate].
  injection Hset as <-.
  pose proof (active_account world Hworld) as [Hstored Hnonce].
  assert (Hactive : WellFormed (W.t.accounts (activate world))) by
    (apply (@account_write_wellformed false); exact Hworld).
  set (storage := store (store (store (A.t.storage (readAccount (activate world) native))
    addr 0 (C.t.settled record)) addr 1 (C.t.pending record)) addr 2 (C.t.valid_from record)).
  change (let after := State.op_pointcaretlparenrparenltminus (activate world)
    (@W.account (Some false) native)
    (A.storage.(Lens.t.set) storage (readAccount (activate world) native)) in
    {| C.t.settled := read (A.t.storage (readAccount after native)) other 0;
       C.t.pending := read (A.t.storage (readAccount after native)) other 1;
       C.t.valid_from := read (A.t.storage (readAccount after native)) other 2 |} = readConfig world other).
  cbn beta zeta. rewrite account_read_after_write by exact Hactive.
  rewrite Strings.String.eqb_refl, nonzero_nonce_not_empty by exact Hnonce.
  change ({| C.t.settled := read storage other 0; C.t.pending := read storage other 1;
             C.t.valid_from := read storage other 2 |} = readConfig world other).
  unfold storage. rewrite Hstored.
  repeat rewrite read_store_config_disjoint by
    (first [repeat apply store_config_wellformed; exact Hstorage|exact Hslots|lia]).
  reflexivity.
Qed.

Both the updated account and every separate record retain the thresholds already usable in this block's consensus/execution window. This remains true for repeated successful settings in the same block.
Lemma native_setter_preserves_window world block addr query value after n :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32)
    (A.t.storage (readAccount world native)) →
  (addr = query ∨ @configSlotsDisjoint addr query) →
  setter world block addr value = Some after →
  (block ≤ n < block + 3)%N →
  @C.active (readConfig after query) n = @C.active (readConfig world query) n.
Proof.
  intros Hworld Hstorage Hbytes Hslots Hset Hn.
  destruct Hslots as [->|Hslots].
  - exact (proj2 (native_setter_activation world block query value after
      Hworld Hstorage Hbytes Hset) n Hn).
  - rewrite (setter_preserves_other_record world block addr query value after
      Hworld Hstorage Hslots Hset). reflexivity.
Qed.

Lemma native_setter_preserves_activation_invariant world block addr query value after :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32)
    (A.t.storage (readAccount world native)) →
  (addr = query ∨ @configSlotsDisjoint addr query) →
  setter world block addr value = Some after →
  (C.t.valid_from (readConfig world query) = 0%N ∨
    (3 ≤ C.t.valid_from (readConfig world query))%N) →
  C.t.valid_from (readConfig after query) = 0%N ∨
    (3 ≤ C.t.valid_from (readConfig after query))%N.
Proof.
  intros Hworld Hstorage Hbytes Hslots Hset Hvalid.
  destruct Hslots as [->|Hslots].
  - pose proof (setter_storage_refines world block query value Hworld Hstorage Hbytes) as Hrecord.
    rewrite Hset in Hrecord. cbn [option_map] in Hrecord. symmetry in Hrecord.
    right. exact (proj1 (proj1 (reserveconfig.setter_output_valid _ _ _ _ _ Hrecord))).
  - rewrite (setter_preserves_other_record world block addr query value after
      Hworld Hstorage Hslots Hset). exact Hvalid.
Qed.

A hash collision aliases the complete record, never individual fields. Aliased accounts therefore obey the same delay as repeated settings for one account. Collision freedom is needed for configuration ownership, but not for stability of thresholds inside the delay window.
Lemma stored_config_same_base world a b :
  @Reserve_balance.config_slot a =
    @Reserve_balance.config_slot b →
  readConfig world a = readConfig world b.
Proof. intro Heq. unfold Reserve_balance.stored_config. rewrite Heq. reflexivity. Qed.

Lemma native_setter_preserves_window_any_account world block addr query value after n :
  WellFormed (W.t.accounts world) →
  WellFormed (A.t.storage (readAccount world native)) →
  List.Forall (fun entry ⇒ OCamlString.length (snd entry) = 32)
    (A.t.storage (readAccount world native)) →
  setter world block addr value = Some after →
  (block ≤ n < block + 3)%N →
  @C.active (readConfig after query) n = @C.active (readConfig world query) n.
Proof.
  intros Hworld Hstorage Hbytes Hset Hn.
  destruct (Z.eq_dec (@Reserve_balance.config_slot addr)
    (@Reserve_balance.config_slot query)) as [Heq|Hneq].
  - rewrite <- (stored_config_same_base after addr query Heq),
      <- (stored_config_same_base world addr query Heq).
    exact (proj2 (native_setter_activation world block addr value after
      Hworld Hstorage Hbytes Hset) n Hn).
  - apply (native_setter_preserves_window world block addr query value after n
      Hworld Hstorage Hbytes); [|exact Hset|exact Hn].
    right. apply config_slots_disjoint_iff. exact Hneq.
Qed.

End Storage.

Print Assumptions setter_storage_refines.
Print Assumptions native_setter_refines_qedk.
Print Assumptions native_setter_activation.
Print Assumptions native_setter_preserves_window.
Print Assumptions native_setter_preserves_window_any_account.