Library SetcodeConcrete.vmnoncecalls
Nonce bounds for the generated child-call interface
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant SetcodeConcrete.checkednonce SetcodeConcrete.noncecreate
SetcodeConcrete.noncebudget SetcodeConcrete.noncegas
SetcodeConcrete.nonceeffects.
Require Import MonadExecutionSpecs.Termination.VmHostGasBound
MonadExecutionSpecs.Termination.VmUnsignedGas
MonadExecutionSpecs.Termination.VmChildGasMerge
MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma bounded_child_gas_valid returned message :
0 ≤ message < 2^63 → 0 ≤ returned ≤ message →
invalid_host_gas returned message = false.
Proof.
intros Hmessage Hreturned.
destruct (int64_range_checks_false returned message ltac:(unfold signed_int64_range; lia)
ltac:(unfold signed_int64_range; lia)) as [Hmin [Hmax Hout]].
unfold invalid_host_gas. rewrite Hmin, Hmax, Hout. cbn [orb].
unfold Int64.compare, OCamlInt64.compare, Int64.zero, OCamlInt64.zero.
destruct (Z.compare_spec returned 0); cbn; try lia.
all: unfold Int64.unsigned_compare, OCamlInt64.unsigned_compare,
OCamlInt64.compare, OCamlInt64.unsigned, OCamlInt64.modulus,
OCamlInt64.size, OCamlInt64.Width.bits.
all: rewrite !Z.mod_small by lia.
all: destruct (Z.compare_spec returned message); cbn; lia.
Qed.
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).
Lemma host_call_exact msg state result after :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
0 ≤ Evmc.Result.t.gas_left result ≤ Evmc.Message.t.gas msg →
V.Host.(Evmc.HOST.call) msg (MS.t.host state) = (result, after) →
V.M.HostAPI.call msg state = (inl result, MS.t.with_host after state).
Proof.
intros Hgas Hresult Hcall.
unfold V.M.HostAPI.call, V.M.HostAPI.lift, V.M.HostAPI.Base.
cbn -[V.Host invalid_host_gas Int64.compare Int64.unsigned_compare
Int64.min_int Int64.max_int Int64.zero].
rewrite Hcall.
change ((if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then V.M.fail Evmc.Result.StatusCode.Internal_error
else V.M._return result) (MS.t.with_host after state) =
(inl result, MS.t.with_host after state)).
rewrite bounded_child_gas_valid by assumption. reflexivity.
Qed.
Lemma push_nonce_frame value state :
MS.t.host (snd (V.push value state)) = MS.t.host state ∧
MS.t.gas (snd (V.push value state)) = MS.t.gas state.
Proof. unfold V.push. destruct (Z.geb (MS.t.stack_depth state) V.max_stack_depth); split; reflexivity. Qed.
Lemma host_call_checked msg state result after :
V.Host.(Evmc.HOST.call) msg (MS.t.host state) = (result, after) →
V.M.HostAPI.call msg state =
(if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then inr Evmc.Result.StatusCode.Internal_error else inl result,
MS.t.with_host after state).
Proof.
intro Hcall. unfold V.M.HostAPI.call, V.M.HostAPI.lift, V.M.HostAPI.Base.
cbn -[V.Host invalid_host_gas Int64.compare Int64.unsigned_compare
Int64.min_int Int64.max_int Int64.zero].
rewrite Hcall.
change ((if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then V.M.fail Evmc.Result.StatusCode.Internal_error else V.M._return result)
(MS.t.with_host after state) =
(if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then inr Evmc.Result.StatusCode.Internal_error else inl result,
MS.t.with_host after state)).
destruct (invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)); reflexivity.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant SetcodeConcrete.checkednonce SetcodeConcrete.noncecreate
SetcodeConcrete.noncebudget SetcodeConcrete.noncegas
SetcodeConcrete.nonceeffects.
Require Import MonadExecutionSpecs.Termination.VmHostGasBound
MonadExecutionSpecs.Termination.VmUnsignedGas
MonadExecutionSpecs.Termination.VmChildGasMerge
MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma bounded_child_gas_valid returned message :
0 ≤ message < 2^63 → 0 ≤ returned ≤ message →
invalid_host_gas returned message = false.
Proof.
intros Hmessage Hreturned.
destruct (int64_range_checks_false returned message ltac:(unfold signed_int64_range; lia)
ltac:(unfold signed_int64_range; lia)) as [Hmin [Hmax Hout]].
unfold invalid_host_gas. rewrite Hmin, Hmax, Hout. cbn [orb].
unfold Int64.compare, OCamlInt64.compare, Int64.zero, OCamlInt64.zero.
destruct (Z.compare_spec returned 0); cbn; try lia.
all: unfold Int64.unsigned_compare, OCamlInt64.unsigned_compare,
OCamlInt64.compare, OCamlInt64.unsigned, OCamlInt64.modulus,
OCamlInt64.size, OCamlInt64.Width.bits.
all: rewrite !Z.mod_small by lia.
all: destruct (Z.compare_spec returned message); cbn; lia.
Qed.
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).
Lemma host_call_exact msg state result after :
0 ≤ Evmc.Message.t.gas msg < 2^63 →
0 ≤ Evmc.Result.t.gas_left result ≤ Evmc.Message.t.gas msg →
V.Host.(Evmc.HOST.call) msg (MS.t.host state) = (result, after) →
V.M.HostAPI.call msg state = (inl result, MS.t.with_host after state).
Proof.
intros Hgas Hresult Hcall.
unfold V.M.HostAPI.call, V.M.HostAPI.lift, V.M.HostAPI.Base.
cbn -[V.Host invalid_host_gas Int64.compare Int64.unsigned_compare
Int64.min_int Int64.max_int Int64.zero].
rewrite Hcall.
change ((if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then V.M.fail Evmc.Result.StatusCode.Internal_error
else V.M._return result) (MS.t.with_host after state) =
(inl result, MS.t.with_host after state)).
rewrite bounded_child_gas_valid by assumption. reflexivity.
Qed.
Lemma push_nonce_frame value state :
MS.t.host (snd (V.push value state)) = MS.t.host state ∧
MS.t.gas (snd (V.push value state)) = MS.t.gas state.
Proof. unfold V.push. destruct (Z.geb (MS.t.stack_depth state) V.max_stack_depth); split; reflexivity. Qed.
Lemma host_call_checked msg state result after :
V.Host.(Evmc.HOST.call) msg (MS.t.host state) = (result, after) →
V.M.HostAPI.call msg state =
(if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then inr Evmc.Result.StatusCode.Internal_error else inl result,
MS.t.with_host after state).
Proof.
intro Hcall. unfold V.M.HostAPI.call, V.M.HostAPI.lift, V.M.HostAPI.Base.
cbn -[V.Host invalid_host_gas Int64.compare Int64.unsigned_compare
Int64.min_int Int64.max_int Int64.zero].
rewrite Hcall.
change ((if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then V.M.fail Evmc.Result.StatusCode.Internal_error else V.M._return result)
(MS.t.with_host after state) =
(if invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)
then inr Evmc.Result.StatusCode.Internal_error else inl result,
MS.t.with_host after state)).
destruct (invalid_host_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas msg)); reflexivity.
Qed.
forwarded is the actual child gas from the CALL quote, represented as
a mathematical natural. The caller has already paid it. Depth failure
refunds that amount; a real child refunds only its unused gas.
Theorem generic_call_nonce_bound kind forwarded value sender recipient codeAddr
delegation static inputStart inputSize outputStart outputSize state :
(Z.of_N forwarded < 2^63)%Z →
(∀ 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) 0 host result after) →
nonceState query (MS.t.host state) →
let after := snd (V.Executor.generic_call_impl kind (Numeric.U256.of_uint_exn forwarded)
value sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize state) in
nonceState query (MS.t.host after) ∧ total after ≤ total state + Z.of_N forwarded.
Proof.
intros Hgas Hcall Hinitial.
unfold V.Executor.generic_call_impl.
cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund
Numeric.U256.of_uint_exn Numeric.U256.to_uint Numeric.U256.to_uint64
V.Memory V.MachineState.t.with_output_buffer].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hdepth end.
- match goal with |- context [V.push ?value ?initial] ⇒
destruct (push_nonce_frame value initial) as [Hhost Hremaining]
end.
split; [rewrite Hhost; exact Hinitial|].
unfold noncegas.nonceAndGas. rewrite Hhost, Hremaining.
change (measure (MS.t.host state) +
Z.of_N (MS.t.gas state + Numeric.U256.to_uint (Numeric.U256.of_uint_exn forwarded)) ≤
measure (MS.t.host state) + Z.of_N (MS.t.gas state) + Z.of_N forwarded).
rewrite N2Z.inj_add.
rewrite u256_forwarded_uint_roundtrip by lia. lia.
- destruct delegation; cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund
Numeric.U256.of_uint_exn Numeric.U256.to_uint64 V.Memory
V.MachineState.t.with_output_buffer].
all: match goal with |- context [V.M.HostAPI.call ?message ?before] ⇒
set (child := message) in *;
set (prepared := before) in ×
end.
all: assert (Hchildgas : Evmc.Message.t.gas child = Z.of_N forwarded) by
(unfold child; cbn [Evmc.Message.t.gas];
rewrite u256_forwarded_gas_small_identity by exact Hgas; reflexivity).
all: pose proof (Hcall child (MS.t.host prepared) eq_refl ltac:(rewrite Hchildgas; lia) Hinitial)
as Hchild.
all: destruct (V.Host.(Evmc.HOST.call) child (MS.t.host prepared)) as [result host] eqn:Hrun.
all: destruct Hchild as [Hstate Htotal].
all: rewrite (host_call_checked child prepared result host Hrun).
all: unfold checkedRefund in Htotal.
all: destruct (invalid_host_gas (Evmc.Result.t.gas_left result)
(Evmc.Message.t.gas child)) eqn:Hvalid.
all: try solve [cbn -[prepared]; split; [exact Hstate|];
unfold noncegas.nonceAndGas; cbn -[prepared];
rewrite Hchildgas in Htotal; unfold prepared in *;
cbn [MS.t.host MS.t.gas MS.t.with_output_buffer] in *; lia].
all: pose proof (valid_returned_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas child) ltac:(rewrite Hchildgas; lia) Hvalid) as Hreturned.
all: destruct result as [status gasleft refund output address].
all: cbn -[V.push V.Executor.merge_child_gas_and_refund V.Memory prepared
Numeric.U256.to_int_opt Byte_string.Bytes.sub Byte_string.Bytes.length].
all: unfold V.Executor.merge_child_gas_and_refund.
all: destruct status;
cbn -[V.push V.Memory prepared Numeric.U256.to_int_opt
Byte_string.Bytes.sub Byte_string.Bytes.length 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 noncegas.nonceAndGas; rewrite Hhost, Hremaining.
all: cbn -[Gas.of_uint64 Gas.op_plus]; 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 Htotal; cbn [MS.t.host MS.t.with_output_buffer] in Htotal; lia.
Qed.
End VM.
Print Assumptions generic_call_nonce_bound.
delegation static inputStart inputSize outputStart outputSize state :
(Z.of_N forwarded < 2^63)%Z →
(∀ 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) 0 host result after) →
nonceState query (MS.t.host state) →
let after := snd (V.Executor.generic_call_impl kind (Numeric.U256.of_uint_exn forwarded)
value sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize state) in
nonceState query (MS.t.host after) ∧ total after ≤ total state + Z.of_N forwarded.
Proof.
intros Hgas Hcall Hinitial.
unfold V.Executor.generic_call_impl.
cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund
Numeric.U256.of_uint_exn Numeric.U256.to_uint Numeric.U256.to_uint64
V.Memory V.MachineState.t.with_output_buffer].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hdepth end.
- match goal with |- context [V.push ?value ?initial] ⇒
destruct (push_nonce_frame value initial) as [Hhost Hremaining]
end.
split; [rewrite Hhost; exact Hinitial|].
unfold noncegas.nonceAndGas. rewrite Hhost, Hremaining.
change (measure (MS.t.host state) +
Z.of_N (MS.t.gas state + Numeric.U256.to_uint (Numeric.U256.of_uint_exn forwarded)) ≤
measure (MS.t.host state) + Z.of_N (MS.t.gas state) + Z.of_N forwarded).
rewrite N2Z.inj_add.
rewrite u256_forwarded_uint_roundtrip by lia. lia.
- destruct delegation; cbn -[V.M.HostAPI.call V.push V.Executor.merge_child_gas_and_refund
Numeric.U256.of_uint_exn Numeric.U256.to_uint64 V.Memory
V.MachineState.t.with_output_buffer].
all: match goal with |- context [V.M.HostAPI.call ?message ?before] ⇒
set (child := message) in *;
set (prepared := before) in ×
end.
all: assert (Hchildgas : Evmc.Message.t.gas child = Z.of_N forwarded) by
(unfold child; cbn [Evmc.Message.t.gas];
rewrite u256_forwarded_gas_small_identity by exact Hgas; reflexivity).
all: pose proof (Hcall child (MS.t.host prepared) eq_refl ltac:(rewrite Hchildgas; lia) Hinitial)
as Hchild.
all: destruct (V.Host.(Evmc.HOST.call) child (MS.t.host prepared)) as [result host] eqn:Hrun.
all: destruct Hchild as [Hstate Htotal].
all: rewrite (host_call_checked child prepared result host Hrun).
all: unfold checkedRefund in Htotal.
all: destruct (invalid_host_gas (Evmc.Result.t.gas_left result)
(Evmc.Message.t.gas child)) eqn:Hvalid.
all: try solve [cbn -[prepared]; split; [exact Hstate|];
unfold noncegas.nonceAndGas; cbn -[prepared];
rewrite Hchildgas in Htotal; unfold prepared in *;
cbn [MS.t.host MS.t.gas MS.t.with_output_buffer] in *; lia].
all: pose proof (valid_returned_gas (Evmc.Result.t.gas_left result) (Evmc.Message.t.gas child) ltac:(rewrite Hchildgas; lia) Hvalid) as Hreturned.
all: destruct result as [status gasleft refund output address].
all: cbn -[V.push V.Executor.merge_child_gas_and_refund V.Memory prepared
Numeric.U256.to_int_opt Byte_string.Bytes.sub Byte_string.Bytes.length].
all: unfold V.Executor.merge_child_gas_and_refund.
all: destruct status;
cbn -[V.push V.Memory prepared Numeric.U256.to_int_opt
Byte_string.Bytes.sub Byte_string.Bytes.length 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 noncegas.nonceAndGas; rewrite Hhost, Hremaining.
all: cbn -[Gas.of_uint64 Gas.op_plus]; 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 Htotal; cbn [MS.t.host MS.t.with_output_buffer] in Htotal; lia.
Qed.
End VM.
Print Assumptions generic_call_nonce_bound.