Library QedkConcrete.hoststoragewords

Word widths in persistent and transient account storage

Storage reads return a stored 32-byte value or a zero word. SSTORE and the reserve setter write encoded words; no assumption about their contents is needed to preserve this representation invariant.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.worldaccounts
  QedkConcrete.wordrange QedkConcrete.bytewords QedkConcrete.memorywords
  QedkConcrete.reserveencoding.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition storageWords (storage : list (string × string)) : Prop :=
  Forall (fun entry ⇒ OCamlString.length (snd entry) = 32) storage.

Definition accountStorageWords (world : W.t) : Prop :=
  Forall (fun entry ⇒ storageWords (A.t.storage (snd entry))) (W.t.accounts world).

Lemma map_remove_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
    (P : Value → Prop) entries key :
  Forall (fun entry ⇒ P (snd entry)) entries →
  Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.remove order key entries).
Proof.
  intro Hentries. induction Hentries as [|[k v] rest Hv Hrest IH]; [constructor|].
  cbn [OCamlMap.Make.remove]. destruct (OCamlMap.Make.key_before order key k).
  - constructor; assumption.
  - destruct (OCamlMap.Make.key_equal order key k); [exact Hrest|constructor; assumption].
Qed.

Section Storage.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Context {UI : Basics.Unreachable int}
    {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma read_account_storage_words world addr :
  accountStorageWords world →
  storageWords (A.t.storage (State.op_pointcaretlparenrparen world (@W.account None addr))).
Proof.
  intro Hworld.
  change (storageWords (A.t.storage
    (match Addr.Map.find_opt addr (W.t.accounts world) with
     | Some account ⇒ account | None ⇒ @A.empty end))).
  destruct (Addr.Map.find_opt addr (W.t.accounts world)) eqn:Hfind.
  - eapply (map_find_value_property _ (fun account ⇒ storageWords (A.t.storage account))).
    + exact Hworld.
    + exact Hfind.
  - constructor.
Qed.

Lemma write_account_storage_words keep world addr value :
  accountStorageWords world → storageWords (A.t.storage value) →
  accountStorageWords (State.op_pointcaretlparenrparenltminus world
    (@W.account (Some keep) addr) value).
Proof.
  intros Hworld Hvalue. destruct keep.
  - change (Forall (fun entry ⇒ storageWords (A.t.storage (snd entry)))
      (OCamlMap.Make.add stringmaps.stringOrder addr value (W.t.accounts world))).
    apply (map_add_value_property _ (fun account ⇒ storageWords (A.t.storage account))); assumption.
  - change (Forall (fun entry ⇒ storageWords (A.t.storage (snd entry)))
      (OCamlMap.Make.update stringmaps.stringOrder addr
        (fun _ ⇒ if @A.is_empty value then None else Some value) (W.t.accounts world))).
    unfold OCamlMap.Make.update. destruct (@A.is_empty value).
    + apply (map_remove_value_property _ (fun account ⇒ storageWords (A.t.storage account))). exact Hworld.
    + apply (map_add_value_property _ (fun account ⇒ storageWords (A.t.storage account))); assumption.
Qed.

Lemma get_storage_word_length {F : H.FArgs} addr key s :
  accountStorageWords (S.t.world_state s) →
  OCamlString.length (fst (@H.get_storage F addr key s)) = 32.
Proof.
  intro Hworld.
  pose proof (read_account_storage_words _ addr Hworld) as Hstorage.
  change (OCamlString.length (Option.value
    (OCamlMap.Make.find_opt stringmaps.stringOrder key
      (A.t.storage (State.op_pointcaretlparenrparen (S.t.world_state s) (@W.account None addr))))
    (@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32)) = 32).
  match goal with |- context [OCamlMap.Make.find_opt ?order ?key ?storage] ⇒
    destruct (OCamlMap.Make.find_opt order key storage) as [value|] eqn:Hfind end.
  - cbn [Option.value].
    eapply (map_find_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
      [exact Hstorage|exact Hfind].
  - apply b32_zeros_length.
Qed.

Lemma set_storage_preserves_words {F : H.FArgs} addr key value s :
  accountStorageWords (S.t.world_state s) → OCamlString.length value = 32 →
  accountStorageWords (S.t.world_state (snd (@H.set_storage F addr key value s))).
Proof.
  intros Hworld Hvalue.
  change (accountStorageWords
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some true) addr)
      (A.t.with_storage
        (OCamlMap.Make.update stringmaps.stringOrder key
          (fun _ ⇒ if Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) value
            (@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32)
            then None else Some value)
          (A.t.storage (State.op_pointcaretlparenrparen (S.t.world_state s) (@W.account None addr))))
        (State.op_pointcaretlparenrparen (S.t.world_state s) (@W.account None addr))))).
  apply write_account_storage_words; [exact Hworld|].
  cbn [A.t.storage A.t.with_storage]. unfold OCamlMap.Make.update.
  destruct (Byte_string.Fixed.Fixed_result.op_eq _ _ _).
  - apply (map_remove_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32)),
      read_account_storage_words. exact Hworld.
  - apply (map_add_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
      [exact Hvalue|apply read_account_storage_words; exact Hworld].
Qed.
End Storage.

Lemma config_field_preserves_words
    {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.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)}
    storage addr offset value :
  storageWords storage →
  storageWords (@storeConfigField storage addr offset value).
Proof.
  intro Hstorage. unfold storeConfigField.
  destruct (Byte_string.Fixed.Fixed_result.op_eq _ _ _).
  - apply (map_remove_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32)). exact Hstorage.
  - apply (map_add_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
      [apply word_encoding_length|exact Hstorage].
Qed.

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 U.Repr.t} {UF : Basics.Unreachable U.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 activate := (@Reserve_balance.activate_precompile).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).

Lemma activate_preserves_storage_words world :
  accountStorageWords world → accountStorageWords (activate world).
Proof.
  intro Hworld. apply write_account_storage_words; [exact Hworld|].
  apply read_account_storage_words. exact Hworld.
Qed.

Lemma setter_preserves_storage_words world block addr value after :
  accountStorageWords world → setter world block addr value = Some after →
  accountStorageWords after.
Proof.
  intros Hworld Hset. unfold Reserve_balance.set_reserve_balance in Hset.
  match type of Hset with context [match ?result with None ⇒ _ | Some _ ⇒ _ end] ⇒
    destruct result as [record|] end; [|discriminate].
  injection Hset as <-.
  eapply (@write_account_storage_words false (activate world)
    (@Reserve_balance.precompile_address)).
  - apply activate_preserves_storage_words. exact Hworld.
  - change (storageWords
      (@storeConfigField
        (@storeConfigField
          (@storeConfigField
            (A.t.storage (State.op_pointcaretlparenrparen (activate world)
              (@W.account None (@Reserve_balance.precompile_address))))
            addr 0 (Reserve_config.t.settled record)) addr 1 (Reserve_config.t.pending record))
        addr 2 (Reserve_config.t.valid_from record))).
    repeat apply config_field_preserves_words.
    apply read_account_storage_words, activate_preserves_storage_words. exact Hworld.
Qed.
End ReserveSetter.

Definition transientStorageWords (s : S.t) : Prop :=
  Forall (fun entry ⇒ storageWords (snd entry)) (S.t.transient_storage s).

Lemma transient_account_words s addr :
  transientStorageWords s →
  storageWords (Option.value (Addr.Map.find_opt addr (S.t.transient_storage s)) nil).
Proof.
  intro Hstorage. destruct (Addr.Map.find_opt addr (S.t.transient_storage s)) eqn:Hfind.
  - eapply (map_find_value_property _ storageWords); [exact Hstorage|exact Hfind].
  - constructor.
Qed.

Section Transient.
Context {F : H.FArgs} {UI : Basics.Unreachable int}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma get_transient_storage_word_length s addr key :
  transientStorageWords s →
  OCamlString.length (fst (@H.get_transient_storage F addr key s)) = 32.
Proof.
  intro Hstorage. pose proof (transient_account_words s addr Hstorage) as Haccount.
  change (OCamlString.length
    (Option.value (OCamlMap.Make.find_opt stringmaps.stringOrder key
      (Option.value (Addr.Map.find_opt addr (S.t.transient_storage s)) nil))
      (@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32)) = 32).
  match goal with |- context [Option.value ?found _] ⇒ destruct found eqn:Hfind end.
  - eapply (map_find_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
      [exact Haccount|exact Hfind].
  - apply b32_zeros_length.
Qed.

Lemma set_transient_storage_preserves_words s addr key value :
  transientStorageWords s → OCamlString.length value = 32 →
  transientStorageWords (snd (@H.set_transient_storage F addr key value s)).
Proof.
  intros Hstorage Hvalue.
  change (Forall (fun entry ⇒ storageWords (snd entry))
    (OCamlMap.Make.add stringmaps.stringOrder addr
      (OCamlMap.Make.add stringmaps.stringOrder key value
        (Option.value (Addr.Map.find_opt addr (S.t.transient_storage s)) nil))
      (S.t.transient_storage s))).
  apply (map_add_value_property _ storageWords); [|exact Hstorage].
  apply (map_add_value_property _ (fun bytes ⇒ OCamlString.length bytes = 32));
    [exact Hvalue|apply transient_account_words; exact Hstorage].
Qed.
End Transient.

Print Assumptions get_storage_word_length.
Print Assumptions set_storage_preserves_words.
Print Assumptions setter_preserves_storage_words.
Print Assumptions set_transient_storage_preserves_words.