Library QedkConcrete.vminvariant

Composing Host invariants through generated VM instructions

Stack, gas, memory and control-flow updates are local to the machine. Only Host callbacks can change account state. These lemmas apply to error exits as well as success, since the caller chooses the rollback boundary.
From Stdlib Require Import List ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm.
Set Default Goal Selector "!".

Module V := Vm.Make.
Module MS := V.MachineState.

Section Machine.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (invariant : Host_t → Prop).

Definition preservesHost {A : Set} (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, invariant (MS.t.host state) →
    invariant (MS.t.host (snd (action state))).

Lemma preserves_bind {A B : Set} (action : @V.M.t Host_t F A)
    (next : A → @V.M.t Host_t F B) :
  preservesHost action → (∀ value, preservesHost (next value)) →
  preservesHost (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hnext state Hinitial.
  specialize (Haction state Hinitial).
  change (invariant (MS.t.host (snd
    (match action state with
     | (inl value, middle) ⇒ next value middle
     | (inr error, middle) ⇒ (inr error, middle)
     end)))).
  destruct (action state) as [[value|error] middle]; cbn [snd] in ×.
  - apply Hnext. exact Haction.
  - exact Haction.
Qed.

Lemma preserves_return {A : Set} (value : A) :
  preservesHost (@V.M._return Host_t F A value).
Proof. intros state Hinitial. exact Hinitial. Qed.

Lemma preserves_fail {A : Set} (error : Evmc.Result.StatusCode.t) :
  preservesHost (@V.M.fail Host_t F A error).
Proof. intros state Hinitial. exact Hinitial. Qed.

Lemma preserves_read {A : Set} (read : @MS.t Host_t F → A) :
  preservesHost (fun state ⇒ (inl (read state), state)).
Proof. intros state Hinitial. exact Hinitial. Qed.

Lemma preserves_lens_read {A : Set} (lens : Lens.t (@MS.t Host_t F) A) :
  preservesHost (@V.M.op_exclamation Host_t F A lens).
Proof. intros state Hinitial. exact Hinitial. Qed.

Lemma preserves_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
  (∀ state, MS.t.host (Lens.t.set lens value state) = MS.t.host state) →
  preservesHost (@V.M.op_coloneq Host_t F A lens value).
Proof.
  intros Hframe state Hinitial. change (invariant (MS.t.host (Lens.t.set lens value state))).
  rewrite Hframe. exact Hinitial.
Qed.

Lemma preserves_update {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
  (∀ state value, MS.t.host (Lens.t.set lens value state) = MS.t.host state) →
  preservesHost (@V.M.update_field Host_t F A lens f).
Proof.
  intros Hframe state Hinitial. change (invariant (MS.t.host
    (Lens.t.set lens (f (Lens.t.get lens state)) state))).
  rewrite Hframe. exact Hinitial.
Qed.

Lemma preserves_lift {A : Set} (action : Host_t → A × Host_t) :
  (∀ host, invariant host → invariant (snd (action host))) →
  preservesHost (@V.M.HostAPI.lift Host_t F A action).
Proof.
  intros Haction state Hinitial. specialize (Haction (MS.t.host state) Hinitial).
  unfold V.M.HostAPI.lift. destruct (action (MS.t.host state)); exact Haction.
Qed.

Lemma preserves_fmap {A B : Set} (f : A → B) (action : @V.M.t Host_t F A) :
  preservesHost action → preservesHost (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
  intro Haction.
  change (preservesHost (@V.M.op_letdollar Host_t F A B action
    (fun value ⇒ @V.M._return Host_t F B (f value)))).
  apply preserves_bind; [exact Haction|]. intro value. apply preserves_return.
Qed.

Lemma preserves_when condition action :
  preservesHost action → preservesHost (@V.M.when_ Host_t F condition action).
Proof.
  intro Haction. destruct condition; [exact Haction|apply preserves_return].
Qed.

Lemma preserves_option {A : Set} error (value : option A) :
  preservesHost (@V.M.Option.or_fail Host_t F A error value).
Proof. destruct value; [apply preserves_return|apply preserves_fail]. Qed.

Lemma preserves_pop : preservesHost (@V.pop Host_t F).
Proof.
  intros state Hinitial. unfold V.pop. destruct (MS.t.stack state); exact Hinitial.
Qed.

Lemma preserves_pop2 : preservesHost (@V.pop2 Host_t F).
Proof.
  intros state Hinitial. unfold V.pop2.
  destruct (MS.t.stack state) as [|a [|b tail]]; exact Hinitial.
Qed.

Lemma preserves_pop3 : preservesHost (@V.pop3 Host_t F).
Proof.
  intros state Hinitial. unfold V.pop3.
  destruct (MS.t.stack state) as [|a [|b [|c tail]]]; exact Hinitial.
Qed.

Lemma preserves_push value : preservesHost (@V.push Host_t F value).
Proof.
  intros state Hinitial. unfold V.push.
  destruct (Z.geb (MS.t.stack_depth state) V.max_stack_depth); exact Hinitial.
Qed.

Lemma preserves_spend amount :
  preservesHost (@V.spend Host_t F amount).
Proof.
  intros state Hinitial. unfold V.spend.
  destruct (Gas.op_lt (MS.t.gas state) amount); exact Hinitial.
Qed.

Lemma preserves_pc f : preservesHost (@V.update_pc_and_continue Host_t F f).
Proof. intros state Hinitial. exact Hinitial. Qed.

Lemma preserves_increase_pc :
  preservesHost (@V.increase_pc_and_continue Host_t F).
Proof. apply preserves_pc. Qed.

Lemma preserves_finish returnOutput : preservesHost (@V.finish_execution Host_t F returnOutput).
Proof.
  unfold V.finish_execution. apply preserves_bind.
  - apply preserves_when, preserves_write. reflexivity.
  - intros []. apply preserves_return.
Qed.

Lemma preserves_seq_map {A B : Set} (f : A → @V.M.t Host_t F B) :
  (∀ x, preservesHost (f x)) →
  ∀ values, preservesHost (@V.M.Seq.mapM Host_t F A B f values).
Proof.
  intro Hf. induction values as [|value values IH].
  all: unfold V.M.Seq.mapM, Monad.Make_Monad.Seq.mapM, Monad.Make_Monad.Seq.sequence.
  all: rewrite Init.Wf.Fix_eq; [|intros seq left right Heq;
    destruct seq; cbn [OCamlSeq.uncons]; [reflexivity|rewrite Heq; reflexivity]].
  - apply preserves_return.
  - change (preservesHost (@V.M.op_letdollar Host_t F B (OCamlSeq.t B)
      (f value) (fun result ⇒ @V.M.op_letdollar Host_t F (OCamlSeq.t B) (OCamlSeq.t B)
        (@V.M.Seq.mapM Host_t F A B f values)
        (fun results ⇒ @V.M._return Host_t F (OCamlSeq.t B) (OCamlSeq.cons result results))))).
    apply preserves_bind; [apply Hf|]. intro result.
    apply preserves_bind; [exact IH|]. intro results. apply preserves_return.
Qed.

End Machine.

Ltac solve_host_invariant_extra := fail.

Ltac solve_host_invariant :=
  lazymatch goal with
  | |- preservesHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply preserves_bind; [solve_host_invariant|];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_host_invariant
  | |- preservesHost _ (@V.M.op_gtgteq _ _ _ _ _ _) ⇒
    apply preserves_bind; [solve_host_invariant|];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_host_invariant
  | |- preservesHost _ (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply preserves_fmap; solve_host_invariant
  | |- preservesHost _ (@V.M.fmap _ _ _ _ _ _) ⇒
    apply preserves_fmap; solve_host_invariant
  | |- preservesHost _ (@V.M.when_ _ _ _ _) ⇒
    apply preserves_when; solve_host_invariant
  | |- preservesHost _ (if ?condition then _ else _) ⇒
    destruct condition; solve_host_invariant
  | |- preservesHost _ (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct value; solve_host_invariant
  | |- preservesHost _ (@V.M._return _ _ _ _) ⇒ apply preserves_return
  | |- preservesHost _ (@V.M.fail _ _ _ _) ⇒ apply preserves_fail
  | |- preservesHost _ (@V.pop _ _) ⇒ apply preserves_pop
  | |- preservesHost _ (@V.pop2 _ _) ⇒ apply preserves_pop2
  | |- preservesHost _ (@V.pop3 _ _) ⇒ apply preserves_pop3
  | |- preservesHost _ (@V.push _ _ _) ⇒ apply preserves_push
  | |- preservesHost _ (@V.spend _ _ _) ⇒ apply preserves_spend
  | |- preservesHost _ (@V.update_pc_and_continue _ _ _) ⇒ apply preserves_pc
  | |- preservesHost _ (@V.increase_pc_and_continue _ _) ⇒ apply preserves_increase_pc
  | |- preservesHost _ (@V.finish_execution _ _ _) ⇒ apply preserves_finish
  | |- preservesHost _ (@V.M.op_exclamation _ _ _ _) ⇒ apply preserves_lens_read
  | |- preservesHost _ (@V.M.Option.or_fail _ _ _ _ _) ⇒ apply preserves_option
  | |- preservesHost _ (@V.M.op_coloneq _ _ _ _ _) ⇒ eapply preserves_write; reflexivity
  | |- preservesHost _ (@V.M.update_field _ _ _ _ _) ⇒ eapply preserves_update; reflexivity
  | |- preservesHost _ (fun state ⇒ _) ⇒ apply preserves_read
  | _ ⇒ solve_host_invariant_extra
  end.

Print Assumptions preserves_lift.