Library SetcodeConcrete.regularaccount
Regular accounts remain regular and cannot be recreated
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostframe QedkConcrete.worldaccounts QedkConcrete.accountcode
QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.systempersistent SetcodeConcrete.codeevolution
SetcodeConcrete.transactioncode.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Definition regularAccount query s :=
∃ code, regularCode code ∧ persistentCode query code s.
Lemma regular_nonempty code : regularCode code → code ≠ Byte_string.Bytes.empty.
Proof. intros H →. discriminate H. Qed.
Lemma regular_lift {R : Set} query (action : S.t → R × S.t) :
(∀ code, preservesCallback (persistentCode query code) action) →
preservesCallback (regularAccount query) action.
Proof.
intros H s [code [Hcode HP]]. ∃ code. split; [exact Hcode|apply H; exact HP].
Qed.
Lemma regular_write query s keep addr value :
regularAccount query s →
(addr = query → regularCode (A.t.code value)) →
regularAccount query (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [code [Hcode HP]] Hvalue.
destruct (String.string_dec addr query) as [Heq|Hneq].
- subst addr. ∃ (A.t.code value). split; [apply Hvalue; reflexivity|].
constructor; try exact (persistent_code_uncreated _ _ _ HP);
try exact (persistent_code_undestroyed _ _ _ HP).
+ apply account_write_wellformed. exact (persistent_code_map _ _ _ HP).
+ cbn [S.t.with_world_state S.t.world_state].
rewrite code_after_account_write by exact (persistent_code_map _ _ _ HP).
rewrite String.eqb_refl. reflexivity.
- ∃ code. split; [exact Hcode|].
apply persistent_code_write; [exact HP|contradiction].
Qed.
Lemma regular_mark query s addr :
regularAccount query s → addr ≠ query →
regularAccount query (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 [code [Hcode HP]] Hneq. ∃ code. split; [exact Hcode|].
apply persistent_code_mark_creation; assumption.
Qed.
Lemma regular_unchanged query before after :
regularAccount query before →
S.t.world_state after = S.t.world_state before →
S.t.accounts_created_in_current_transaction after = S.t.accounts_created_in_current_transaction before →
S.t.self_destruct after = S.t.self_destruct before → regularAccount query after.
Proof.
intros [code [Hcode HP]] Hw Hc Hd. ∃ code. split; [exact Hcode|].
eapply persistent_unchanged; eassumption.
Qed.
Lemma regular_create_guard query s target other :
regularAccount query s →
orb other (Byte_string.Bytes.op_ltgt
(A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None target)))
Byte_string.Bytes.empty) = false → target ≠ query.
Proof.
intros [code [Hcode HP]] Hguard.
exact (create_guard_excludes_existing_code query code s target other HP
(regular_nonempty code Hcode) Hguard).
Qed.
Section Host.
Context {F : H.FArgs}.
Lemma transfer_regular query sender recipient amount :
preservesCallback (regularAccount query) (H.transfer_ether sender recipient amount).
Proof. apply regular_lift. intro code. apply transfer_preserves_persistent_code. Qed.
Lemma increment_regular query addr :
preservesCallback (regularAccount query) (H.try_increment_nonce addr).
Proof. apply regular_lift. intro code. apply increment_nonce_preserves_persistent_code. Qed.
Lemma setcode_regular query current source static gas :
preservesCallback (regularAccount query) (H.setcodefrom current source static gas).
Proof.
intros s Hbefore. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] access] write] reserve] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact Hbefore.
all: try (eapply regular_unchanged; [exact Hbefore|reflexivity|reflexivity|reflexivity]).
all: apply regular_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current).
all: try (eapply regular_unchanged; [exact Hbefore|reflexivity|reflexivity|reflexivity]).
all: intro Heq; apply regular_source_is_regular.
all: match goal with
Hvalid : orb (negb (Setcode_rules.regular_source ?code)) _ = false |- _ ⇒
apply Bool.orb_false_iff in Hvalid as [Hvalid _];
apply Bool.negb_false_iff in Hvalid; exact Hvalid end.
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_regular vm query :
PrimitiveHostFrame (HostF vm) (regularAccount query).
Proof.
constructor; intros; apply regular_lift; intro code.
all: destruct (@concrete_primitives_persistent F vm query code); auto.
Qed.
End Assembly.
Lemma precompile_regular {F : Precompiles.Make.FArgs}
cold warm access write reserve query msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (regularAccount query) action.
Proof.
intros Hrevision Hselected. apply regular_lift. intro code.
exact (precompile_persistent cold warm access write reserve query code msg addr action
Hrevision Hselected).
Qed.
Section Creation.
Context {F : H.FArgs}
{UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
{UF : Basics.Unreachable Numeric.U256.t} {UG : Basics.Unreachable Gas.t}
{UA : Basics.Unreachable Addr.t} {UB : Basics.Unreachable Byte_string.Bytes.t}
{UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{U64i : Basics.Unreachable int64}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation protected := regularAccount.
Lemma process_create_preserves_regular query msg :
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preservesCallback (protected query) ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
preservesCallback (protected query)
(@H.process_create F msg).
Proof.
intros Hvm Hgas s 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].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
{ exact Hinitial. }
match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
assert (Htarget : target ≠ query).
{ exact (regular_create_guard query s target _ Hinitial Hcollision). }
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 : protected query marked).
{ exact (regular_mark query s target Hinitial Htarget). }
assert (Hstored : protected query stored).
{ unfold stored. eapply regular_write with (s := marked) (keep := false) (addr := target);
[exact Hmarked|intro Heq; contradiction]. }
assert (Hprepared : protected query prepared).
{ unfold prepared. eapply regular_write with (s := stored) (keep := false) (addr := target);
[exact Hstored|intro Heq; contradiction]. }
pose proof (@transfer_regular F query
(Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared Hprepared) as Htransfer.
destruct (@H.transfer_ether F (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.
cbn -[W.account Addr.of_contract_creation target stored prepared marked].
destruct ok.
2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked].
exact Htransfer. }
cbn -[W.account Addr.of_contract_creation target stored prepared marked].
match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
pose proof (Hvm nested code Hgas middle Htransfer) as Hrecursive;
destruct ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
end.
cbn beta iota zeta delta [fst snd] in Hrecursive.
destruct result as [status gasleft refund output address]. destruct status;
cbn -[W.account Addr.of_contract_creation target stored prepared marked];
try exact Hrecursive.
try match goal with |- context [if andb ?enabled ?adopted then _ else _] ⇒
destruct (andb enabled adopted); [exact Hrecursive|] end.
match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct failure as [error|] end.
- exact Hrecursive.
- eapply regular_write with (s := afterVm) (keep := false) (addr := target);
[exact Hrecursive|intro Heq; contradiction].
Qed.
End Creation.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostframe QedkConcrete.worldaccounts QedkConcrete.accountcode
QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.systempersistent SetcodeConcrete.codeevolution
SetcodeConcrete.transactioncode.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Definition regularAccount query s :=
∃ code, regularCode code ∧ persistentCode query code s.
Lemma regular_nonempty code : regularCode code → code ≠ Byte_string.Bytes.empty.
Proof. intros H →. discriminate H. Qed.
Lemma regular_lift {R : Set} query (action : S.t → R × S.t) :
(∀ code, preservesCallback (persistentCode query code) action) →
preservesCallback (regularAccount query) action.
Proof.
intros H s [code [Hcode HP]]. ∃ code. split; [exact Hcode|apply H; exact HP].
Qed.
Lemma regular_write query s keep addr value :
regularAccount query s →
(addr = query → regularCode (A.t.code value)) →
regularAccount query (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [code [Hcode HP]] Hvalue.
destruct (String.string_dec addr query) as [Heq|Hneq].
- subst addr. ∃ (A.t.code value). split; [apply Hvalue; reflexivity|].
constructor; try exact (persistent_code_uncreated _ _ _ HP);
try exact (persistent_code_undestroyed _ _ _ HP).
+ apply account_write_wellformed. exact (persistent_code_map _ _ _ HP).
+ cbn [S.t.with_world_state S.t.world_state].
rewrite code_after_account_write by exact (persistent_code_map _ _ _ HP).
rewrite String.eqb_refl. reflexivity.
- ∃ code. split; [exact Hcode|].
apply persistent_code_write; [exact HP|contradiction].
Qed.
Lemma regular_mark query s addr :
regularAccount query s → addr ≠ query →
regularAccount query (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 [code [Hcode HP]] Hneq. ∃ code. split; [exact Hcode|].
apply persistent_code_mark_creation; assumption.
Qed.
Lemma regular_unchanged query before after :
regularAccount query before →
S.t.world_state after = S.t.world_state before →
S.t.accounts_created_in_current_transaction after = S.t.accounts_created_in_current_transaction before →
S.t.self_destruct after = S.t.self_destruct before → regularAccount query after.
Proof.
intros [code [Hcode HP]] Hw Hc Hd. ∃ code. split; [exact Hcode|].
eapply persistent_unchanged; eassumption.
Qed.
Lemma regular_create_guard query s target other :
regularAccount query s →
orb other (Byte_string.Bytes.op_ltgt
(A.t.code (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None target)))
Byte_string.Bytes.empty) = false → target ≠ query.
Proof.
intros [code [Hcode HP]] Hguard.
exact (create_guard_excludes_existing_code query code s target other HP
(regular_nonempty code Hcode) Hguard).
Qed.
Section Host.
Context {F : H.FArgs}.
Lemma transfer_regular query sender recipient amount :
preservesCallback (regularAccount query) (H.transfer_ether sender recipient amount).
Proof. apply regular_lift. intro code. apply transfer_preserves_persistent_code. Qed.
Lemma increment_regular query addr :
preservesCallback (regularAccount query) (H.try_increment_nonce addr).
Proof. apply regular_lift. intro code. apply increment_nonce_preserves_persistent_code. Qed.
Lemma setcode_regular query current source static gas :
preservesCallback (regularAccount query) (H.setcodefrom current source static gas).
Proof.
intros s Hbefore. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] access] write] reserve] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact Hbefore.
all: try (eapply regular_unchanged; [exact Hbefore|reflexivity|reflexivity|reflexivity]).
all: apply regular_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current).
all: try (eapply regular_unchanged; [exact Hbefore|reflexivity|reflexivity|reflexivity]).
all: intro Heq; apply regular_source_is_regular.
all: match goal with
Hvalid : orb (negb (Setcode_rules.regular_source ?code)) _ = false |- _ ⇒
apply Bool.orb_false_iff in Hvalid as [Hvalid _];
apply Bool.negb_false_iff in Hvalid; exact Hvalid end.
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_regular vm query :
PrimitiveHostFrame (HostF vm) (regularAccount query).
Proof.
constructor; intros; apply regular_lift; intro code.
all: destruct (@concrete_primitives_persistent F vm query code); auto.
Qed.
End Assembly.
Lemma precompile_regular {F : Precompiles.Make.FArgs}
cold warm access write reserve query msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (regularAccount query) action.
Proof.
intros Hrevision Hselected. apply regular_lift. intro code.
exact (precompile_persistent cold warm access write reserve query code msg addr action
Hrevision Hselected).
Qed.
Section Creation.
Context {F : H.FArgs}
{UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
{UF : Basics.Unreachable Numeric.U256.t} {UG : Basics.Unreachable Gas.t}
{UA : Basics.Unreachable Addr.t} {UB : Basics.Unreachable Byte_string.Bytes.t}
{UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{U64i : Basics.Unreachable int64}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation protected := regularAccount.
Lemma process_create_preserves_regular query msg :
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preservesCallback (protected query) ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
preservesCallback (protected query)
(@H.process_create F msg).
Proof.
intros Hvm Hgas s 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].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
{ exact Hinitial. }
match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
assert (Htarget : target ≠ query).
{ exact (regular_create_guard query s target _ Hinitial Hcollision). }
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 : protected query marked).
{ exact (regular_mark query s target Hinitial Htarget). }
assert (Hstored : protected query stored).
{ unfold stored. eapply regular_write with (s := marked) (keep := false) (addr := target);
[exact Hmarked|intro Heq; contradiction]. }
assert (Hprepared : protected query prepared).
{ unfold prepared. eapply regular_write with (s := stored) (keep := false) (addr := target);
[exact Hstored|intro Heq; contradiction]. }
pose proof (@transfer_regular F query
(Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared Hprepared) as Htransfer.
destruct (@H.transfer_ether F (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.
cbn -[W.account Addr.of_contract_creation target stored prepared marked].
destruct ok.
2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked].
exact Htransfer. }
cbn -[W.account Addr.of_contract_creation target stored prepared marked].
match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
pose proof (Hvm nested code Hgas middle Htransfer) as Hrecursive;
destruct ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
end.
cbn beta iota zeta delta [fst snd] in Hrecursive.
destruct result as [status gasleft refund output address]. destruct status;
cbn -[W.account Addr.of_contract_creation target stored prepared marked];
try exact Hrecursive.
try match goal with |- context [if andb ?enabled ?adopted then _ else _] ⇒
destruct (andb enabled adopted); [exact Hrecursive|] end.
match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct failure as [error|] end.
- exact Hrecursive.
- eapply regular_write with (s := afterVm) (keep := false) (addr := target);
[exact Hrecursive|intro Heq; contradiction].
Qed.
End Creation.