Library SetcodeConcrete.noncegas
Accounting for nonce increments and returned child gas
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import MonadExecutionSpecs.Termination.VmChildGasMerge
MonadExecutionSpecs.Termination.VmCreateGasMerge
MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module V := Vm.Make.
Module MS := V.MachineState.
Section Frame.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (measure : Host_t → Z).
Definition nonceAndGas (s : @MS.t Host_t F) :=
measure (MS.t.host s) + Z.of_N (MS.t.gas s).
Lemma spend_preserves_host amount state next :
@V.spend Host_t F amount state = (inl tt, next) →
MS.t.host next = MS.t.host state.
Proof.
unfold V.spend. destruct (Gas.op_lt (MS.t.gas state) amount); [discriminate|].
intro Hrun. inversion Hrun. reflexivity.
Qed.
Lemma subtract_preserves_host forwarded state next :
@V.M.update_field Host_t F _ MS.gas
(fun gas ⇒ Support.UintN.op_minus gas forwarded) state = (inl tt, next) →
MS.t.host next = MS.t.host state.
Proof. intro Hrun. inversion Hrun. reflexivity. Qed.
Lemma merge_preserves_host status gas refund state next :
@V.Executor.merge_child_gas_and_refund Host_t F E status gas refund state =
(inl tt, next) → MS.t.host next = MS.t.host state.
Proof. destruct status; intro Hrun; inversion Hrun; reflexivity. Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import MonadExecutionSpecs.Termination.VmChildGasMerge
MonadExecutionSpecs.Termination.VmCreateGasMerge
MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module V := Vm.Make.
Module MS := V.MachineState.
Section Frame.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (measure : Host_t → Z).
Definition nonceAndGas (s : @MS.t Host_t F) :=
measure (MS.t.host s) + Z.of_N (MS.t.gas s).
Lemma spend_preserves_host amount state next :
@V.spend Host_t F amount state = (inl tt, next) →
MS.t.host next = MS.t.host state.
Proof.
unfold V.spend. destruct (Gas.op_lt (MS.t.gas state) amount); [discriminate|].
intro Hrun. inversion Hrun. reflexivity.
Qed.
Lemma subtract_preserves_host forwarded state next :
@V.M.update_field Host_t F _ MS.gas
(fun gas ⇒ Support.UintN.op_minus gas forwarded) state = (inl tt, next) →
MS.t.host next = MS.t.host state.
Proof. intro Hrun. inversion Hrun. reflexivity. Qed.
Lemma merge_preserves_host status gas refund state next :
@V.Executor.merge_child_gas_and_refund Host_t F E status gas refund state =
(inl tt, next) → MS.t.host next = MS.t.host state.
Proof. destruct status; intro Hrun; inversion Hrun; reflexivity. Qed.
Applies after the CREATE charge, child-gas allocation, child call, and
merge in the generated handler. It includes failed child results: the
induction obligation must use the actual post-rollback Host state.
Theorem create_charge_and_merge_nonce_bound memory initcode msg
initial charged forwarded middle next result :
V.spend (memory + Gas.create_cost + initcode)%N initial = (inl tt, charged) →
V.M.update_field MS.gas
(fun gas ⇒ Support.UintN.op_minus gas (Gas.minus_1_64th (MS.t.gas charged)))
charged = (inl tt, forwarded) →
V.M.HostAPI.call msg forwarded = (inl result, middle) →
V.Executor.merge_child_gas_and_refund (Evmc.Result.t.status_code result)
(Evmc.Result.t.gas_left result) (Evmc.Result.t.gas_refund result)
middle = (inl tt, next) →
measure (MS.t.host middle) + Evmc.Result.t.gas_left result ≤
measure (MS.t.host forwarded) +
Z.of_N (Gas.minus_1_64th (MS.t.gas charged)) + 2 →
nonceAndGas next ≤ nonceAndGas initial.
Proof.
intros Hspend Hsubtract Hcall Hmerge Hchild.
destruct (spend_success_exact _ _ _ Hspend) as [Hafford Hcharged].
pose proof (subtract_forwarded_gas_exact _ _ _ Hsubtract
(create_forwarded_le (MS.t.gas charged))) as Hforwarded.
destruct (host_call_merge_exact msg forwarded middle next result Hcall Hmerge)
as [Hnext [Hnonnegative Hbounded]].
pose proof (spend_preserves_host _ _ _ Hspend) as Hhost1.
pose proof (subtract_preserves_host _ _ _ Hsubtract) as Hhost2.
pose proof (merge_preserves_host _ _ _ _ _ Hmerge) as Hhost3.
unfold nonceAndGas. rewrite Hhost3, Hnext, Hforwarded.
rewrite N2Z.inj_add, Z2N.id by exact Hnonnegative.
rewrite N2Z.inj_sub by apply create_forwarded_le.
rewrite Hcharged, N2Z.inj_sub by exact Hafford.
rewrite Hhost2, Hhost1, Hcharged in Hchild.
change Gas.create_cost with 32000%N in ×.
repeat rewrite N2Z.inj_add. lia.
Qed.
initial charged forwarded middle next result :
V.spend (memory + Gas.create_cost + initcode)%N initial = (inl tt, charged) →
V.M.update_field MS.gas
(fun gas ⇒ Support.UintN.op_minus gas (Gas.minus_1_64th (MS.t.gas charged)))
charged = (inl tt, forwarded) →
V.M.HostAPI.call msg forwarded = (inl result, middle) →
V.Executor.merge_child_gas_and_refund (Evmc.Result.t.status_code result)
(Evmc.Result.t.gas_left result) (Evmc.Result.t.gas_refund result)
middle = (inl tt, next) →
measure (MS.t.host middle) + Evmc.Result.t.gas_left result ≤
measure (MS.t.host forwarded) +
Z.of_N (Gas.minus_1_64th (MS.t.gas charged)) + 2 →
nonceAndGas next ≤ nonceAndGas initial.
Proof.
intros Hspend Hsubtract Hcall Hmerge Hchild.
destruct (spend_success_exact _ _ _ Hspend) as [Hafford Hcharged].
pose proof (subtract_forwarded_gas_exact _ _ _ Hsubtract
(create_forwarded_le (MS.t.gas charged))) as Hforwarded.
destruct (host_call_merge_exact msg forwarded middle next result Hcall Hmerge)
as [Hnext [Hnonnegative Hbounded]].
pose proof (spend_preserves_host _ _ _ Hspend) as Hhost1.
pose proof (subtract_preserves_host _ _ _ Hsubtract) as Hhost2.
pose proof (merge_preserves_host _ _ _ _ _ Hmerge) as Hhost3.
unfold nonceAndGas. rewrite Hhost3, Hnext, Hforwarded.
rewrite N2Z.inj_add, Z2N.id by exact Hnonnegative.
rewrite N2Z.inj_sub by apply create_forwarded_le.
rewrite Hcharged, N2Z.inj_sub by exact Hafford.
rewrite Hhost2, Hhost1, Hcharged in Hchild.
change Gas.create_cost with 32000%N in ×.
repeat rewrite N2Z.inj_add. lia.
Qed.
The same calculation covers CALL with a value stipend. The allocated
child gas includes that stipend, while the caller pays a larger value
transfer charge; the ordinary call quote proves Hcharge below.
Theorem call_charge_and_merge_nonce_bound charge childGas msg
initial charged middle next result :
V.spend charge initial = (inl tt, charged) →
V.M.HostAPI.call msg charged = (inl result, middle) →
V.Executor.merge_child_gas_and_refund (Evmc.Result.t.status_code result)
(Evmc.Result.t.gas_left result) (Evmc.Result.t.gas_refund result)
middle = (inl tt, next) →
(childGas ≤ charge)%N →
measure (MS.t.host middle) + Evmc.Result.t.gas_left result ≤
measure (MS.t.host charged) + Z.of_N childGas →
nonceAndGas next ≤ nonceAndGas initial.
Proof.
intros Hspend Hcall Hmerge Hcharge Hchild.
destruct (spend_success_exact _ _ _ Hspend) as [Hafford Hcharged].
destruct (host_call_merge_exact msg charged middle next result Hcall Hmerge)
as [Hnext [Hnonnegative Hbounded]].
pose proof (spend_preserves_host _ _ _ Hspend) as Hhost1.
pose proof (merge_preserves_host _ _ _ _ _ Hmerge) as Hhost2.
unfold nonceAndGas. rewrite Hhost2, Hnext, N2Z.inj_add, Z2N.id by exact Hnonnegative.
rewrite Hcharged, N2Z.inj_sub by exact Hafford.
rewrite Hhost1 in Hchild. lia.
Qed.
Lemma call_quote_pays_child (transfer : bool) requested before memory access delegation newAccount :
let extra := (access + delegation +
(if transfer then Gas.call_value else 0) + newAccount)%N in
let quote := Gas.call_gas_value transfer requested before memory extra in
(Gas.call_gas.callee_available_gas quote ≤
Gas.call_gas.caller_spent_gas quote + memory)%N.
Proof.
destruct transfer; unfold Gas.call_gas_value;
cbn [Gas.call_gas.callee_available_gas Gas.call_gas.caller_spent_gas];
unfold Support.UintN.op_plus;
rewrite ?call_value_cost, ?call_stipend_value; lia.
Qed.
Corollary quoted_call_nonce_bound (transfer : bool) requested before memory access delegation
newAccount msg initial charged middle next result :
let extra := (access + delegation +
(if transfer then Gas.call_value else 0) + newAccount)%N in
let quote := Gas.call_gas_value transfer requested before memory extra in
V.spend (Gas.call_gas.caller_spent_gas quote + memory)%N initial = (inl tt, charged) →
V.M.HostAPI.call msg charged = (inl result, middle) →
V.Executor.merge_child_gas_and_refund (Evmc.Result.t.status_code result)
(Evmc.Result.t.gas_left result) (Evmc.Result.t.gas_refund result)
middle = (inl tt, next) →
measure (MS.t.host middle) + Evmc.Result.t.gas_left result ≤
measure (MS.t.host charged) + Z.of_N (Gas.call_gas.callee_available_gas quote) →
nonceAndGas next ≤ nonceAndGas initial.
Proof.
cbn zeta. intros Hspend Hcall Hmerge Hchild.
eapply call_charge_and_merge_nonce_bound; try eassumption.
apply call_quote_pays_child.
Qed.
End Frame.
initial charged middle next result :
V.spend charge initial = (inl tt, charged) →
V.M.HostAPI.call msg charged = (inl result, middle) →
V.Executor.merge_child_gas_and_refund (Evmc.Result.t.status_code result)
(Evmc.Result.t.gas_left result) (Evmc.Result.t.gas_refund result)
middle = (inl tt, next) →
(childGas ≤ charge)%N →
measure (MS.t.host middle) + Evmc.Result.t.gas_left result ≤
measure (MS.t.host charged) + Z.of_N childGas →
nonceAndGas next ≤ nonceAndGas initial.
Proof.
intros Hspend Hcall Hmerge Hcharge Hchild.
destruct (spend_success_exact _ _ _ Hspend) as [Hafford Hcharged].
destruct (host_call_merge_exact msg charged middle next result Hcall Hmerge)
as [Hnext [Hnonnegative Hbounded]].
pose proof (spend_preserves_host _ _ _ Hspend) as Hhost1.
pose proof (merge_preserves_host _ _ _ _ _ Hmerge) as Hhost2.
unfold nonceAndGas. rewrite Hhost2, Hnext, N2Z.inj_add, Z2N.id by exact Hnonnegative.
rewrite Hcharged, N2Z.inj_sub by exact Hafford.
rewrite Hhost1 in Hchild. lia.
Qed.
Lemma call_quote_pays_child (transfer : bool) requested before memory access delegation newAccount :
let extra := (access + delegation +
(if transfer then Gas.call_value else 0) + newAccount)%N in
let quote := Gas.call_gas_value transfer requested before memory extra in
(Gas.call_gas.callee_available_gas quote ≤
Gas.call_gas.caller_spent_gas quote + memory)%N.
Proof.
destruct transfer; unfold Gas.call_gas_value;
cbn [Gas.call_gas.callee_available_gas Gas.call_gas.caller_spent_gas];
unfold Support.UintN.op_plus;
rewrite ?call_value_cost, ?call_stipend_value; lia.
Qed.
Corollary quoted_call_nonce_bound (transfer : bool) requested before memory access delegation
newAccount msg initial charged middle next result :
let extra := (access + delegation +
(if transfer then Gas.call_value else 0) + newAccount)%N in
let quote := Gas.call_gas_value transfer requested before memory extra in
V.spend (Gas.call_gas.caller_spent_gas quote + memory)%N initial = (inl tt, charged) →
V.M.HostAPI.call msg charged = (inl result, middle) →
V.Executor.merge_child_gas_and_refund (Evmc.Result.t.status_code result)
(Evmc.Result.t.gas_left result) (Evmc.Result.t.gas_refund result)
middle = (inl tt, next) →
measure (MS.t.host middle) + Evmc.Result.t.gas_left result ≤
measure (MS.t.host charged) + Z.of_N (Gas.call_gas.callee_available_gas quote) →
nonceAndGas next ≤ nonceAndGas initial.
Proof.
cbn zeta. intros Hspend Hcall Hmerge Hchild.
eapply call_charge_and_merge_nonce_bound; try eassumption.
apply call_quote_pays_child.
Qed.
End Frame.