Library SetcodeConcrete.vmnonceloop

Nonce growth through the entire generated instruction loop

The loop preserves account-map well-formedness, nonce nonnegativity and the initial nonce plus gas limit as an upper bound on nonce plus unused gas. All instructions are covered. The child Host-call property remains an induction hypothesis to discharge in the recursive-module proof; this is not a theorem about the fully assembled executor.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
  QedkConcrete.vmhostframe QedkConcrete.hostnonce QedkConcrete.hostframe.
Require Import SetcodeConcrete.vmprogress SetcodeConcrete.vmframes
  SetcodeConcrete.vmgasframe SetcodeConcrete.vmgasopcodes
  SetcodeConcrete.vmnoncesetcode SetcodeConcrete.vmnonceops
  SetcodeConcrete.vmnoncecallops SetcodeConcrete.checkednonce SetcodeConcrete.noncecreate
  SetcodeConcrete.noncebudget SetcodeConcrete.noncegas.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section VM.
Context {F : @V.FArgs S.t} {E : @V.Executor.FArgs S.t F}.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
  Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hprogress : @setcodeGasProgress S.t F).
Context (query : Addr.t).
Context (Hprimitive : ∀ nonce, PrimitiveHostFrame V.Host (nonceInvariant query nonce)).
Context (Hsetcode : ∀ nonce current source static gas,
  preservesCallback (nonceInvariant query nonce)
    (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).
Context (Hbalance : ∀ addr host, snd (V.Host.(Evmc.HOST.get_balance) addr host) = host).
Context (Hcall : ∀ msg host, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query host →
  let '(result, after) := V.Host.(Evmc.HOST.call) msg host in
  checkedNonceResult query (Evmc.Message.t.gas msg)
    (if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
    host result after).
Local Abbreviation measure := (fun host ⇒ nonceAt (S.t.world_state host) query).
Local Abbreviation total := (@nonceAndGas S.t F measure).

Lemma unchanged_nonce_and_gas_bound {A : Set} (action : @V.M.t S.t F A) :
  (∀ nonce, preservesHost (nonceInvariant query nonce) action) →
  gasBound action → boundedNonce query 0 action.
Proof.
  intros Hframe Hgas state Hbound Hinitial.
  destruct (Hframe (measure (MS.t.host state)) state (conj (proj1 Hinitial) eq_refl))
    as [Hmap Hsame].
  change (measure (MS.t.host (snd (action state))) = measure (MS.t.host state)) in Hsame.
  split; [split; [exact Hmap|rewrite Hsame; exact (proj2 Hinitial)]|].
  specialize (Hgas state). unfold nonceAndGas. change (measure (MS.t.host (snd (action state))) +
    Z.of_N (MS.t.gas (snd (action state))) ≤ measure (MS.t.host state) + Z.of_N (MS.t.gas state) + 0).
  cbv beta in Hsame |- ×. lia.
Qed.

Theorem dispatch_nonce_bound opcode : boundedNonce query 0 (dispatch opcode).
Proof.
  destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
  - assert (Hcreate : ∀ msg host, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query host →
      let '(result, after) := V.Host.(Evmc.HOST.call) msg host in
      checkedNonceResult query (Evmc.Message.t.gas msg) 2 host result after).
    { intros msg host Hgas Hinitial. specialize (Hcall msg host Hgas Hinitial).
      destruct (V.Host.(Evmc.HOST.call) msg host) as [result after].
      destruct Hcall as [Hstate Htotal].
      split; [exact Hstate|].
      destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg)); lia. }
    destruct (create_opcodes_nonce_bound query cold warm access write reserve Hrevision Hbalance Hcreate)
      as [Hcreate1 Hcreate2].
    assert (Hordinary : ∀ msg host, Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) = false →
      0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query host →
      let '(result, after) := V.Host.(Evmc.HOST.call) msg host in
      checkedNonceResult query (Evmc.Message.t.gas msg) 0 host result after).
    { intros msg host Hkind Hgas Hinitial. specialize (Hcall msg host Hgas Hinitial).
      rewrite Hkind in Hcall. exact Hcall. }
    destruct (call_family_nonce_bound query Hprimitive Hordinary) as [Hcall1 [Hcall2 [Hcall3 Hcall4]]].
    destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate;
      assumption.
  - apply unchanged_nonce_and_gas_bound.
    + intro nonce. destruct (machineOnlyOpcode opcode) eqn:Hmachine.
      × apply machine_only_preserves_host_core; [| |exact Hdebug|exact Hmachine].
        -- eapply setcode_clz_preserves_host; exact Hrevision.
        -- intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
           ++ eapply setcode_opcode_preserves_host; [exact Hrevision|apply Hsetcode].
           ++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
      × eapply primitive_host_preserves_at_any_revision;
          [exact Hdebug|apply primitive_frame_at, Hprimitive|].
        destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
    + destruct (machineOnlyOpcode opcode) eqn:Hmachine.
      × apply machine_gas_bound; [| |exact Hdebug|exact Hmachine].
        -- unfold dispatch, V.Executor.execute_opcode, V.Executor.clz.
           rewrite Hrevision. solve_gas_bound.
        -- intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
           ++ unfold dispatch, V.Executor.execute_opcode.
              eapply setcode_opcode_gas_bound; eassumption.
           ++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply gas_fail.
      × apply primitive_gas_bound; [exact Hdebug|].
        destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.

Qed.

Theorem run_nonce_bound state :
  Z.of_N (MS.t.gas state) < 2^63 → nonceState query (MS.t.host state) →
  nonceState query (MS.t.host (snd (@generated_run S.t F E state))) ∧
  total (snd (@generated_run S.t F E state)) ≤ total state.
Proof.
  intros Hgas Hinitial.
  eapply (setcode_run_preserves cold warm access write reserve
    (fun current ⇒ nonceState query (MS.t.host current) ∧ total current ≤ total state)
    state Hrevision Hprogress).
  - intros current Hbound [Hstate Hbudget].
    destruct (dispatch_nonce_bound (run_selected_opcode current) current Hbound Hstate)
      as [Hafter Htotal]. split; [exact Hafter|lia].
  - exact Hgas.
  - split; [exact Hinitial|lia].
Qed.
End VM.

Print Assumptions run_nonce_bound.