Library SetcodeConcrete.adoptionsupply

Code adoption preserves balance and representation invariants

The source code is read from a bounded account in the current world. Copying it changes neither storage nor balance. These results cover every outcome of the actual Host callback, including failures; they are not assumptions about what the VM does.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.supply QedkConcrete.hostsupply QedkConcrete.hostaccountformat
  QedkConcrete.hostcontextwords QedkConcrete.hostframe
  QedkConcrete.vmhostframe QedkConcrete.memorymodel.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Adoption.
Context {F : H.FArgs}.

Theorem setcodefrom_preserves_supply current source static gas :
  preservesSupply (H.setcodefrom current source static gas).
Proof.
  intros s Hinitial. unfold H.setcodefrom.
  destruct H.ChainParams.(Chain.Monad.PARAMS.revision);
    try (split; [exact Hinitial|apply Z.le_refl]).
  match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
    destruct prices as [[[[cold warm] currentCost] writeCost] reserveCost] end.
  cbn beta iota zeta.
  repeat match goal with |- context [if ?condition then _ else _] ⇒
    destruct condition; cbn beta iota zeta end.
  all: try (split; [exact Hinitial|apply Z.le_refl]).
  all: match goal with |- balanceInvariant ?post ∧ _ ⇒
    assert (Hwrite : balanceInvariant post ∧
      totalBalance post = totalBalance (S.t.world_state s)) by
      (apply account_write_preserves_supply; [exact Hinitial|reflexivity]);
    split; [exact (proj1 Hwrite)|apply Z.eq_le_incl; exact (proj2 Hwrite)] end.
Qed.

Theorem setcodefrom_preserves_format current source static gas :
  preservesCallback hostFormat (H.setcodefrom current source static gas).
Proof.
  intros s [Hworld Htransient]. unfold H.setcodefrom.
  destruct H.ChainParams.(Chain.Monad.PARAMS.revision);
    try (split; assumption).
  match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
    destruct prices as [[[[cold warm] currentCost] writeCost] reserveCost] end.
  cbn beta iota zeta.
  repeat match goal with |- context [if ?condition then _ else _] ⇒
    destruct condition; cbn beta iota zeta end.
  all: try (split; assumption).
  all: split; [|exact Htransient].
  all: apply write_account_format; [exact Hworld|].
  all: split.
  all: first [exact (proj1 (read_account_format _ current Hworld)) |
    exact (proj2 (read_account_format _ source Hworld))].
Qed.

Theorem setcodefrom_preserves_context current source static gas :
  preservesContext (H.setcodefrom current source static gas).
Proof.
  intro s. unfold H.setcodefrom.
  destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try reflexivity.
  match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
    destruct prices as [[[[cold warm] currentCost] writeCost] reserveCost] end.
  cbn beta iota zeta.
  repeat match goal with |- context [if ?condition then _ else _] ⇒
    destruct condition; cbn beta iota zeta end.
  all: reflexivity.
Qed.
End Adoption.

The new revision selects the same bounded-memory implementation as revision Eleven. The generic memory proofs require only these equations.
Lemma setcode_bounded_memory {Host_t : Set} {F : @Vm.Make.FArgs Host_t} prices :
  (@Vm.Make.Params Host_t F).(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode prices → BoundedMemory F.
Proof.
  intro Hrevision. constructor; intros;
    unfold Vm.Make.Memory.extend_to, Vm.Make.Memory.available_memory_size;
    rewrite Hrevision; reflexivity.
Qed.

Print Assumptions setcodefrom_preserves_supply.
Print Assumptions setcodefrom_preserves_format.