Library QedkConcrete.vmcreatewords

Machine-word preservation through contract creation

Creation forwards a bounded share of the remaining gas. Its endowment comes from the word-checked stack. Success pushes the address converted to a 32-byte word; failure pushes zero. No contract-address width premise is needed for that conversion.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmmachinewords QedkConcrete.vmbytewords
  QedkConcrete.vmhostwords QedkConcrete.vmenvironmentwords
  QedkConcrete.vmhostframe QedkConcrete.wordrange QedkConcrete.vmboundedframe.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas
  MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".

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

Definition boundedMachineSafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :=
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → machineWords invariant state →
    machineWords invariant (snd (action state)) ∧
    match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end.

Lemma bounded_words_from_unbounded {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
  machineSafe invariant P action → boundedMachineSafe P action.
Proof. intros Haction state _ Hwords. apply Haction. exact Hwords. Qed.

Lemma bounded_words_bind_prefix {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) :
  machineSafe invariant P action → gas_nonincreasing action →
  (∀ value, P value → boundedMachineSafe Q (next value)) →
  boundedMachineSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hgas Hnext state Hbound Hwords.
  pose proof (Haction state Hwords) as Hprefix.
  change (machineWords invariant (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] eqn:Hrun; cbn [fst snd] in Hprefix.
  - apply Hnext; [exact (proj2 Hprefix)| |exact (proj1 Hprefix)].
    pose proof (Hgas state value middle Hrun). lia.
  - exact Hprefix.
Qed.

Lemma bounded_words_bind_suffix {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) :
  boundedMachineSafe P action →
  (∀ value, P value → machineSafe invariant Q (next value)) →
  boundedMachineSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hnext state Hbound Hwords.
  pose proof (Haction state Hbound Hwords) as Hprefix.
  change (machineWords invariant (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 [fst snd] in Hprefix.
  - apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
  - exact Hprefix.
Qed.

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

Lemma safe_fmap_ignore {A B : Set} (f : A → B) (action : @V.M.t Host_t F A) :
  machineSafe invariant (fun _ ⇒ True) action →
  machineSafe invariant (fun _ ⇒ True) (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
  intro Haction. change (machineSafe invariant (fun _ ⇒ True)
    (@V.M.op_letdollar Host_t F A B action (fun value ⇒ @V.M._return Host_t F B (f value)))).
  eapply safe_bind; [exact Haction|]. intros value _. apply safe_return. exact I.
Qed.

Lemma safe_call msg :
  preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg) →
  machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.call Host_t F msg).
Proof.
  intro Hcall. unfold V.M.HostAPI.call.
  eapply safe_bind; [apply safe_host_frame; exact Hcall|]. intros result _.
  match goal with |- machineSafe _ _ (if ?test then _ else _) ⇒ destruct test end.
  - apply safe_fail.
  - apply safe_return. exact I.
Qed.
End Composition.

Ltac solve_create_words Hchild :=
  lazymatch goal with
  | |- machineSafe _ _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    eapply safe_bind with (P := fun _ ⇒ True);
    [solve_create_words Hchild|];
    let v := fresh "value" in intros v _;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_create_words Hchild
  | |- machineSafe _ _ (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply safe_fmap_ignore; solve_create_words Hchild
  | |- machineSafe _ _ (@V.M.when_ _ _ _ _) ⇒ apply safe_when; solve_create_words Hchild
  | |- machineSafe _ _ (if ?test then _ else _) ⇒ destruct test; solve_create_words Hchild
  | |- machineSafe _ _ (@V.M._return _ _ _ _) ⇒ apply safe_return; exact I
  | |- machineSafe _ _ (@V.M.fail _ _ _ _) ⇒ apply safe_fail
  | |- machineSafe _ _ (@V.M.op_exclamation _ _ _ _) ⇒ apply safe_read; intros; exact I
  | |- machineSafe _ _ (@V.M.op_coloneq _ _ _ _ _) ⇒ apply safe_write; let st := fresh "state" in let Hw := fresh "Hwords" in intros st Hw; exact Hw
  | |- machineSafe _ _ (@V.M.update_field _ _ _ _ _) ⇒ apply safe_update; let st := fresh "state" in let Hw := fresh "Hwords" in intros st Hw; exact Hw
  | |- machineSafe _ _ (@V.spend _ _ _) ⇒ apply safe_spend
  | |- machineSafe _ _ (@V.push _ _ _) ⇒ apply safe_push; first [apply word_zero|apply address_word]
  | |- machineSafe _ _ (@V.M.HostAPI.lift _ _ _ _) ⇒ apply safe_host_frame; unfold preservesCallback in *; eauto
  | |- machineSafe _ _ (@V.M.HostAPI.call _ _ _) ⇒ apply safe_call; apply Hchild;
    [cbn [Evmc.Message.t.gas]; unfold Gas.to_uint64, UintN.to_uint64, UintN.to_z;
    rewrite int64_unsigned_small_identity by lia; lia|assumption]
  | |- machineSafe _ _ (match ?value with _ ⇒ _ end) ⇒ destruct value; solve_create_words Hchild
  | |- ?g ⇒ fail "Unhandled creation word goal:" g
  end.

Section Opcodes.
  Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
  Context `{U0 : RocqOfOCaml.Basics.Unreachable Z.t}.
  Context `{U1 :
    RocqOfOCaml.Basics.Unreachable (RocqOfOCaml.OCamlSeq.t int)}.
  Context `{U2 : RocqOfOCaml.Basics.Unreachable RocqOfOCaml.OCamlInt32.t}.
  Context `{U3 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}.
  Context `{U4 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}.
  Context `{U5 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}.
  Context `{U7 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.Integer.t}.
  Context `{U8 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.I256.t}.
  Context `{U9 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}.
  Context `{U10 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Delegation.Address.t}.
  Context `{U11 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}.
  Context `{U12 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}.
  Context `{U13 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.M.Option.t Host_t F MonadVM.Numeric.U256.t)}.
  Context `{U15 : RocqOfOCaml.Basics.Unreachable unit}.
  Context `{U16 : RocqOfOCaml.Basics.Unreachable string}.
  Context `{U17 :
    RocqOfOCaml.Basics.Unreachable (option MonadVM.Numeric.U256.t)}.
  Context `{U18 : RocqOfOCaml.Basics.Unreachable (option string)}.
  Context `{U19 :
    RocqOfOCaml.Basics.Unreachable
      (list MonadVM.Byte_string.Bytes.t)}.
  Context `{U20 : RocqOfOCaml.Basics.Unreachable (list int)}.
  Context `{U21 : RocqOfOCaml.Basics.Unreachable int}.
  Context `{U22 : RocqOfOCaml.Basics.Unreachable ascii}.
  Context `{U23 : RocqOfOCaml.Basics.Unreachable (int × int)}.
  Context `{U24 :
    RocqOfOCaml.Basics.Unreachable
      (Z.t → MonadVM.Numeric.U256.t)}.
  Context `{U25 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Support.UintN.t → MonadVM.Support.UintN.t)}.
  Context `{U26 :
    RocqOfOCaml.Basics.Unreachable
      (unit → RocqOfOCaml.OCamlSeq.node int)}.
  Context `{U27 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.opcode_impl Host_t F)}.
  Context `{U28 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.t)}.
  Context `{U31 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}.
  Context `{U32 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Numeric.U256.t → MonadVM.Numeric.U256.t →
        @MonadVM.Vm.Make.Memory.t Host_t F →
        @MonadVM.Vm.Make.M.Option.t Host_t F
          (@MonadVM.Vm.Make.Memory.t Host_t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t Host_t F →
        MonadVM.Support.UintN.t)}.

  Let State := @MonadVM.Vm.Make.MachineState.t Host_t F.

Context (invariant : Host_t → Prop).
Section Environment.
Context {E : @V.Executor.FArgs Host_t F}.

Lemma generic_create_preserves_words kind salt value inputStart inputSize :
  (0 ≤ U.to_z value)%Z →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  boundedMachineSafe invariant (fun _ ⇒ True) (@V.Executor.generic_create_impl Host_t F E
    kind salt value inputStart inputSize).
Proof.
  intros Hvalue Hprimitive Hchild. destruct Hprimitive.
  unfold V.Executor.generic_create_impl.
  eapply bounded_words_bind_prefix with (P := fun _ ⇒ True).
  - solve_create_words Hchild.
  - apply stipend_guard_nonincreasing.
  - intros [] _. eapply bounded_words_bind_prefix with (P := fun _ ⇒ True).
    + unfold V.Executor.check_write_permissions. solve_create_words Hchild.
    + apply check_write_permissions_nonincreasing.
    + intros [] _. eapply bounded_words_bind_prefix with (P := fun _ ⇒ True).
      × unfold V.M.HostAPI.get_balance, V.M.HostAPI.Base. solve_create_words Hchild.
      × apply get_balance_nonincreasing.
      × intros selfBalance _. eapply bounded_words_bind_prefix with (P := fun _ ⇒ True).
        { unfold V.Executor.access_delegation, V.M.HostAPI.get_code_size,
            V.M.HostAPI.copy_code, V.M.HostAPI.access_account, V.M.HostAPI.Base.
          solve_create_words Hchild. }
        { first [apply create_delegation_guard_nonincreasing |
            destruct (@V.Params Host_t F).(Vm.Make_Params_signature.revision);
            try apply create_delegation_guard_nonincreasing;
            match goal with |- gas_nonincreasing (@V.M.when_ _ _ ?condition _) ⇒
              unfold V.M.when_; destruct condition end;
            [apply fail_nonincreasing | apply return_nonincreasing]]. }
        intros [] _.
        match goal with |- boundedMachineSafe _ _ (if ?skip then _ else _) ⇒ destruct skip end.
        { apply bounded_words_from_unbounded. solve_create_words Hchild. }
        intros state Hgas Hwords.
        pose proof (@create_forwarded_le (MS.t.gas state)) as Hforwarded.
        change (@UintN.minus_1_64th (MS.t.gas state) ≤ MS.t.gas state)%N in Hforwarded.
        match goal with |- machineWords _ (snd
          (@V.M.op_letdollar _ _ _ _ _ ?next state)) ∧ _ ⇒
          let forwarded := constr:(@UintN.minus_1_64th (MS.t.gas state)) in
          assert (Hnext : machineSafe invariant (fun _ ⇒ True) (next forwarded)) by
            (unfold V.Executor.merge_child_gas_and_refund; solve_create_words Hchild);
          exact (Hnext state Hwords)
        end.
Qed.

Lemma create_preserves_words :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  boundedMachineSafe invariant (fun _ ⇒ True) (@V.Executor.create Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild. unfold V.Executor.create.
  eapply bounded_words_bind_prefix; [apply safe_pop3|apply pop3_nonincreasing|].
  intros [[value start] size] [Hvalue _]. cbn [fst] in Hvalue.
  eapply bounded_words_bind_prefix.
  - apply (extend_memory_preserves_words invariant ). exact Hrevision.
  - apply extend_memory_to_nonincreasing.
  - intros memoryGas _. eapply bounded_words_bind_prefix.
    + apply safe_spend.
    + apply spend_nonincreasing.
    + intros [] _. eapply bounded_words_bind_suffix.
      × apply generic_create_preserves_words; [exact (proj1 Hvalue)|exact Hprimitive|exact Hchild].
      × intros [] _. apply safe_increase_pc.
Qed.

Lemma create2_preserves_words :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  boundedMachineSafe invariant (fun _ ⇒ True) (@V.Executor.create2 Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild. unfold V.Executor.create2.
  eapply bounded_words_bind_prefix; [apply safe_pop3|apply pop3_nonincreasing|].
  intros [[value start] size] [Hvalue _]. cbn [fst] in Hvalue.
  eapply bounded_words_bind_prefix.
  - apply safe_fmap, safe_pop.
  - apply map_nonincreasing, pop_nonincreasing.
  - intros salt _. eapply bounded_words_bind_prefix.
    + apply (extend_memory_preserves_words invariant ). exact Hrevision.
    + apply extend_memory_to_nonincreasing.
    + intros memoryGas _. eapply bounded_words_bind_prefix.
      × apply safe_spend.
      × apply spend_nonincreasing.
      × intros [] _. eapply bounded_words_bind_suffix.
        { apply generic_create_preserves_words; [exact (proj1 Hvalue)|exact Hprimitive|exact Hchild]. }
        intros [] _. apply safe_increase_pc.
Qed.
End Environment.
End Opcodes.

Print Assumptions create_preserves_words.
Print Assumptions create2_preserves_words.