Library QedkConcrete.vmsizes

Byte-size invariants for the generated machine

The child-memory budget is encoded as uint32. Keeping active memory within that budget makes its subtraction and conversion safe. The separate output bound is what RETURNDATASIZE needs to produce a U256 word. These are machine invariants to prove from execution, not contracts imposed on account state.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.memorysizes QedkConcrete.memorywords
  QedkConcrete.vmmachinewords QedkConcrete.wordrange.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Machine.
Context {Host_t : Set} {F : @V.FArgs Host_t}.

Definition memoryFits (memory : @Mem.t Host_t F) : Prop :=
  (Mem.t.active_bytes memory ≤ Mem.t.memory_capacity memory)%N ∧
  Z.of_N (Mem.t.memory_capacity memory) < 2^32.

Definition machineByteSizes (state : @MS.t Host_t F) : Prop :=
  memoryFits (MS.t.memory state) ∧ OCamlString.length (MS.t.output_buffer state) < 2^256.

Definition sizesSafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, machineByteSizes state →
    machineByteSizes (snd (action state)) ∧
    match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end.

Lemma initial_machine_sizes {UN : Basics.Unreachable Support.UintN.t}
    {UF : Basics.Unreachable U.t} {UZ : Basics.Unreachable Numeric.Integer.t}
    {UU : Basics.Unreachable unit} host gas capacity :
  Z.of_N capacity < 2^32 →
  machineByteSizes (@MS.initial Host_t F host gas capacity).
Proof. intros Hcapacity. split; [split; [apply N.le_0_l|exact Hcapacity]|cbn; lia]. Qed.

Lemma sizes_safe_bind {A B : Set} (P : A → Prop) (Q : B → Prop)
    (action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
  sizesSafe P action → (∀ value, P value → sizesSafe Q (next value)) →
  sizesSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hnext state Hinitial. specialize (Haction state Hinitial).
  change (machineByteSizes (snd
    (match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
    match fst
      (match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
    with inl v ⇒ Q v | inr _ ⇒ True end).
  destruct (action state) as [[value|error] middle]; cbn [snd fst] in ×.
  - apply Hnext; [exact (proj2 Haction)|exact (proj1 Haction)].
  - exact Haction.
Qed.

Lemma sizes_safe_return {A : Set} (P : A → Prop) value :
  P value → sizesSafe P (@V.M._return Host_t F A value).
Proof. intros HP state Hinitial. split; assumption. Qed.

Lemma sizes_safe_fail {A : Set} (P : A → Prop) error :
  sizesSafe P (@V.M.fail Host_t F A error).
Proof. intros state Hinitial. split; [exact Hinitial|exact I]. Qed.

Lemma sizes_safe_read {A : Set} (P : A → Prop) (read : @MS.t Host_t F → A) :
  (∀ state, machineByteSizes state → P (read state)) →
  sizesSafe P (fun state ⇒ (inl (read state), state)).
Proof. intros Hread state Hinitial. split; [exact Hinitial|apply Hread; exact Hinitial]. Qed.

Lemma sizes_safe_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
  (∀ state, machineByteSizes state → machineByteSizes (Lens.t.set lens value state)) →
  sizesSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F A lens value).
Proof. intros Hwrite state Hinitial. split; [apply Hwrite; exact Hinitial|exact I]. Qed.

Lemma sizes_safe_update {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
  (∀ state, machineByteSizes state →
    machineByteSizes (Lens.t.set lens (f (Lens.t.get lens state)) state)) →
  sizesSafe (fun _ ⇒ True) (@V.M.update_field Host_t F A lens f).
Proof. intros Hupdate state Hinitial. split; [apply Hupdate; exact Hinitial|exact I]. Qed.

Lemma sizes_safe_update_frame {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
  (∀ state value, machineByteSizes state → machineByteSizes (Lens.t.set lens value state)) →
  sizesSafe (fun _ ⇒ True) (@V.M.update_field Host_t F A lens f).
Proof. intro Hframe. apply sizes_safe_update. intros. apply Hframe. assumption. Qed.

Lemma sizes_safe_fmap_ignore {A B : Set} (f : A → B) action :
  sizesSafe (fun _ ⇒ True) action →
  sizesSafe (fun _ ⇒ True) (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
  intro Haction. change (sizesSafe (fun _ ⇒ True)
    (@V.M.op_letdollar Host_t F A B action (fun v ⇒ @V.M._return Host_t F B (f v)))).
  eapply sizes_safe_bind; [exact Haction|]. intros. apply sizes_safe_return. exact I.
Qed.

Lemma sizes_safe_option {A : Set} (P : A → Prop) error (value : option A) :
  (∀ v, value = Some v → P v) → sizesSafe P (@V.M.Option.or_fail Host_t F A error value).
Proof.
  intro Hvalue. destruct value as [v|].
  - apply sizes_safe_return, Hvalue. reflexivity.
  - apply sizes_safe_fail.
Qed.

Lemma sizes_safe_host {A : Set} (action : Host_t → A × Host_t) :
  sizesSafe (fun _ ⇒ True) (@V.M.HostAPI.lift Host_t F A action).
Proof.
  intros state Hinitial. unfold V.M.HostAPI.lift.
  destruct (action (MS.t.host state)). split; [exact Hinitial|exact I].
Qed.

Lemma sizes_safe_pop : sizesSafe (fun _ ⇒ True) (@V.pop Host_t F).
Proof.
  intros state Hinitial. unfold V.pop. destruct (MS.t.stack state);
    split; [exact Hinitial|exact I|exact Hinitial|exact I].
Qed.

Lemma sizes_safe_pop2 : sizesSafe (fun _ ⇒ True) (@V.pop2 Host_t F).
Proof.
  intros state Hinitial. unfold V.pop2. destruct (MS.t.stack state) as [|a [|b tail]];
    split; first [exact Hinitial|exact I].
Qed.

Lemma sizes_safe_pop3 : sizesSafe (fun _ ⇒ True) (@V.pop3 Host_t F).
Proof.
  intros state Hinitial. unfold V.pop3. destruct (MS.t.stack state) as [|a [|b [|c tail]]];
    split; first [exact Hinitial|exact I].
Qed.

Lemma sizes_safe_push value : sizesSafe (fun _ ⇒ True) (@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); split; first [exact Hinitial|exact I].
Qed.

Lemma sizes_safe_spend {UG : Basics.Unreachable Gas.t} amount :
  sizesSafe (fun _ ⇒ True) (@V.spend Host_t F amount).
Proof.
  intros state Hinitial. unfold V.spend.
  destruct (Gas.op_lt (MS.t.gas state) amount); split; first [exact Hinitial|exact I].
Qed.

Lemma sizes_safe_pc f : sizesSafe (fun _ ⇒ True) (@V.update_pc_and_continue Host_t F f).
Proof. intros state Hinitial. split; [exact Hinitial|exact I]. Qed.

Lemma sizes_safe_increase_pc {UF : Basics.Unreachable U.t} :
  sizesSafe (fun _ ⇒ True) (@V.increase_pc_and_continue Host_t F).
Proof. apply sizes_safe_pc. Qed.

Lemma sizes_safe_memory_write memory : memoryFits memory →
  sizesSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.memory memory).
Proof.
  intros Hmemory. apply sizes_safe_write. intros state [Hbefore Hout]. split; assumption.
Qed.

Lemma sizes_safe_output_write bytes : OCamlString.length bytes < 2^256 →
  sizesSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.output_buffer bytes).
Proof.
  intros Hbytes. apply sizes_safe_write. intros state [Hmemory Hout]. split; assumption.
Qed.

Lemma sizes_safe_memory_read : sizesSafe memoryFits (@V.M.op_exclamation Host_t F _ MS.memory).
Proof. apply sizes_safe_read. intros state [Hmemory _]. exact Hmemory. Qed.

Lemma sizes_safe_finish keepOutput : sizesSafe (fun _ ⇒ True) (@V.finish_execution Host_t F keepOutput).
Proof.
  unfold V.finish_execution. eapply sizes_safe_bind with (P := fun _ ⇒ True).
  - destruct keepOutput; [apply sizes_safe_return; exact I|apply sizes_safe_output_write; cbn; lia].
  - intros. apply sizes_safe_return. exact I.
Qed.

Lemma sizes_safe_seq {A B : Set} (f : A → @V.M.t Host_t F B) :
  (∀ x, sizesSafe (fun _ ⇒ True) (f x)) →
  ∀ values, sizesSafe (fun _ ⇒ True) (@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 sizes_safe_return. exact I.
  - change (sizesSafe (fun _ ⇒ True) (@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))))).
    eapply sizes_safe_bind; [apply Hf|]. intros result _.
    eapply sizes_safe_bind; [exact IH|]. intros results _. apply sizes_safe_return. exact I.
Qed.

Section Memory.
Context {UN : Basics.Unreachable Support.UintN.t} {UF : Basics.Unreachable U.t}
  {UB : Basics.Unreachable Byte_string.Bytes.t} {UU : Basics.Unreachable unit}
  {US : Basics.Unreachable string} {ULI : Basics.Unreachable (list int)}
  {UI : Basics.Unreachable int}
  {UZ : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma write_block_preserves_sizes start bytes memory : memoryFits memory →
  memoryFits (@Mem.write_block_at Host_t F start bytes memory).
Proof.
  unfold memoryFits. rewrite write_block_active_bytes, write_block_capacity. trivial.
Qed.

Lemma write_word_preserves_sizes {UR : Basics.Unreachable U.Repr.t}
    {UC : Basics.Unreachable ascii} {UP : Basics.Unreachable (int × int)} start value memory :
  memoryFits memory →
  memoryFits (@Mem.write_word_at Host_t F start value memory).
Proof. unfold memoryFits. rewrite write_word_active_bytes, write_word_capacity. trivial. Qed.

Lemma write_byte_preserves_sizes start byte memory : memoryFits memory →
  memoryFits (@Mem.write_byte_at Host_t F start byte memory).
Proof. unfold memoryFits. rewrite write_byte_active_bytes, write_byte_capacity. trivial. Qed.

Lemma sizes_safe_memory_update f :
  (∀ memory, memoryFits memory → memoryFits (f memory)) →
  sizesSafe (fun _ ⇒ True) (@V.M.update_field Host_t F _ MS.memory f).
Proof.
  intros Hf. apply sizes_safe_update. intros state [Hmemory Hout].
  split; [apply Hf; exact Hmemory|exact Hout].
Qed.

Lemma extend_preserves_sizes
    {UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}
    start size before after :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  memoryFits before → @Mem.extend_to Host_t F start size before = Some after → memoryFits after.
Proof.
  intros Hrevision [Hactive Hcap] Hextend. unfold Mem.extend_to in Hextend. rewrite Hrevision in Hextend.
  split.
  - eapply extend_nine_active_within_capacity; eassumption.
  - unfold Mem.extend_to_monad_nine in Hextend.
    repeat match type of Hextend with context [if ?test then _ else _] ⇒ destruct test end;
      inversion Hextend; subst; exact Hcap.
Qed.

Lemma sizes_safe_extend_memory {E : @V.Executor.FArgs Host_t F}
    {UG : Basics.Unreachable Gas.t}
    {Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
    {UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))} start size :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  sizesSafe (fun _ ⇒ True) (@V.Executor.extend_memory_to Host_t F E start size).
Proof.
  intro Hrevision. unfold V.Executor.extend_memory_to.
  destruct (U.op_eq size (@U.zero)); [apply sizes_safe_return; exact I|].
  eapply sizes_safe_bind; [apply sizes_safe_memory_read|]. intros memory Hmemory.
  eapply sizes_safe_bind.
  - apply (sizes_safe_option memoryFits). intros extended Hextend.
    eapply extend_preserves_sizes; eassumption.
  - intros extended Hextended.
    eapply sizes_safe_bind; [apply sizes_safe_memory_write; exact Hextended|]. intros [] _.
    eapply sizes_safe_bind with (P := fun _ ⇒ True).
    + apply sizes_safe_fmap_ignore, sizes_safe_read. intros; exact I.
    + intros newSize _. destruct (Support.UintN.op_gteq _ _); apply sizes_safe_return; exact I.
Qed.
End Memory.
End Machine.

Print Assumptions sizes_safe_extend_memory.