Library QedkConcrete.hostaccountformat
Account representation through concrete Host writes
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hoststoragewords
QedkConcrete.hostbalance QedkConcrete.hostframe
QedkConcrete.worldaccounts QedkConcrete.memorywords QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hoststoragewords
QedkConcrete.hostbalance QedkConcrete.hostframe
QedkConcrete.worldaccounts QedkConcrete.memorywords QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Native OCaml int is signed 63-bit: byte lengths must be below 2^62.
This corrects the former Int64-sized bound; initial inputs need review.
Definition accountCodeBounds (world : W.t) : Prop :=
Forall (fun entry ⇒ OCamlString.length (A.t.code (snd entry)) < 2^62) (W.t.accounts world).
Definition accountFormat (account : A.t) : Prop :=
storageWords (A.t.storage account) ∧ OCamlString.length (A.t.code account) < 2^62.
Definition worldFormat (world : W.t) : Prop :=
Forall (fun entry ⇒ accountFormat (snd entry)) (W.t.accounts world).
Definition hostFormat (s : S.t) : Prop :=
worldFormat (S.t.world_state s) ∧ transientStorageWords s.
Lemma world_format_components world :
worldFormat world ↔ accountStorageWords world ∧ accountCodeBounds world.
Proof.
unfold worldFormat, accountFormat, accountStorageWords, accountCodeBounds.
split.
- intro H. induction H as [|entry entries [Hstorage Hcode] Hrest [IHstorage IHcode]].
+ split; constructor.
+ split; constructor; assumption.
- intros [Hstorage Hcode]. apply Forall_and; assumption.
Qed.
Section AccountProperties.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Lemma read_account_property (P : A.t → Prop) world addr :
P (@A.empty) →
Forall (fun entry ⇒ P (snd entry)) (W.t.accounts world) →
P (State.op_pointcaretlparenrparen world (@W.account None addr)).
Proof.
intros Hempty Hworld.
change (P (match Addr.Map.find_opt addr (W.t.accounts world) with
| Some account ⇒ account | None ⇒ @A.empty end)).
destruct (Addr.Map.find_opt addr (W.t.accounts world)) eqn:Hfind.
- eapply (map_find_value_property _ P); eassumption.
- exact Hempty.
Qed.
Lemma write_account_property (P : A.t → Prop) keep world addr value :
Forall (fun entry ⇒ P (snd entry)) (W.t.accounts world) → P value →
Forall (fun entry ⇒ P (snd entry)) (W.t.accounts
(State.op_pointcaretlparenrparenltminus world (@W.account (Some keep) addr) value)).
Proof.
intros Hworld Hvalue. destruct keep.
- change (Forall (fun entry ⇒ P (snd entry))
(OCamlMap.Make.add stringmaps.stringOrder addr value (W.t.accounts world))).
apply (map_add_value_property _ P); assumption.
- change (Forall (fun entry ⇒ P (snd entry))
(OCamlMap.Make.update stringmaps.stringOrder addr
(fun _ ⇒ if @A.is_empty value then None else Some value) (W.t.accounts world))).
unfold OCamlMap.Make.update. destruct (@A.is_empty value).
+ apply (map_remove_value_property _ P). exact Hworld.
+ apply (map_add_value_property _ P); assumption.
Qed.
Lemma empty_account_format : accountFormat (@A.empty).
Proof. split; [constructor|change (0 < 2^62); lia]. Qed.
Lemma read_account_format world addr :
worldFormat world → accountFormat
(State.op_pointcaretlparenrparen world (@W.account None addr)).
Proof. apply read_account_property, empty_account_format. Qed.
Lemma write_account_format keep world addr account :
worldFormat world → accountFormat account →
worldFormat (State.op_pointcaretlparenrparenltminus world
(@W.account (Some keep) addr) account).
Proof. apply write_account_property. Qed.
Lemma balance_update_format balance account :
accountFormat account → accountFormat (A.t.with_balance balance account).
Proof. exact (fun H ⇒ H). Qed.
Lemma nonce_update_format nonce account :
accountFormat account → accountFormat (A.t.with_nonce nonce account).
Proof. exact (fun H ⇒ H). Qed.
Lemma code_update_format code account :
accountFormat account → OCamlString.length code < 2^62 →
accountFormat (A.t.with_code code account).
Proof. intros [Hstorage _] Hcode. split; assumption. Qed.
Lemma storage_update_format storage account :
accountFormat account → storageWords storage →
accountFormat (A.t.with_storage storage account).
Proof. intros [_ Hcode] Hstorage. split; assumption. Qed.
Context {F : H.FArgs}.
Lemma transfer_preserves_format sender recipient amount s :
hostFormat s → hostFormat (snd (@H.transfer_ether F sender recipient amount s)).
Proof.
intros [Hworld Htransient]. unfold H.transfer_ether.
cbn -[U.op_gteq U.op_plus U.op_minus W.account].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
- split; [|exact Htransient].
apply write_account_format.
+ apply write_account_format; [exact Hworld|apply balance_update_format, read_account_format; exact Hworld].
+ apply balance_update_format, read_account_format.
apply write_account_format; [exact Hworld|apply balance_update_format, read_account_format; exact Hworld].
- split; assumption.
Qed.
Context {UU : Basics.Unreachable unit}.
Lemma selfdestruct_preserves_format addr beneficiary s :
hostFormat s → hostFormat (snd (@H.selfdestruct F addr beneficiary s)).
Proof.
intro Hformat. unfold H.selfdestruct.
cbn -[H.transfer_ether Addr._Set W.account].
match goal with |- context [H.transfer_ether ?from ?to ?value ?before] ⇒
pose proof (transfer_preserves_format from to value before Hformat) as Htransfer;
destruct (H.transfer_ether from to value before) as [ok middle] end.
cbn [snd] in Htransfer. destruct Htransfer as [Hworld Htransient].
cbn -[Addr._Set W.account].
match goal with |- context [if ?created then _ else _] ⇒ destruct created end.
- split; [|exact Htransient].
apply write_account_format; [exact Hworld|apply balance_update_format, read_account_format; exact Hworld].
- split; assumption.
Qed.
End AccountProperties.
Lemma format_bind {A B : Set} (action : S.t → A × S.t)
(next : A → S.t → B × S.t) :
preservesCallback hostFormat action →
(∀ value, preservesCallback hostFormat (next value)) →
preservesCallback hostFormat (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
intros Haction Hnext s Hformat.
specialize (Haction s Hformat).
change (hostFormat (snd (let '(value, middle) := action s in next value middle))).
destruct (action s) as [value middle]. apply Hnext. exact Haction.
Qed.
Lemma format_return {A : Set} (value : A) :
preservesCallback hostFormat (S.M.(Monad.State.State_result._return) value).
Proof. intros s Hformat. exact Hformat. Qed.
Lemma format_read {A : Set} (lens : Lens.t S.t A) :
preservesCallback hostFormat (S.M.(Monad.State.State_result.op_exclamation) lens).
Proof. intros s Hformat. exact Hformat. Qed.
Lemma format_update {A : Set} (lens : Lens.t S.t A) f :
(∀ s value, S.t.world_state (Lens.t.set lens value s) = S.t.world_state s ∧
S.t.transient_storage (Lens.t.set lens value s) = S.t.transient_storage s) →
preservesCallback hostFormat (S.M.(Monad.State.State_result.update_field) lens f).
Proof.
intros Hframe s [Hworld Htransient].
specialize (Hframe s (f (Lens.t.get lens s))) as [Haccounts Hstorage].
change (worldFormat (S.t.world_state (Lens.t.set lens (f (Lens.t.get lens s)) s)) ∧
transientStorageWords (Lens.t.set lens (f (Lens.t.get lens s)) s)).
unfold transientStorageWords. rewrite Haccounts, Hstorage. split; assumption.
Qed.
Section Fields.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Lemma format_account_field {T : Set} keep addr (field : Lens.t A.t T) value :
(∀ account, accountFormat account → accountFormat (Lens.t.set field value account)) →
preservesCallback hostFormat (S.M.(Monad.State.State_result.op_coloneq)
(Lens.Infix.op_pipeminusminus (@S.account keep addr) field) value).
Proof.
intros Hfield s [Hworld Htransient].
destruct keep as [keep|]; [|set (keep := false)].
all: split; [|exact Htransient].
all: apply write_account_format; [exact Hworld|apply Hfield, read_account_format; exact Hworld].
Qed.
Context {F : H.FArgs} {UI : Basics.Unreachable int}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Lemma set_storage_preserves_format addr key value s :
hostFormat s → OCamlString.length value = 32 →
hostFormat (snd (@H.set_storage F addr key value s)).
Proof.
intros [Hworld Htransient] Hvalue. split; [|exact Htransient].
apply world_format_components in Hworld as [Hstorage Hcode].
apply world_format_components. split.
- apply set_storage_preserves_words; assumption.
- apply (@write_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62)
true (S.t.world_state s) addr); [exact Hcode|].
apply (@read_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62));
[change (0 < 2^62); lia|exact Hcode].
Qed.
Lemma set_transient_storage_preserves_format addr key value s :
hostFormat s → OCamlString.length value = 32 →
hostFormat (snd (@H.set_transient_storage F addr key value s)).
Proof.
intros [Hworld Htransient] Hvalue. split; [exact Hworld|].
apply set_transient_storage_preserves_words; assumption.
Qed.
End Fields.
Lemma creation_code_bound code :
Z.gtb (Byte_string.Bytes.length code) Chain.Monad.Constants.max_code_size = false →
OCamlString.length code < 2^62.
Proof.
intro Hguard.
assert (Hbound : Byte_string.Bytes.length code ≤ Chain.Monad.Constants.max_code_size).
{ apply Z.nlt_ge. intro Hlarge. apply Z.gtb_lt in Hlarge. congruence. }
change (OCamlString.length code ≤ 131072) in Hbound. lia.
Qed.
Ltac solve_format :=
lazymatch goal with
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.op_letdollar) _ _) ⇒
apply format_bind; [solve_format|];
let value := fresh "value" in intro value;
repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
cbn beta iota zeta; solve_format
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result._return) _) ⇒ apply format_return
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.op_exclamation) _) ⇒ apply format_read
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.update_field) _ _) ⇒
apply format_update; intros; split; reflexivity
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.op_coloneq)
(Lens.Infix.op_pipeminusminus _ ?field) _) ⇒
apply format_account_field; intros account Hformat;
lazymatch field with
| A.TLens.nonce ⇒ exact Hformat
| A.TLens.storage ⇒
apply storage_update_format; [exact Hformat|change (storageWords nil); exact (Forall_nil _)]
| A.TLens.code ⇒
apply code_update_format; [exact Hformat|apply creation_code_bound; assumption]
end
| |- preservesCallback hostFormat (if ?test then _ else _) ⇒
let Hguard := fresh "Hguard" in destruct test eqn:Hguard; solve_format
| |- preservesCallback hostFormat (match (if ?test then _ else _) with _ ⇒ _ end) ⇒
let Hguard := fresh "Hguard" in destruct test eqn:Hguard; cbn beta iota zeta; solve_format
| |- preservesCallback hostFormat (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
destruct value; solve_format
| |- preservesCallback hostFormat (match ?status with Evmc.Result.StatusCode.Success ⇒ _ | _ ⇒ _ end) ⇒
destruct status; solve_format
| |- preservesCallback hostFormat (@H.transfer_ether _ _ _ _) ⇒
intro; apply transfer_preserves_format
| Hvm : ∀ msg code, preservesCallback hostFormat (_ msg code)
|- preservesCallback hostFormat (_ ?msg ?code) ⇒ apply Hvm
| Hvm : ∀ msg code, _ → preservesCallback hostFormat (_ msg code)
|- preservesCallback hostFormat (_ ?msg ?code) ⇒ apply Hvm; assumption
| |- ?g ⇒ fail 2 "Unhandled account-format action:" g
end.
Section Creation.
Context {F : H.FArgs}.
Context {UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
{UF : Basics.Unreachable U.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)}.
Lemma creation_address_preserves_format msg :
preservesCallback hostFormat (@H.contract_creation_address F msg).
Proof. unfold H.contract_creation_address, H.M. solve_format. Qed.
Forall (fun entry ⇒ OCamlString.length (A.t.code (snd entry)) < 2^62) (W.t.accounts world).
Definition accountFormat (account : A.t) : Prop :=
storageWords (A.t.storage account) ∧ OCamlString.length (A.t.code account) < 2^62.
Definition worldFormat (world : W.t) : Prop :=
Forall (fun entry ⇒ accountFormat (snd entry)) (W.t.accounts world).
Definition hostFormat (s : S.t) : Prop :=
worldFormat (S.t.world_state s) ∧ transientStorageWords s.
Lemma world_format_components world :
worldFormat world ↔ accountStorageWords world ∧ accountCodeBounds world.
Proof.
unfold worldFormat, accountFormat, accountStorageWords, accountCodeBounds.
split.
- intro H. induction H as [|entry entries [Hstorage Hcode] Hrest [IHstorage IHcode]].
+ split; constructor.
+ split; constructor; assumption.
- intros [Hstorage Hcode]. apply Forall_and; assumption.
Qed.
Section AccountProperties.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Lemma read_account_property (P : A.t → Prop) world addr :
P (@A.empty) →
Forall (fun entry ⇒ P (snd entry)) (W.t.accounts world) →
P (State.op_pointcaretlparenrparen world (@W.account None addr)).
Proof.
intros Hempty Hworld.
change (P (match Addr.Map.find_opt addr (W.t.accounts world) with
| Some account ⇒ account | None ⇒ @A.empty end)).
destruct (Addr.Map.find_opt addr (W.t.accounts world)) eqn:Hfind.
- eapply (map_find_value_property _ P); eassumption.
- exact Hempty.
Qed.
Lemma write_account_property (P : A.t → Prop) keep world addr value :
Forall (fun entry ⇒ P (snd entry)) (W.t.accounts world) → P value →
Forall (fun entry ⇒ P (snd entry)) (W.t.accounts
(State.op_pointcaretlparenrparenltminus world (@W.account (Some keep) addr) value)).
Proof.
intros Hworld Hvalue. destruct keep.
- change (Forall (fun entry ⇒ P (snd entry))
(OCamlMap.Make.add stringmaps.stringOrder addr value (W.t.accounts world))).
apply (map_add_value_property _ P); assumption.
- change (Forall (fun entry ⇒ P (snd entry))
(OCamlMap.Make.update stringmaps.stringOrder addr
(fun _ ⇒ if @A.is_empty value then None else Some value) (W.t.accounts world))).
unfold OCamlMap.Make.update. destruct (@A.is_empty value).
+ apply (map_remove_value_property _ P). exact Hworld.
+ apply (map_add_value_property _ P); assumption.
Qed.
Lemma empty_account_format : accountFormat (@A.empty).
Proof. split; [constructor|change (0 < 2^62); lia]. Qed.
Lemma read_account_format world addr :
worldFormat world → accountFormat
(State.op_pointcaretlparenrparen world (@W.account None addr)).
Proof. apply read_account_property, empty_account_format. Qed.
Lemma write_account_format keep world addr account :
worldFormat world → accountFormat account →
worldFormat (State.op_pointcaretlparenrparenltminus world
(@W.account (Some keep) addr) account).
Proof. apply write_account_property. Qed.
Lemma balance_update_format balance account :
accountFormat account → accountFormat (A.t.with_balance balance account).
Proof. exact (fun H ⇒ H). Qed.
Lemma nonce_update_format nonce account :
accountFormat account → accountFormat (A.t.with_nonce nonce account).
Proof. exact (fun H ⇒ H). Qed.
Lemma code_update_format code account :
accountFormat account → OCamlString.length code < 2^62 →
accountFormat (A.t.with_code code account).
Proof. intros [Hstorage _] Hcode. split; assumption. Qed.
Lemma storage_update_format storage account :
accountFormat account → storageWords storage →
accountFormat (A.t.with_storage storage account).
Proof. intros [_ Hcode] Hstorage. split; assumption. Qed.
Context {F : H.FArgs}.
Lemma transfer_preserves_format sender recipient amount s :
hostFormat s → hostFormat (snd (@H.transfer_ether F sender recipient amount s)).
Proof.
intros [Hworld Htransient]. unfold H.transfer_ether.
cbn -[U.op_gteq U.op_plus U.op_minus W.account].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
- split; [|exact Htransient].
apply write_account_format.
+ apply write_account_format; [exact Hworld|apply balance_update_format, read_account_format; exact Hworld].
+ apply balance_update_format, read_account_format.
apply write_account_format; [exact Hworld|apply balance_update_format, read_account_format; exact Hworld].
- split; assumption.
Qed.
Context {UU : Basics.Unreachable unit}.
Lemma selfdestruct_preserves_format addr beneficiary s :
hostFormat s → hostFormat (snd (@H.selfdestruct F addr beneficiary s)).
Proof.
intro Hformat. unfold H.selfdestruct.
cbn -[H.transfer_ether Addr._Set W.account].
match goal with |- context [H.transfer_ether ?from ?to ?value ?before] ⇒
pose proof (transfer_preserves_format from to value before Hformat) as Htransfer;
destruct (H.transfer_ether from to value before) as [ok middle] end.
cbn [snd] in Htransfer. destruct Htransfer as [Hworld Htransient].
cbn -[Addr._Set W.account].
match goal with |- context [if ?created then _ else _] ⇒ destruct created end.
- split; [|exact Htransient].
apply write_account_format; [exact Hworld|apply balance_update_format, read_account_format; exact Hworld].
- split; assumption.
Qed.
End AccountProperties.
Lemma format_bind {A B : Set} (action : S.t → A × S.t)
(next : A → S.t → B × S.t) :
preservesCallback hostFormat action →
(∀ value, preservesCallback hostFormat (next value)) →
preservesCallback hostFormat (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
intros Haction Hnext s Hformat.
specialize (Haction s Hformat).
change (hostFormat (snd (let '(value, middle) := action s in next value middle))).
destruct (action s) as [value middle]. apply Hnext. exact Haction.
Qed.
Lemma format_return {A : Set} (value : A) :
preservesCallback hostFormat (S.M.(Monad.State.State_result._return) value).
Proof. intros s Hformat. exact Hformat. Qed.
Lemma format_read {A : Set} (lens : Lens.t S.t A) :
preservesCallback hostFormat (S.M.(Monad.State.State_result.op_exclamation) lens).
Proof. intros s Hformat. exact Hformat. Qed.
Lemma format_update {A : Set} (lens : Lens.t S.t A) f :
(∀ s value, S.t.world_state (Lens.t.set lens value s) = S.t.world_state s ∧
S.t.transient_storage (Lens.t.set lens value s) = S.t.transient_storage s) →
preservesCallback hostFormat (S.M.(Monad.State.State_result.update_field) lens f).
Proof.
intros Hframe s [Hworld Htransient].
specialize (Hframe s (f (Lens.t.get lens s))) as [Haccounts Hstorage].
change (worldFormat (S.t.world_state (Lens.t.set lens (f (Lens.t.get lens s)) s)) ∧
transientStorageWords (Lens.t.set lens (f (Lens.t.get lens s)) s)).
unfold transientStorageWords. rewrite Haccounts, Hstorage. split; assumption.
Qed.
Section Fields.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Lemma format_account_field {T : Set} keep addr (field : Lens.t A.t T) value :
(∀ account, accountFormat account → accountFormat (Lens.t.set field value account)) →
preservesCallback hostFormat (S.M.(Monad.State.State_result.op_coloneq)
(Lens.Infix.op_pipeminusminus (@S.account keep addr) field) value).
Proof.
intros Hfield s [Hworld Htransient].
destruct keep as [keep|]; [|set (keep := false)].
all: split; [|exact Htransient].
all: apply write_account_format; [exact Hworld|apply Hfield, read_account_format; exact Hworld].
Qed.
Context {F : H.FArgs} {UI : Basics.Unreachable int}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Lemma set_storage_preserves_format addr key value s :
hostFormat s → OCamlString.length value = 32 →
hostFormat (snd (@H.set_storage F addr key value s)).
Proof.
intros [Hworld Htransient] Hvalue. split; [|exact Htransient].
apply world_format_components in Hworld as [Hstorage Hcode].
apply world_format_components. split.
- apply set_storage_preserves_words; assumption.
- apply (@write_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62)
true (S.t.world_state s) addr); [exact Hcode|].
apply (@read_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62));
[change (0 < 2^62); lia|exact Hcode].
Qed.
Lemma set_transient_storage_preserves_format addr key value s :
hostFormat s → OCamlString.length value = 32 →
hostFormat (snd (@H.set_transient_storage F addr key value s)).
Proof.
intros [Hworld Htransient] Hvalue. split; [exact Hworld|].
apply set_transient_storage_preserves_words; assumption.
Qed.
End Fields.
Lemma creation_code_bound code :
Z.gtb (Byte_string.Bytes.length code) Chain.Monad.Constants.max_code_size = false →
OCamlString.length code < 2^62.
Proof.
intro Hguard.
assert (Hbound : Byte_string.Bytes.length code ≤ Chain.Monad.Constants.max_code_size).
{ apply Z.nlt_ge. intro Hlarge. apply Z.gtb_lt in Hlarge. congruence. }
change (OCamlString.length code ≤ 131072) in Hbound. lia.
Qed.
Ltac solve_format :=
lazymatch goal with
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.op_letdollar) _ _) ⇒
apply format_bind; [solve_format|];
let value := fresh "value" in intro value;
repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
cbn beta iota zeta; solve_format
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result._return) _) ⇒ apply format_return
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.op_exclamation) _) ⇒ apply format_read
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.update_field) _ _) ⇒
apply format_update; intros; split; reflexivity
| |- preservesCallback hostFormat (S.M.(Monad.State.State_result.op_coloneq)
(Lens.Infix.op_pipeminusminus _ ?field) _) ⇒
apply format_account_field; intros account Hformat;
lazymatch field with
| A.TLens.nonce ⇒ exact Hformat
| A.TLens.storage ⇒
apply storage_update_format; [exact Hformat|change (storageWords nil); exact (Forall_nil _)]
| A.TLens.code ⇒
apply code_update_format; [exact Hformat|apply creation_code_bound; assumption]
end
| |- preservesCallback hostFormat (if ?test then _ else _) ⇒
let Hguard := fresh "Hguard" in destruct test eqn:Hguard; solve_format
| |- preservesCallback hostFormat (match (if ?test then _ else _) with _ ⇒ _ end) ⇒
let Hguard := fresh "Hguard" in destruct test eqn:Hguard; cbn beta iota zeta; solve_format
| |- preservesCallback hostFormat (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒
destruct value; solve_format
| |- preservesCallback hostFormat (match ?status with Evmc.Result.StatusCode.Success ⇒ _ | _ ⇒ _ end) ⇒
destruct status; solve_format
| |- preservesCallback hostFormat (@H.transfer_ether _ _ _ _) ⇒
intro; apply transfer_preserves_format
| Hvm : ∀ msg code, preservesCallback hostFormat (_ msg code)
|- preservesCallback hostFormat (_ ?msg ?code) ⇒ apply Hvm
| Hvm : ∀ msg code, _ → preservesCallback hostFormat (_ msg code)
|- preservesCallback hostFormat (_ ?msg ?code) ⇒ apply Hvm; assumption
| |- ?g ⇒ fail 2 "Unhandled account-format action:" g
end.
Section Creation.
Context {F : H.FArgs}.
Context {UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
{UF : Basics.Unreachable U.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)}.
Lemma creation_address_preserves_format msg :
preservesCallback hostFormat (@H.contract_creation_address F msg).
Proof. unfold H.contract_creation_address, H.M. solve_format. Qed.
The recursive VM only needs to preserve representation. Its returned
code may have any length: the actual CREATE code-size check rejects an
oversized result before installing it.
Lemma process_create_preserves_format msg :
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preservesCallback hostFormat ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
preservesCallback hostFormat (@H.process_create F msg).
Proof.
intros Hvm Hgas. unfold H.process_create, H.M.
apply format_bind; [apply creation_address_preserves_format|].
intro addr. solve_format.
Qed.
End Creation.
Section ReserveSetter.
Context {UZ : Basics.Unreachable Z.t} {Uarr : Basics.Unreachable (OCamlIarray.t int)}
{UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
{UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.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}
{UP : Basics.Unreachable (int × int)}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation activate := (@Reserve_balance.activate_precompile).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
Lemma activate_preserves_format world : worldFormat world → worldFormat (activate world).
Proof.
intro Hworld. apply write_account_format; [exact Hworld|].
apply nonce_update_format, read_account_format. exact Hworld.
Qed.
Lemma setter_preserves_format world block addr value after :
worldFormat world → setter world block addr value = Some after → worldFormat after.
Proof.
intros Hworld Hset. apply world_format_components. split.
- eapply setter_preserves_storage_words; [exact (proj1 (proj1 (world_format_components world) Hworld))|exact Hset].
- pose proof (activate_preserves_format world Hworld) as Hactive.
apply world_format_components in Hactive as [_ Hcodes].
unfold Reserve_balance.set_reserve_balance in Hset.
match type of Hset with context [match ?result with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct result as [record|] end; [|discriminate].
injection Hset as <-.
apply (@write_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62)
false (activate world)); [exact Hcodes|].
apply (@read_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62)
(activate world) (@Reserve_balance.precompile_address));
[change (0 < 2^62); lia|exact Hcodes].
Qed.
End ReserveSetter.
Lemma transaction_initial_format
block sender tx :
worldFormat (State.BlockState.t.world_state block) →
hostFormat (@S.make block sender tx).
Proof. intro Hworld. split; [exact Hworld|constructor]. Qed.
Print Assumptions transfer_preserves_format.
Print Assumptions selfdestruct_preserves_format.
Print Assumptions process_create_preserves_format.
Print Assumptions setter_preserves_format.
(∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
preservesCallback hostFormat ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
preservesCallback hostFormat (@H.process_create F msg).
Proof.
intros Hvm Hgas. unfold H.process_create, H.M.
apply format_bind; [apply creation_address_preserves_format|].
intro addr. solve_format.
Qed.
End Creation.
Section ReserveSetter.
Context {UZ : Basics.Unreachable Z.t} {Uarr : Basics.Unreachable (OCamlIarray.t int)}
{UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
{UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.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}
{UP : Basics.Unreachable (int × int)}
{U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation activate := (@Reserve_balance.activate_precompile).
Local Abbreviation setter := (@Reserve_balance.set_reserve_balance).
Lemma activate_preserves_format world : worldFormat world → worldFormat (activate world).
Proof.
intro Hworld. apply write_account_format; [exact Hworld|].
apply nonce_update_format, read_account_format. exact Hworld.
Qed.
Lemma setter_preserves_format world block addr value after :
worldFormat world → setter world block addr value = Some after → worldFormat after.
Proof.
intros Hworld Hset. apply world_format_components. split.
- eapply setter_preserves_storage_words; [exact (proj1 (proj1 (world_format_components world) Hworld))|exact Hset].
- pose proof (activate_preserves_format world Hworld) as Hactive.
apply world_format_components in Hactive as [_ Hcodes].
unfold Reserve_balance.set_reserve_balance in Hset.
match type of Hset with context [match ?result with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct result as [record|] end; [|discriminate].
injection Hset as <-.
apply (@write_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62)
false (activate world)); [exact Hcodes|].
apply (@read_account_property (fun ac ⇒ OCamlString.length (A.t.code ac) < 2^62)
(activate world) (@Reserve_balance.precompile_address));
[change (0 < 2^62); lia|exact Hcodes].
Qed.
End ReserveSetter.
Lemma transaction_initial_format
block sender tx :
worldFormat (State.BlockState.t.world_state block) →
hostFormat (@S.make block sender tx).
Proof. intro Hworld. split; [exact Hworld|constructor]. Qed.
Print Assumptions transfer_preserves_format.
Print Assumptions selfdestruct_preserves_format.
Print Assumptions process_create_preserves_format.
Print Assumptions setter_preserves_format.