Library SetcodeConcrete.vmnoncecreate

CREATE forwarding and nonce accounting

The generic helper is called after the instruction's creation charge. It accounts for returned child gas and at most two nonce changes. The outer instruction's charge pays those two units. Errors before the Host call change neither the account nonce nor the gas allowance.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import SetcodeConcrete.checkednonce SetcodeConcrete.noncecreate SetcodeConcrete.nonceeffects
  SetcodeConcrete.noncebudget SetcodeConcrete.noncegas SetcodeConcrete.vmnoncecalls.
Require Import MonadExecutionSpecs.Termination.VmHostGasBound
  MonadExecutionSpecs.Termination.VmCreateGasMerge
  MonadExecutionSpecs.Termination.VmUnsignedGas
  MonadExecutionSpecs.Termination.VmCallGas.
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 (query : Addr.t).
Local Abbreviation measure := (fun host ⇒ nonceAt (S.t.world_state host) query).
Local Abbreviation total := (@nonceAndGas S.t F measure).

Theorem generic_create_nonce_bound cold warm access write reserve kind salt
    value inputStart inputSize state :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (∀ addr host, snd (V.Host.(Evmc.HOST.get_balance) addr host) = host) →
  (∀ msg host, Evmc.Message.t.kind msg = kind →
    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) →
  Z.of_N (MS.t.gas state) < 2^63 → nonceState query (MS.t.host state) →
  let after := snd (V.Executor.generic_create_impl kind salt value inputStart inputSize state) in
  nonceState query (MS.t.host after) ∧ total after ≤ total state + 2.
Proof.
  intros Hrevision Hbalance Hcall Hgas Hinitial.
  unfold V.Executor.generic_create_impl, V.Executor.check_write_permissions,
    V.M.when_, Monad.Make_Monad.when_.
  rewrite Hrevision.
  cbn -[V.M.HostAPI.get_balance V.M.HostAPI.call V.push
    V.Executor.merge_child_gas_and_refund V.Memory V.Params].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  { cbn. split; [exact Hinitial|unfold nonceAndGas; lia]. }
  cbn -[V.M.HostAPI.get_balance V.M.HostAPI.call V.push
    V.Executor.merge_child_gas_and_refund V.Memory V.Params].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  2: { cbn. split; [exact Hinitial|unfold nonceAndGas; lia]. }
  unfold V.M.HostAPI.get_balance, V.M.HostAPI.lift, V.M.HostAPI.Base.
  cbn -[V.Host V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund V.Memory].
  pose proof (Hbalance V.Executor.self (MS.t.host state)) as Hsame.
  destruct (V.Host.(Evmc.HOST.get_balance) V.Executor.self (MS.t.host state)) as [balance host].
  cbn [snd] in Hsame. subst host.
  cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund V.Memory
    Support.UintN.minus_1_64th Support.UintN.op_minus Support.UintN.to_uint64].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  { cbn. split; [exact Hinitial|unfold nonceAndGas; cbn; lia]. }
  cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund V.Memory
    Support.UintN.minus_1_64th Support.UintN.op_minus Support.UintN.to_uint64].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  - cbn -[V.push].
    match goal with |- context [V.push ?value ?before] ⇒
      destruct (push_nonce_frame value before) as [Hhost Hremaining]
    end.
    split; [rewrite Hhost; exact Hinitial|].
    unfold nonceAndGas. rewrite Hhost, Hremaining. cbn. lia.
  - cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund V.Memory
      Support.UintN.minus_1_64th Support.UintN.op_minus Support.UintN.to_uint64].
    match goal with |- context [V.M.HostAPI.call ?message ?before] ⇒
      set (child := message) in *; set (prepared := before) in ×
    end.
    set (forwarded := Gas.minus_1_64th (MS.t.gas state)).
    assert (Hforwarded : (forwarded ≤ MS.t.gas state)%N) by apply create_forwarded_le.
    assert (Hchildgas : Evmc.Message.t.gas child = Z.of_N forwarded).
    { unfold child. cbn [Evmc.Message.t.gas].
      unfold Support.UintN.to_uint64, Support.UintN.to_z.
      rewrite int64_unsigned_small_identity by (change (0 ≤ Z.of_N forwarded < 2^63); lia).
      reflexivity. }
    pose proof (Hcall child (MS.t.host prepared) eq_refl ltac:(rewrite Hchildgas; lia) Hinitial)
      as Hchild.
    destruct (V.Host.(Evmc.HOST.call) child (MS.t.host prepared)) as [result host] eqn:Hrun.
    destruct Hchild as [Hstate Htotal].
    rewrite (host_call_checked child prepared result host Hrun).
    unfold checkedRefund in Htotal.
    destruct (invalid_host_gas (Evmc.Result.t.gas_left result)
      (Evmc.Message.t.gas child)) eqn:Hvalid.
    { cbn -[prepared]. split; [exact Hstate|].
      unfold nonceAndGas. cbn -[prepared]. rewrite Hchildgas in Htotal.
      unfold prepared in *; cbn -[Support.UintN.op_minus Support.UintN.minus_1_64th] in ×.
      change (nonceAt (S.t.world_state host) query +
        Z.of_N (Support.UintN.op_minus (MS.t.gas state) forwarded) ≤
        nonceAt (S.t.world_state (MS.t.host state)) query + Z.of_N (MS.t.gas state) + 2).
      unfold Support.UintN.op_minus.
      assert (Hafford : (forwarded <=? MS.t.gas state)%N = true) by (apply N.leb_le; exact Hforwarded).
      rewrite Hafford, N2Z.inj_sub by exact Hforwarded. lia. }
    pose proof (valid_returned_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas child) ltac:(rewrite Hchildgas; lia) Hvalid) as Hreturned.
    destruct result as [status gasleft refund output address].
    cbn -[V.push V.Executor.merge_child_gas_and_refund V.Memory prepared].
    unfold V.Executor.merge_child_gas_and_refund.
    destruct status; cbn -[V.push V.Memory prepared Gas.of_uint64 Gas.op_plus].
    all: match goal with |- context [V.push ?value ?before] ⇒
      destruct (push_nonce_frame value before) as [Hhost Hremaining]
    end.
    all: split; [rewrite Hhost; exact Hstate|].
    all: unfold nonceAndGas; rewrite Hhost, Hremaining.
    all: cbn -[Gas.of_uint64 Gas.op_plus prepared]; unfold Gas.op_plus, Support.UintN.op_plus;
      rewrite N2Z.inj_add.
    all: cbn [Evmc.Result.t.gas_left] in Hreturned, Htotal.
    all: rewrite nonnegative_unsigned_host_gas by lia.
    all: rewrite Z2N.id by lia.
    all: rewrite Hchildgas in Htotal.
    all: unfold prepared in *; cbn -[Support.UintN.op_minus Support.UintN.minus_1_64th] in ×.
    all: change (nonceAt (S.t.world_state host) query +
      (Z.of_N (Support.UintN.op_minus (MS.t.gas state) forwarded) + gasleft) ≤
      nonceAt (S.t.world_state (MS.t.host state)) query + Z.of_N (MS.t.gas state) + 2).
    all: unfold Support.UintN.op_minus.
    all: assert (Hafford : (forwarded <=? MS.t.gas state)%N = true) by (apply N.leb_le; exact Hforwarded).
    all: rewrite Hafford, N2Z.inj_sub by exact Hforwarded; lia.
Qed.
End VM.

Print Assumptions generic_create_nonce_bound.