Library QedkConcrete.uncreatednonce
Nonces of empty-code accounts during VM execution
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostnonce QedkConcrete.nondebitframe
QedkConcrete.hostcode QedkConcrete.hostframe QedkConcrete.worldaccounts
QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Nonce.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun s addr ⇒ State.op_pointcaretlparenrparen
(S.t.world_state s) (@W.account None addr)).
Definition uncreatedNonce query nonce (s : S.t) :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
(List.In query (S.t.accounts_created_in_current_transaction s) ∨
(A.t.code (readAccount s query) = Byte_string.Bytes.empty ∧
A.t.nonce (readAccount s query) = nonce)).
Lemma uncreated_nonce_not_created query nonce s :
uncreatedNonce query nonce s →
¬ List.In query (S.t.accounts_created_in_current_transaction s) →
nonceInvariant query nonce s ∧ A.t.code (readAccount s query) = Byte_string.Bytes.empty.
Proof. unfold uncreatedNonce, nonceInvariant. intros [Hwf [Hin|Haccount]] Hnot;
[contradiction|tauto]. Qed.
Lemma uncreated_nonce_callback {T : Set} query nonce (action : S.t → T × S.t) :
@preservesCreation T action →
preservesCallback (nonceInvariant query nonce) action →
preservesCallback (uncreatedNonce query nonce) action.
Proof.
intros Hcreation Hnonce s [Hwf Haccount].
destruct (Hcreation s Hwf) as [Hafter Hframe]. split; [exact Hafter|].
destruct Haccount as [Hcreated|[Hcode Hnumber]].
- left. exact (creations_retained _ _ Hframe query Hcreated).
- destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction (snd (action s)))) as [Hin|Hnot].
+ left. exact Hin.
+ right. split.
× rewrite (noncreated_code _ _ Hframe query Hnot). exact Hcode.
× exact (proj2 (Hnonce s (conj Hwf Hnumber))).
Qed.
Lemma mark_creation_uncreated_nonce query nonce s addr :
uncreatedNonce query nonce s →
uncreatedNonce query nonce (S.t.with_accounts_created_in_current_transaction
(Addr._Set.(OCamlSet.S.add) addr (S.t.accounts_created_in_current_transaction s)) s).
Proof.
intros [Hwf [Hin|Haccount]]; split; [exact Hwf| |exact Hwf|].
- left. apply address_set_add_members. right. exact Hin.
- right. exact Haccount.
Qed.
Lemma account_write_uncreated_nonce query nonce s keep addr value :
uncreatedNonce query nonce s →
(List.In addr (S.t.accounts_created_in_current_transaction s) ∨
(A.t.nonce value = A.t.nonce (readAccount s addr) ∧
A.t.code value = A.t.code (readAccount s addr))) →
uncreatedNonce query nonce (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(@W.account (Some keep) addr) value) s).
Proof.
intros [Hwf Haccount] Hvalue. split; [apply account_write_wellformed; exact Hwf|].
destruct Haccount as [Hin|[Hcode Hnonce]]; [left; exact Hin|].
destruct (List.in_dec String.string_dec query (S.t.accounts_created_in_current_transaction s))
as [Hin|Hnot]; [left; exact Hin|].
right. split.
- pose proof (@world_write_code_frame UF s keep addr value Hwf) as Hframe.
assert (Hallowed : A.t.code value = A.t.code (readAccount s addr) ∨
List.In addr (S.t.accounts_created_in_current_transaction s)) by tauto.
pose proof (noncreated_code _ _ (proj2 (Hframe Hallowed)) query Hnot) as Hsame.
etransitivity; [exact Hsame|exact Hcode].
- assert (Hsame : addr = query → A.t.nonce value = nonce).
{ intro Heq. subst addr. destruct Hvalue as [Hin|[Heq _]]; [contradiction|].
rewrite Heq. exact Hnonce. }
exact (proj2 (nonce_write query nonce s keep addr value (conj Hwf Hnonce) Hsame)).
Qed.
Context {F : H.FArgs}.
Lemma transfer_preserves_uncreated_nonce query nonce sender recipient amount :
preservesCallback (uncreatedNonce query nonce) (@H.transfer_ether F sender recipient amount).
Proof.
apply uncreated_nonce_callback.
- apply transfer_preserves_creation.
- exact (@transfer_preserves_nonce F query nonce sender recipient amount).
Qed.
Lemma increment_preserves_other_uncreated_nonce query nonce addr :
addr ≠ query →
preservesCallback (uncreatedNonce query nonce) (@H.try_increment_nonce F addr).
Proof.
intro Hdifferent. apply uncreated_nonce_callback.
- intros s Hwf. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
+ split; [exact Hwf|apply creation_frame_refl].
+ apply world_write_code_frame; [exact Hwf|left; reflexivity].
- exact (@increment_preserves_other_nonce F query nonce addr Hdifferent).
Qed.
End Nonce.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostnonce QedkConcrete.nondebitframe
QedkConcrete.hostcode QedkConcrete.hostframe QedkConcrete.worldaccounts
QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Nonce.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun s addr ⇒ State.op_pointcaretlparenrparen
(S.t.world_state s) (@W.account None addr)).
Definition uncreatedNonce query nonce (s : S.t) :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
(List.In query (S.t.accounts_created_in_current_transaction s) ∨
(A.t.code (readAccount s query) = Byte_string.Bytes.empty ∧
A.t.nonce (readAccount s query) = nonce)).
Lemma uncreated_nonce_not_created query nonce s :
uncreatedNonce query nonce s →
¬ List.In query (S.t.accounts_created_in_current_transaction s) →
nonceInvariant query nonce s ∧ A.t.code (readAccount s query) = Byte_string.Bytes.empty.
Proof. unfold uncreatedNonce, nonceInvariant. intros [Hwf [Hin|Haccount]] Hnot;
[contradiction|tauto]. Qed.
Lemma uncreated_nonce_callback {T : Set} query nonce (action : S.t → T × S.t) :
@preservesCreation T action →
preservesCallback (nonceInvariant query nonce) action →
preservesCallback (uncreatedNonce query nonce) action.
Proof.
intros Hcreation Hnonce s [Hwf Haccount].
destruct (Hcreation s Hwf) as [Hafter Hframe]. split; [exact Hafter|].
destruct Haccount as [Hcreated|[Hcode Hnumber]].
- left. exact (creations_retained _ _ Hframe query Hcreated).
- destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction (snd (action s)))) as [Hin|Hnot].
+ left. exact Hin.
+ right. split.
× rewrite (noncreated_code _ _ Hframe query Hnot). exact Hcode.
× exact (proj2 (Hnonce s (conj Hwf Hnumber))).
Qed.
Lemma mark_creation_uncreated_nonce query nonce s addr :
uncreatedNonce query nonce s →
uncreatedNonce query nonce (S.t.with_accounts_created_in_current_transaction
(Addr._Set.(OCamlSet.S.add) addr (S.t.accounts_created_in_current_transaction s)) s).
Proof.
intros [Hwf [Hin|Haccount]]; split; [exact Hwf| |exact Hwf|].
- left. apply address_set_add_members. right. exact Hin.
- right. exact Haccount.
Qed.
Lemma account_write_uncreated_nonce query nonce s keep addr value :
uncreatedNonce query nonce s →
(List.In addr (S.t.accounts_created_in_current_transaction s) ∨
(A.t.nonce value = A.t.nonce (readAccount s addr) ∧
A.t.code value = A.t.code (readAccount s addr))) →
uncreatedNonce query nonce (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(@W.account (Some keep) addr) value) s).
Proof.
intros [Hwf Haccount] Hvalue. split; [apply account_write_wellformed; exact Hwf|].
destruct Haccount as [Hin|[Hcode Hnonce]]; [left; exact Hin|].
destruct (List.in_dec String.string_dec query (S.t.accounts_created_in_current_transaction s))
as [Hin|Hnot]; [left; exact Hin|].
right. split.
- pose proof (@world_write_code_frame UF s keep addr value Hwf) as Hframe.
assert (Hallowed : A.t.code value = A.t.code (readAccount s addr) ∨
List.In addr (S.t.accounts_created_in_current_transaction s)) by tauto.
pose proof (noncreated_code _ _ (proj2 (Hframe Hallowed)) query Hnot) as Hsame.
etransitivity; [exact Hsame|exact Hcode].
- assert (Hsame : addr = query → A.t.nonce value = nonce).
{ intro Heq. subst addr. destruct Hvalue as [Hin|[Heq _]]; [contradiction|].
rewrite Heq. exact Hnonce. }
exact (proj2 (nonce_write query nonce s keep addr value (conj Hwf Hnonce) Hsame)).
Qed.
Context {F : H.FArgs}.
Lemma transfer_preserves_uncreated_nonce query nonce sender recipient amount :
preservesCallback (uncreatedNonce query nonce) (@H.transfer_ether F sender recipient amount).
Proof.
apply uncreated_nonce_callback.
- apply transfer_preserves_creation.
- exact (@transfer_preserves_nonce F query nonce sender recipient amount).
Qed.
Lemma increment_preserves_other_uncreated_nonce query nonce addr :
addr ≠ query →
preservesCallback (uncreatedNonce query nonce) (@H.try_increment_nonce F addr).
Proof.
intro Hdifferent. apply uncreated_nonce_callback.
- intros s Hwf. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
+ split; [exact Hwf|apply creation_frame_refl].
+ apply world_write_code_frame; [exact Hwf|left; reflexivity].
- exact (@increment_preserves_other_nonce F query nonce addr Hdifferent).
Qed.
End Nonce.