Library QedkConcrete.vmsupplywords

Concrete Host values for the VM supply invariant

Balance conservation alone does not bound every word read by the VM: code sizes and storage bytes also need their representation invariants. This combines the already-proved concrete Host properties, so instruction proofs need no balance, storage, or code-size oracle.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.supply
  QedkConcrete.vmhostframe QedkConcrete.contexthostinstance
  QedkConcrete.supplyhostinstance QedkConcrete.hostaccountformat
  QedkConcrete.hoststoragewords QedkConcrete.vmmachinewords QedkConcrete.wordrange
  QedkConcrete.vmhostwords QedkConcrete.vmstoragewords QedkConcrete.vmhashwords
  QedkConcrete.hashwords QedkConcrete.vmmemorywords QedkConcrete.vmmiscwords
  QedkConcrete.vmdispatchwords QedkConcrete.vmcallmachinewords
  QedkConcrete.vmenvironmentwords QedkConcrete.vmopcodeframe
  QedkConcrete.vmchecked QedkConcrete.vmcallchecked QedkConcrete.vmcreatechecked
  QedkConcrete.vmsizes QedkConcrete.vmlocalsizes QedkConcrete.vmrun
  QedkConcrete.hostcontextwords QedkConcrete.vminvariant QedkConcrete.hostcopybounds.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition hostSupplyWords limit (s : S.t) : Prop :=
  (balanceInvariant (S.t.world_state s) ∧ totalBalance (S.t.world_state s) ≤ limit) ∧
  hostFormat s.

Definition hostSupplyContext params limit (s : S.t) : Prop :=
  hostSupplyWords limit s ∧ hostContextWords params s.

Lemma primitive_frame_conj {Host_t : Set} (host : Evmc.HOST (t := Host_t)) P Q :
  PrimitiveHostFrame host P → PrimitiveHostFrame host Q →
  PrimitiveHostFrame host (fun s ⇒ P s ∧ Q s).
Proof.
  intros HP HQ. destruct HP, HQ. constructor; unfold preservesCallback in *;
    intros; split; intuition eauto.
Qed.

Section Concrete.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation invariant := (hostSupplyWords limit).

Lemma supply_words_primitives : PrimitiveHostFrame (HostF vm) invariant.
Proof.
  apply primitive_frame_conj; [apply concrete_primitives_preserve_supply|apply concrete_primitives_preserve_format].
Qed.

Lemma context_words_primitives :
  PrimitiveHostFrame (HostF vm) (hostContextWords HF.(Host.Instantiate.ChainParams)).
Proof.
  constructor; unfold preservesCallback; intros.
  all: match goal with Hinitial : hostContextWords _ ?before |- _ ⇒
    eapply host_context_words_preserved; [|
      exact Hinitial];
    pose proof (concrete_primitives_preserve_context vm before) as Hframe;
    destruct Hframe; unfold preservesCallback in *; eauto
  end.
Qed.

Lemma supply_context_primitives :
  PrimitiveHostFrame (HostF vm) (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).
Proof.
  apply primitive_frame_conj; [apply supply_words_primitives|apply context_words_primitives].
Qed.

Lemma supply_words_get_balance addr :
  machineSafe invariant wordRange (@V.M.HostAPI.get_balance S.t F addr).
Proof.
  apply safe_host_lift. intros s Hinv. split.
  - exact (@frame_get_balance S.t (HostF vm) invariant supply_words_primitives addr s Hinv).
  - apply concrete_balance_word. exact (proj1 (proj1 Hinv)).
Qed.

Lemma supply_words_get_code_size addr :
  machineSafe invariant (fun size ⇒ 0 ≤ size < 2^63) (@V.M.HostAPI.get_code_size S.t F addr).
Proof.
  apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_code_size]. apply get_code_size_nonnegative.
  apply world_format_components. exact (proj1 (proj2 Hinv)).
Qed.

Lemma supply_words_get_storage addr key :
  machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
    (@V.M.HostAPI.get_storage S.t F addr key).
Proof.
  apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_storage]. apply get_storage_word_length.
  apply world_format_components. exact (proj1 (proj2 Hinv)).
Qed.

Lemma supply_words_get_transient_storage addr key :
  machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
    (@V.M.HostAPI.get_transient_storage S.t F addr key).
Proof.
  apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_transient_storage]. apply get_transient_storage_word_length.
  exact (proj2 (proj2 Hinv)).
Qed.

Lemma supply_words_get_code_hash addr :
  keccak256OutputSize →
  machineSafe invariant
    (fun result ⇒ ∀ hash, result = Some hash → OCamlString.length hash = 32)
    (@V.M.HostAPI.get_code_hash S.t F addr).
Proof.
  intro Hsize. apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_code_hash]. intros hash Hhash. eapply get_code_hash_length; eassumption.
Qed.

Lemma supply_words_get_block_hash number :
  keccak256OutputSize →
  machineSafe invariant
    (fun result ⇒ ∀ hash, result = Some hash → OCamlString.length hash = 32)
    (@V.M.HostAPI.get_block_hash S.t F number).
Proof.
  intro Hsize. apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
  cbn -[H.get_block_hash]. intros hash Hhash. eapply get_block_hash_length; eassumption.
Qed.

Lemma supply_words_copy_code_length addr offset count state :
  0 ≤ offset → 0 ≤ count < 2^32 → invariant state →
  OCamlString.length (fst ((HostF vm).(Evmc.HOST.copy_code) addr offset count state)) ≤ count.
Proof.
  intros Hoffset Hcount Hinitial. cbn -[H.copy_code].
  apply copy_code_length; [|exact Hoffset|lia].
  exact (proj2 (read_account_format (S.t.world_state state) addr
    (proj1 (proj2 Hinitial)))).
Qed.

Section Instructions.
Context {E : @V.Executor.FArgs S.t F}.
  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 S.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 S.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 S.t F →
        @MonadVM.Vm.Make.M.Option.t S.t F
          (@MonadVM.Vm.Make.Memory.t S.t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t S.t F →
        MonadVM.Support.UintN.t)}.

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

Local Abbreviation executeOpcode := (@dispatch S.t F E).

Lemma primitive_host_preserves_supply_words opcode :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  primitiveHostOpcode opcode = true →
  sizedMemorySafe invariant (fun _ ⇒ True) (executeOpcode opcode).
Proof.
  intros Hrevision Hdebug Hsize Hprimitive.
  pose proof supply_words_primitives as Hframe. destruct Hframe.
  destruct opcode; cbn [primitiveHostOpcode] in Hprimitive; try discriminate.
  all: cbn [dispatch V.Executor.execute_opcode].
  all: try solve [lazymatch goal with |- context [@V.Executor.extcodecopy] ⇒
    apply sized_memory_from_checked, extcodecopy_preserves_words_or_bad_comparison;
    try assumption; try exact supply_words_primitives;
    apply supply_words_copy_code_length end].
  all: apply sized_memory_from_memory, memory_safe_lift.
  all: try assumption.
  all: lazymatch goal with
  | |- context [@V.Executor.balance] ⇒ apply balance_words_from_host
  | |- context [@V.Executor.extcodesize] ⇒ apply extcodesize_words_from_host
  | |- context [@V.Executor.extcodehash] ⇒ apply extcodehash_words_from_host
  | |- context [@V.Executor.blockhash] ⇒ apply blockhash_words_from_host
  | |- context [@V.Executor.selfbalance] ⇒ apply selfbalance_words_from_host
  | |- context [@V.Executor.sload] ⇒ apply sload_words_from_host
  | |- context [@V.Executor.sstore] ⇒ apply sstore_words_from_host
  | |- context [@V.Executor.tload] ⇒ apply tload_words_from_host
  | |- context [@V.Executor.tstore] ⇒ apply tstore_words_from_host
  | |- context [@V.Executor.log] ⇒ apply log_preserves_words;
    try assumption; try exact supply_words_primitives
  | |- context [@V.Executor.selfdestruct] ⇒ apply selfdestruct_preserves_words;
    try assumption; try exact supply_words_primitives
  end.
  all: intros.
  all: lazymatch goal with
  | |- context [@V.M.HostAPI.get_balance] ⇒ apply supply_words_get_balance
  | |- context [@V.M.HostAPI.get_code_size] ⇒ apply supply_words_get_code_size
  | |- context [@V.M.HostAPI.get_storage] ⇒ apply supply_words_get_storage
  | |- context [@V.M.HostAPI.get_transient_storage] ⇒ apply supply_words_get_transient_storage
  | |- context [@V.M.HostAPI.get_code_hash] ⇒ apply supply_words_get_code_hash; exact Hsize
  | |- context [@V.M.HostAPI.get_block_hash] ⇒ apply supply_words_get_block_hash; exact Hsize
  | _ ⇒ apply safe_host_frame; eauto
  end.
Qed.

Lemma nonrecursive_dispatch_preserves_supply_words opcode state :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
  opcodeWordParameters opcode →
  recursiveCallOpcode opcode = false →
  memoryFits (MS.t.memory state) →
  OCamlString.length (MS.t.output_buffer state) < 2^256 →
  machineWords invariant state →
  machineWords invariant (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hparams Hnonrecursive Hfits Hbuffer Hinitial.
  destruct (machineOnlyOpcode opcode) eqn:Hlocal.
  { apply machine_only_preserves_words_or_bad_comparison; assumption. }
  destruct (primitiveHostOpcode opcode) eqn:Hprimitive.
  { pose proof (primitive_host_preserves_supply_words opcode Hrevision Hdebug Hhash Hprimitive
      state (conj Hinitial (conj Hfits Hbuffer))) as Hsafe. tauto. }
  destruct opcode; cbn [machineOnlyOpcode] in Hlocal;
    cbn [primitiveHostOpcode] in Hprimitive;
    cbn [recursiveCallOpcode] in Hnonrecursive; discriminate.
Qed.

Recursive calls now require bounded messages, rather than arbitrary input strings and memory capacities. CALL and CREATE establish those bounds from checked caller memory; their returned-buffer bound is needed by RETURNDATASIZE. The remaining child premise is the VM/Host induction obligation.
Lemma dispatch_preserves_supply_checked_from_child opcode state :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
  opcodeWordParameters opcode →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (HostF vm).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  Z.of_N (MS.t.gas state) < 2^63 →
  checkedMachine invariant state →
  checkedMachine invariant (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hparams Hchild Hgas Hinitial.
  destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
  - assert (Hvalue : wordRange (Env.t.value
      ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)))).
    { exact (List.Forall_inv (List.Forall_inv_tail (proj1 Henv))). }
    assert (Hlift : ∀ action : @V.M.t S.t F bool,
      boundedCheckedSafe invariant (fun _ ⇒ True) action →
      checkedMachine invariant (snd (action state)) ∨ badMemoryInitComparison).
    { intros action Hsafe. specialize (Hsafe state Hgas Hinitial). tauto. }
    destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate.
    all: cbn [dispatch V.Executor.execute_opcode]; apply Hlift.
    all: lazymatch goal with
    | |- context [@V.Executor.call] ⇒ apply call_checked_from_child
    | |- context [@V.Executor.callcode] ⇒ apply callcode_checked_from_child
    | |- context [@V.Executor.staticcall] ⇒ apply staticcall_checked_from_child
    | |- context [@V.Executor.delegatecall] ⇒ apply delegatecall_checked_from_child
    | |- context [@V.Executor.create] ⇒ apply create_checked_from_child
    | |- context [@V.Executor.create2] ⇒ apply create2_checked_from_child
    end.
    all: first [assumption|exact Hrevision|exact supply_words_primitives|exact Hchild|exact Hvalue].
  - destruct Hinitial as [Hwords Hsizes].
    destruct (nonrecursive_dispatch_preserves_supply_words opcode state Hrevision Hdebug Hhash
      Henv Hparams Hrecursive (proj1 Hsizes) (proj2 Hsizes) Hwords) as [HafterWords|Hbad];
      [|right; exact Hbad].
    pose proof (@nonrecursive_preserves_sizes S.t F E U9 U21 U22 U23 U25 U28 U31 U32
      opcode state Hrevision Hdebug Hrecursive (proj1 Hwords)
      (proj2 (proj2 (proj2 Hwords))) Hsizes) as HafterSizes.
    unfold checkedMachine. tauto.
Qed.

Word-only corollary, on the same bounded machine and recursive-message domain as the joint invariant. Native memory arithmetic is not specified for unrepresentable capacities.
Lemma dispatch_preserves_supply_words opcode state :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
  opcodeWordParameters opcode →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (HostF vm).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  Z.of_N (MS.t.gas state) < 2^63 →
  checkedMachine invariant state →
  machineWords invariant (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hparams Hchild Hgas Hinitial.
  pose proof (dispatch_preserves_supply_checked_from_child opcode state
    Hrevision Hdebug Hhash Henv Hparams Hchild Hgas Hinitial) as Hsafe.
  unfold checkedMachine in Hsafe. tauto.
Qed.

Lemma run_preserves_supply_checked_from_child
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)} state :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (HostF vm).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  Z.of_N (MS.t.gas state) < 2^63 →
  checkedMachine invariant state →
  checkedMachine invariant (snd (@generated_run S.t F E state)) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hchild Hgas Hinitial.
  apply (@run_preserves S.t F E
    (fun st ⇒ checkedMachine invariant st ∨ badMemoryInitComparison) state).
  - right; exact Hrevision.
  - intros before Hbound [Hbefore|Hbad]; [|right; exact Hbad].
    apply dispatch_preserves_supply_checked_from_child; try assumption.
    apply selected_opcode_word_parameters.
  - exact Hgas.
  - left; exact Hinitial.
Qed.

Local Abbreviation contextInvariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).

A nested VM needs the header/price bounds as well as account invariants. Carrying them through each opcode makes the child-call premise usable by induction on the actual Host/VM assembly.
Lemma dispatch_preserves_supply_context_from_child opcode state :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
  opcodeWordParameters opcode →
  (∀ msg, @vmMessageBounds msg → ∀ host, contextInvariant host →
    let result := (HostF vm).(Evmc.HOST.call) msg host in
    (contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  Z.of_N (MS.t.gas state) < 2^63 →
  checkedMachine contextInvariant state →
  checkedMachine contextInvariant (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hparams Hchild Hgas Hinitial.
  destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
  - assert (Hvalue : wordRange (Env.t.value
      ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)))).
    { exact (List.Forall_inv (List.Forall_inv_tail (proj1 Henv))). }
    assert (Hlift : ∀ action : @V.M.t S.t F bool,
      boundedCheckedSafe contextInvariant (fun _ ⇒ True) action →
      checkedMachine contextInvariant (snd (action state)) ∨ badMemoryInitComparison).
    { intros action Hsafe. specialize (Hsafe state Hgas Hinitial). tauto. }
    destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate.
    all: cbn [dispatch V.Executor.execute_opcode]; apply Hlift.
    all: lazymatch goal with
    | |- context [@V.Executor.call] ⇒ apply call_checked_from_child
    | |- context [@V.Executor.callcode] ⇒ apply callcode_checked_from_child
    | |- context [@V.Executor.staticcall] ⇒ apply staticcall_checked_from_child
    | |- context [@V.Executor.delegatecall] ⇒ apply delegatecall_checked_from_child
    | |- context [@V.Executor.create] ⇒ apply create_checked_from_child
    | |- context [@V.Executor.create2] ⇒ apply create2_checked_from_child
    end.
    all: first [assumption|exact Hrevision|exact supply_context_primitives|exact Hchild|exact Hvalue].
  - assert (Hwords : machineWords invariant state).
    { unfold checkedMachine, machineWords, hostSupplyContext in Hinitial |- ×. tauto. }
    pose proof (proj2 Hinitial) as Hsizes.
    destruct (nonrecursive_dispatch_preserves_supply_words opcode state Hrevision Hdebug Hhash
      Henv Hparams Hrecursive (proj1 Hsizes) (proj2 Hsizes) Hwords) as [HafterWords|Hbad];
      [|right; exact Hbad].
    pose proof (@nonrecursive_preserves_sizes S.t F E U9 U21 U22 U23 U25 U28 U31 U32
      opcode state Hrevision Hdebug Hrecursive (proj1 Hwords)
      (proj2 (proj2 (proj2 Hwords))) Hsizes) as HafterSizes.
    assert (Hcontext : preservesHost (hostContextWords HF.(Host.Instantiate.ChainParams))
      (executeOpcode opcode)).
    { destruct (machineOnlyOpcode opcode) eqn:Hlocal.
      - apply machine_only_preserves_host; assumption.
      - apply primitive_host_preserves; try assumption; [apply context_words_primitives|].
        destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *;
          congruence. }
    specialize (Hcontext state (proj2 (proj1 (proj2 (proj2 (proj1 Hinitial)))))).
    unfold checkedMachine, machineWords, hostSupplyContext in ×. tauto.
Qed.

Lemma run_preserves_supply_context_from_child
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)} state :
  params.(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
  (∀ msg, @vmMessageBounds msg → ∀ host, contextInvariant host →
    let result := (HostF vm).(Evmc.HOST.call) msg host in
    (contextInvariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  Z.of_N (MS.t.gas state) < 2^63 →
  checkedMachine contextInvariant state →
  checkedMachine contextInvariant (snd (@generated_run S.t F E state)) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hchild Hgas Hinitial.
  apply (@run_preserves S.t F E
    (fun st ⇒ checkedMachine contextInvariant st ∨ badMemoryInitComparison) state).
  - right; exact Hrevision.
  - intros before Hbound [Hbefore|Hbad]; [|right; exact Hbad].
    apply dispatch_preserves_supply_context_from_child; try assumption.
    apply selected_opcode_word_parameters.
  - exact Hgas.
  - left; exact Hinitial.
Qed.

End Instructions.
End Concrete.

Print Assumptions supply_words_get_storage.
Print Assumptions primitive_host_preserves_supply_words.
Print Assumptions dispatch_preserves_supply_words.
Print Assumptions dispatch_preserves_supply_checked_from_child.
Print Assumptions run_preserves_supply_checked_from_child.
Print Assumptions run_preserves_supply_context_from_child.