Library QedkConcrete.hoststoragewf

Storage-map ordering through concrete Host writes

Reading a just-written reserve slot requires a well-formed map, not only 32-byte values. SSTORE, the reserve setter and account normalization preserve that invariant; CREATE starts with empty storage.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostaccountformat QedkConcrete.stringmaps
  QedkConcrete.hostframe QedkConcrete.worldaccounts QedkConcrete.vmhostframe
  QedkConcrete.reserveencoding.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition worldStorageWf (world : W.t) : Prop :=
  Forall (fun entry ⇒ stringmaps.WellFormed (A.t.storage (snd entry)))
    (W.t.accounts world).

Definition hostStorageWf (s : S.t) : Prop := worldStorageWf (S.t.world_state s).

Section Accounts.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.

Lemma read_account_storage_wf world addr :
  worldStorageWf world → stringmaps.WellFormed (A.t.storage
    (State.op_pointcaretlparenrparen world (@W.account None addr))).
Proof.
  apply (read_account_property (fun ac ⇒ stringmaps.WellFormed (A.t.storage ac))).
  apply stringmaps.empty_wellformed.
Qed.

Lemma write_account_storage_wf keep world addr account :
  worldStorageWf world → stringmaps.WellFormed (A.t.storage account) →
  worldStorageWf (State.op_pointcaretlparenrparenltminus world
    (@W.account (Some keep) addr) account).
Proof. apply (write_account_property (fun ac ⇒ stringmaps.WellFormed (A.t.storage ac))). Qed.

Context {F : H.FArgs}.

Lemma transfer_preserves_storage_wf sender recipient amount s :
  hostStorageWf s → hostStorageWf (snd (@H.transfer_ether F sender recipient amount s)).
Proof.
  intro Hworld. unfold H.transfer_ether.
  cbn -[U.op_gteq U.op_plus U.op_minus W.account].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  - apply write_account_storage_wf.
    + apply write_account_storage_wf; [exact Hworld|apply read_account_storage_wf; exact Hworld].
    + apply read_account_storage_wf.
      apply write_account_storage_wf; [exact Hworld|apply read_account_storage_wf; exact Hworld].
  - exact Hworld.
Qed.

Context {UU : Basics.Unreachable unit}.

Lemma selfdestruct_preserves_storage_wf addr beneficiary s :
  hostStorageWf s → hostStorageWf (snd (@H.selfdestruct F addr beneficiary s)).
Proof.
  intro Hworld. unfold H.selfdestruct.
  cbn -[H.transfer_ether Addr._Set W.account].
  match goal with |- context [H.transfer_ether ?from ?to ?value ?before] ⇒
    pose proof (transfer_preserves_storage_wf from to value before Hworld) as Htransfer;
    destruct (H.transfer_ether from to value before) as [ok middle] end.
  cbn [snd] in Htransfer. cbn -[Addr._Set W.account].
  match goal with |- context [if ?created then _ else _] ⇒ destruct created end.
  - apply write_account_storage_wf; [exact Htransfer|apply read_account_storage_wf; exact Htransfer].
  - exact Htransfer.
Qed.

Context {UI : Basics.Unreachable int}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma set_storage_preserves_wf addr key value s :
  hostStorageWf s → hostStorageWf (snd (@H.set_storage F addr key value s)).
Proof.
  intro Hworld.
  change (worldStorageWf
    (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_wf; [exact Hworld|].
  apply stringmaps.update_wellformed, read_account_storage_wf. exact Hworld.
Qed.
End Accounts.

Lemma storage_wf_bind {A B : Set} (action : S.t → A × S.t)
    (next : A → S.t → B × S.t) :
  preservesCallback hostStorageWf action →
  (∀ value, preservesCallback hostStorageWf (next value)) →
  preservesCallback hostStorageWf (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
  intros Haction Hnext s Hwf. specialize (Haction s Hwf).
  change (hostStorageWf (snd (let '(value, middle) := action s in next value middle))).
  destruct (action s) as [value middle]. apply Hnext. exact Haction.
Qed.

Lemma storage_wf_return {A : Set} (value : A) :
  preservesCallback hostStorageWf (S.M.(Monad.State.State_result._return) value).
Proof. intros s Hwf. exact Hwf. Qed.

Lemma storage_wf_read {A : Set} (lens : Lens.t S.t A) :
  preservesCallback hostStorageWf (S.M.(Monad.State.State_result.op_exclamation) lens).
Proof. intros s Hwf. exact Hwf. Qed.

Lemma storage_wf_update {A : Set} (lens : Lens.t S.t A) f :
  (∀ s value, S.t.world_state (Lens.t.set lens value s) = S.t.world_state s) →
  preservesCallback hostStorageWf (S.M.(Monad.State.State_result.update_field) lens f).
Proof.
  intros Hframe s Hwf.
  change (worldStorageWf (S.t.world_state (Lens.t.set lens (f (Lens.t.get lens s)) s))).
  rewrite Hframe. exact Hwf.
Qed.

Lemma storage_wf_account_field
    {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}
    {T : Set} keep addr (field : Lens.t A.t T) value :
  (∀ ac, stringmaps.WellFormed (A.t.storage ac) →
    stringmaps.WellFormed (A.t.storage (Lens.t.set field value ac))) →
  preservesCallback hostStorageWf (S.M.(Monad.State.State_result.op_coloneq)
    (Lens.Infix.op_pipeminusminus (@S.account keep addr) field) value).
Proof.
  intros Hfield s Hwf. destruct keep as [keep|]; [|set (keep := false)].
  all: apply write_account_storage_wf; [exact Hwf|apply Hfield, read_account_storage_wf; exact Hwf].
Qed.

Ltac solve_storage_wf :=
  lazymatch goal with
  | |- preservesCallback hostStorageWf (S.M.(Monad.State.State_result.op_letdollar) _ _) ⇒
    apply storage_wf_bind; [solve_storage_wf|];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_storage_wf
  | |- preservesCallback hostStorageWf (S.M.(Monad.State.State_result._return) _) ⇒ apply storage_wf_return
  | |- preservesCallback hostStorageWf (S.M.(Monad.State.State_result.op_exclamation) _) ⇒ apply storage_wf_read
  | |- preservesCallback hostStorageWf (S.M.(Monad.State.State_result.update_field) _ _) ⇒
    apply storage_wf_update; intros; reflexivity
  | |- preservesCallback hostStorageWf (S.M.(Monad.State.State_result.op_coloneq)
      (Lens.Infix.op_pipeminusminus _ ?field) _) ⇒
    apply storage_wf_account_field; intros ac Hwf;
    lazymatch field with
    | A.TLens.nonce ⇒ exact Hwf
    | A.TLens.storage ⇒ apply stringmaps.empty_wellformed
    | A.TLens.code ⇒ exact Hwf
    end
  | |- preservesCallback hostStorageWf (if ?test then _ else _) ⇒
    destruct test; solve_storage_wf
  | |- preservesCallback hostStorageWf (match (if ?test then _ else _) with _ ⇒ _ end) ⇒
    destruct test; cbn beta iota zeta; solve_storage_wf
  | |- preservesCallback hostStorageWf (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct value; solve_storage_wf
  | |- preservesCallback hostStorageWf (match ?status with Evmc.Result.StatusCode.Success ⇒ _ | _ ⇒ _ end) ⇒
    destruct status; solve_storage_wf
  | |- preservesCallback hostStorageWf (@H.transfer_ether _ _ _ _) ⇒
    intro; apply transfer_preserves_storage_wf
  | Hvm : ∀ msg code, _ → preservesCallback hostStorageWf (_ msg code)
    |- preservesCallback hostStorageWf (_ ?msg ?code) ⇒ apply Hvm; assumption
  end.

Section Creation.
Context {F : H.FArgs}.
Context {UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
  {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.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}
  {U64i : Basics.Unreachable int64}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma creation_address_preserves_storage_wf msg :
  preservesCallback hostStorageWf (@H.contract_creation_address F msg).
Proof. unfold H.contract_creation_address, H.M. solve_storage_wf. Qed.

Lemma process_create_preserves_storage_wf msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    preservesCallback hostStorageWf ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback hostStorageWf (@H.process_create F msg).
Proof.
  intros Hvm Hgas. unfold H.process_create, H.M.
  apply storage_wf_bind; [apply creation_address_preserves_storage_wf|].
  intro addr. solve_storage_wf.
Qed.
End Creation.

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_wf world : worldStorageWf world → worldStorageWf (activate world).
Proof.
  intro Hwf. apply write_account_storage_wf; [exact Hwf|].
  apply read_account_storage_wf. exact Hwf.
Qed.

Lemma setter_preserves_storage_wf world block addr value after :
  worldStorageWf world → setter world block addr value = Some after → worldStorageWf after.
Proof.
  intros Hwf 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_wf false (activate world)
    (@Reserve_balance.precompile_address)).
  - apply activate_preserves_storage_wf. exact Hwf.
  - change (stringmaps.WellFormed
      (@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 store_config_wellformed.
    apply read_account_storage_wf, activate_preserves_storage_wf. exact Hwf.
Qed.
End ReserveSetter.

Print Assumptions process_create_preserves_storage_wf.
Print Assumptions setter_preserves_storage_wf.