Library SetcodeConcrete.vmnonceops

Nonce accounting for complete CREATE instructions

Stack and memory preparation leave Host state and gas unchanged. The generated CREATE/CREATE2 charge includes 32000 gas, which covers the creator increment and the new account's initial nonce. The proof includes errors at each preparation, charging and child-execution stage.
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.vmnoncecreate.
Require Import 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}.

Definition unchangedResources {A : Set} (action : @V.M.t S.t F A) : Prop :=
  ∀ state, MS.t.host (snd (action state)) = MS.t.host state ∧
    MS.t.gas (snd (action state)) = MS.t.gas state.

Lemma resources_bind {A B : Set} (action : @V.M.t S.t F A) next :
  unchangedResources action → (∀ value, unchangedResources (next value)) →
  unchangedResources (@V.M.op_letdollar S.t F A B action next).
Proof.
  intros Hfirst Hnext state. specialize (Hfirst state).
  change (let after := snd (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle) end) in
    MS.t.host after = MS.t.host state ∧ MS.t.gas after = MS.t.gas state).
  destruct (action state) as [[value|error] middle]; cbn [snd] in *; [|exact Hfirst].
  specialize (Hnext value middle). destruct Hfirst, Hnext. split; congruence.
Qed.

Lemma resources_fmap {A B : Set} (f : A → B) action :
  unchangedResources action → unchangedResources (@V.M.op_ltdollargt S.t F A B f action).
Proof.
  intro H. change (unchangedResources (@V.M.op_letdollar S.t F A B action
    (fun value ⇒ V.M._return (f value)))).
  apply resources_bind; [exact H|]. intros value state. split; reflexivity.
Qed.

Lemma resources_pop : unchangedResources (@V.pop S.t F).
Proof. intro state. unfold V.pop. destruct (MS.t.stack state); split; reflexivity. Qed.
Lemma resources_pop3 : unchangedResources (@V.pop3 S.t F).
Proof.
  intro state. unfold V.pop3. destruct (MS.t.stack state) as [|a [|b [|c rest]]]; split; reflexivity.
Qed.
Lemma resources_pc : unchangedResources (@V.increase_pc_and_continue S.t F).
Proof. intro state. split; reflexivity. Qed.

Lemma resources_memory start size : unchangedResources (@V.Executor.extend_memory_to S.t F E start size).
Proof.
  intro state. unfold V.Executor.extend_memory_to.
  destruct (Numeric.U256.op_eq size Numeric.U256.zero); [split; reflexivity|].
  unfold V.M.Option.or_fail, Monad.Result.Make.Option.or_fail.
  cbn -[V.Memory].
  match goal with |- context [match ?outcome with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct outcome as [memory|]
  end.
  all: cbn -[V.Memory].
  all: try (match goal with |- context [if ?test then _ else _] ⇒ destruct test end).
  all: split; reflexivity.
Qed.

Context (query : Addr.t).
Local Abbreviation measure := (fun host ⇒ nonceAt (S.t.world_state host) query).
Local Abbreviation total := (@nonceAndGas S.t F measure).

Definition boundedNonce {A : Set} allowance (action : @V.M.t S.t F A) : Prop :=
  ∀ state, Z.of_N (MS.t.gas state) < 2^63 → nonceState query (MS.t.host state) →
  nonceState query (MS.t.host (snd (action state))) ∧
    total (snd (action state)) ≤ total state + allowance.

Lemma bounded_frame {A : Set} (action : @V.M.t S.t F A) :
  unchangedResources action → boundedNonce 0 action.
Proof.
  intros Hframe state Hgas Hinitial. destruct (Hframe state) as [Hhost Hremaining].
  split; [rewrite Hhost; exact Hinitial|]. unfold nonceAndGas. rewrite Hhost, Hremaining. lia.
Qed.

Lemma bounded_prefix {A B : Set} (action : @V.M.t S.t F A) (next : A → @V.M.t S.t F B) allowance :
  0 ≤ allowance → unchangedResources action →
  (∀ value, boundedNonce allowance (next value)) →
  boundedNonce allowance (@V.M.op_letdollar S.t F A B action next).
Proof.
  intros Hallowance Hframe Hnext state Hgas Hinitial.
  destruct (Hframe state) as [Hhost Hremaining].
  change (let after := snd (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle) end) in
    nonceState query (MS.t.host after) ∧ total after ≤ total state + allowance).
  destruct (action state) as [[value|error] middle]; cbn [snd] in ×.
  - specialize (Hnext value middle ltac:(rewrite Hremaining; exact Hgas)
      ltac:(rewrite Hhost; exact Hinitial)).
    destruct Hnext as [Hstate Hbound]. split; [exact Hstate|].
    unfold nonceAndGas in ×. rewrite Hhost, Hremaining in Hbound. exact Hbound.
  - split; [rewrite Hhost; exact Hinitial|].
    unfold nonceAndGas. rewrite Hhost, Hremaining. lia.
Qed.

Lemma bounded_suffix {A B : Set} (action : @V.M.t S.t F A) (next : A → @V.M.t S.t F B) allowance :
  boundedNonce allowance action → (∀ value, unchangedResources (next value)) →
  boundedNonce allowance (@V.M.op_letdollar S.t F A B action next).
Proof.
  intros Hfirst Hframe state Hgas Hinitial. specialize (Hfirst state Hgas Hinitial).
  change (let after := snd (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle) end) in
    nonceState query (MS.t.host after) ∧ total after ≤ total state + allowance).
  destruct (action state) as [[value|error] middle]; cbn [snd] in *; [|exact Hfirst].
  destruct (Hframe value middle) as [Hhost Hremaining].
  split; [rewrite Hhost; exact (proj1 Hfirst)|].
  unfold nonceAndGas. rewrite Hhost, Hremaining. exact (proj2 Hfirst).
Qed.

Lemma charged_nonce_bound {A : Set} charge allowance (next : @V.M.t S.t F A) :
  allowance ≤ Z.of_N charge → boundedNonce allowance next →
  boundedNonce 0 (V.M.op_letdollar (V.spend charge) (fun _ ⇒ next)).
Proof.
  intros Hcharge Hnext state Hgas Hinitial.
  change (let after := snd (match V.spend charge state with
    | (inl _, middle) ⇒ next middle
    | (inr error, middle) ⇒ (inr error, middle) end) in
    nonceState query (MS.t.host after) ∧ total after ≤ total state + 0).
  unfold V.spend.
  destruct (Gas.op_lt (MS.t.gas state) charge) eqn:Hafford; cbn beta iota zeta delta [snd].
  - split; [exact Hinitial|unfold nonceAndGas; cbn; lia].
  - change ((MS.t.gas state <? charge)%N = false) in Hafford.
    apply N.ltb_ge in Hafford.
    unfold Gas.op_minus, Support.UintN.op_minus.
    assert (Htest : (charge <=? MS.t.gas state)%N = true) by (apply N.leb_le; exact Hafford).
    rewrite Htest.
    specialize (Hnext (MS.t.with_gas (MS.t.gas state - charge)%N state)
      ltac:(cbn; pose proof (N.le_sub_l (MS.t.gas state) charge); lia) Hinitial).
    destruct Hnext as [Hstate Htotal]. split; [exact Hstate|].
    unfold nonceAndGas in ×. cbn [MS.t.host MS.t.gas MS.t.with_gas] in Htotal.
    rewrite N2Z.inj_sub in Htotal by exact Hafford. lia.
Qed.

Theorem create_opcodes_nonce_bound cold warm access write reserve :
  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, 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) →
  boundedNonce 0 (@V.Executor.create S.t F E) ∧
  boundedNonce 0 (@V.Executor.create2 S.t F E).
Proof.
  intros Hrevision Hbalance Hcall.
  assert (Hgeneric : ∀ kind salt value start size,
    boundedNonce 2 (V.Executor.generic_create_impl kind salt value start size)).
  { intros kind salt value start size state Hgas Hinitial.
    eapply generic_create_nonce_bound;
      [exact Hrevision|exact Hbalance| |exact Hgas|exact Hinitial].
    intros msg host Hkind Hmsg Hstate. apply Hcall; assumption. }
  split; unfold V.Executor.create, V.Executor.create2.
  all: apply bounded_prefix; [lia|apply resources_pop3|].
  all: intros [[value start] size].
  2: (apply bounded_prefix; [lia|apply resources_fmap, resources_pop|]; intro salt).
  all: apply bounded_prefix; [lia|apply resources_memory|]; intro memoryCost.
  all: apply charged_nonce_bound with (allowance := 2).
  all: try (unfold Gas.op_plus, Gas.op_star, Support.UintN.op_plus, Support.UintN.op_star;
    change Gas.create_cost with 32000%N; repeat rewrite N2Z.inj_add; lia).
  all: apply bounded_suffix; [apply Hgeneric|]; intros []; apply resources_pc.
Qed.
End VM.

Print Assumptions create_opcodes_nonce_bound.