Library QedkConcrete.reservewindowtrace

Reserve settings across a concrete execution window

Each entry contains one block's surviving settings. Settings within a block are newest first, as in reserveStorageWrites; blocks are oldest first. Rollback is already accounted for by the write witnesses.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.protectedstoragenative QedkConcrete.reserveencoding
  QedkConcrete.hoststoragewords QedkConcrete.worldaccounts QedkConcrete.reservecollision.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Trace.
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 writes := (@reserveStorageWrites).
Local Abbreviation slotsDisjoint := (@configSlotsDisjoint).
Local Abbreviation readField := (@readConfigField).
Local Abbreviation threshold := (fun storage query target ⇒ @Reserve_config.active
  {| Reserve_config.t.settled := readField storage query 0;
     Reserve_config.t.pending := readField storage query 1;
     Reserve_config.t.valid_from := readField storage query 2 |} target).

Inductive reserveStorageTrace : list (string × string) →
    list (N × list (Addr.t × Numeric.U256.t)) → list (string × string) → Prop :=
| reserve_trace_empty initial : reserveStorageTrace initial nil initial
| reserve_trace_step initial middle final block updates rest :
    writes block initial updates middle →
    reserveStorageTrace middle rest final →
    reserveStorageTrace initial ((block, updates) :: rest) final.

Lemma reserve_trace_representation initial trace final :
  stringmaps.WellFormed initial → storageWords initial →
  reserveStorageTrace initial trace final →
  stringmaps.WellFormed final ∧ storageWords final.
Proof using U32 UC UI UP UU.
  intros Hmap Hwords Htrace. revert Hmap Hwords.
  induction Htrace as [initial|initial middle final block updates rest Hwrites Htrace IH];
    intros Hmap Hwords.
  - split; assumption.
  - destruct (reserve_storage_writes_representation _ _ _ _ Hmap Hwords Hwrites) as [Hmap' Hwords'].
    apply IH; assumption.
Qed.

Every setting is too recent to affect the queried block. Telescoping the setter law needs neither monotone configured levels nor promotion at a particular transaction boundary. It also permits repeated settings for the same account. Cross-account slot separation remains explicit.
Theorem reserve_trace_threshold initial trace final query target :
  stringmaps.WellFormed initial → storageWords initial →
  reserveStorageTrace initial trace final →
  Forall (fun step ⇒ (fst step ≤ target < fst step + 3)%N ∧
    Forall (fun update ⇒ fst update = query ∨ slotsDisjoint (fst update) query) (snd step)) trace →
  threshold final query target = threshold initial query target.
Proof using U32 UC UI UP UU.
  intros Hmap Hwords Htrace. revert Hmap Hwords.
  induction Htrace as [initial|initial middle final block updates rest Hwrites Htrace IH];
    intros Hmap Hwords Hsafe.
  - reflexivity.
  - inversion Hsafe as [|? ? [Htime Hslots] Hrest]; subst.
    destruct (reserve_storage_writes_representation _ _ _ _ Hmap Hwords Hwrites) as [Hmap' Hwords'].
    etransitivity; [exact (IH Hmap' Hwords' Hrest)|].
    exact (reserve_storage_writes_threshold _ _ _ _ query target Hmap Hwords Hwrites Hslots Htime).
Qed.

Lemma reserve_trace_app initial first middle last final :
  reserveStorageTrace initial first middle → reserveStorageTrace middle last final →
  reserveStorageTrace initial (first ++ last) final.
Proof using U32 UC UI UP UU.
  intros Hfirst Hlast. induction Hfirst.
  - exact Hlast.
  - cbn [app]. econstructor; [eassumption|]. apply IHHfirst. exact Hlast.
Qed.

Local Abbreviation key := (fun addr offset ⇒ @Numeric.U256.to_repr
  (@Numeric.U256.op_plus (@Reserve_balance.config_slot addr)
    (@Numeric.U256.of_int offset))).

Instead of assuming all hashed records are separate, a changed threshold can be traced to a particular setting for a different address and two colliding slots. This keeps the cryptographic obligation finite and separate from the proved execution behavior.
Theorem reserve_trace_threshold_or_collision initial trace final query target :
  stringmaps.WellFormed initial → storageWords initial →
  reserveStorageTrace initial trace final →
  Forall (fun step ⇒ (fst step ≤ target < fst step + 3)%N) trace →
  threshold final query target = threshold initial query target ∨
  ∃ block updates addr value i j,
    List.In (block, updates) trace ∧ List.In (addr,value) updates ∧
    addr ≠ query ∧ 0 ≤ i < 3 ∧ 0 ≤ j < 3 ∧ key addr i = key query j.
Proof using U32 UC UI UP UU.
  intros Hmap Hwords Htrace. revert Hmap Hwords.
  induction Htrace as [initial|initial middle final block updates rest Hwrites Htrace IH];
    intros Hmap Hwords Htimes.
  - left. reflexivity.
  - inversion Htimes as [|? ? Htime Hrest]; subst.
    destruct (@settings_separation updates query) as [Hslots|Hcollision].
    + destruct (reserve_storage_writes_representation _ _ _ _ Hmap Hwords Hwrites) as [Hmap' Hwords'].
      destruct (IH Hmap' Hwords' Hrest) as [Hthreshold|Hcollision].
      × left. etransitivity; [exact Hthreshold|].
        exact (reserve_storage_writes_threshold _ _ _ _ query target Hmap Hwords Hwrites Hslots Htime).
      × right. destruct Hcollision as [n [us [a [v [i [j [Hin Hcollision]]]]]]].
        ∃ n, us, a, v, i, j. split; [right; exact Hin|exact Hcollision].
    + right. destruct Hcollision as [a [v [i [j Hcollision]]]].
      ∃ block, updates, a, v, i, j. split; [left; reflexivity|exact Hcollision].
Qed.
Shared hash pages share their activation time too. The delay law therefore telescopes across all surviving settings, without assuming isolation of different accounts' records. Rollback is already reflected in the trace.
Theorem reserve_trace_threshold_any_account initial trace final query target :
  stringmaps.WellFormed initial → storageWords initial →
  reserveStorageTrace initial trace final →
  Forall (fun step ⇒ (fst step ≤ target < fst step + 3)%N) trace →
  threshold final query target = threshold initial query target.
Proof using U32 UC UI UP UU.
  intros Hmap Hwords Htrace. revert Hmap Hwords.
  induction Htrace as [initial|initial middle final block updates rest Hwrites Htrace IH];
    intros Hmap Hwords Htimes.
  - reflexivity.
  - inversion Htimes as [|? ? Htime Hrest]; subst.
    destruct (reserve_storage_writes_representation _ _ _ _ Hmap Hwords Hwrites) as [Hmap' Hwords'].
    etransitivity; [exact (IH Hmap' Hwords' Hrest)|].
    exact (reserve_storage_writes_threshold_any_account _ _ _ _ query target
      Hmap Hwords Hwrites Htime).
Qed.
End Trace.