Library QedkConcrete.hostsupply

Supply through Host composition and creation

Transfers redistribute supply; SELFDESTRUCT may burn it. The invariant therefore permits decreases. Restoring a failed call's entry state must be compared with that entry state, not with a partially executed state.
Recursive VM premises below are induction obligations, not new executor axioms. In particular, nonnegative message values must be derived from valid EVM words: the generated U256 type itself is an unrefined integer.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostbalance QedkConcrete.supply QedkConcrete.worldaccounts.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition preservesSupply {A : Set} (action : S.t → A × S.t) : Prop :=
  ∀ s, balanceInvariant (S.t.world_state s) →
    balanceInvariant (S.t.world_state (snd (action s))) ∧
    totalBalance (S.t.world_state (snd (action s))) ≤
      totalBalance (S.t.world_state s).

Lemma supply_mbind {A B : Set} (action : S.t → A × S.t)
    (next : A → S.t → B × S.t) :
  preservesSupply action → (∀ value, preservesSupply (next value)) →
  preservesSupply (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
  intros Haction Hnext s Hinv.
  specialize (Haction s Hinv). destruct (action s) as [value middle] eqn:Hrun.
  destruct Haction as [Hmiddle Htotal].
  specialize (Hnext value middle Hmiddle).
  change (balanceInvariant (S.t.world_state (snd (let '(v, st) := action s in next v st))) ∧
    totalBalance (S.t.world_state (snd (let '(v, st) := action s in next v st))) ≤
      totalBalance (S.t.world_state s)).
  rewrite Hrun.
  change (balanceInvariant (S.t.world_state (snd (next value middle))) ∧
    totalBalance (S.t.world_state (snd (next value middle))) ≤ totalBalance (S.t.world_state s)).
  split; [exact (proj1 Hnext)|]. cbn [snd] in Htotal. lia.
Qed.

Lemma supply_world_frame {A : Set} (action : S.t → A × S.t) :
  (∀ s, S.t.world_state (snd (action s)) = S.t.world_state s) →
  preservesSupply action.
Proof. intros Hframe s Hinv. rewrite Hframe. split; [exact Hinv|lia]. Qed.

Lemma supply_return {A : Set} (value : A) :
  preservesSupply (S.M.(Monad.State.State_result._return) value).
Proof. apply supply_world_frame. reflexivity. Qed.

Lemma supply_read {A : Set} (lens : Lens.t S.t A) :
  preservesSupply (S.M.(Monad.State.State_result.op_exclamation) lens).
Proof. apply supply_world_frame. reflexivity. Qed.

Lemma supply_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) →
  preservesSupply (S.M.(Monad.State.State_result.update_field) lens f).
Proof. intro Hframe. apply supply_world_frame. intro s. apply Hframe. Qed.

Section AccountWrites.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable U.t}.

Lemma supply_account_field {T : Set} keep addr (field : Lens.t A.t T) value :
  (∀ account, A.t.balance (Lens.t.set field value account) = A.t.balance account) →
  preservesSupply (S.M.(Monad.State.State_result.op_coloneq)
    (Lens.Infix.op_pipeminusminus (@S.account keep addr) field) value).
Proof.
  intros Hbalance s Hinv.
  destruct keep as [keep|]; [|set (keep := false)].
  all: pose proof (@account_write_preserves_supply keep (S.t.world_state s) addr
    (Lens.t.set field value (State.op_pointcaretlparenrparen (S.t.world_state s)
      (@W.account None addr))) Hinv) as Hwrite.
  all: rewrite Hbalance in Hwrite; specialize (Hwrite eq_refl).
  all: split; [exact (proj1 Hwrite)|].
  all: apply Z.eq_le_incl; exact (proj2 Hwrite).
Qed.

Lemma supply_transfer {F : H.FArgs} sender recipient amount :
  0 ≤ U.to_z amount →
  preservesSupply (@H.transfer_ether F sender recipient amount).
Proof.
  intros Hamount s Hinv.
  destruct (@transfer_preserves_supply F sender recipient amount s Hinv Hamount)
    as [Hafter Htotal]. split; [exact Hafter|rewrite Htotal; lia].
Qed.
End AccountWrites.

Ltac solve_supply_frame :=
  lazymatch goal with
  | |- preservesSupply (S.M.(Monad.State.State_result.op_letdollar) _ _) ⇒
    apply supply_mbind; [solve_supply_frame|];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_supply_frame
  | |- preservesSupply (S.M.(Monad.State.State_result._return) _) ⇒ apply supply_return
  | |- preservesSupply (S.M.(Monad.State.State_result.op_exclamation) _) ⇒ apply supply_read
  | |- preservesSupply (S.M.(Monad.State.State_result.op_coloneq) _ _) ⇒
    apply supply_account_field; reflexivity
  | |- preservesSupply (S.M.(Monad.State.State_result.update_field) _ _) ⇒
    apply supply_update; reflexivity
  | |- preservesSupply (if ?test then _ else _) ⇒ destruct test; solve_supply_frame
  | |- preservesSupply (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct value; solve_supply_frame
  | |- preservesSupply (match ?status with Evmc.Result.StatusCode.Success ⇒ _ | _ ⇒ _ end) ⇒
    destruct status; solve_supply_frame
  | |- preservesSupply (@H.transfer_ether _ _ _ _) ⇒ apply supply_transfer; assumption
  | Hvm : ∀ msg code, _ → _ → preservesSupply (_ msg code)
    |- preservesSupply (_ ?msg ?code) ⇒ apply Hvm; assumption
  end.

Section Creation.
Context {F : H.FArgs}.
Context {UN : Basics.Unreachable Support.UintN.t}.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable Numeric.U256.t}.
Context {Ugas : Basics.Unreachable Gas.t}.
Context {Uaddr : Basics.Unreachable Chain.Ethereum.Address.t}.
Context {Ubytes : Basics.Unreachable Byte_string.Bytes.t}.
Context {Uunit : Basics.Unreachable unit}.
Context {Ustr : Basics.Unreachable string}.
Context {Uint : Basics.Unreachable int}.
Context {Ubyte : Basics.Unreachable ascii}.
Context {Uint64 : Basics.Unreachable int64}.
Context {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma creation_address_preserves_supply msg :
  preservesSupply (@H.contract_creation_address F msg).
Proof.
  unfold H.contract_creation_address, H.M. apply supply_mbind.
  - apply supply_read.
  - intro nonce. apply supply_return.
Qed.

Lemma process_create_preserves_supply msg :
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    0 ≤ U.to_z (Evmc.Message.t.value nested) →
    preservesSupply ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  0 ≤ U.to_z (Evmc.Message.t.value msg) →
  preservesSupply (@H.process_create F msg).
Proof.
  intros Hvm Hgas Hvalue. unfold H.process_create, H.M.
  apply supply_mbind; [apply creation_address_preserves_supply|].
  intro addr. solve_supply_frame.
Qed.
End Creation.

Print Assumptions process_create_preserves_supply.