Library SetcodeConcrete.noncecreate
Nonce accounting through concrete contract creation
From Stdlib Require Import String ZArith NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.hostnonce QedkConcrete.worldaccounts
SetcodeConcrete.noncebudget SetcodeConcrete.nonceeffects.
Require Import MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition nonceState query (state : S.t) : Prop :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) ∧
0 ≤ nonceAt (S.t.world_state state) query.
Definition nonceResult query gas allowance (before : S.t)
(result : Evmc.Result.t) (after : S.t) : Prop :=
nonceState query after ∧
0 ≤ Evmc.Result.t.gas_left result ≤ gas ∧
nonceAt (S.t.world_state after) query + Evmc.Result.t.gas_left result ≤
nonceAt (S.t.world_state before) query + gas + allowance.
Lemma deposit_gas_bound gas cost :
0 ≤ gas < 2^63 →
Gas.op_lt (Gas.of_uint64 gas) cost = false →
0 ≤ Gas.to_uint64 (Gas.op_minus (Gas.of_uint64 gas) cost) ≤ gas.
Proof.
intros Hgas Hafford.
assert (Hread : Gas.of_uint64 gas = Z.to_N gas).
{ unfold Gas.of_uint64, Support.UintN.of_uint64.
unfold OCamlZ.of_int64_unsigned.
destruct (Z.ltb gas 0) eqn:Hnegative;
[apply Z.ltb_lt in Hnegative; lia|].
unfold Support.UintN.of_z_exn, Support.UintN.of_z_opt, Support.UintN.in_range.
destruct (Z.leb 0 gas) eqn:Hpositive;
[reflexivity|apply Z.leb_gt in Hpositive; lia]. }
rewrite Hread in Hafford |- ×.
change ((Z.to_N gas <? cost)%N = false) in Hafford.
apply N.ltb_ge in Hafford.
unfold Gas.op_minus, Support.UintN.op_minus.
assert (Htest : (cost <=? Z.to_N gas)%N = true) by (apply N.leb_le; exact Hafford).
rewrite Htest.
unfold Gas.to_uint64, Support.UintN.to_uint64, Support.UintN.to_z.
rewrite int64_unsigned_small_identity by
(pose proof (N.le_sub_l (Z.to_N gas) cost); pose proof (Z2N.id gas ltac:(lia)); lia).
split; [lia|].
pose proof (N.le_sub_l (Z.to_N gas) cost). rewrite <- (Z2N.id gas) by lia. lia.
Qed.
Section Creation.
Context {F : H.FArgs}.
Theorem process_create_nonce_bound query msg :
(∀ nested code state,
0 ≤ Evmc.Message.t.gas nested < 2^63 → nonceState query state →
let '(result, after) := H.Vm.(Evmc.Vm.SIG.execute) nested code state in
nonceResult query (Evmc.Message.t.gas nested) 0 state result after) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
∀ state, nonceState query state →
let '(result, after) := H.process_create msg state in
nonceResult query (Evmc.Message.t.gas msg) 1 state result after.
Proof.
intros Hvm Hgas state Hinitial.
unfold H.process_create, H.contract_creation_address, H.M.
cbn -[Addr.of_contract_creation W.account Numeric.U64.op_ltgt
Byte_string.Bytes.op_ltgt H.transfer_ether nonceResult nonceState].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
{ unfold nonceResult, Evmc.Result.failure. cbn.
split; [exact Hinitial|]. split; lia. }
match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
match goal with |- context [H.transfer_ether _ _ _
(S.t.with_world_state ?nonceWorld (S.t.with_world_state ?storageWorld ?created))] ⇒
set (marked := created) in *;
set (stored := S.t.with_world_state storageWorld marked) in *;
set (prepared := S.t.with_world_state nonceWorld stored) in ×
end.
assert (Hmarked : nonceInvariant query (nonceAt (S.t.world_state state) query) marked).
{ split; [exact (proj1 Hinitial)|reflexivity]. }
assert (Hstored : nonceInvariant query (nonceAt (S.t.world_state state) query) stored).
{ unfold stored. eapply nonce_write with (s := marked) (keep := false) (addr := target);
[exact Hmarked|].
intro Heq. change (nonceAt (S.t.world_state state) target = nonceAt (S.t.world_state state) query).
rewrite Heq. reflexivity. }
assert (Hprepared : nonceState query prepared ∧
nonceAt (S.t.world_state prepared) query ≤ nonceAt (S.t.world_state state) query + 1).
{ unfold prepared. split.
- split; [apply account_write_wellformed; exact (proj1 Hstored)|].
change (0 ≤ nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state stored)
(W.account (Some false) target)
(A.t.with_nonce Numeric.U64.one
(State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None target)))) query).
rewrite nonce_after_write by exact (proj1 Hstored).
destruct (String.eqb target query); [change (0 ≤ 1); lia|].
change (0 ≤ A.t.nonce (State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None query))).
rewrite (proj2 Hstored). exact (proj2 Hinitial).
- change (nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state stored)
(W.account (Some false) target)
(A.t.with_nonce Numeric.U64.one
(State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None target)))) query ≤ nonceAt (S.t.world_state state) query + 1).
rewrite nonce_after_write by exact (proj1 Hstored).
destruct (String.eqb target query); [change (1 ≤ nonceAt (S.t.world_state state) query + 1); pose proof (proj2 Hinitial); lia|].
change (A.t.nonce (State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None query)) ≤ nonceAt (S.t.world_state state) query + 1).
rewrite (proj2 Hstored). lia. }
pose proof (transfer_preserves_nonce query (nonceAt (S.t.world_state prepared) query)
(Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared
(conj (proj1 (proj1 Hprepared)) eq_refl)) as Htransfer.
destruct (H.transfer_ether (Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared)
as [ok middle] eqn:HtransferRun.
cbn beta iota zeta delta [fst snd] in Htransfer.
assert (Hmiddle : nonceState query middle ∧
nonceAt (S.t.world_state middle) query ≤ nonceAt (S.t.world_state state) query + 1).
{ destruct Htransfer as [Hmap Hnonce].
change (nonceAt (S.t.world_state middle) query = nonceAt (S.t.world_state prepared) query) in Hnonce.
split; [split; [exact Hmap|rewrite Hnonce; exact (proj2 (proj1 Hprepared))]|].
rewrite Hnonce. exact (proj2 Hprepared). }
cbn -[W.account Addr.of_contract_creation target stored prepared marked nonceState nonceResult].
destruct ok.
2: { unfold nonceResult, Evmc.Result.failure. cbn -[nonceAt nonceState W.account target stored prepared marked].
split; [exact (proj1 Hmiddle)|]. split; pose proof (proj2 Hmiddle); lia. }
cbn -[W.account Addr.of_contract_creation target stored prepared marked nonceState nonceResult].
match goal with |- context [H.Vm.(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
pose proof (Hvm nested code middle Hgas (proj1 Hmiddle)) as Hrecursive;
destruct (H.Vm.(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
end.
destruct Hrecursive as [Hafter [Hreturned Htotal]].
cbn beta iota zeta delta [Evmc.Message.t.gas Evmc.Message.t.with_code_address
Evmc.Message.t.with_create2_salt Evmc.Message.t.with_input_data
Evmc.Message.t.with_recipient Evmc.Message.t.with_kind] in Hreturned, Htotal.
destruct result as [status gasleft refund output address].
cbn [Evmc.Result.t.gas_left] in Hreturned, Htotal.
destruct status; cbn -[W.account Addr.of_contract_creation target stored prepared marked nonceState nonceResult].
all: try solve [unfold nonceResult; cbn -[nonceAt nonceState W.account target stored prepared marked];
split; [exact Hafter|]; split; [exact Hreturned|pose proof (proj2 Hmiddle); lia]].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hadopted end.
{ unfold nonceResult. cbn -[nonceAt nonceState W.account target stored prepared marked].
split; [exact Hafter|]. split; [exact Hreturned|pose proof (proj2 Hmiddle); lia]. }
match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct failure as [error|] eqn:Hfailure end.
- unfold nonceResult, Evmc.Result.failure. cbn -[nonceAt nonceState W.account target stored prepared marked].
split; [exact Hafter|]. split; pose proof (proj2 Hmiddle); lia.
- unfold nonceResult.
cbn -[W.account nonceState nonceAt Gas.to_uint64 Gas.op_minus Gas.of_uint64
Gas.op_star Gas.of_int Byte_string.Bytes.length target stored prepared marked].
match goal with |- nonceState _ ?last ∧ _ ⇒ set (final := last) in × end.
assert (Hfinal : nonceInvariant query (nonceAt (S.t.world_state afterVm) query) final).
{ unfold final.
(eapply nonce_write with (s := afterVm) (keep := false) (addr := target);
[split; [exact (proj1 Hafter)|reflexivity]|intro Heq;
change (nonceAt (S.t.world_state afterVm) target = nonceAt (S.t.world_state afterVm) query);
rewrite Heq; reflexivity]). }
destruct Hfinal as [Hmap Hnonce].
change (nonceAt (S.t.world_state final) query = nonceAt (S.t.world_state afterVm) query) in Hnonce.
split; [split; [exact Hmap|rewrite Hnonce; exact (proj2 Hafter)]|].
match goal with |- (0 ≤ ?remaining ≤ _) ∧ _ ⇒ set (unused := remaining) in × end.
change (0 ≤ unused ≤ Evmc.Message.t.gas msg ∧
nonceAt (S.t.world_state final) query + unused ≤
nonceAt (S.t.world_state state) query + Evmc.Message.t.gas msg + 1).
rewrite Hnonce.
unfold unused.
repeat match type of Hfailure with
| context [if ?test then _ else _] ⇒ destruct test eqn:? in Hfailure; try discriminate
end.
match goal with |- (0 ≤ Gas.to_uint64 (Gas.op_minus (Gas.of_uint64 gasleft) ?cost) ≤ _) ∧ _ ⇒
pose proof (deposit_gas_bound gasleft cost ltac:(lia) ltac:(eassumption)) as Hdeposit
end.
split; pose proof (proj2 Hmiddle); lia.
Qed.
End Creation.
Print Assumptions process_create_nonce_bound.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.hostnonce QedkConcrete.worldaccounts
SetcodeConcrete.noncebudget SetcodeConcrete.nonceeffects.
Require Import MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition nonceState query (state : S.t) : Prop :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) ∧
0 ≤ nonceAt (S.t.world_state state) query.
Definition nonceResult query gas allowance (before : S.t)
(result : Evmc.Result.t) (after : S.t) : Prop :=
nonceState query after ∧
0 ≤ Evmc.Result.t.gas_left result ≤ gas ∧
nonceAt (S.t.world_state after) query + Evmc.Result.t.gas_left result ≤
nonceAt (S.t.world_state before) query + gas + allowance.
Lemma deposit_gas_bound gas cost :
0 ≤ gas < 2^63 →
Gas.op_lt (Gas.of_uint64 gas) cost = false →
0 ≤ Gas.to_uint64 (Gas.op_minus (Gas.of_uint64 gas) cost) ≤ gas.
Proof.
intros Hgas Hafford.
assert (Hread : Gas.of_uint64 gas = Z.to_N gas).
{ unfold Gas.of_uint64, Support.UintN.of_uint64.
unfold OCamlZ.of_int64_unsigned.
destruct (Z.ltb gas 0) eqn:Hnegative;
[apply Z.ltb_lt in Hnegative; lia|].
unfold Support.UintN.of_z_exn, Support.UintN.of_z_opt, Support.UintN.in_range.
destruct (Z.leb 0 gas) eqn:Hpositive;
[reflexivity|apply Z.leb_gt in Hpositive; lia]. }
rewrite Hread in Hafford |- ×.
change ((Z.to_N gas <? cost)%N = false) in Hafford.
apply N.ltb_ge in Hafford.
unfold Gas.op_minus, Support.UintN.op_minus.
assert (Htest : (cost <=? Z.to_N gas)%N = true) by (apply N.leb_le; exact Hafford).
rewrite Htest.
unfold Gas.to_uint64, Support.UintN.to_uint64, Support.UintN.to_z.
rewrite int64_unsigned_small_identity by
(pose proof (N.le_sub_l (Z.to_N gas) cost); pose proof (Z2N.id gas ltac:(lia)); lia).
split; [lia|].
pose proof (N.le_sub_l (Z.to_N gas) cost). rewrite <- (Z2N.id gas) by lia. lia.
Qed.
Section Creation.
Context {F : H.FArgs}.
Theorem process_create_nonce_bound query msg :
(∀ nested code state,
0 ≤ Evmc.Message.t.gas nested < 2^63 → nonceState query state →
let '(result, after) := H.Vm.(Evmc.Vm.SIG.execute) nested code state in
nonceResult query (Evmc.Message.t.gas nested) 0 state result after) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
∀ state, nonceState query state →
let '(result, after) := H.process_create msg state in
nonceResult query (Evmc.Message.t.gas msg) 1 state result after.
Proof.
intros Hvm Hgas state Hinitial.
unfold H.process_create, H.contract_creation_address, H.M.
cbn -[Addr.of_contract_creation W.account Numeric.U64.op_ltgt
Byte_string.Bytes.op_ltgt H.transfer_ether nonceResult nonceState].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
{ unfold nonceResult, Evmc.Result.failure. cbn.
split; [exact Hinitial|]. split; lia. }
match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
match goal with |- context [H.transfer_ether _ _ _
(S.t.with_world_state ?nonceWorld (S.t.with_world_state ?storageWorld ?created))] ⇒
set (marked := created) in *;
set (stored := S.t.with_world_state storageWorld marked) in *;
set (prepared := S.t.with_world_state nonceWorld stored) in ×
end.
assert (Hmarked : nonceInvariant query (nonceAt (S.t.world_state state) query) marked).
{ split; [exact (proj1 Hinitial)|reflexivity]. }
assert (Hstored : nonceInvariant query (nonceAt (S.t.world_state state) query) stored).
{ unfold stored. eapply nonce_write with (s := marked) (keep := false) (addr := target);
[exact Hmarked|].
intro Heq. change (nonceAt (S.t.world_state state) target = nonceAt (S.t.world_state state) query).
rewrite Heq. reflexivity. }
assert (Hprepared : nonceState query prepared ∧
nonceAt (S.t.world_state prepared) query ≤ nonceAt (S.t.world_state state) query + 1).
{ unfold prepared. split.
- split; [apply account_write_wellformed; exact (proj1 Hstored)|].
change (0 ≤ nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state stored)
(W.account (Some false) target)
(A.t.with_nonce Numeric.U64.one
(State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None target)))) query).
rewrite nonce_after_write by exact (proj1 Hstored).
destruct (String.eqb target query); [change (0 ≤ 1); lia|].
change (0 ≤ A.t.nonce (State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None query))).
rewrite (proj2 Hstored). exact (proj2 Hinitial).
- change (nonceAt (State.op_pointcaretlparenrparenltminus (S.t.world_state stored)
(W.account (Some false) target)
(A.t.with_nonce Numeric.U64.one
(State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None target)))) query ≤ nonceAt (S.t.world_state state) query + 1).
rewrite nonce_after_write by exact (proj1 Hstored).
destruct (String.eqb target query); [change (1 ≤ nonceAt (S.t.world_state state) query + 1); pose proof (proj2 Hinitial); lia|].
change (A.t.nonce (State.op_pointcaretlparenrparen (S.t.world_state stored) (W.account None query)) ≤ nonceAt (S.t.world_state state) query + 1).
rewrite (proj2 Hstored). lia. }
pose proof (transfer_preserves_nonce query (nonceAt (S.t.world_state prepared) query)
(Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared
(conj (proj1 (proj1 Hprepared)) eq_refl)) as Htransfer.
destruct (H.transfer_ether (Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared)
as [ok middle] eqn:HtransferRun.
cbn beta iota zeta delta [fst snd] in Htransfer.
assert (Hmiddle : nonceState query middle ∧
nonceAt (S.t.world_state middle) query ≤ nonceAt (S.t.world_state state) query + 1).
{ destruct Htransfer as [Hmap Hnonce].
change (nonceAt (S.t.world_state middle) query = nonceAt (S.t.world_state prepared) query) in Hnonce.
split; [split; [exact Hmap|rewrite Hnonce; exact (proj2 (proj1 Hprepared))]|].
rewrite Hnonce. exact (proj2 Hprepared). }
cbn -[W.account Addr.of_contract_creation target stored prepared marked nonceState nonceResult].
destruct ok.
2: { unfold nonceResult, Evmc.Result.failure. cbn -[nonceAt nonceState W.account target stored prepared marked].
split; [exact (proj1 Hmiddle)|]. split; pose proof (proj2 Hmiddle); lia. }
cbn -[W.account Addr.of_contract_creation target stored prepared marked nonceState nonceResult].
match goal with |- context [H.Vm.(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
pose proof (Hvm nested code middle Hgas (proj1 Hmiddle)) as Hrecursive;
destruct (H.Vm.(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
end.
destruct Hrecursive as [Hafter [Hreturned Htotal]].
cbn beta iota zeta delta [Evmc.Message.t.gas Evmc.Message.t.with_code_address
Evmc.Message.t.with_create2_salt Evmc.Message.t.with_input_data
Evmc.Message.t.with_recipient Evmc.Message.t.with_kind] in Hreturned, Htotal.
destruct result as [status gasleft refund output address].
cbn [Evmc.Result.t.gas_left] in Hreturned, Htotal.
destruct status; cbn -[W.account Addr.of_contract_creation target stored prepared marked nonceState nonceResult].
all: try solve [unfold nonceResult; cbn -[nonceAt nonceState W.account target stored prepared marked];
split; [exact Hafter|]; split; [exact Hreturned|pose proof (proj2 Hmiddle); lia]].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hadopted end.
{ unfold nonceResult. cbn -[nonceAt nonceState W.account target stored prepared marked].
split; [exact Hafter|]. split; [exact Hreturned|pose proof (proj2 Hmiddle); lia]. }
match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct failure as [error|] eqn:Hfailure end.
- unfold nonceResult, Evmc.Result.failure. cbn -[nonceAt nonceState W.account target stored prepared marked].
split; [exact Hafter|]. split; pose proof (proj2 Hmiddle); lia.
- unfold nonceResult.
cbn -[W.account nonceState nonceAt Gas.to_uint64 Gas.op_minus Gas.of_uint64
Gas.op_star Gas.of_int Byte_string.Bytes.length target stored prepared marked].
match goal with |- nonceState _ ?last ∧ _ ⇒ set (final := last) in × end.
assert (Hfinal : nonceInvariant query (nonceAt (S.t.world_state afterVm) query) final).
{ unfold final.
(eapply nonce_write with (s := afterVm) (keep := false) (addr := target);
[split; [exact (proj1 Hafter)|reflexivity]|intro Heq;
change (nonceAt (S.t.world_state afterVm) target = nonceAt (S.t.world_state afterVm) query);
rewrite Heq; reflexivity]). }
destruct Hfinal as [Hmap Hnonce].
change (nonceAt (S.t.world_state final) query = nonceAt (S.t.world_state afterVm) query) in Hnonce.
split; [split; [exact Hmap|rewrite Hnonce; exact (proj2 Hafter)]|].
match goal with |- (0 ≤ ?remaining ≤ _) ∧ _ ⇒ set (unused := remaining) in × end.
change (0 ≤ unused ≤ Evmc.Message.t.gas msg ∧
nonceAt (S.t.world_state final) query + unused ≤
nonceAt (S.t.world_state state) query + Evmc.Message.t.gas msg + 1).
rewrite Hnonce.
unfold unused.
repeat match type of Hfailure with
| context [if ?test then _ else _] ⇒ destruct test eqn:? in Hfailure; try discriminate
end.
match goal with |- (0 ≤ Gas.to_uint64 (Gas.op_minus (Gas.of_uint64 gasleft) ?cost) ≤ _) ∧ _ ⇒
pose proof (deposit_gas_bound gasleft cost ltac:(lia) ltac:(eassumption)) as Hdeposit
end.
split; pose proof (proj2 Hmiddle); lia.
Qed.
End Creation.
Print Assumptions process_create_nonce_bound.