Library QedkConcrete.reserveencoding

Storage encoding of reserve records

Masking the base key leaves room for all three fields without U256 wrap. This proves separation within one record, not collision freedom between different addresses' hashed records.
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Reserve_balance QedkConcrete.worldaccounts
  QedkConcrete.wordrange QedkConcrete.bytecodec QedkConcrete.stringmaps.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Encoding.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.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 base := (@Reserve_balance.config_slot).
Local Abbreviation encode := (@U.to_repr).
Local Abbreviation key := (fun addr offset ⇒ encode (@U.op_plus (base addr) (@U.of_int offset))).

Lemma config_base_bound addr : 0 ≤ base addr ≤ 2^256 - 128.
Proof.
  unfold Reserve_balance.config_slot.
  match goal with |- context [@U.logand ?hash _] ⇒ set (digest := hash) end.
  change (0 ≤ Z.land (Z.shiftl (Z.land digest (2^256-128)) 0) (Z.ones 256) ≤ 2^256-128).
  assert (Hlo : 0 ≤ Z.land digest (2^256-128)) by (apply Z.land_nonneg; right; lia).
  assert (Hhi : Z.land digest (2^256-128) ≤ 2^256-128).
  { assert (Hdiff : 0 ≤ Z.land (2^256-128) (Z.lnot digest)) by (apply Z.land_nonneg; left; lia).
    rewrite <- Z.sub_land_same_l, Z.land_comm in Hdiff. lia. }
  rewrite Z.shiftl_0_r, Z.land_ones, Z.mod_small by lia. lia.
Qed.

Lemma config_key_word addr offset : 0 ≤ offset < 128 →
  @U.op_plus (base addr) (@U.of_int offset) = base addr + offset.
Proof.
  intro Hoffset. change (@U.op_plus (base addr) (@U.of_z_exn offset) = base addr + offset).
  rewrite word_of_z by (change (0 ≤ offset < 2^256); lia).
  apply u256_add. pose proof (config_base_bound addr). change (0 ≤ base addr + offset < 2^256). lia.
Qed.

Lemma config_keys_distinct addr left right :
  0 ≤ left < 128 → 0 ≤ right < 128 → left ≠ right → key addr left ≠ key addr right.
Proof.
  intros Hleft Hright Hneq Heq.
  apply word_encoding_injective in Heq;
    [|apply word_add|apply word_add].
  rewrite !config_key_word in Heq by assumption. lia.
Qed.

Lemma config_value_roundtrip value : (value < (2^256)%N)%N →
  @U.to_uint (@U.of_repr (encode (@U.of_uint_exn value))) = value.
Proof.
  intro Hvalue.
  change (@U.to_uint (@U.of_repr (encode (@U.of_z_exn (Z.of_N value)))) = value).
  rewrite word_of_z by (change (0 ≤ Z.of_N value < 2^256); nia).
  rewrite word_encoding_roundtrip by (change (0 ≤ Z.of_N value < 2^256); nia).
  change (@Support.UintN.of_z_exn (Z.of_N value) = value).
  unfold Support.UintN.of_z_exn, Support.UintN.of_z_opt, Support.UintN.in_range.
  assert (Hnonneg : Z.leb 0 (Z.of_N value) = true) by (apply Z.leb_le; lia).
  rewrite Hnonneg. apply N2Z.id.
Qed.

Local Abbreviation zeroBytes :=
  (@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32).

Proof abbreviations for the two local functions in the generated setter and reader; zero storage values are represented by absent map entries.
Definition storeConfigField storage addr offset value :=
  let bytes := encode (@U.of_uint_exn value) in
  if Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) bytes zeroBytes
  then OCamlMap.Make.remove stringOrder (key addr offset) storage
  else OCamlMap.Make.add stringOrder (key addr offset) bytes storage.

Definition readConfigField storage addr offset :=
  @U.to_uint (@U.of_repr
    (Option.value (OCamlMap.Make.find_opt stringOrder (key addr offset) storage) zeroBytes)).

Lemma b32_equal_true left right :
  Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) left right = true → left = right.
Proof.
  change (Z.eqb (OCamlString.compare left right) 0 = true → left = right).
  rewrite OCamlString.compare_matches_structural. unfold OCamlString.compare_structural. destruct (Strings.String.eqb left right) eqn:Heq.
  - intros _. apply Strings.String.eqb_eq. exact Heq.
  - destruct (Basics.String.ltb left right); discriminate.
Qed.

Lemma store_config_wellformed storage addr offset value :
  WellFormed storage → WellFormed (storeConfigField storage addr offset value).
Proof.
  intro Hwf. unfold storeConfigField. destruct (Byte_string.Fixed.Fixed_result.op_eq _ _ _);
    [apply remove_wellformed|apply add_wellformed]; exact Hwf.
Qed.

Lemma read_store_config_same storage addr offset value :
  WellFormed storage → (value < 2^256)%N →
  readConfigField (storeConfigField storage addr offset value) addr offset = value.
Proof.
  intros Hwf Hvalue. unfold readConfigField, storeConfigField.
  destruct (Byte_string.Fixed.Fixed_result.op_eq _ _ _) eqn:Heq.
  - rewrite find_remove_same by exact Hwf. cbn [Option.value].
    apply b32_equal_true in Heq. rewrite <- Heq. apply config_value_roundtrip. exact Hvalue.
  - rewrite find_add_same by exact Hwf. cbn [Option.value].
    apply config_value_roundtrip. exact Hvalue.
Qed.

Lemma read_store_config_other storage addr offset other value :
  WellFormed storage → 0 ≤ offset < 128 → 0 ≤ other < 128 → offset ≠ other →
  readConfigField (storeConfigField storage addr offset value) addr other = readConfigField storage addr other.
Proof.
  intros Hwf Hoffset Hother Hneq. unfold readConfigField, storeConfigField.
  destruct (Byte_string.Fixed.Fixed_result.op_eq _ _ _);
    [rewrite find_remove_other|rewrite find_add_other];
    try exact Hwf; try (apply config_keys_distinct; assumption); reflexivity.
Qed.

This is the cross-account hash-separation obligation. It is not inferred from the hash's output size or from separation within a single record.
Definition configSlotsDisjoint a b : Prop :=
  ∀ i j, 0 ≤ i < 3 → 0 ≤ j < 3 → key a i ≠ key b j.

Lemma read_store_config_disjoint storage addr other offset otherOffset value :
  WellFormed storage → configSlotsDisjoint addr other →
  0 ≤ offset < 3 → 0 ≤ otherOffset < 3 →
  readConfigField (storeConfigField storage addr offset value) other otherOffset =
    readConfigField storage other otherOffset.
Proof.
  intros Hwf Hslots Hoffset Hother. unfold readConfigField, storeConfigField.
  destruct (Byte_string.Fixed.Fixed_result.op_eq _ _ _);
    [rewrite find_remove_other|rewrite find_add_other];
    try exact Hwf; try (apply Hslots; assumption); reflexivity.
Qed.

End Encoding.

Print Assumptions config_keys_distinct.
Print Assumptions read_store_config_same.