Library SetcodeConcrete.executionnonce
Nonce growth in the actual recursive SETCODE executor
From Stdlib Require Import ZArith NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.recursiveassembly QedkConcrete.hostnonce
QedkConcrete.hostframe QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.nonceeffects SetcodeConcrete.noncebudget
SetcodeConcrete.noncecreate SetcodeConcrete.checkednonce
SetcodeConcrete.noncecall SetcodeConcrete.noncecallimpl SetcodeConcrete.nonceprocesscall
SetcodeConcrete.noncehost SetcodeConcrete.assembly
SetcodeConcrete.vmprogress SetcodeConcrete.vmerrors SetcodeConcrete.vmnonceentry.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (query : Addr.t) (Hnative : query ≠ Reserve_balance.precompile_address).
Record nonceHost (host : Evmc.HOST (t := S.t)) : Prop := {
host_primitives : ∀ nonce, PrimitiveHostFrame host (nonceInvariant query nonce);
host_setcode : ∀ nonce current source static gas,
preservesCallback (nonceInvariant query nonce)
(host.(Evmc.HOST.setcodefrom) current source static gas);
host_progress : ∀ params, @setcodeGasProgress S.t (Vm.Make.Build_FArgs params host);
host_error : ∀ params, @setcodeNoSuccessError S.t (Vm.Make.Build_FArgs params host);
host_balance : ∀ addr st, snd (host.(Evmc.HOST.get_balance) addr st) = st;
host_context : ∀ st, snd (host.(Evmc.HOST.get_tx_context) st) = st;
host_size : ∀ addr st, snd (host.(Evmc.HOST.get_code_size) addr st) = st;
host_copy : ∀ addr offset size st, snd (host.(Evmc.HOST.copy_code) addr offset size st) = st;
host_call : ∀ msg st, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := host.(Evmc.HOST.call) msg st in
checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
st result after
}.
Definition nonceVm {T : Evmc.Vm_H_signature (t := S.t)}
(vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs T)) :=
∀ msg code st, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := vm.(Evmc.Vm.SIG.execute) msg code st in
nonceResult query (Evmc.Message.t.gas msg) 0 st result after.
Lemma vm_constructor_nonce host : nonceHost host → nonceVm (VmF host).
Proof.
intros Hhost msg code st Hgas Hstate.
cbn -[Vm.Make.execute].
eapply execute_nonce_bound; try exact Hrevision; try reflexivity.
- apply (host_progress _ Hhost).
- apply (host_error _ Hhost).
- apply (host_primitives _ Hhost).
- apply (host_setcode _ Hhost).
- apply (host_balance _ Hhost).
- apply (host_context _ Hhost).
- apply (host_size _ Hhost).
- apply (host_copy _ Hhost).
- apply (host_call _ Hhost).
- exact Hgas.
- exact Hstate.
Qed.
Lemma host_constructor_nonce vm : nonceVm vm → nonceHost (HostF vm).
Proof.
intro Hvm. constructor.
- apply concrete_primitives_preserve_nonce.
- apply concrete_setcode_preserves_nonce.
- intro params. eapply concrete_setcode_gas_progress;
[exact Hrevision|exact Hcold|exact Hwarm].
- apply assembled_setcode_no_success_error.
- intros; reflexivity.
- intros; reflexivity.
- intros; reflexivity.
- intros. apply copy_code_read_only.
- intros msg st Hgas Hstate.
change (let '(result, after) := @H.call
(H.Build_FArgs args.(Host.Instantiate.ChainParams) vm) msg st in
checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
st result after).
apply call_nonce_bound; [exact Hvm| |exact Hgas|exact Hstate].
eapply process_call_nonce_bound;
[exact Hrevision|exact Hnative|exact Hvm|exact Hgas].
Qed.
Theorem execution_vm_nonce_bound msg code st :
0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := @Host.Instantiate.Vm.execute args msg code st in
nonceResult query (Evmc.Message.t.gas msg) 0 st result after.
Proof.
assert (Hassembly : nonceHost (@Evmc.Instantiate.Host S.t (@recursiveArgs args)) ∧
nonceVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args))).
{ unfold Evmc.Instantiate.Host, Evmc.Instantiate.Vm,
Evmc.Instantiate._recursive_modules_Host_Vm.
generalize Evmc.Instantiate.recursive_module_fuel. intro fuel.
induction fuel as [|fuel IH].
- assert (Hbase : nonceVm (@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args))).
{ intros nested body initial Hgas Hstate.
split; [exact Hstate|]. cbn. split; lia. }
split; [apply host_constructor_nonce; exact Hbase|exact Hbase].
- cbn [recursive_module_unroll fst snd]. split.
+ apply host_constructor_nonce. exact (proj2 IH).
+ apply vm_constructor_nonce. exact (proj1 IH). }
apply (proj2 Hassembly).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.recursiveassembly QedkConcrete.hostnonce
QedkConcrete.hostframe QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.nonceeffects SetcodeConcrete.noncebudget
SetcodeConcrete.noncecreate SetcodeConcrete.checkednonce
SetcodeConcrete.noncecall SetcodeConcrete.noncecallimpl SetcodeConcrete.nonceprocesscall
SetcodeConcrete.noncehost SetcodeConcrete.assembly
SetcodeConcrete.vmprogress SetcodeConcrete.vmerrors SetcodeConcrete.vmnonceentry.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (query : Addr.t) (Hnative : query ≠ Reserve_balance.precompile_address).
Record nonceHost (host : Evmc.HOST (t := S.t)) : Prop := {
host_primitives : ∀ nonce, PrimitiveHostFrame host (nonceInvariant query nonce);
host_setcode : ∀ nonce current source static gas,
preservesCallback (nonceInvariant query nonce)
(host.(Evmc.HOST.setcodefrom) current source static gas);
host_progress : ∀ params, @setcodeGasProgress S.t (Vm.Make.Build_FArgs params host);
host_error : ∀ params, @setcodeNoSuccessError S.t (Vm.Make.Build_FArgs params host);
host_balance : ∀ addr st, snd (host.(Evmc.HOST.get_balance) addr st) = st;
host_context : ∀ st, snd (host.(Evmc.HOST.get_tx_context) st) = st;
host_size : ∀ addr st, snd (host.(Evmc.HOST.get_code_size) addr st) = st;
host_copy : ∀ addr offset size st, snd (host.(Evmc.HOST.copy_code) addr offset size st) = st;
host_call : ∀ msg st, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := host.(Evmc.HOST.call) msg st in
checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
st result after
}.
Definition nonceVm {T : Evmc.Vm_H_signature (t := S.t)}
(vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs T)) :=
∀ msg code st, 0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := vm.(Evmc.Vm.SIG.execute) msg code st in
nonceResult query (Evmc.Message.t.gas msg) 0 st result after.
Lemma vm_constructor_nonce host : nonceHost host → nonceVm (VmF host).
Proof.
intros Hhost msg code st Hgas Hstate.
cbn -[Vm.Make.execute].
eapply execute_nonce_bound; try exact Hrevision; try reflexivity.
- apply (host_progress _ Hhost).
- apply (host_error _ Hhost).
- apply (host_primitives _ Hhost).
- apply (host_setcode _ Hhost).
- apply (host_balance _ Hhost).
- apply (host_context _ Hhost).
- apply (host_size _ Hhost).
- apply (host_copy _ Hhost).
- apply (host_call _ Hhost).
- exact Hgas.
- exact Hstate.
Qed.
Lemma host_constructor_nonce vm : nonceVm vm → nonceHost (HostF vm).
Proof.
intro Hvm. constructor.
- apply concrete_primitives_preserve_nonce.
- apply concrete_setcode_preserves_nonce.
- intro params. eapply concrete_setcode_gas_progress;
[exact Hrevision|exact Hcold|exact Hwarm].
- apply assembled_setcode_no_success_error.
- intros; reflexivity.
- intros; reflexivity.
- intros; reflexivity.
- intros. apply copy_code_read_only.
- intros msg st Hgas Hstate.
change (let '(result, after) := @H.call
(H.Build_FArgs args.(Host.Instantiate.ChainParams) vm) msg st in
checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 2 else 0)
st result after).
apply call_nonce_bound; [exact Hvm| |exact Hgas|exact Hstate].
eapply process_call_nonce_bound;
[exact Hrevision|exact Hnative|exact Hvm|exact Hgas].
Qed.
Theorem execution_vm_nonce_bound msg code st :
0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := @Host.Instantiate.Vm.execute args msg code st in
nonceResult query (Evmc.Message.t.gas msg) 0 st result after.
Proof.
assert (Hassembly : nonceHost (@Evmc.Instantiate.Host S.t (@recursiveArgs args)) ∧
nonceVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args))).
{ unfold Evmc.Instantiate.Host, Evmc.Instantiate.Vm,
Evmc.Instantiate._recursive_modules_Host_Vm.
generalize Evmc.Instantiate.recursive_module_fuel. intro fuel.
induction fuel as [|fuel IH].
- assert (Hbase : nonceVm (@Evmc.Instantiate.recursive_module_base_vm S.t (@recursiveArgs args))).
{ intros nested body initial Hgas Hstate.
split; [exact Hstate|]. cbn. split; lia. }
split; [apply host_constructor_nonce; exact Hbase|exact Hbase].
- cbn [recursive_module_unroll fst snd]. split.
+ apply host_constructor_nonce. exact (proj2 IH).
+ apply vm_constructor_nonce. exact (proj1 IH). }
apply (proj2 Hassembly).
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
predicted is the already-admitted nonce, not necessarily the actual
nonce after migration. remaining reserves increments for later work.
No half-range condition is reimposed on the returned state.
Corollary execution_vm_retains_nonce_capacity msg code st predicted remaining :
0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
nonceCapacity (nonceAt (S.t.world_state st) query) predicted
(Evmc.Message.t.gas msg + remaining) → 0 ≤ remaining →
let after := snd (@Host.Instantiate.Vm.execute args msg code st) in
nonceState query after ∧
nonceCapacity (nonceAt (S.t.world_state after) query) predicted remaining.
Proof.
intros Hgas Hstate Hcapacity Hremaining.
pose proof (execution_vm_nonce_bound msg code st Hgas Hstate) as Hrun.
destruct (@Host.Instantiate.Vm.execute args msg code st) as [result after].
destruct Hrun as [Hafter [Hreturned Hbound]].
split; [exact Hafter|]. eapply capacity_after_gas with
(actual := nonceAt (S.t.world_state st) query) (gas := Evmc.Message.t.gas msg);
[exact Hcapacity|lia|exact Hremaining|]. split; [exact (proj2 Hafter)|cbn [snd]; lia].
Qed.
0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
nonceCapacity (nonceAt (S.t.world_state st) query) predicted
(Evmc.Message.t.gas msg + remaining) → 0 ≤ remaining →
let after := snd (@Host.Instantiate.Vm.execute args msg code st) in
nonceState query after ∧
nonceCapacity (nonceAt (S.t.world_state after) query) predicted remaining.
Proof.
intros Hgas Hstate Hcapacity Hremaining.
pose proof (execution_vm_nonce_bound msg code st Hgas Hstate) as Hrun.
destruct (@Host.Instantiate.Vm.execute args msg code st) as [result after].
destruct Hrun as [Hafter [Hreturned Hbound]].
split; [exact Hafter|]. eapply capacity_after_gas with
(actual := nonceAt (S.t.world_state st) query) (gas := Evmc.Message.t.gas msg);
[exact Hcapacity|lia|exact Hremaining|]. split; [exact (proj2 Hafter)|cbn [snd]; lia].
Qed.
The transaction wrapper also performs reserve checking and rollback.
Its one extra unit is the initialized nonce of a newly created account;
the protocol has already consumed the sender nonce before this call.
Theorem execution_transaction_call_nonce_bound tx msg st :
0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := @E.Host.call_from_eoa F tx msg st in
checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 1 else 0)
st result after.
Proof.
intros Hgas Hstate.
change (let '(result, after) := @H.call_impl (@Host.Instantiate.Host_fargs args)
(Some tx) msg st in checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 1 else 0)
st result after).
apply call_impl_nonce_bound.
- intros nested code initial Hbound Hinitial.
exact (execution_vm_nonce_bound nested code initial Hbound Hinitial).
- eapply process_call_nonce_bound; [exact Hrevision|exact Hnative| |exact Hgas].
intros nested code initial Hbound Hinitial.
exact (execution_vm_nonce_bound nested code initial Hbound Hinitial).
- exact Hgas.
- exact Hstate.
Qed.
End Execution.
Print Assumptions execution_vm_nonce_bound.
Print Assumptions execution_transaction_call_nonce_bound.
0 ≤ Evmc.Message.t.gas msg < 2^63 → nonceState query st →
let '(result, after) := @E.Host.call_from_eoa F tx msg st in
checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 1 else 0)
st result after.
Proof.
intros Hgas Hstate.
change (let '(result, after) := @H.call_impl (@Host.Instantiate.Host_fargs args)
(Some tx) msg st in checkedNonceResult query (Evmc.Message.t.gas msg)
(if Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg) then 1 else 0)
st result after).
apply call_impl_nonce_bound.
- intros nested code initial Hbound Hinitial.
exact (execution_vm_nonce_bound nested code initial Hbound Hinitial).
- eapply process_call_nonce_bound; [exact Hrevision|exact Hnative| |exact Hgas].
intros nested code initial Hbound Hinitial.
exact (execution_vm_nonce_bound nested code initial Hbound Hinitial).
- exact Hgas.
- exact Hstate.
Qed.
End Execution.
Print Assumptions execution_vm_nonce_bound.
Print Assumptions execution_transaction_call_nonce_bound.