Library QedkConcrete.reservecollision

Finite storage-key collision witnesses

Hash output size alone cannot justify cross-account storage isolation. These decisions either establish separation or exhibit the two actual record slots that coincide; they assume no cryptographic property.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.reserveencoding QedkConcrete.worldaccounts.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.

Section Collision.
Context {UR : Basics.Unreachable Numeric.U256.Repr.t} {UF : Basics.Unreachable Numeric.U256.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 slotsDisjoint := (@configSlotsDisjoint).
Local Abbreviation key := (fun addr offset ⇒ @Numeric.U256.to_repr
  (@Numeric.U256.op_plus (@Reserve_balance.config_slot addr)
    (@Numeric.U256.of_int offset))).

Lemma config_slots_separation a b :
  {slotsDisjoint a b} +
  {∃ i j, 0 ≤ i < 3 ∧ 0 ≤ j < 3 ∧ key a i = key b j}.
Proof.
  set (pairs := [(0,0); (0,1); (0,2); (1,0); (1,1); (1,2); (2,0); (2,1); (2,2)]).
  set (same := fun ij : Z × Z ⇒ String.eqb (key a (fst ij)) (key b (snd ij))).
  destruct (Lists.List.find same pairs) as [[i j]|] eqn:Hfind.
  - right. apply find_some in Hfind as [Hin Heq].
    ∃ i, j. unfold pairs in Hin. cbn in Hin.
    unfold same in Heq. cbn [fst snd] in Heq. apply String.eqb_eq in Heq.
    assert (Hi : i = 0 ∨ i = 1 ∨ i = 2) by intuition congruence.
    assert (Hj : j = 0 ∨ j = 1 ∨ j = 2) by intuition congruence.
    split; [lia|]. split; [lia|exact Heq].
  - left. intros i j Hi Hj Heq.
    assert (Hin : List.In (i,j) pairs).
    { assert (i = 0 ∨ i = 1 ∨ i = 2) by lia.
      assert (j = 0 ∨ j = 1 ∨ j = 2) by lia.
      unfold pairs. intuition subst; cbn; tauto. }
    pose proof (find_none _ _ Hfind _ Hin) as Hneq.
    unfold same in Hneq. cbn [fst snd] in Hneq.
    rewrite Heq, String.eqb_refl in Hneq. discriminate.
Qed.

Lemma settings_separation (updates : list (Addr.t × Numeric.U256.t)) query :
  {Forall (fun update ⇒ fst update = query ∨ slotsDisjoint (fst update) query) updates} +
  {∃ addr value i j,
    List.In (addr,value) updates ∧ addr ≠ query ∧
    0 ≤ i < 3 ∧ 0 ≤ j < 3 ∧ key addr i = key query j}.
Proof.
  induction updates as [|[addr value] updates IH].
  - left. constructor.
  - destruct (String.string_dec addr query) as [Heq|Hneq].
    + destruct IH as [Htail|Hcollision].
      × left. constructor; [left; exact Heq|exact Htail].
      × right. destruct Hcollision as [a [v [i [j [Hin Hrest]]]]].
        ∃ a, v, i, j. split; [right; exact Hin|exact Hrest].
    + destruct (config_slots_separation addr query) as [Hslots|Hcollision].
      × destruct IH as [Htail|Hcollision].
        -- left. constructor; [right; exact Hslots|exact Htail].
        -- right. destruct Hcollision as [a [v [i [j [Hin Hrest]]]]].
           ∃ a, v, i, j. split; [right; exact Hin|exact Hrest].
      × right. destruct Hcollision as [i [j Hcollision]].
        ∃ addr, value, i, j. split; [left; reflexivity|]. split; assumption.
Qed.
End Collision.