Library SetcodeConcrete.emptyaccounthost
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.hostnonce QedkConcrete.vmcallframe QedkConcrete.recursiveassembly
QedkConcrete.hostframe QedkConcrete.hostcode QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.emptyaccount SetcodeConcrete.accountframe
SetcodeConcrete.accountcreate SetcodeConcrete.creationtracking.
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.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation protected := emptyAccount.
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation nativeDispatch := (@Precompiles.Make.try_precompile
(@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).
Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)).
Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)).
Local Abbreviation hostCall := (@H.call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)).
Lemma process_call_preserves_empty_account from_tx query nonce msg :
(∀ addr action, nativeDispatch msg addr = Some action →
preservesCallback (protected query nonce) action) →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code s, protected query nonce s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
protected query nonce (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
preservesCallback (protected query nonce) (processCall from_tx msg).
Proof.
intros Hnative HcodeAddress Hvm s Hinitial.
assert (Htransfer : protected query nonce (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ destruct (H.should_transfer msg); [apply transfer_account_frame|]; exact Hinitial. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : protected query nonce (snd run)) by exact Htransfer;
destruct run as [ok middle] eqn:Hrun end.
cbn beta iota zeta delta [snd] in Hmiddle.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply Hnative; eassumption).
all: apply Hvm; [exact Hmiddle|].
all: intro Hrecipient;
destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction middle)) as [Hin|Hnot];
[right; exact Hin|left].
all: pose proof (empty_account_code query nonce middle (conj Hmiddle Hnot)) as Hempty;
first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.hostnonce QedkConcrete.vmcallframe QedkConcrete.recursiveassembly
QedkConcrete.hostframe QedkConcrete.hostcode QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.emptyaccount SetcodeConcrete.accountframe
SetcodeConcrete.accountcreate SetcodeConcrete.creationtracking.
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.
Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation protected := emptyAccount.
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation nativeDispatch := (@Precompiles.Make.try_precompile
(@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).
Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)).
Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)).
Local Abbreviation hostCall := (@H.call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)).
Lemma process_call_preserves_empty_account from_tx query nonce msg :
(∀ addr action, nativeDispatch msg addr = Some action →
preservesCallback (protected query nonce) action) →
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
(∀ code s, protected query nonce s →
(Evmc.Message.t.recipient msg = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
protected query nonce (snd (vm.(Evmc.Vm.SIG.execute) msg code s))) →
preservesCallback (protected query nonce) (processCall from_tx msg).
Proof.
intros Hnative HcodeAddress Hvm s Hinitial.
assert (Htransfer : protected query nonce (snd
((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)) s))).
{ destruct (H.should_transfer msg); [apply transfer_account_frame|]; exact Hinitial. }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hmiddle : protected query nonce (snd run)) by exact Htransfer;
destruct run as [ok middle] eqn:Hrun end.
cbn beta iota zeta delta [snd] in Hmiddle.
destruct ok; [|exact Hmiddle].
destruct from_tx as [tx|].
all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address].
all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
all: cbn -[W.account Precompiles.Make.try_precompile].
all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
all: try (eapply Hnative; eassumption).
all: apply Hvm; [exact Hmiddle|].
all: intro Hrecipient;
destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction middle)) as [Hin|Hnot];
[right; exact Hin|left].
all: pose proof (empty_account_code query nonce middle (conj Hmiddle Hnot)) as Hempty;
first [rewrite (HcodeAddress Hrecipient); exact Hempty|idtac].
exfalso. match type of Hdelegation with
| @Delegation.get_delegated_address ?bytes = Some _ ⇒
assert (Hbytes : bytes = Byte_string.Bytes.empty) by
(rewrite (HcodeAddress Hrecipient); exact Hempty);
pose proof (eq_trans
(eq_sym (f_equal (@Delegation.get_delegated_address) Hbytes))
Hdelegation) as Hfalse;
discriminate Hfalse
end.
Qed.
On failure the Host restores the call-entry state. On success it keeps
the state protected by the recursive VM/native-call induction steps.
The top-level reserve check is read-only and can only cause rollback.
Lemma call_impl_preserves_empty_account from_tx query nonce msg :
(∀ nested addr action, nativeDispatch nested addr = Some action →
preservesCallback (protected query nonce) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ initial, preservesCallback (creationsRetained initial) (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected query nonce s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
protected query nonce (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
preservesCallback (protected query nonce) (callImpl from_tx msg).
Proof.
intros Hnative Hcreation Hvm Hgas Htarget s Hinitial.
pose proof (process_create_account_frame (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query (fun code n ⇒ code = Byte_string.Bytes.empty ∧ n = nonce) msg) as Hcreate.
specialize (Hcreate Hcreation (fun nested code Hb st HP Htarget ⇒
Hvm nested code Hb st HP (fun Heq ⇒ or_intror (Htarget Heq))) Hgas).
assert (Hcall :
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
preservesCallback (protected query nonce) (processCall from_tx msg)).
{ intro Hcode. exact (process_call_preserves_empty_account from_tx query nonce msg
(Hnative msg) Hcode (fun code initial ⇒ Hvm msg code Hgas initial)). }
unfold H.call_impl.
destruct (Evmc.Message.t.kind msg) eqn:Hkind;
cbn -[H.process_call H.process_create Addr.of_contract_creation Reserve_balance.dipped_into_reserve].
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : protected query nonce (snd run)) by
(lazymatch run with ?f ?initial ⇒
assert (Hentry : protected query nonce initial) by
(destruct Hinitial as [Hwf Haccount]; split; assumption);
first [exact (Hcreate initial Hentry)|
destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate;
exact (Hcall Htarget initial Hentry)] end);
destruct run as [result intermediate] eqn:Hrun
end.
all: destruct result as [status gasleft refund output address].
all: destruct from_tx as [tx|]; destruct status;
cbn -[Reserve_balance.dipped_into_reserve];
unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
try exact Hcore; try exact Hinitial.
all: try (destruct Hinitial as [Hwf Haccount]; split; assumption).
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn; first [exact Hinitial|exact Hcore|
destruct Hinitial as [Hwf Haccount]; split; assumption].
Qed.
Lemma call_preserves_empty_account self query nonce msg :
(∀ nested addr action, nativeDispatch nested addr = Some action →
preservesCallback (protected query nonce) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ initial, preservesCallback (creationsRetained initial) (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected query nonce s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
protected query nonce (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
self ≠ query → childCallFrom self msg →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
preservesCallback (protected query nonce) (hostCall msg).
Proof.
intros Hnative Hcreation Hvm Hgas Hdifferent Horigin Htarget s Hinitial.
pose proof (call_impl_preserves_empty_account None query nonce msg
Hnative Hcreation Hvm Hgas Htarget) as Himpl.
unfold H.call.
destruct (Evmc.Message.t.kind msg) eqn:Hkind.
all: cbn -[H.call_impl H.try_increment_nonce].
all: try (apply Himpl; exact Hinitial).
all: assert (Hsender : Evmc.Message.t.sender msg ≠ query) by
(unfold childCallFrom in Horigin; rewrite Hkind in Horigin; rewrite Horigin; exact Hdifferent).
all: pose proof (increment_other_account_frame
(F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) query (fun code n ⇒ code = Byte_string.Bytes.empty ∧ n = nonce)
(Evmc.Message.t.sender msg) Hsender s Hinitial) as Hincrement.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
destruct run as [ok middle] end.
all: cbn [snd] in Hincrement; destruct ok;
cbn -[H.call_impl]; [apply Himpl; exact Hincrement|exact Hincrement].
Qed.
End Host.
(∀ nested addr action, nativeDispatch nested addr = Some action →
preservesCallback (protected query nonce) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ initial, preservesCallback (creationsRetained initial) (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected query nonce s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
protected query nonce (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
preservesCallback (protected query nonce) (callImpl from_tx msg).
Proof.
intros Hnative Hcreation Hvm Hgas Htarget s Hinitial.
pose proof (process_create_account_frame (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
query (fun code n ⇒ code = Byte_string.Bytes.empty ∧ n = nonce) msg) as Hcreate.
specialize (Hcreate Hcreation (fun nested code Hb st HP Htarget ⇒
Hvm nested code Hb st HP (fun Heq ⇒ or_intror (Htarget Heq))) Hgas).
assert (Hcall :
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query) →
preservesCallback (protected query nonce) (processCall from_tx msg)).
{ intro Hcode. exact (process_call_preserves_empty_account from_tx query nonce msg
(Hnative msg) Hcode (fun code initial ⇒ Hvm msg code Hgas initial)). }
unfold H.call_impl.
destruct (Evmc.Message.t.kind msg) eqn:Hkind;
cbn -[H.process_call H.process_create Addr.of_contract_creation Reserve_balance.dipped_into_reserve].
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
assert (Hcore : protected query nonce (snd run)) by
(lazymatch run with ?f ?initial ⇒
assert (Hentry : protected query nonce initial) by
(destruct Hinitial as [Hwf Haccount]; split; assumption);
first [exact (Hcreate initial Hentry)|
destruct Htarget as [Hbad|[Hbad|Htarget]]; try discriminate;
exact (Hcall Htarget initial Hentry)] end);
destruct run as [result intermediate] eqn:Hrun
end.
all: destruct result as [status gasleft refund output address].
all: destruct from_tx as [tx|]; destruct status;
cbn -[Reserve_balance.dipped_into_reserve];
unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
try exact Hcore; try exact Hinitial.
all: try (destruct Hinitial as [Hwf Haccount]; split; assumption).
all: match goal with
| |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
assert (Hcheck : snd check = intermediate) by reflexivity;
destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
end.
all: destruct dipped; cbn; first [exact Hinitial|exact Hcore|
destruct Hinitial as [Hwf Haccount]; split; assumption].
Qed.
Lemma call_preserves_empty_account self query nonce msg :
(∀ nested addr action, nativeDispatch nested addr = Some action →
preservesCallback (protected query nonce) action) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ initial, preservesCallback (creationsRetained initial) (vm.(Evmc.Vm.SIG.execute) nested code)) →
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
∀ s, protected query nonce s →
(Evmc.Message.t.recipient nested = query →
code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
protected query nonce (snd (vm.(Evmc.Vm.SIG.execute) nested code s))) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
self ≠ query → childCallFrom self msg →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
(Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
preservesCallback (protected query nonce) (hostCall msg).
Proof.
intros Hnative Hcreation Hvm Hgas Hdifferent Horigin Htarget s Hinitial.
pose proof (call_impl_preserves_empty_account None query nonce msg
Hnative Hcreation Hvm Hgas Htarget) as Himpl.
unfold H.call.
destruct (Evmc.Message.t.kind msg) eqn:Hkind.
all: cbn -[H.call_impl H.try_increment_nonce].
all: try (apply Himpl; exact Hinitial).
all: assert (Hsender : Evmc.Message.t.sender msg ≠ query) by
(unfold childCallFrom in Horigin; rewrite Hkind in Horigin; rewrite Horigin; exact Hdifferent).
all: pose proof (increment_other_account_frame
(F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) query (fun code n ⇒ code = Byte_string.Bytes.empty ∧ n = nonce)
(Evmc.Message.t.sender msg) Hsender s Hinitial) as Hincrement.
all: match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
destruct run as [ok middle] end.
all: cbn [snd] in Hincrement; destruct ok;
cbn -[H.call_impl]; [apply Himpl; exact Hincrement|exact Hincrement].
Qed.
End Host.