Library QedkConcrete.storagepreservation
Storage writes through the generated contract location lens do not spend
funds. Positive balance retains the account even when its nonce is zero.
This is a component of the staking proof, not its accounting invariant.
From Stdlib Require Import List String ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Contract._Type MonadVM.Contract.Storage
QedkConcrete.stringmaps QedkConcrete.wordrange QedkConcrete.bytewords
QedkConcrete.bytecodec QedkConcrete.worldaccounts QedkConcrete.stakingretention
QedkConcrete.supply.
Import ListNotations.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module CT := Contract._Type.
Module CS := Contract.Storage.Make.
Module TS := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module U := Numeric.U256.
Local Abbreviation zeroBytes :=
(@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32).
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (W.account None addr)).
Section Storage.
Context {F : CS.FArgs}.
Local Abbreviation owner := (@CS.Addr F).(Contract.Storage.Make_Addr_signature.address).
Theorem storage_write_retains_account_fields (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query).
Proof.
intros Hmap Hpositive. cbn zeta. intro query.
destruct location as [typ kind offset].
unfold CS.Loc.lens.
cbn [Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.t.get Lens.t.set TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- apply String.eqb_eq in Hquery. subst query.
rewrite positive_balance_not_empty by exact Hpositive.
repeat split; reflexivity.
- repeat split; reflexivity.
Qed.
Corollary storage_write_preserves_positive_balance (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount
(TS.t.world_state (Lens.t.set (CS.Loc.lens fallback location) value state)) owner)).
Proof.
intros Hmap Hpositive.
pose proof (storage_write_retains_account_fields a fallback location value state
Hmap Hpositive owner) as [Hbalance _].
rewrite Hbalance. exact Hpositive.
Qed.
Lemma account_empty_fields account :
@A.is_empty account = true →
A.t.balance account = A.t.balance (@A.empty) ∧
A.t.nonce account = A.t.nonce (@A.empty) ∧
A.t.code account = A.t.code (@A.empty).
Proof.
unfold A.is_empty. intro Hempty.
apply Bool.andb_true_iff in Hempty as [Hbal Hnc].
apply Bool.andb_true_iff in Hnc as [Hnonce Hcode].
change (Z.eqb (match Z.compare (A.t.balance account) 0 with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hbal.
change (Z.eqb (match Z.compare (A.t.nonce account) 0 with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hnonce.
change (Z.eqb (OCamlString.compare (A.t.code account) EmptyString) 0 = true) in Hcode.
rewrite OCamlString.compare_matches_structural in Hcode.
unfold OCamlString.compare_structural in Hcode.
split; [|split].
- destruct (Z.compare_spec (A.t.balance account) 0); cbn in Hbal; [ assumption | discriminate | discriminate ].
- destruct (Z.compare_spec (A.t.nonce account) 0); cbn in Hnonce; [ assumption | discriminate | discriminate ].
- destruct (String.eqb (A.t.code account) EmptyString) eqn:Heq;
[ apply String.eqb_eq; exact Heq | destruct (Basics.String.ltb (A.t.code account) EmptyString); discriminate ].
Qed.
Theorem account_storage_update_retains_fields (f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state post) query) =
A.t.storage (readAccount (TS.t.world_state state) query)).
Proof.
intros Hmap post query. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- apply String.eqb_eq in Hquery. subst query.
destruct (A.is_empty _) eqn:Hempty.
+ destruct (account_empty_fields _ Hempty) as [Hbal [Hnonce Hcode]].
cbn [A.t.with_storage A.t.balance A.t.nonce A.t.code] in Hbal, Hnonce, Hcode.
rewrite <- Hbal, <- Hnonce, <- Hcode.
repeat split; try reflexivity; contradiction.
+ repeat split; try reflexivity; contradiction.
- apply String.eqb_neq in Hquery.
repeat split; reflexivity.
Qed.
Theorem storage_write_retains_account_fields_unconditional (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state post) query) =
A.t.storage (readAccount (TS.t.world_state state) query)).
Proof.
intros Hmap post query. unfold post.
destruct location as [typ kind offset].
exact (account_storage_update_retains_fields
(fun s ⇒ fst (OCamlList.fold_left
(fun '(storage, i_value) val ⇒
let key := CT.U256.to_repr (CT.U256.op_plus offset i_value) in
let storage := Chain.Ethereum.Storage.add key val storage in
let i_value := CT.U256.op_plus i_value CT.U256.one in
(storage, i_value))
(s, CT.U256.zero) (CT.enc typ value))) state Hmap query).
Qed.
Lemma fix_preserves {A B : Set} (R : A → A → Prop) (wf : well_founded R)
(body : ∀ x, (∀ y, R y x → B) → B)
(I : A → Prop) (P : B → Prop) :
(∀ x recur, I x → (∀ y edge, I y → P (recur y edge)) → P (body x recur)) →
∀ x, I x → P (Init.Wf.Fix wf (fun _ ⇒ B) body x).
Proof.
intro Hbody.
assert (Hfix : ∀ x (access : Acc R x), I x →
P (Init.Wf.Fix_F (fun _ ⇒ B) body access)).
{ fix IH 2. intros x [children] Hx.
cbn [Init.Wf.Fix_F]. apply Hbody; [exact Hx|].
intros y edge Hy. apply IH; exact Hy. }
intros x Hx. apply Hfix; exact Hx.
Qed.
Lemma dependent_bool_property {A : Type} (P : A → Prop) (test : bool)
(yes : test = true → A) (no : test = false → A) :
(∀ eq, P (yes eq)) → (∀ eq, P (no eq)) →
P ((if test as b return (test = b → A) then yes else no) eq_refl).
Proof. intros Hyes Hno. destruct test; [apply Hyes|apply Hno]. Qed.
Lemma clear_preserves_state_predicate (P : TS.t → Prop) (a : Set)
(location : @CS.Loc.Impl.t F a) state :
(∀ key s, P s →
P (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage)
(Chain.Ethereum.Storage.add key zeroBytes) s))) →
P state →
P (snd (CS.Loc.clear location state)).
Proof.
intros Hstep Hinit.
unfold CS.Loc.clear.
revert state Hinit.
eapply (fix_preserves _ _ _ (fun _ ⇒ True)
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))); [|exact I].
intros slot recur _ IH.
apply (dependent_bool_property
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))).
- intros Hlt st Hst.
eapply IH; [exact I|].
exact (Hstep (CT.U256.to_repr (CT.U256.of_uint_exn slot)) st Hst).
- intros Hge st Hst. exact Hst.
Qed.
Theorem account_storage_update_wellformed (f : Chain.Ethereum.Storage.t → Chain.Ethereum.Storage.t) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
stringmaps.WellFormed (W.t.accounts (TS.t.world_state post)).
Proof.
intros Hmap post. unfold post.
exact (@account_write_wellformed false (TS.t.world_state state) owner
(A.t.with_storage (f (A.t.storage (readAccount (TS.t.world_state state) owner)))
(readAccount (TS.t.world_state state) owner)) Hmap).
Qed.
Theorem storage_clear_retains_account_fields (a : Set)
(location : @CS.Loc.Impl.t F a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := snd (CS.Loc.clear location state) in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state post) query) =
A.t.storage (readAccount (TS.t.world_state state) query)).
Proof.
intros Hmap post query. unfold post.
refine (proj2 (clear_preserves_state_predicate
(fun s ⇒
stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
(A.t.balance (readAccount (TS.t.world_state s) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state s) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state s) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state s) query) =
A.t.storage (readAccount (TS.t.world_state state) query))))
a location state _ _)).
- intros key s [Hs [Hbal [Hnonce [Hcode Hstor]]]].
split.
+ exact (account_storage_update_wellformed (Chain.Ethereum.Storage.add key zeroBytes) s Hs).
+ destruct (account_storage_update_retains_fields (Chain.Ethereum.Storage.add key zeroBytes) s Hs query)
as [Hb [Hn [Hc Hst]]].
rewrite Hb, Hn, Hc.
refine (conj Hbal (conj Hnonce (conj Hcode _))).
intro Hneq. rewrite (Hst Hneq). exact (Hstor Hneq).
- split; [ exact Hmap | repeat split; reflexivity ].
Qed.
Theorem account_storage_update_preserves_supply (f : list (string × string) → list (string × string)) state :
balanceInvariant (TS.t.world_state state) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
balanceInvariant (TS.t.world_state post) ∧
totalBalance (TS.t.world_state post) = totalBalance (TS.t.world_state state).
Proof.
intros Hinv post. unfold post.
eapply (@account_write_preserves_supply false (TS.t.world_state state) owner
(A.t.with_storage (f (A.t.storage (readAccount (TS.t.world_state state) owner)))
(readAccount (TS.t.world_state state) owner))).
- exact Hinv.
- reflexivity.
Qed.
Theorem storage_write_preserves_supply (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
balanceInvariant (TS.t.world_state state) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
balanceInvariant (TS.t.world_state post) ∧
totalBalance (TS.t.world_state post) = totalBalance (TS.t.world_state state).
Proof.
intros Hinv post. unfold post.
destruct location as [typ kind offset].
exact (account_storage_update_preserves_supply
(fun s ⇒ fst (OCamlList.fold_left
(fun '(storage, i_value) val ⇒
let key := CT.U256.to_repr (CT.U256.op_plus offset i_value) in
let storage := Chain.Ethereum.Storage.add key val storage in
let i_value := CT.U256.op_plus i_value CT.U256.one in
(storage, i_value))
(s, CT.U256.zero) (CT.enc typ value))) state Hinv).
Qed.
Theorem storage_clear_preserves_supply (a : Set)
(location : @CS.Loc.Impl.t F a) state :
balanceInvariant (TS.t.world_state state) →
let post := snd (CS.Loc.clear location state) in
balanceInvariant (TS.t.world_state post) ∧
totalBalance (TS.t.world_state post) = totalBalance (TS.t.world_state state).
Proof.
intros Hinv post. unfold post.
refine (clear_preserves_state_predicate
(fun s ⇒
balanceInvariant (TS.t.world_state s) ∧
totalBalance (TS.t.world_state s) = totalBalance (TS.t.world_state state))
a location state _ _).
- intros key s [Hbinv Htot].
destruct (account_storage_update_preserves_supply
(Chain.Ethereum.Storage.add key zeroBytes) s Hbinv) as [Hbinv' Htot'].
split; [exact Hbinv'|rewrite Htot'; exact Htot].
- split; [exact Hinv|reflexivity].
Qed.
Lemma b32_equal_true x y :
Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) x y = true → x = y.
Proof.
change (Z.eqb (OCamlString.compare x y) 0 = true → x = y).
rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural.
destruct (String.eqb x y) eqn:Heq.
- intro. apply String.eqb_eq. exact Heq.
- destruct (Basics.String.ltb x y); intro H; discriminate H.
Qed.
Definition storageWords (s : Chain.Ethereum.Storage.t) : Prop :=
Forall (fun entry ⇒ OCamlString.length (snd entry) = 32) s.
Lemma map_add_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key value :
P value → Forall (fun pair ⇒ P (snd pair)) entries →
Forall (fun pair ⇒ P (snd pair)) (OCamlMap.Make.add order key value entries).
Proof.
intros Hvalue Hentries. induction Hentries as [|[k v] rest Hv Hrest IH].
- constructor; [exact Hvalue|constructor].
- cbn [OCamlMap.Make.add].
destruct (OCamlMap.Make.key_before order key k).
+ constructor; [exact Hvalue|constructor; assumption].
+ destruct (OCamlMap.Make.key_equal order key k); constructor; assumption.
Qed.
Lemma map_remove_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key :
Forall (fun entry ⇒ P (snd entry)) entries →
Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.remove order key entries).
Proof.
intro Hentries. induction Hentries as [|[k v] rest Hv Hrest IH]; [constructor|].
cbn [OCamlMap.Make.remove]. destruct (OCamlMap.Make.key_before order key k).
- constructor; assumption.
- destruct (OCamlMap.Make.key_equal order key k); [exact Hrest|constructor; assumption].
Qed.
Lemma storage_add_wellformed key val (s : Chain.Ethereum.Storage.t) :
stringmaps.WellFormed s →
stringmaps.WellFormed (Chain.Ethereum.Storage.add key val s).
Proof.
intro Hwf. unfold Chain.Ethereum.Storage.add.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes).
- exact (remove_wellformed s key Hwf).
- exact (add_wellformed s key val Hwf).
Qed.
Lemma storage_add_words key val (s : Chain.Ethereum.Storage.t) :
storageWords s →
OCamlString.length val = 32 →
storageWords (Chain.Ethereum.Storage.add key val s).
Proof.
intros Hwords Hval. unfold Chain.Ethereum.Storage.add, storageWords in ×.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes).
- exact (map_remove_value_property stringmaps.stringOrder
(fun v ⇒ OCamlString.length v = 32) s key Hwords).
- exact (map_add_value_property stringmaps.stringOrder
(fun v ⇒ OCamlString.length v = 32) s key val Hval Hwords).
Qed.
Lemma storage_find_add_same key val (s : Chain.Ethereum.Storage.t) :
stringmaps.WellFormed s →
Chain.Ethereum.Storage.find key (Chain.Ethereum.Storage.add key val s) = val.
Proof.
intro Hwf. unfold Chain.Ethereum.Storage.find, Chain.Ethereum.Storage.add.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes) eqn:Hzero.
- apply b32_equal_true in Hzero. subst val.
change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) key
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.remove) key s))
with (OCamlMap.Make.find_opt stringOrder key (OCamlMap.Make.remove stringOrder key s)).
rewrite (find_remove_same s key Hwf). reflexivity.
- change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) key
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.add) key val s))
with (OCamlMap.Make.find_opt stringOrder key (OCamlMap.Make.add stringOrder key val s)).
rewrite (find_add_same s key val Hwf). reflexivity.
Qed.
Lemma storage_find_add_other key query val (s : Chain.Ethereum.Storage.t) :
stringmaps.WellFormed s →
key ≠ query →
Chain.Ethereum.Storage.find query (Chain.Ethereum.Storage.add key val s) =
Chain.Ethereum.Storage.find query s.
Proof.
intros Hwf Hneq. unfold Chain.Ethereum.Storage.find, Chain.Ethereum.Storage.add.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes).
- change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) query
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.remove) key s))
with (OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.remove stringOrder key s)).
rewrite (find_remove_other s key query Hwf Hneq). reflexivity.
- change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) query
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.add) key val s))
with (OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.add stringOrder key val s)).
rewrite (find_add_other s key query val Hwf Hneq). reflexivity.
Qed.
Lemma word_add_small left right :
0 ≤ left + right < 2^256 →
@U.op_plus left right = left + right.
Proof.
intro Hrange.
change (Z.land (Z.shiftl (left + right) 0) (Z.ones 256) = left + right).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
apply Z.mod_small. exact Hrange.
Qed.
Definition writeStorageWordsFrom (offset idx : CT.U256.t) (words : list string)
(s : Chain.Ethereum.Storage.t) : Chain.Ethereum.Storage.t × CT.U256.t :=
OCamlList.fold_left
(fun '(storage, i_value) val ⇒
let key := CT.U256.to_repr (CT.U256.op_plus offset i_value) in
let storage := Chain.Ethereum.Storage.add key val storage in
let i_value := CT.U256.op_plus i_value CT.U256.one in
(storage, i_value))
(s, idx) words.
Definition writeStorageWords (offset : CT.U256.t) (words : list string)
(s : Chain.Ethereum.Storage.t) : Chain.Ethereum.Storage.t :=
fst (writeStorageWordsFrom offset CT.U256.zero words s).
Lemma writeStorageWordsFrom_wellformed offset idx words s :
stringmaps.WellFormed s →
stringmaps.WellFormed (fst (writeStorageWordsFrom offset idx words s)).
Proof.
revert idx s. induction words as [|word rest IH]; intros idx s Hwf.
- exact Hwf.
- cbn [writeStorageWordsFrom OCamlList.fold_left].
apply IH. apply storage_add_wellformed. exact Hwf.
Qed.
Lemma writeStorageWords_wellformed offset words s :
stringmaps.WellFormed s →
stringmaps.WellFormed (writeStorageWords offset words s).
Proof.
apply writeStorageWordsFrom_wellformed.
Qed.
Lemma writeStorageWordsFrom_words offset idx words s :
storageWords s →
Forall (fun w ⇒ OCamlString.length w = 32) words →
storageWords (fst (writeStorageWordsFrom offset idx words s)).
Proof.
revert idx s. induction words as [|word rest IH]; intros idx s Hwords Hfa.
- exact Hwords.
- inversion Hfa as [|? ? Hw Hrest]; subst.
cbn [writeStorageWordsFrom OCamlList.fold_left].
apply IH; [|exact Hrest].
apply storage_add_words; assumption.
Qed.
Lemma writeStorageWords_words offset words s :
storageWords s →
Forall (fun w ⇒ OCamlString.length w = 32) words →
storageWords (writeStorageWords offset words s).
Proof.
apply writeStorageWordsFrom_words.
Qed.
Lemma storage_find_writeStorageWordsFrom_other offset idx words query s :
stringmaps.WellFormed s →
0 ≤ offset →
0 ≤ idx →
offset + idx + Z.of_nat (Lists.List.length words) ≤ 2^256 →
(∀ i, 0 ≤ i < Z.of_nat (Lists.List.length words) →
CT.U256.to_repr (offset + idx + i) ≠ query) →
Chain.Ethereum.Storage.find query (fst (writeStorageWordsFrom offset idx words s)) =
Chain.Ethereum.Storage.find query s.
Proof.
revert idx s. induction words as [|word rest IH]; intros idx s Hwf Hoff Hidx Hbound Hdisj.
- reflexivity.
- cbn [writeStorageWordsFrom OCamlList.fold_left].
cbn [Lists.List.length] in Hbound, Hdisj.
rewrite Nat2Z.inj_succ in Hbound, Hdisj.
replace (CT.U256.op_plus offset idx) with (offset + idx)
by (symmetry; apply word_add_small; lia).
destruct rest as [|w2 rest'].
+ cbn [OCamlList.fold_left fst].
apply storage_find_add_other; [exact Hwf|].
specialize (Hdisj 0 ltac:(lia)).
replace (offset + idx + 0) with (offset + idx) in Hdisj by lia.
exact Hdisj.
+ assert (Hpos_rest : 1 ≤ Z.of_nat (Lists.List.length (w2 :: rest')))
by (cbn [Lists.List.length]; lia).
replace (CT.U256.op_plus idx CT.U256.one) with (idx + 1)
by (symmetry; apply word_add_small; change (0 ≤ idx + 1 < 2^256); lia).
change (Chain.Ethereum.Storage.find query
(fst (writeStorageWordsFrom offset (idx + 1) (w2 :: rest')
(Chain.Ethereum.Storage.add (CT.U256.to_repr (offset + idx)) word s))) =
Chain.Ethereum.Storage.find query s).
rewrite IH.
× apply storage_find_add_other; [exact Hwf|].
specialize (Hdisj 0 ltac:(lia)).
replace (offset + idx + 0) with (offset + idx) in Hdisj by lia.
exact Hdisj.
× apply storage_add_wellformed. exact Hwf.
× exact Hoff.
× lia.
× lia.
× intros i Hi.
replace (offset + (idx + 1) + i) with (offset + idx + (i + 1)) by lia.
apply Hdisj. lia.
Qed.
Lemma storage_find_writeStorageWords_other offset words query s :
stringmaps.WellFormed s →
0 ≤ offset →
offset + Z.of_nat (Lists.List.length words) ≤ 2^256 →
(∀ i, 0 ≤ i < Z.of_nat (Lists.List.length words) →
CT.U256.to_repr (offset + i) ≠ query) →
Chain.Ethereum.Storage.find query (writeStorageWords offset words s) =
Chain.Ethereum.Storage.find query s.
Proof.
intros Hwf Hoff Hbound Hdisj.
unfold writeStorageWords.
apply storage_find_writeStorageWordsFrom_other.
- exact Hwf.
- exact Hoff.
- change (0 ≤ 0). lia.
- change (offset + 0 + Z.of_nat (Lists.List.length words) ≤ 2^256). lia.
- intros i Hi. change (CT.U256.to_repr (offset + 0 + i) ≠ query).
replace (offset + 0 + i) with (offset + i) by lia.
exact (Hdisj i Hi).
Qed.
Lemma storage_find_writeStorageWordsFrom_nth offset idx words i s :
stringmaps.WellFormed s →
0 ≤ offset →
0 ≤ idx →
offset + idx + Z.of_nat (Lists.List.length words) ≤ 2^256 →
0 ≤ i < Z.of_nat (Lists.List.length words) →
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + idx + i))
(fst (writeStorageWordsFrom offset idx words s)) =
Lists.List.nth (Z.to_nat i) words zeroBytes.
Proof.
revert idx i s. induction words as [|word rest IH]; intros idx i s Hwf Hoff Hidx Hbound Hi.
- cbn [Lists.List.length] in Hi. lia.
- cbn [writeStorageWordsFrom OCamlList.fold_left].
cbn [Lists.List.length] in Hbound, Hi.
rewrite Nat2Z.inj_succ in Hbound, Hi.
replace (CT.U256.op_plus offset idx) with (offset + idx)
by (symmetry; apply word_add_small; lia).
destruct rest as [|w2 rest'].
+ cbn [Lists.List.length] in Hi. assert (i = 0) by lia. subst i.
replace (offset + idx + 0) with (offset + idx) by lia.
cbn [OCamlList.fold_left fst Z.to_nat Lists.List.nth].
apply storage_find_add_same. exact Hwf.
+ assert (Hpos_rest : 1 ≤ Z.of_nat (Lists.List.length (w2 :: rest')))
by (cbn [Lists.List.length]; lia).
replace (CT.U256.op_plus idx CT.U256.one) with (idx + 1)
by (symmetry; apply word_add_small; change (0 ≤ idx + 1 < 2^256); lia).
change (Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + idx + i))
(fst (writeStorageWordsFrom offset (idx + 1) (w2 :: rest')
(Chain.Ethereum.Storage.add (CT.U256.to_repr (offset + idx)) word s))) =
Lists.List.nth (Z.to_nat i) (word :: w2 :: rest') zeroBytes).
assert (i = 0 ∨ 1 ≤ i) as [->|Hpos] by lia.
× replace (offset + idx + 0) with (offset + idx) by lia.
cbn [Z.to_nat Lists.List.nth].
rewrite storage_find_writeStorageWordsFrom_other.
-- apply storage_find_add_same. exact Hwf.
-- apply storage_add_wellformed. exact Hwf.
-- exact Hoff.
-- lia.
-- lia.
-- intros j Hj Hcol.
apply word_encoding_injective in Hcol;
[lia | unfold wordRange; change (0 ≤ offset + (idx + 1) + j < 2^256); lia
| unfold wordRange; change (0 ≤ offset + idx < 2^256); lia].
× replace (offset + idx + i) with (offset + (idx + 1) + (i - 1)) by lia.
rewrite IH.
-- replace (Z.to_nat i) with (S (Z.to_nat (i - 1))) by lia.
reflexivity.
-- apply storage_add_wellformed. exact Hwf.
-- exact Hoff.
-- lia.
-- lia.
-- lia.
Qed.
Lemma storage_find_writeStorageWords_nth offset words i s :
stringmaps.WellFormed s →
0 ≤ offset →
offset + Z.of_nat (Lists.List.length words) ≤ 2^256 →
0 ≤ i < Z.of_nat (Lists.List.length words) →
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + i))
(writeStorageWords offset words s) =
Lists.List.nth (Z.to_nat i) words zeroBytes.
Proof.
intros Hwf Hoff Hbound Hi.
unfold writeStorageWords.
replace (offset + i) with (offset + CT.U256.zero + i) by (change CT.U256.zero with 0; lia).
apply storage_find_writeStorageWordsFrom_nth.
- exact Hwf.
- exact Hoff.
- change (0 ≤ 0). lia.
- change (offset + 0 + Z.of_nat (Lists.List.length words) ≤ 2^256). lia.
- exact Hi.
Qed.
Lemma account_storage_update_owner_storage
(f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
A.t.storage (readAccount (TS.t.world_state post) owner) =
f (A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hpositive post. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account owner Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
rewrite String.eqb_refl.
rewrite positive_balance_not_empty by exact Hpositive.
reflexivity.
Qed.
Lemma account_storage_update_preserves_storage_wf
(f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) query))) →
(stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
stringmaps.WellFormed (f (A.t.storage (readAccount (TS.t.world_state state) owner)))) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwf Hupd post query. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- destruct (A.is_empty _) eqn:Hempty.
+ exact stringmaps.empty_wellformed.
+ cbn [A.t.storage A.t.with_storage].
apply Hupd. exact (Hwf owner).
- exact (Hwf query).
Qed.
Lemma account_storage_update_preserves_storage_words
(f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state state) query))) →
(storageWords (A.t.storage (readAccount (TS.t.world_state state) owner)) →
storageWords (f (A.t.storage (readAccount (TS.t.world_state state) owner)))) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwords Hupd post query. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- destruct (A.is_empty _) eqn:Hempty.
+ unfold storageWords. cbn. constructor.
+ cbn [A.t.storage A.t.with_storage].
apply Hupd. exact (Hwords owner).
- exact (Hwords query).
Qed.
Theorem storage_write_preserves_storage_wf (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwf post query. unfold post.
destruct location as [typ kind offset].
apply (account_storage_update_preserves_storage_wf
(writeStorageWords offset (CT.enc typ value)) state Hmap Hwf).
intro Howner. apply writeStorageWords_wellformed. exact Howner.
Qed.
Theorem storage_clear_preserves_storage_wf (a : Set)
(location : @CS.Loc.Impl.t F a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := snd (CS.Loc.clear location state) in
∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwf post query. unfold post.
revert query.
refine (proj2 (clear_preserves_state_predicate
(fun s ⇒
stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state s) query))))
a location state _ _)).
- intros key s [Hs_map Hs_wf].
split.
+ exact (account_storage_update_wellformed (Chain.Ethereum.Storage.add key zeroBytes) s Hs_map).
+ apply (account_storage_update_preserves_storage_wf
(Chain.Ethereum.Storage.add key zeroBytes) s Hs_map Hs_wf).
intro Howner. apply storage_add_wellformed. exact Howner.
- split; [exact Hmap | exact Hwf].
Qed.
Theorem storage_write_preserves_storage_words (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
Forall (fun w ⇒ OCamlString.length w = 32) (CT.enc (CS.Loc.Impl.t.typ location) value) →
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Henc Hwords post query. unfold post.
destruct location as [typ kind offset].
apply (account_storage_update_preserves_storage_words
(writeStorageWords offset (CT.enc typ value)) state Hmap Hwords).
intro Howner. apply writeStorageWords_words; assumption.
Qed.
Theorem storage_clear_preserves_storage_words (a : Set)
(location : @CS.Loc.Impl.t F a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := snd (CS.Loc.clear location state) in
∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwords post query. unfold post.
revert query.
refine (proj2 (clear_preserves_state_predicate
(fun s ⇒
stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state s) query))))
a location state _ _)).
- intros key s [Hs_map Hs_words].
split.
+ exact (account_storage_update_wellformed (Chain.Ethereum.Storage.add key zeroBytes) s Hs_map).
+ apply (account_storage_update_preserves_storage_words
(Chain.Ethereum.Storage.add key zeroBytes) s Hs_map Hs_words).
intro Howner. apply storage_add_words; [exact Howner | vm_compute; reflexivity].
- split; [exact Hmap | exact Hwords].
Qed.
Lemma u256_of_int_small w :
0 ≤ w < 2^256 →
CT.U256.op_tildedollar w = w.
Proof.
intro Hw.
apply word_of_z. unfold wordRange. change (0 ≤ w < 2^256). exact Hw.
Qed.
Lemma u256_of_uint_small slot :
0 ≤ Z.of_N slot < 2^256 →
CT.U256.of_uint_exn slot = Z.of_N slot.
Proof.
intro Hslot.
apply word_of_z. unfold wordRange. change (0 ≤ Z.of_N slot < 2^256). exact Hslot.
Qed.
Lemma u256_to_uint_nonneg x :
0 ≤ x →
CT.U256.to_uint x = Z.to_N x.
Proof.
intro Hx.
change (match (if Z.leb 0 x then Some (Z.to_N x) else None) with
| Some r ⇒ r
| None ⇒ Support.UintN.fallback_uint
end = Z.to_N x).
destruct (Z.leb_spec0 0 x); [reflexivity | lia].
Qed.
Lemma clear_preserves_state_predicate_bounded (P : TS.t → Prop) (a : Set)
(location : @CS.Loc.Impl.t F a) (w : int) state :
CS.Loc.Impl.t.kind location = CT.Static {| CT.kind.Static.word_width := w |} →
0 ≤ CS.Loc.Impl.t.offset location →
0 ≤ w →
CS.Loc.Impl.t.offset location + w < 2^256 →
(∀ i s,
0 ≤ i < w →
P s →
P (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage)
(Chain.Ethereum.Storage.add (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i)) zeroBytes) s))) →
P state →
P (snd (CS.Loc.clear location state)).
Proof.
intros Hkind Hoff Hw Hbound Hstep Hinit.
unfold CS.Loc.clear.
revert state Hinit.
eapply (fix_preserves _ _ _
(fun slot : UintN.t ⇒ CS.Loc.Impl.t.offset location ≤ Z.of_N slot)
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))).
- intros slot recur Hge IH.
apply (dependent_bool_property
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))).
+ intros Hlt st Hst.
eapply IH.
× change (CS.Loc.Impl.t.offset location ≤ Z.of_N (N.add slot 1%N)). lia.
× rewrite Hkind in Hlt.
cbn [ConstructorRecords_kind.kind.Static.word_width] in Hlt.
replace (CT.U256.op_tildedollar w) with w in Hlt
by (symmetry; apply u256_of_int_small; lia).
replace (CT.U256.op_plus (CS.Loc.Impl.t.offset location) w)
with (CS.Loc.Impl.t.offset location + w) in Hlt
by (symmetry; apply word_add_small; lia).
rewrite u256_to_uint_nonneg in Hlt by lia.
change (N.ltb slot (Z.to_N (CS.Loc.Impl.t.offset location + w)) = true) in Hlt.
apply N.ltb_lt in Hlt.
assert (Hslot_lt : Z.of_N slot < CS.Loc.Impl.t.offset location + w) by lia.
replace (CT.U256.of_uint_exn slot) with (CS.Loc.Impl.t.offset location + (Z.of_N slot - CS.Loc.Impl.t.offset location)).
-- apply Hstep; [lia | exact Hst].
-- replace (CS.Loc.Impl.t.offset location + (Z.of_N slot - CS.Loc.Impl.t.offset location))
with (Z.of_N slot) by lia.
symmetry; apply u256_of_uint_small; lia.
+ intros _ st Hst. exact Hst.
- rewrite u256_to_uint_nonneg by exact Hoff. lia.
Qed.
Theorem storage_clear_find_other (a : Set) (location : @CS.Loc.Impl.t F a) (w : int) query state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
CS.Loc.Impl.t.kind location = CT.Static {| CT.kind.Static.word_width := w |} →
0 ≤ CS.Loc.Impl.t.offset location →
0 ≤ w →
CS.Loc.Impl.t.offset location + w < 2^256 →
(∀ i, 0 ≤ i < w → CT.U256.to_repr (CS.Loc.Impl.t.offset location + i) ≠ query) →
let post := snd (CS.Loc.clear location state) in
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state post) owner)) =
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hkind Hoff Hw Hbound Hdisj post. unfold post.
refine (proj2 (clear_preserves_state_predicate_bounded
(fun s ⇒
(stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state s) owner)) ∧
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state s) owner))) ∧
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state s) owner)) =
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state state) owner)))
a location w state Hkind Hoff Hw Hbound _ _)).
- intros i s Hi [[Hs_map [Hs_wf Hs_pos]] Hfind].
pose proof (account_storage_update_retains_fields
(Chain.Ethereum.Storage.add (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i)) zeroBytes)
s Hs_map owner) as [Hbal _].
pose proof (account_storage_update_owner_storage
(Chain.Ethereum.Storage.add (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i)) zeroBytes)
s Hs_map Hs_pos) as Hstor.
cbn zeta in Hbal, Hstor.
split; [split; [|split]|].
+ exact (account_storage_update_wellformed _ s Hs_map).
+ rewrite Hstor. apply storage_add_wellformed. exact Hs_wf.
+ rewrite Hbal. exact Hs_pos.
+ rewrite Hstor.
rewrite storage_find_add_other; [exact Hfind | exact Hs_wf | exact (Hdisj i Hi)].
- repeat split; assumption.
Qed.
Theorem storage_write_owner_storage (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
A.t.storage (readAccount (TS.t.world_state post) owner) =
writeStorageWords (CS.Loc.Impl.t.offset location)
(CT.enc (CS.Loc.Impl.t.typ location) value)
(A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hpos post. unfold post.
destruct location as [typ kind offset].
exact (account_storage_update_owner_storage
(writeStorageWords offset (CT.enc typ value)) state Hmap Hpos).
Qed.
Theorem storage_write_find_other (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) query state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 ≤ CS.Loc.Impl.t.offset location →
CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ 2^256 →
(∀ i,
0 ≤ i < Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) →
CT.U256.to_repr (CS.Loc.Impl.t.offset location + i) ≠ query) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state post) owner)) =
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hoff Hbound Hdisj post. unfold post.
pose proof (storage_write_owner_storage a fallback location value state Hmap Hpos) as Hstor.
cbn zeta in Hstor. rewrite Hstor.
apply storage_find_writeStorageWords_other; assumption.
Qed.
Theorem storage_write_find_nth (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) i state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 ≤ CS.Loc.Impl.t.offset location →
CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ 2^256 →
0 ≤ i < Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
Chain.Ethereum.Storage.find (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i))
(A.t.storage (readAccount (TS.t.world_state post) owner)) =
Lists.List.nth (Z.to_nat i) (CT.enc (CS.Loc.Impl.t.typ location) value) zeroBytes.
Proof.
intros Hmap Hwf Hpos Hoff Hbound Hi post. unfold post.
pose proof (storage_write_owner_storage a fallback location value state Hmap Hpos) as Hstor.
cbn zeta in Hstor. rewrite Hstor.
apply storage_find_writeStorageWords_nth; assumption.
Qed.
Definition storageAgreesOn (offset w : Z) (s1 s2 : Chain.Ethereum.Storage.t) : Prop :=
∀ i, 0 ≤ i < w →
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + i)) s1 =
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + i)) s2.
Lemma storageAgreesOn_refl offset w s :
storageAgreesOn offset w s s.
Proof.
intros i _. reflexivity.
Qed.
Lemma storageAgreesOn_sym offset w s1 s2 :
storageAgreesOn offset w s1 s2 →
storageAgreesOn offset w s2 s1.
Proof.
intros H i Hi. symmetry. exact (H i Hi).
Qed.
Lemma storageAgreesOn_trans offset w s1 s2 s3 :
storageAgreesOn offset w s1 s2 →
storageAgreesOn offset w s2 s3 →
storageAgreesOn offset w s1 s3.
Proof.
intros H12 H23 i Hi. rewrite (H12 i Hi). exact (H23 i Hi).
Qed.
Lemma storageAgreesOn_sub offset1 w1 offset2 w2 s1 s2 :
offset1 ≤ offset2 →
offset2 + w2 ≤ offset1 + w1 →
storageAgreesOn offset1 w1 s1 s2 →
storageAgreesOn offset2 w2 s1 s2.
Proof.
intros Hlo Hhi Hagree i Hi.
replace (offset2 + i) with (offset1 + (offset2 - offset1 + i)) by lia.
apply Hagree. lia.
Qed.
Lemma storageAgreesOn_write_other (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) offset2 w2 state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 ≤ CS.Loc.Impl.t.offset location →
CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ 2^256 →
0 ≤ offset2 →
offset2 + w2 ≤ 2^256 →
(CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ offset2 ∨
offset2 + w2 ≤ CS.Loc.Impl.t.offset location) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
storageAgreesOn offset2 w2
(A.t.storage (readAccount (TS.t.world_state post) owner))
(A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hoff1 Hbound1 Hoff2 Hbound2 Hdisj post j Hj.
unfold post.
apply storage_write_find_other; try assumption.
intros i Hi Hcol.
apply word_encoding_injective in Hcol;
[lia | unfold wordRange; change (0 ≤ CS.Loc.Impl.t.offset location + i < 2^256); lia
| unfold wordRange; change (0 ≤ offset2 + j < 2^256); lia].
Qed.
Lemma storageAgreesOn_clear_other (a : Set)
(location : @CS.Loc.Impl.t F a) (w1 : int) offset2 w2 state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
CS.Loc.Impl.t.kind location = CT.Static {| CT.kind.Static.word_width := w1 |} →
0 ≤ CS.Loc.Impl.t.offset location →
0 ≤ w1 →
CS.Loc.Impl.t.offset location + w1 < 2^256 →
0 ≤ offset2 →
offset2 + w2 ≤ 2^256 →
(CS.Loc.Impl.t.offset location + w1 ≤ offset2 ∨ offset2 + w2 ≤ CS.Loc.Impl.t.offset location) →
let post := snd (CS.Loc.clear location state) in
storageAgreesOn offset2 w2
(A.t.storage (readAccount (TS.t.world_state post) owner))
(A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hkind Hoff1 Hw1 Hbound1 Hoff2 Hbound2 Hdisj post j Hj.
unfold post.
eapply storage_clear_find_other; try eassumption.
intros i Hi Hcol.
apply word_encoding_injective in Hcol;
[lia | unfold wordRange; change (0 ≤ CS.Loc.Impl.t.offset location + i < 2^256); lia
| unfold wordRange; change (0 ≤ offset2 + j < 2^256); lia].
Qed.
Lemma loc_get_ext (a : Set) fallback (location : @CS.Loc.Impl.t F a) state1 state2 :
CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner))
(CS.Loc.Impl.t.offset location)) =
CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner))
(CS.Loc.Impl.t.offset location)) →
Lens.t.get (CS.Loc.lens fallback location) state1 =
Lens.t.get (CS.Loc.lens fallback location) state2.
Proof.
destruct location as [typ kind offset].
cbn [CS.Loc.Impl.t.typ CS.Loc.Impl.t.offset].
intro Hdec.
change (fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner)) offset))) =
fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner)) offset)))).
f_equal. f_equal. exact Hdec.
Qed.
Lemma take_words_1_storage s offset :
0 ≤ offset →
offset + 1 < 2^256 →
CT.Input_view.take_words 1 (CT.Input_view.of_storage s offset) =
Some (Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s,
CT.Input_view.of_storage s (offset + 1)).
Proof.
intros Hoff Hbound.
change (Some (Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s,
CT.Input_view.of_storage s (CT.U256.op_plus offset CT.U256.one)) =
Some (Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s,
CT.Input_view.of_storage s (offset + 1))).
replace (CT.U256.op_plus offset CT.U256.one) with (offset + 1)
by (symmetry; apply word_add_small; change (0 ≤ offset + 1 < 2^256); lia).
reflexivity.
Qed.
Lemma take_words_2_storage s offset :
0 ≤ offset →
offset + 2 < 2^256 →
CT.Input_view.take_words 2 (CT.Input_view.of_storage s offset) =
Some (Strings.String.append
(Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s)
(Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + 1)) s),
CT.Input_view.of_storage s (offset + 2)).
Proof.
intros Hoff Hbound.
change (Some (Strings.String.append
(Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s)
(Chain.Ethereum.Storage.find (CT.U256.to_repr (CT.U256.op_plus offset CT.U256.one)) s),
CT.Input_view.of_storage s (CT.U256.op_plus (CT.U256.op_plus offset CT.U256.one) CT.U256.one)) =
Some (Strings.String.append
(Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s)
(Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + 1)) s),
CT.Input_view.of_storage s (offset + 2))).
replace (CT.U256.op_plus offset CT.U256.one) with (offset + 1)
by (symmetry; apply word_add_small; change (0 ≤ offset + 1 < 2^256); lia).
replace (CT.U256.op_plus (offset + 1) CT.U256.one) with (offset + 2)
by (replace (offset + 2) with ((offset + 1) + 1) by lia;
symmetry; apply word_add_small; change (0 ≤ (offset + 1) + 1 < 2^256); lia).
reflexivity.
Qed.
Definition sameDecodeResult {A : Set} (s1 s2 : Chain.Ethereum.Storage.t) (next_offset : Z)
(r1 r2 : sum (A × CT.Input_view.t) CT.decode_error) : Prop :=
(∃ v,
r1 = inl (v, CT.Input_view.of_storage s1 next_offset) ∧
r2 = inl (v, CT.Input_view.of_storage s2 next_offset)) ∨
(∃ err, r1 = inr err ∧ r2 = inr err).
Lemma sameDecodeResult_get_ok {A : Set} fallback s1 s2 next_offset
(r1 r2 : sum (A × CT.Input_view.t) CT.decode_error) :
sameDecodeResult s1 s2 next_offset r1 r2 →
fst (Result.get_ok fallback r1) = fst (Result.get_ok fallback r2).
Proof.
intros [[v [-> ->]] | [err [-> ->]]]; reflexivity.
Qed.
Lemma loc_get_sameDecodeResult (a : Set) fallback (location : @CS.Loc.Impl.t F a)
next_offset state1 state2 :
sameDecodeResult
(A.t.storage (readAccount (TS.t.world_state state1) owner))
(A.t.storage (readAccount (TS.t.world_state state2) owner))
next_offset
(CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner))
(CS.Loc.Impl.t.offset location)))
(CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner))
(CS.Loc.Impl.t.offset location))) →
Lens.t.get (CS.Loc.lens fallback location) state1 =
Lens.t.get (CS.Loc.lens fallback location) state2.
Proof.
destruct location as [typ kind offset].
cbn [CS.Loc.Impl.t.typ CS.Loc.Impl.t.offset].
intro Hsame.
change (fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner)) offset))) =
fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner)) offset)))).
eapply sameDecodeResult_get_ok. exact Hsame.
Qed.
Lemma fixed_bytes_sameDecodeResult_1 fb s1 s2 offset :
(OCamlInt.div (H := RocqOfOCaml.Opaque.legacy_witness CT._rocq_fallback_align_1)
(CT.bytes_to_padded_bytes fb.(CT.fixed_bytes.byte_width)) 32) = 1 →
0 ≤ offset →
offset + 1 < 2^256 →
storageAgreesOn offset 1 s1 s2 →
sameDecodeResult s1 s2 (offset + 1)
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s2 offset)).
Proof.
destruct fb as [alignment bw].
cbn [CT.fixed_bytes.byte_width].
intros Hnw Hoff Hbound Hagree.
unfold CT.dec, CT.fixed_bytes_value.
cbn [CT.typ.decode].
rewrite Hnw.
rewrite (take_words_1_storage s1 offset Hoff Hbound).
rewrite (take_words_1_storage s2 offset Hoff Hbound).
pose proof (Hagree 0 ltac:(lia)) as H0.
replace (offset + 0) with offset in H0 by lia.
rewrite H0.
cbn [Result.Option.or_fail Result.op_letdollar].
destruct alignment; left; eexists; split; reflexivity.
Qed.
Lemma fixed_bytes_sameDecodeResult_2 fb s1 s2 offset :
(OCamlInt.div (H := RocqOfOCaml.Opaque.legacy_witness CT._rocq_fallback_align_1)
(CT.bytes_to_padded_bytes fb.(CT.fixed_bytes.byte_width)) 32) = 2 →
0 ≤ offset →
offset + 2 < 2^256 →
storageAgreesOn offset 2 s1 s2 →
sameDecodeResult s1 s2 (offset + 2)
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s2 offset)).
Proof.
destruct fb as [alignment bw].
cbn [CT.fixed_bytes.byte_width].
intros Hnw Hoff Hbound Hagree.
unfold CT.dec, CT.fixed_bytes_value.
cbn [CT.typ.decode].
rewrite Hnw.
rewrite (take_words_2_storage s1 offset Hoff Hbound).
rewrite (take_words_2_storage s2 offset Hoff Hbound).
pose proof (Hagree 0 ltac:(lia)) as H0.
pose proof (Hagree 1 ltac:(lia)) as H1.
replace (offset + 0) with offset in H0 by lia.
rewrite H0, H1.
cbn [Result.Option.or_fail Result.op_letdollar].
destruct alignment; left; eexists; split; reflexivity.
Qed.
Lemma mapped_sameDecodeResult {a repr : Set} (m : CT.mapping a repr) s1 s2 offset next_offset :
sameDecodeResult s1 s2 next_offset
(CT.dec m.(CT.mapping.repr) (CT.Input_view.of_storage s1 offset))
(CT.dec m.(CT.mapping.repr) (CT.Input_view.of_storage s2 offset)) →
sameDecodeResult s1 s2 next_offset
(CT.dec (CT.mapped m) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.mapped m) (CT.Input_view.of_storage s2 offset)).
Proof.
intros [[r [H1 H2]] | [err [H1 H2]]];
unfold CT.dec, CT.mapped; cbn [CT.typ.decode];
change (CT.mapping.repr m).(CT.typ.decode) with (CT.dec m.(CT.mapping.repr));
rewrite H1, H2; unfold Result.op_letdollar, Result.bind; cbn.
- destruct (m.(CT.mapping.decode) r) as [v|msg] eqn:Hdec.
+ left. ∃ v. split; reflexivity.
+ right. eexists. split; reflexivity.
- right. ∃ err. split; reflexivity.
Qed.
Lemma cons_sameDecodeParts {A B : Set} (hd : CT.typ A) (tl : CT.tup B)
w_hd s1 s2 offset next_offset tail_offset :
CT.kind_value hd = CT.Static {| CT.kind.Static.word_width := w_hd |} →
sameDecodeResult s1 s2 (offset + w_hd)
(CT.dec hd (CT.Input_view.of_storage s1 offset))
(CT.dec hd (CT.Input_view.of_storage s2 offset)) →
sameDecodeResult s1 s2 tail_offset
(tl.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 (offset + w_hd))
(CT.Input_view.of_storage s1 next_offset))
(tl.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 (offset + w_hd))
(CT.Input_view.of_storage s2 next_offset)) →
sameDecodeResult s1 s2 tail_offset
((CT.cons hd tl).(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 offset)
(CT.Input_view.of_storage s1 next_offset))
((CT.cons hd tl).(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 offset)
(CT.Input_view.of_storage s2 next_offset)).
Proof.
intros Hkind Hhd Htl.
unfold CT.cons. cbn [CT.tup.decode_parts].
rewrite Hkind.
destruct Hhd as [[elt [Hhd1 Hhd2]] | [err [Hhd1 Hhd2]]].
- rewrite Hhd1, Hhd2. unfold Result.op_letdollar, Result.bind. cbn.
destruct Htl as [[elts [Htl1 Htl2]] | [err [Htl1 Htl2]]].
+ rewrite Htl1, Htl2. cbn.
left. ∃ (elt, elts). split; reflexivity.
+ rewrite Htl1, Htl2. cbn.
right. ∃ err. split; reflexivity.
- rewrite Hhd1, Hhd2. unfold Result.op_letdollar, Result.bind. cbn.
right. ∃ err. split; reflexivity.
Qed.
Lemma tup0_sameDecodeParts s1 s2 head_offset tail_offset :
sameDecodeResult s1 s2 tail_offset
(CT.tup0.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 head_offset)
(CT.Input_view.of_storage s1 tail_offset))
(CT.tup0.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 head_offset)
(CT.Input_view.of_storage s2 tail_offset)).
Proof.
left. ∃ tt. split; reflexivity.
Qed.
Lemma map_tuple_sameDecodeParts {A B : Set} (enc_fn : A → B) (dec_fn : B → A) (t : CT.tup B)
s1 s2 head_offset tail_offset res_offset :
sameDecodeResult s1 s2 res_offset
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 head_offset)
(CT.Input_view.of_storage s1 tail_offset))
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 head_offset)
(CT.Input_view.of_storage s2 tail_offset)) →
sameDecodeResult s1 s2 res_offset
((CT.map_tuple enc_fn dec_fn t).(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 head_offset)
(CT.Input_view.of_storage s1 tail_offset))
((CT.map_tuple enc_fn dec_fn t).(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 head_offset)
(CT.Input_view.of_storage s2 tail_offset)).
Proof.
intros [[v [H1 H2]] | [err [H1 H2]]];
unfold CT.map_tuple; cbn [CT.tup.decode_parts];
rewrite H1, H2; cbn [Result.map Result.op_letdollar].
- left. ∃ (dec_fn v). split; reflexivity.
- right. ∃ err. split; reflexivity.
Qed.
Lemma tuple_sameDecodeResult {A : Set} (t : CT.tup A) w s1 s2 offset :
t.(CT.tup.header_words) = w →
0 ≤ w →
0 ≤ offset →
offset + w < 2^256 →
sameDecodeResult s1 s2 (offset + w)
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 offset)
(CT.Input_view.of_storage s1 (offset + w)))
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 offset)
(CT.Input_view.of_storage s2 (offset + w))) →
sameDecodeResult s1 s2 (offset + w)
(CT.dec (CT.tuple t) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.tuple t) (CT.Input_view.of_storage s2 offset)).
Proof.
intros Hhw Hw Hoff Hbound Hparts.
unfold CT.dec, CT.tuple, CT.dec_tuple.
cbn [CT.typ.decode].
rewrite Hhw.
unfold CT.Input_view.drop, CT.Input_view.of_storage.
replace (Numeric.U256.of_int w) with w
by (symmetry; apply u256_of_int_small; lia).
replace (Numeric.U256.op_plus offset w) with (offset + w)
by (symmetry; apply word_add_small; lia).
exact Hparts.
Qed.
End Storage.
Print Assumptions storage_write_retains_account_fields.
Print Assumptions storage_write_preserves_positive_balance.
Print Assumptions account_storage_update_retains_fields.
Print Assumptions storage_write_retains_account_fields_unconditional.
Print Assumptions storage_clear_retains_account_fields.
Print Assumptions account_storage_update_preserves_supply.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Contract._Type MonadVM.Contract.Storage
QedkConcrete.stringmaps QedkConcrete.wordrange QedkConcrete.bytewords
QedkConcrete.bytecodec QedkConcrete.worldaccounts QedkConcrete.stakingretention
QedkConcrete.supply.
Import ListNotations.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module CT := Contract._Type.
Module CS := Contract.Storage.Make.
Module TS := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module U := Numeric.U256.
Local Abbreviation zeroBytes :=
(@Byte_string.Fixed.Fixed_result.zeros _ _ _ Byte_string.B32).
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (W.account None addr)).
Section Storage.
Context {F : CS.FArgs}.
Local Abbreviation owner := (@CS.Addr F).(Contract.Storage.Make_Addr_signature.address).
Theorem storage_write_retains_account_fields (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query).
Proof.
intros Hmap Hpositive. cbn zeta. intro query.
destruct location as [typ kind offset].
unfold CS.Loc.lens.
cbn [Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.t.get Lens.t.set TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- apply String.eqb_eq in Hquery. subst query.
rewrite positive_balance_not_empty by exact Hpositive.
repeat split; reflexivity.
- repeat split; reflexivity.
Qed.
Corollary storage_write_preserves_positive_balance (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount
(TS.t.world_state (Lens.t.set (CS.Loc.lens fallback location) value state)) owner)).
Proof.
intros Hmap Hpositive.
pose proof (storage_write_retains_account_fields a fallback location value state
Hmap Hpositive owner) as [Hbalance _].
rewrite Hbalance. exact Hpositive.
Qed.
Lemma account_empty_fields account :
@A.is_empty account = true →
A.t.balance account = A.t.balance (@A.empty) ∧
A.t.nonce account = A.t.nonce (@A.empty) ∧
A.t.code account = A.t.code (@A.empty).
Proof.
unfold A.is_empty. intro Hempty.
apply Bool.andb_true_iff in Hempty as [Hbal Hnc].
apply Bool.andb_true_iff in Hnc as [Hnonce Hcode].
change (Z.eqb (match Z.compare (A.t.balance account) 0 with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hbal.
change (Z.eqb (match Z.compare (A.t.nonce account) 0 with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hnonce.
change (Z.eqb (OCamlString.compare (A.t.code account) EmptyString) 0 = true) in Hcode.
rewrite OCamlString.compare_matches_structural in Hcode.
unfold OCamlString.compare_structural in Hcode.
split; [|split].
- destruct (Z.compare_spec (A.t.balance account) 0); cbn in Hbal; [ assumption | discriminate | discriminate ].
- destruct (Z.compare_spec (A.t.nonce account) 0); cbn in Hnonce; [ assumption | discriminate | discriminate ].
- destruct (String.eqb (A.t.code account) EmptyString) eqn:Heq;
[ apply String.eqb_eq; exact Heq | destruct (Basics.String.ltb (A.t.code account) EmptyString); discriminate ].
Qed.
Theorem account_storage_update_retains_fields (f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state post) query) =
A.t.storage (readAccount (TS.t.world_state state) query)).
Proof.
intros Hmap post query. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- apply String.eqb_eq in Hquery. subst query.
destruct (A.is_empty _) eqn:Hempty.
+ destruct (account_empty_fields _ Hempty) as [Hbal [Hnonce Hcode]].
cbn [A.t.with_storage A.t.balance A.t.nonce A.t.code] in Hbal, Hnonce, Hcode.
rewrite <- Hbal, <- Hnonce, <- Hcode.
repeat split; try reflexivity; contradiction.
+ repeat split; try reflexivity; contradiction.
- apply String.eqb_neq in Hquery.
repeat split; reflexivity.
Qed.
Theorem storage_write_retains_account_fields_unconditional (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state post) query) =
A.t.storage (readAccount (TS.t.world_state state) query)).
Proof.
intros Hmap post query. unfold post.
destruct location as [typ kind offset].
exact (account_storage_update_retains_fields
(fun s ⇒ fst (OCamlList.fold_left
(fun '(storage, i_value) val ⇒
let key := CT.U256.to_repr (CT.U256.op_plus offset i_value) in
let storage := Chain.Ethereum.Storage.add key val storage in
let i_value := CT.U256.op_plus i_value CT.U256.one in
(storage, i_value))
(s, CT.U256.zero) (CT.enc typ value))) state Hmap query).
Qed.
Lemma fix_preserves {A B : Set} (R : A → A → Prop) (wf : well_founded R)
(body : ∀ x, (∀ y, R y x → B) → B)
(I : A → Prop) (P : B → Prop) :
(∀ x recur, I x → (∀ y edge, I y → P (recur y edge)) → P (body x recur)) →
∀ x, I x → P (Init.Wf.Fix wf (fun _ ⇒ B) body x).
Proof.
intro Hbody.
assert (Hfix : ∀ x (access : Acc R x), I x →
P (Init.Wf.Fix_F (fun _ ⇒ B) body access)).
{ fix IH 2. intros x [children] Hx.
cbn [Init.Wf.Fix_F]. apply Hbody; [exact Hx|].
intros y edge Hy. apply IH; exact Hy. }
intros x Hx. apply Hfix; exact Hx.
Qed.
Lemma dependent_bool_property {A : Type} (P : A → Prop) (test : bool)
(yes : test = true → A) (no : test = false → A) :
(∀ eq, P (yes eq)) → (∀ eq, P (no eq)) →
P ((if test as b return (test = b → A) then yes else no) eq_refl).
Proof. intros Hyes Hno. destruct test; [apply Hyes|apply Hno]. Qed.
Lemma clear_preserves_state_predicate (P : TS.t → Prop) (a : Set)
(location : @CS.Loc.Impl.t F a) state :
(∀ key s, P s →
P (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage)
(Chain.Ethereum.Storage.add key zeroBytes) s))) →
P state →
P (snd (CS.Loc.clear location state)).
Proof.
intros Hstep Hinit.
unfold CS.Loc.clear.
revert state Hinit.
eapply (fix_preserves _ _ _ (fun _ ⇒ True)
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))); [|exact I].
intros slot recur _ IH.
apply (dependent_bool_property
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))).
- intros Hlt st Hst.
eapply IH; [exact I|].
exact (Hstep (CT.U256.to_repr (CT.U256.of_uint_exn slot)) st Hst).
- intros Hge st Hst. exact Hst.
Qed.
Theorem account_storage_update_wellformed (f : Chain.Ethereum.Storage.t → Chain.Ethereum.Storage.t) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
stringmaps.WellFormed (W.t.accounts (TS.t.world_state post)).
Proof.
intros Hmap post. unfold post.
exact (@account_write_wellformed false (TS.t.world_state state) owner
(A.t.with_storage (f (A.t.storage (readAccount (TS.t.world_state state) owner)))
(readAccount (TS.t.world_state state) owner)) Hmap).
Qed.
Theorem storage_clear_retains_account_fields (a : Set)
(location : @CS.Loc.Impl.t F a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
let post := snd (CS.Loc.clear location state) in
∀ query,
A.t.balance (readAccount (TS.t.world_state post) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state post) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state post) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state post) query) =
A.t.storage (readAccount (TS.t.world_state state) query)).
Proof.
intros Hmap post query. unfold post.
refine (proj2 (clear_preserves_state_predicate
(fun s ⇒
stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
(A.t.balance (readAccount (TS.t.world_state s) query) =
A.t.balance (readAccount (TS.t.world_state state) query) ∧
A.t.nonce (readAccount (TS.t.world_state s) query) =
A.t.nonce (readAccount (TS.t.world_state state) query) ∧
A.t.code (readAccount (TS.t.world_state s) query) =
A.t.code (readAccount (TS.t.world_state state) query) ∧
(owner ≠ query →
A.t.storage (readAccount (TS.t.world_state s) query) =
A.t.storage (readAccount (TS.t.world_state state) query))))
a location state _ _)).
- intros key s [Hs [Hbal [Hnonce [Hcode Hstor]]]].
split.
+ exact (account_storage_update_wellformed (Chain.Ethereum.Storage.add key zeroBytes) s Hs).
+ destruct (account_storage_update_retains_fields (Chain.Ethereum.Storage.add key zeroBytes) s Hs query)
as [Hb [Hn [Hc Hst]]].
rewrite Hb, Hn, Hc.
refine (conj Hbal (conj Hnonce (conj Hcode _))).
intro Hneq. rewrite (Hst Hneq). exact (Hstor Hneq).
- split; [ exact Hmap | repeat split; reflexivity ].
Qed.
Theorem account_storage_update_preserves_supply (f : list (string × string) → list (string × string)) state :
balanceInvariant (TS.t.world_state state) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
balanceInvariant (TS.t.world_state post) ∧
totalBalance (TS.t.world_state post) = totalBalance (TS.t.world_state state).
Proof.
intros Hinv post. unfold post.
eapply (@account_write_preserves_supply false (TS.t.world_state state) owner
(A.t.with_storage (f (A.t.storage (readAccount (TS.t.world_state state) owner)))
(readAccount (TS.t.world_state state) owner))).
- exact Hinv.
- reflexivity.
Qed.
Theorem storage_write_preserves_supply (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
balanceInvariant (TS.t.world_state state) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
balanceInvariant (TS.t.world_state post) ∧
totalBalance (TS.t.world_state post) = totalBalance (TS.t.world_state state).
Proof.
intros Hinv post. unfold post.
destruct location as [typ kind offset].
exact (account_storage_update_preserves_supply
(fun s ⇒ fst (OCamlList.fold_left
(fun '(storage, i_value) val ⇒
let key := CT.U256.to_repr (CT.U256.op_plus offset i_value) in
let storage := Chain.Ethereum.Storage.add key val storage in
let i_value := CT.U256.op_plus i_value CT.U256.one in
(storage, i_value))
(s, CT.U256.zero) (CT.enc typ value))) state Hinv).
Qed.
Theorem storage_clear_preserves_supply (a : Set)
(location : @CS.Loc.Impl.t F a) state :
balanceInvariant (TS.t.world_state state) →
let post := snd (CS.Loc.clear location state) in
balanceInvariant (TS.t.world_state post) ∧
totalBalance (TS.t.world_state post) = totalBalance (TS.t.world_state state).
Proof.
intros Hinv post. unfold post.
refine (clear_preserves_state_predicate
(fun s ⇒
balanceInvariant (TS.t.world_state s) ∧
totalBalance (TS.t.world_state s) = totalBalance (TS.t.world_state state))
a location state _ _).
- intros key s [Hbinv Htot].
destruct (account_storage_update_preserves_supply
(Chain.Ethereum.Storage.add key zeroBytes) s Hbinv) as [Hbinv' Htot'].
split; [exact Hbinv'|rewrite Htot'; exact Htot].
- split; [exact Hinv|reflexivity].
Qed.
Lemma b32_equal_true x y :
Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) x y = true → x = y.
Proof.
change (Z.eqb (OCamlString.compare x y) 0 = true → x = y).
rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural.
destruct (String.eqb x y) eqn:Heq.
- intro. apply String.eqb_eq. exact Heq.
- destruct (Basics.String.ltb x y); intro H; discriminate H.
Qed.
Definition storageWords (s : Chain.Ethereum.Storage.t) : Prop :=
Forall (fun entry ⇒ OCamlString.length (snd entry) = 32) s.
Lemma map_add_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key value :
P value → Forall (fun pair ⇒ P (snd pair)) entries →
Forall (fun pair ⇒ P (snd pair)) (OCamlMap.Make.add order key value entries).
Proof.
intros Hvalue Hentries. induction Hentries as [|[k v] rest Hv Hrest IH].
- constructor; [exact Hvalue|constructor].
- cbn [OCamlMap.Make.add].
destruct (OCamlMap.Make.key_before order key k).
+ constructor; [exact Hvalue|constructor; assumption].
+ destruct (OCamlMap.Make.key_equal order key k); constructor; assumption.
Qed.
Lemma map_remove_value_property {Key Value : Set} (order : OCamlMap.OrderedType (t := Key))
(P : Value → Prop) entries key :
Forall (fun entry ⇒ P (snd entry)) entries →
Forall (fun entry ⇒ P (snd entry)) (OCamlMap.Make.remove order key entries).
Proof.
intro Hentries. induction Hentries as [|[k v] rest Hv Hrest IH]; [constructor|].
cbn [OCamlMap.Make.remove]. destruct (OCamlMap.Make.key_before order key k).
- constructor; assumption.
- destruct (OCamlMap.Make.key_equal order key k); [exact Hrest|constructor; assumption].
Qed.
Lemma storage_add_wellformed key val (s : Chain.Ethereum.Storage.t) :
stringmaps.WellFormed s →
stringmaps.WellFormed (Chain.Ethereum.Storage.add key val s).
Proof.
intro Hwf. unfold Chain.Ethereum.Storage.add.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes).
- exact (remove_wellformed s key Hwf).
- exact (add_wellformed s key val Hwf).
Qed.
Lemma storage_add_words key val (s : Chain.Ethereum.Storage.t) :
storageWords s →
OCamlString.length val = 32 →
storageWords (Chain.Ethereum.Storage.add key val s).
Proof.
intros Hwords Hval. unfold Chain.Ethereum.Storage.add, storageWords in ×.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes).
- exact (map_remove_value_property stringmaps.stringOrder
(fun v ⇒ OCamlString.length v = 32) s key Hwords).
- exact (map_add_value_property stringmaps.stringOrder
(fun v ⇒ OCamlString.length v = 32) s key val Hval Hwords).
Qed.
Lemma storage_find_add_same key val (s : Chain.Ethereum.Storage.t) :
stringmaps.WellFormed s →
Chain.Ethereum.Storage.find key (Chain.Ethereum.Storage.add key val s) = val.
Proof.
intro Hwf. unfold Chain.Ethereum.Storage.find, Chain.Ethereum.Storage.add.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes) eqn:Hzero.
- apply b32_equal_true in Hzero. subst val.
change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) key
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.remove) key s))
with (OCamlMap.Make.find_opt stringOrder key (OCamlMap.Make.remove stringOrder key s)).
rewrite (find_remove_same s key Hwf). reflexivity.
- change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) key
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.add) key val s))
with (OCamlMap.Make.find_opt stringOrder key (OCamlMap.Make.add stringOrder key val s)).
rewrite (find_add_same s key val Hwf). reflexivity.
Qed.
Lemma storage_find_add_other key query val (s : Chain.Ethereum.Storage.t) :
stringmaps.WellFormed s →
key ≠ query →
Chain.Ethereum.Storage.find query (Chain.Ethereum.Storage.add key val s) =
Chain.Ethereum.Storage.find query s.
Proof.
intros Hwf Hneq. unfold Chain.Ethereum.Storage.find, Chain.Ethereum.Storage.add.
destruct (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_eq) val zeroBytes).
- change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) query
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.remove) key s))
with (OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.remove stringOrder key s)).
rewrite (find_remove_other s key query Hwf Hneq). reflexivity.
- change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.find_opt) query
(Byte_string.B32.(Byte_string.Fixed.Fixed_result.Map).(Byte_string.Fixed.Map.Map_signature.add) key val s))
with (OCamlMap.Make.find_opt stringOrder query (OCamlMap.Make.add stringOrder key val s)).
rewrite (find_add_other s key query val Hwf Hneq). reflexivity.
Qed.
Lemma word_add_small left right :
0 ≤ left + right < 2^256 →
@U.op_plus left right = left + right.
Proof.
intro Hrange.
change (Z.land (Z.shiftl (left + right) 0) (Z.ones 256) = left + right).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
apply Z.mod_small. exact Hrange.
Qed.
Definition writeStorageWordsFrom (offset idx : CT.U256.t) (words : list string)
(s : Chain.Ethereum.Storage.t) : Chain.Ethereum.Storage.t × CT.U256.t :=
OCamlList.fold_left
(fun '(storage, i_value) val ⇒
let key := CT.U256.to_repr (CT.U256.op_plus offset i_value) in
let storage := Chain.Ethereum.Storage.add key val storage in
let i_value := CT.U256.op_plus i_value CT.U256.one in
(storage, i_value))
(s, idx) words.
Definition writeStorageWords (offset : CT.U256.t) (words : list string)
(s : Chain.Ethereum.Storage.t) : Chain.Ethereum.Storage.t :=
fst (writeStorageWordsFrom offset CT.U256.zero words s).
Lemma writeStorageWordsFrom_wellformed offset idx words s :
stringmaps.WellFormed s →
stringmaps.WellFormed (fst (writeStorageWordsFrom offset idx words s)).
Proof.
revert idx s. induction words as [|word rest IH]; intros idx s Hwf.
- exact Hwf.
- cbn [writeStorageWordsFrom OCamlList.fold_left].
apply IH. apply storage_add_wellformed. exact Hwf.
Qed.
Lemma writeStorageWords_wellformed offset words s :
stringmaps.WellFormed s →
stringmaps.WellFormed (writeStorageWords offset words s).
Proof.
apply writeStorageWordsFrom_wellformed.
Qed.
Lemma writeStorageWordsFrom_words offset idx words s :
storageWords s →
Forall (fun w ⇒ OCamlString.length w = 32) words →
storageWords (fst (writeStorageWordsFrom offset idx words s)).
Proof.
revert idx s. induction words as [|word rest IH]; intros idx s Hwords Hfa.
- exact Hwords.
- inversion Hfa as [|? ? Hw Hrest]; subst.
cbn [writeStorageWordsFrom OCamlList.fold_left].
apply IH; [|exact Hrest].
apply storage_add_words; assumption.
Qed.
Lemma writeStorageWords_words offset words s :
storageWords s →
Forall (fun w ⇒ OCamlString.length w = 32) words →
storageWords (writeStorageWords offset words s).
Proof.
apply writeStorageWordsFrom_words.
Qed.
Lemma storage_find_writeStorageWordsFrom_other offset idx words query s :
stringmaps.WellFormed s →
0 ≤ offset →
0 ≤ idx →
offset + idx + Z.of_nat (Lists.List.length words) ≤ 2^256 →
(∀ i, 0 ≤ i < Z.of_nat (Lists.List.length words) →
CT.U256.to_repr (offset + idx + i) ≠ query) →
Chain.Ethereum.Storage.find query (fst (writeStorageWordsFrom offset idx words s)) =
Chain.Ethereum.Storage.find query s.
Proof.
revert idx s. induction words as [|word rest IH]; intros idx s Hwf Hoff Hidx Hbound Hdisj.
- reflexivity.
- cbn [writeStorageWordsFrom OCamlList.fold_left].
cbn [Lists.List.length] in Hbound, Hdisj.
rewrite Nat2Z.inj_succ in Hbound, Hdisj.
replace (CT.U256.op_plus offset idx) with (offset + idx)
by (symmetry; apply word_add_small; lia).
destruct rest as [|w2 rest'].
+ cbn [OCamlList.fold_left fst].
apply storage_find_add_other; [exact Hwf|].
specialize (Hdisj 0 ltac:(lia)).
replace (offset + idx + 0) with (offset + idx) in Hdisj by lia.
exact Hdisj.
+ assert (Hpos_rest : 1 ≤ Z.of_nat (Lists.List.length (w2 :: rest')))
by (cbn [Lists.List.length]; lia).
replace (CT.U256.op_plus idx CT.U256.one) with (idx + 1)
by (symmetry; apply word_add_small; change (0 ≤ idx + 1 < 2^256); lia).
change (Chain.Ethereum.Storage.find query
(fst (writeStorageWordsFrom offset (idx + 1) (w2 :: rest')
(Chain.Ethereum.Storage.add (CT.U256.to_repr (offset + idx)) word s))) =
Chain.Ethereum.Storage.find query s).
rewrite IH.
× apply storage_find_add_other; [exact Hwf|].
specialize (Hdisj 0 ltac:(lia)).
replace (offset + idx + 0) with (offset + idx) in Hdisj by lia.
exact Hdisj.
× apply storage_add_wellformed. exact Hwf.
× exact Hoff.
× lia.
× lia.
× intros i Hi.
replace (offset + (idx + 1) + i) with (offset + idx + (i + 1)) by lia.
apply Hdisj. lia.
Qed.
Lemma storage_find_writeStorageWords_other offset words query s :
stringmaps.WellFormed s →
0 ≤ offset →
offset + Z.of_nat (Lists.List.length words) ≤ 2^256 →
(∀ i, 0 ≤ i < Z.of_nat (Lists.List.length words) →
CT.U256.to_repr (offset + i) ≠ query) →
Chain.Ethereum.Storage.find query (writeStorageWords offset words s) =
Chain.Ethereum.Storage.find query s.
Proof.
intros Hwf Hoff Hbound Hdisj.
unfold writeStorageWords.
apply storage_find_writeStorageWordsFrom_other.
- exact Hwf.
- exact Hoff.
- change (0 ≤ 0). lia.
- change (offset + 0 + Z.of_nat (Lists.List.length words) ≤ 2^256). lia.
- intros i Hi. change (CT.U256.to_repr (offset + 0 + i) ≠ query).
replace (offset + 0 + i) with (offset + i) by lia.
exact (Hdisj i Hi).
Qed.
Lemma storage_find_writeStorageWordsFrom_nth offset idx words i s :
stringmaps.WellFormed s →
0 ≤ offset →
0 ≤ idx →
offset + idx + Z.of_nat (Lists.List.length words) ≤ 2^256 →
0 ≤ i < Z.of_nat (Lists.List.length words) →
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + idx + i))
(fst (writeStorageWordsFrom offset idx words s)) =
Lists.List.nth (Z.to_nat i) words zeroBytes.
Proof.
revert idx i s. induction words as [|word rest IH]; intros idx i s Hwf Hoff Hidx Hbound Hi.
- cbn [Lists.List.length] in Hi. lia.
- cbn [writeStorageWordsFrom OCamlList.fold_left].
cbn [Lists.List.length] in Hbound, Hi.
rewrite Nat2Z.inj_succ in Hbound, Hi.
replace (CT.U256.op_plus offset idx) with (offset + idx)
by (symmetry; apply word_add_small; lia).
destruct rest as [|w2 rest'].
+ cbn [Lists.List.length] in Hi. assert (i = 0) by lia. subst i.
replace (offset + idx + 0) with (offset + idx) by lia.
cbn [OCamlList.fold_left fst Z.to_nat Lists.List.nth].
apply storage_find_add_same. exact Hwf.
+ assert (Hpos_rest : 1 ≤ Z.of_nat (Lists.List.length (w2 :: rest')))
by (cbn [Lists.List.length]; lia).
replace (CT.U256.op_plus idx CT.U256.one) with (idx + 1)
by (symmetry; apply word_add_small; change (0 ≤ idx + 1 < 2^256); lia).
change (Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + idx + i))
(fst (writeStorageWordsFrom offset (idx + 1) (w2 :: rest')
(Chain.Ethereum.Storage.add (CT.U256.to_repr (offset + idx)) word s))) =
Lists.List.nth (Z.to_nat i) (word :: w2 :: rest') zeroBytes).
assert (i = 0 ∨ 1 ≤ i) as [->|Hpos] by lia.
× replace (offset + idx + 0) with (offset + idx) by lia.
cbn [Z.to_nat Lists.List.nth].
rewrite storage_find_writeStorageWordsFrom_other.
-- apply storage_find_add_same. exact Hwf.
-- apply storage_add_wellformed. exact Hwf.
-- exact Hoff.
-- lia.
-- lia.
-- intros j Hj Hcol.
apply word_encoding_injective in Hcol;
[lia | unfold wordRange; change (0 ≤ offset + (idx + 1) + j < 2^256); lia
| unfold wordRange; change (0 ≤ offset + idx < 2^256); lia].
× replace (offset + idx + i) with (offset + (idx + 1) + (i - 1)) by lia.
rewrite IH.
-- replace (Z.to_nat i) with (S (Z.to_nat (i - 1))) by lia.
reflexivity.
-- apply storage_add_wellformed. exact Hwf.
-- exact Hoff.
-- lia.
-- lia.
-- lia.
Qed.
Lemma storage_find_writeStorageWords_nth offset words i s :
stringmaps.WellFormed s →
0 ≤ offset →
offset + Z.of_nat (Lists.List.length words) ≤ 2^256 →
0 ≤ i < Z.of_nat (Lists.List.length words) →
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + i))
(writeStorageWords offset words s) =
Lists.List.nth (Z.to_nat i) words zeroBytes.
Proof.
intros Hwf Hoff Hbound Hi.
unfold writeStorageWords.
replace (offset + i) with (offset + CT.U256.zero + i) by (change CT.U256.zero with 0; lia).
apply storage_find_writeStorageWordsFrom_nth.
- exact Hwf.
- exact Hoff.
- change (0 ≤ 0). lia.
- change (offset + 0 + Z.of_nat (Lists.List.length words) ≤ 2^256). lia.
- exact Hi.
Qed.
Lemma account_storage_update_owner_storage
(f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
A.t.storage (readAccount (TS.t.world_state post) owner) =
f (A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hpositive post. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account owner Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
rewrite String.eqb_refl.
rewrite positive_balance_not_empty by exact Hpositive.
reflexivity.
Qed.
Lemma account_storage_update_preserves_storage_wf
(f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) query))) →
(stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
stringmaps.WellFormed (f (A.t.storage (readAccount (TS.t.world_state state) owner)))) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwf Hupd post query. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- destruct (A.is_empty _) eqn:Hempty.
+ exact stringmaps.empty_wellformed.
+ cbn [A.t.storage A.t.with_storage].
apply Hupd. exact (Hwf owner).
- exact (Hwf query).
Qed.
Lemma account_storage_update_preserves_storage_words
(f : list (string × string) → list (string × string)) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state state) query))) →
(storageWords (A.t.storage (readAccount (TS.t.world_state state) owner)) →
storageWords (f (A.t.storage (readAccount (TS.t.world_state state) owner)))) →
let post := snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state) in
∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwords Hupd post query. unfold post.
change (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f state))
with (State.op_pointcaretdollarlparenrparenltminus state
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage) f).
unfold State.op_pointcaretdollarlparenrparenltminus, Lens.modify, Lens.set, Lens.get.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set TS.account
TS.world_state TS.t.with_world_state TS.t.world_state A.storage].
unfold TS.world_state, A.storage. cbn beta iota zeta.
unfold TS.TLens.world_state, A.TLens.storage. cbn beta iota zeta.
cbn [Lens.modify Lens.Infix.op_pipeminusminus Lens.t.get Lens.t.set
TS.t.with_world_state TS.t.world_state].
match goal with
| |- context [Lens.t.set (W.account (Some false) owner) ?account ?world] ⇒
pose proof (worldaccounts.account_read_after_write false world owner account query Hmap) as Hread
end.
unfold State.op_pointcaretlparenrparenltminus in Hread. rewrite Hread.
destruct (String.eqb owner query) eqn:Hquery.
- destruct (A.is_empty _) eqn:Hempty.
+ unfold storageWords. cbn. constructor.
+ cbn [A.t.storage A.t.with_storage].
apply Hupd. exact (Hwords owner).
- exact (Hwords query).
Qed.
Theorem storage_write_preserves_storage_wf (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwf post query. unfold post.
destruct location as [typ kind offset].
apply (account_storage_update_preserves_storage_wf
(writeStorageWords offset (CT.enc typ value)) state Hmap Hwf).
intro Howner. apply writeStorageWords_wellformed. exact Howner.
Qed.
Theorem storage_clear_preserves_storage_wf (a : Set)
(location : @CS.Loc.Impl.t F a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := snd (CS.Loc.clear location state) in
∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwf post query. unfold post.
revert query.
refine (proj2 (clear_preserves_state_predicate
(fun s ⇒
stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
(∀ query, stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state s) query))))
a location state _ _)).
- intros key s [Hs_map Hs_wf].
split.
+ exact (account_storage_update_wellformed (Chain.Ethereum.Storage.add key zeroBytes) s Hs_map).
+ apply (account_storage_update_preserves_storage_wf
(Chain.Ethereum.Storage.add key zeroBytes) s Hs_map Hs_wf).
intro Howner. apply storage_add_wellformed. exact Howner.
- split; [exact Hmap | exact Hwf].
Qed.
Theorem storage_write_preserves_storage_words (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
Forall (fun w ⇒ OCamlString.length w = 32) (CT.enc (CS.Loc.Impl.t.typ location) value) →
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Henc Hwords post query. unfold post.
destruct location as [typ kind offset].
apply (account_storage_update_preserves_storage_words
(writeStorageWords offset (CT.enc typ value)) state Hmap Hwords).
intro Howner. apply writeStorageWords_words; assumption.
Qed.
Theorem storage_clear_preserves_storage_words (a : Set)
(location : @CS.Loc.Impl.t F a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state state) query))) →
let post := snd (CS.Loc.clear location state) in
∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state post) query)).
Proof.
intros Hmap Hwords post query. unfold post.
revert query.
refine (proj2 (clear_preserves_state_predicate
(fun s ⇒
stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
(∀ query, storageWords (A.t.storage (readAccount (TS.t.world_state s) query))))
a location state _ _)).
- intros key s [Hs_map Hs_words].
split.
+ exact (account_storage_update_wellformed (Chain.Ethereum.Storage.add key zeroBytes) s Hs_map).
+ apply (account_storage_update_preserves_storage_words
(Chain.Ethereum.Storage.add key zeroBytes) s Hs_map Hs_words).
intro Howner. apply storage_add_words; [exact Howner | vm_compute; reflexivity].
- split; [exact Hmap | exact Hwords].
Qed.
Lemma u256_of_int_small w :
0 ≤ w < 2^256 →
CT.U256.op_tildedollar w = w.
Proof.
intro Hw.
apply word_of_z. unfold wordRange. change (0 ≤ w < 2^256). exact Hw.
Qed.
Lemma u256_of_uint_small slot :
0 ≤ Z.of_N slot < 2^256 →
CT.U256.of_uint_exn slot = Z.of_N slot.
Proof.
intro Hslot.
apply word_of_z. unfold wordRange. change (0 ≤ Z.of_N slot < 2^256). exact Hslot.
Qed.
Lemma u256_to_uint_nonneg x :
0 ≤ x →
CT.U256.to_uint x = Z.to_N x.
Proof.
intro Hx.
change (match (if Z.leb 0 x then Some (Z.to_N x) else None) with
| Some r ⇒ r
| None ⇒ Support.UintN.fallback_uint
end = Z.to_N x).
destruct (Z.leb_spec0 0 x); [reflexivity | lia].
Qed.
Lemma clear_preserves_state_predicate_bounded (P : TS.t → Prop) (a : Set)
(location : @CS.Loc.Impl.t F a) (w : int) state :
CS.Loc.Impl.t.kind location = CT.Static {| CT.kind.Static.word_width := w |} →
0 ≤ CS.Loc.Impl.t.offset location →
0 ≤ w →
CS.Loc.Impl.t.offset location + w < 2^256 →
(∀ i s,
0 ≤ i < w →
P s →
P (snd (TS.M.(Monad.State.State_result.update_field)
(Lens.Infix.op_pipeminusminus (TS.account None owner) A.storage)
(Chain.Ethereum.Storage.add (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i)) zeroBytes) s))) →
P state →
P (snd (CS.Loc.clear location state)).
Proof.
intros Hkind Hoff Hw Hbound Hstep Hinit.
unfold CS.Loc.clear.
revert state Hinit.
eapply (fix_preserves _ _ _
(fun slot : UintN.t ⇒ CS.Loc.Impl.t.offset location ≤ Z.of_N slot)
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))).
- intros slot recur Hge IH.
apply (dependent_bool_property
(fun action : TS.M.(Monad.State.State_result.t) unit ⇒
∀ st, P st → P (snd (action st)))).
+ intros Hlt st Hst.
eapply IH.
× change (CS.Loc.Impl.t.offset location ≤ Z.of_N (N.add slot 1%N)). lia.
× rewrite Hkind in Hlt.
cbn [ConstructorRecords_kind.kind.Static.word_width] in Hlt.
replace (CT.U256.op_tildedollar w) with w in Hlt
by (symmetry; apply u256_of_int_small; lia).
replace (CT.U256.op_plus (CS.Loc.Impl.t.offset location) w)
with (CS.Loc.Impl.t.offset location + w) in Hlt
by (symmetry; apply word_add_small; lia).
rewrite u256_to_uint_nonneg in Hlt by lia.
change (N.ltb slot (Z.to_N (CS.Loc.Impl.t.offset location + w)) = true) in Hlt.
apply N.ltb_lt in Hlt.
assert (Hslot_lt : Z.of_N slot < CS.Loc.Impl.t.offset location + w) by lia.
replace (CT.U256.of_uint_exn slot) with (CS.Loc.Impl.t.offset location + (Z.of_N slot - CS.Loc.Impl.t.offset location)).
-- apply Hstep; [lia | exact Hst].
-- replace (CS.Loc.Impl.t.offset location + (Z.of_N slot - CS.Loc.Impl.t.offset location))
with (Z.of_N slot) by lia.
symmetry; apply u256_of_uint_small; lia.
+ intros _ st Hst. exact Hst.
- rewrite u256_to_uint_nonneg by exact Hoff. lia.
Qed.
Theorem storage_clear_find_other (a : Set) (location : @CS.Loc.Impl.t F a) (w : int) query state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
CS.Loc.Impl.t.kind location = CT.Static {| CT.kind.Static.word_width := w |} →
0 ≤ CS.Loc.Impl.t.offset location →
0 ≤ w →
CS.Loc.Impl.t.offset location + w < 2^256 →
(∀ i, 0 ≤ i < w → CT.U256.to_repr (CS.Loc.Impl.t.offset location + i) ≠ query) →
let post := snd (CS.Loc.clear location state) in
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state post) owner)) =
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hkind Hoff Hw Hbound Hdisj post. unfold post.
refine (proj2 (clear_preserves_state_predicate_bounded
(fun s ⇒
(stringmaps.WellFormed (W.t.accounts (TS.t.world_state s)) ∧
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state s) owner)) ∧
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state s) owner))) ∧
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state s) owner)) =
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state state) owner)))
a location w state Hkind Hoff Hw Hbound _ _)).
- intros i s Hi [[Hs_map [Hs_wf Hs_pos]] Hfind].
pose proof (account_storage_update_retains_fields
(Chain.Ethereum.Storage.add (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i)) zeroBytes)
s Hs_map owner) as [Hbal _].
pose proof (account_storage_update_owner_storage
(Chain.Ethereum.Storage.add (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i)) zeroBytes)
s Hs_map Hs_pos) as Hstor.
cbn zeta in Hbal, Hstor.
split; [split; [|split]|].
+ exact (account_storage_update_wellformed _ s Hs_map).
+ rewrite Hstor. apply storage_add_wellformed. exact Hs_wf.
+ rewrite Hbal. exact Hs_pos.
+ rewrite Hstor.
rewrite storage_find_add_other; [exact Hfind | exact Hs_wf | exact (Hdisj i Hi)].
- repeat split; assumption.
Qed.
Theorem storage_write_owner_storage (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
A.t.storage (readAccount (TS.t.world_state post) owner) =
writeStorageWords (CS.Loc.Impl.t.offset location)
(CT.enc (CS.Loc.Impl.t.typ location) value)
(A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hpos post. unfold post.
destruct location as [typ kind offset].
exact (account_storage_update_owner_storage
(writeStorageWords offset (CT.enc typ value)) state Hmap Hpos).
Qed.
Theorem storage_write_find_other (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) query state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 ≤ CS.Loc.Impl.t.offset location →
CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ 2^256 →
(∀ i,
0 ≤ i < Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) →
CT.U256.to_repr (CS.Loc.Impl.t.offset location + i) ≠ query) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state post) owner)) =
Chain.Ethereum.Storage.find query (A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hoff Hbound Hdisj post. unfold post.
pose proof (storage_write_owner_storage a fallback location value state Hmap Hpos) as Hstor.
cbn zeta in Hstor. rewrite Hstor.
apply storage_find_writeStorageWords_other; assumption.
Qed.
Theorem storage_write_find_nth (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) i state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 ≤ CS.Loc.Impl.t.offset location →
CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ 2^256 →
0 ≤ i < Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
Chain.Ethereum.Storage.find (CT.U256.to_repr (CS.Loc.Impl.t.offset location + i))
(A.t.storage (readAccount (TS.t.world_state post) owner)) =
Lists.List.nth (Z.to_nat i) (CT.enc (CS.Loc.Impl.t.typ location) value) zeroBytes.
Proof.
intros Hmap Hwf Hpos Hoff Hbound Hi post. unfold post.
pose proof (storage_write_owner_storage a fallback location value state Hmap Hpos) as Hstor.
cbn zeta in Hstor. rewrite Hstor.
apply storage_find_writeStorageWords_nth; assumption.
Qed.
Definition storageAgreesOn (offset w : Z) (s1 s2 : Chain.Ethereum.Storage.t) : Prop :=
∀ i, 0 ≤ i < w →
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + i)) s1 =
Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + i)) s2.
Lemma storageAgreesOn_refl offset w s :
storageAgreesOn offset w s s.
Proof.
intros i _. reflexivity.
Qed.
Lemma storageAgreesOn_sym offset w s1 s2 :
storageAgreesOn offset w s1 s2 →
storageAgreesOn offset w s2 s1.
Proof.
intros H i Hi. symmetry. exact (H i Hi).
Qed.
Lemma storageAgreesOn_trans offset w s1 s2 s3 :
storageAgreesOn offset w s1 s2 →
storageAgreesOn offset w s2 s3 →
storageAgreesOn offset w s1 s3.
Proof.
intros H12 H23 i Hi. rewrite (H12 i Hi). exact (H23 i Hi).
Qed.
Lemma storageAgreesOn_sub offset1 w1 offset2 w2 s1 s2 :
offset1 ≤ offset2 →
offset2 + w2 ≤ offset1 + w1 →
storageAgreesOn offset1 w1 s1 s2 →
storageAgreesOn offset2 w2 s1 s2.
Proof.
intros Hlo Hhi Hagree i Hi.
replace (offset2 + i) with (offset1 + (offset2 - offset1 + i)) by lia.
apply Hagree. lia.
Qed.
Lemma storageAgreesOn_write_other (a : Set) fallback
(location : @CS.Loc.Impl.t F a) (value : a) offset2 w2 state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
0 ≤ CS.Loc.Impl.t.offset location →
CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ 2^256 →
0 ≤ offset2 →
offset2 + w2 ≤ 2^256 →
(CS.Loc.Impl.t.offset location + Z.of_nat (Lists.List.length (CT.enc (CS.Loc.Impl.t.typ location) value)) ≤ offset2 ∨
offset2 + w2 ≤ CS.Loc.Impl.t.offset location) →
let post := Lens.t.set (CS.Loc.lens fallback location) value state in
storageAgreesOn offset2 w2
(A.t.storage (readAccount (TS.t.world_state post) owner))
(A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hoff1 Hbound1 Hoff2 Hbound2 Hdisj post j Hj.
unfold post.
apply storage_write_find_other; try assumption.
intros i Hi Hcol.
apply word_encoding_injective in Hcol;
[lia | unfold wordRange; change (0 ≤ CS.Loc.Impl.t.offset location + i < 2^256); lia
| unfold wordRange; change (0 ≤ offset2 + j < 2^256); lia].
Qed.
Lemma storageAgreesOn_clear_other (a : Set)
(location : @CS.Loc.Impl.t F a) (w1 : int) offset2 w2 state :
stringmaps.WellFormed (W.t.accounts (TS.t.world_state state)) →
stringmaps.WellFormed (A.t.storage (readAccount (TS.t.world_state state) owner)) →
0 < U.to_z (A.t.balance (readAccount (TS.t.world_state state) owner)) →
CS.Loc.Impl.t.kind location = CT.Static {| CT.kind.Static.word_width := w1 |} →
0 ≤ CS.Loc.Impl.t.offset location →
0 ≤ w1 →
CS.Loc.Impl.t.offset location + w1 < 2^256 →
0 ≤ offset2 →
offset2 + w2 ≤ 2^256 →
(CS.Loc.Impl.t.offset location + w1 ≤ offset2 ∨ offset2 + w2 ≤ CS.Loc.Impl.t.offset location) →
let post := snd (CS.Loc.clear location state) in
storageAgreesOn offset2 w2
(A.t.storage (readAccount (TS.t.world_state post) owner))
(A.t.storage (readAccount (TS.t.world_state state) owner)).
Proof.
intros Hmap Hwf Hpos Hkind Hoff1 Hw1 Hbound1 Hoff2 Hbound2 Hdisj post j Hj.
unfold post.
eapply storage_clear_find_other; try eassumption.
intros i Hi Hcol.
apply word_encoding_injective in Hcol;
[lia | unfold wordRange; change (0 ≤ CS.Loc.Impl.t.offset location + i < 2^256); lia
| unfold wordRange; change (0 ≤ offset2 + j < 2^256); lia].
Qed.
Lemma loc_get_ext (a : Set) fallback (location : @CS.Loc.Impl.t F a) state1 state2 :
CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner))
(CS.Loc.Impl.t.offset location)) =
CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner))
(CS.Loc.Impl.t.offset location)) →
Lens.t.get (CS.Loc.lens fallback location) state1 =
Lens.t.get (CS.Loc.lens fallback location) state2.
Proof.
destruct location as [typ kind offset].
cbn [CS.Loc.Impl.t.typ CS.Loc.Impl.t.offset].
intro Hdec.
change (fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner)) offset))) =
fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner)) offset)))).
f_equal. f_equal. exact Hdec.
Qed.
Lemma take_words_1_storage s offset :
0 ≤ offset →
offset + 1 < 2^256 →
CT.Input_view.take_words 1 (CT.Input_view.of_storage s offset) =
Some (Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s,
CT.Input_view.of_storage s (offset + 1)).
Proof.
intros Hoff Hbound.
change (Some (Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s,
CT.Input_view.of_storage s (CT.U256.op_plus offset CT.U256.one)) =
Some (Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s,
CT.Input_view.of_storage s (offset + 1))).
replace (CT.U256.op_plus offset CT.U256.one) with (offset + 1)
by (symmetry; apply word_add_small; change (0 ≤ offset + 1 < 2^256); lia).
reflexivity.
Qed.
Lemma take_words_2_storage s offset :
0 ≤ offset →
offset + 2 < 2^256 →
CT.Input_view.take_words 2 (CT.Input_view.of_storage s offset) =
Some (Strings.String.append
(Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s)
(Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + 1)) s),
CT.Input_view.of_storage s (offset + 2)).
Proof.
intros Hoff Hbound.
change (Some (Strings.String.append
(Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s)
(Chain.Ethereum.Storage.find (CT.U256.to_repr (CT.U256.op_plus offset CT.U256.one)) s),
CT.Input_view.of_storage s (CT.U256.op_plus (CT.U256.op_plus offset CT.U256.one) CT.U256.one)) =
Some (Strings.String.append
(Chain.Ethereum.Storage.find (CT.U256.to_repr offset) s)
(Chain.Ethereum.Storage.find (CT.U256.to_repr (offset + 1)) s),
CT.Input_view.of_storage s (offset + 2))).
replace (CT.U256.op_plus offset CT.U256.one) with (offset + 1)
by (symmetry; apply word_add_small; change (0 ≤ offset + 1 < 2^256); lia).
replace (CT.U256.op_plus (offset + 1) CT.U256.one) with (offset + 2)
by (replace (offset + 2) with ((offset + 1) + 1) by lia;
symmetry; apply word_add_small; change (0 ≤ (offset + 1) + 1 < 2^256); lia).
reflexivity.
Qed.
Definition sameDecodeResult {A : Set} (s1 s2 : Chain.Ethereum.Storage.t) (next_offset : Z)
(r1 r2 : sum (A × CT.Input_view.t) CT.decode_error) : Prop :=
(∃ v,
r1 = inl (v, CT.Input_view.of_storage s1 next_offset) ∧
r2 = inl (v, CT.Input_view.of_storage s2 next_offset)) ∨
(∃ err, r1 = inr err ∧ r2 = inr err).
Lemma sameDecodeResult_get_ok {A : Set} fallback s1 s2 next_offset
(r1 r2 : sum (A × CT.Input_view.t) CT.decode_error) :
sameDecodeResult s1 s2 next_offset r1 r2 →
fst (Result.get_ok fallback r1) = fst (Result.get_ok fallback r2).
Proof.
intros [[v [-> ->]] | [err [-> ->]]]; reflexivity.
Qed.
Lemma loc_get_sameDecodeResult (a : Set) fallback (location : @CS.Loc.Impl.t F a)
next_offset state1 state2 :
sameDecodeResult
(A.t.storage (readAccount (TS.t.world_state state1) owner))
(A.t.storage (readAccount (TS.t.world_state state2) owner))
next_offset
(CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner))
(CS.Loc.Impl.t.offset location)))
(CT.dec (CS.Loc.Impl.t.typ location)
(CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner))
(CS.Loc.Impl.t.offset location))) →
Lens.t.get (CS.Loc.lens fallback location) state1 =
Lens.t.get (CS.Loc.lens fallback location) state2.
Proof.
destruct location as [typ kind offset].
cbn [CS.Loc.Impl.t.typ CS.Loc.Impl.t.offset].
intro Hsame.
change (fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state1) owner)) offset))) =
fst (Result.get_ok fallback
(CT.dec typ (CT.Input_view.of_storage (A.t.storage (readAccount (TS.t.world_state state2) owner)) offset)))).
eapply sameDecodeResult_get_ok. exact Hsame.
Qed.
Lemma fixed_bytes_sameDecodeResult_1 fb s1 s2 offset :
(OCamlInt.div (H := RocqOfOCaml.Opaque.legacy_witness CT._rocq_fallback_align_1)
(CT.bytes_to_padded_bytes fb.(CT.fixed_bytes.byte_width)) 32) = 1 →
0 ≤ offset →
offset + 1 < 2^256 →
storageAgreesOn offset 1 s1 s2 →
sameDecodeResult s1 s2 (offset + 1)
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s2 offset)).
Proof.
destruct fb as [alignment bw].
cbn [CT.fixed_bytes.byte_width].
intros Hnw Hoff Hbound Hagree.
unfold CT.dec, CT.fixed_bytes_value.
cbn [CT.typ.decode].
rewrite Hnw.
rewrite (take_words_1_storage s1 offset Hoff Hbound).
rewrite (take_words_1_storage s2 offset Hoff Hbound).
pose proof (Hagree 0 ltac:(lia)) as H0.
replace (offset + 0) with offset in H0 by lia.
rewrite H0.
cbn [Result.Option.or_fail Result.op_letdollar].
destruct alignment; left; eexists; split; reflexivity.
Qed.
Lemma fixed_bytes_sameDecodeResult_2 fb s1 s2 offset :
(OCamlInt.div (H := RocqOfOCaml.Opaque.legacy_witness CT._rocq_fallback_align_1)
(CT.bytes_to_padded_bytes fb.(CT.fixed_bytes.byte_width)) 32) = 2 →
0 ≤ offset →
offset + 2 < 2^256 →
storageAgreesOn offset 2 s1 s2 →
sameDecodeResult s1 s2 (offset + 2)
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.fixed_bytes_value fb) (CT.Input_view.of_storage s2 offset)).
Proof.
destruct fb as [alignment bw].
cbn [CT.fixed_bytes.byte_width].
intros Hnw Hoff Hbound Hagree.
unfold CT.dec, CT.fixed_bytes_value.
cbn [CT.typ.decode].
rewrite Hnw.
rewrite (take_words_2_storage s1 offset Hoff Hbound).
rewrite (take_words_2_storage s2 offset Hoff Hbound).
pose proof (Hagree 0 ltac:(lia)) as H0.
pose proof (Hagree 1 ltac:(lia)) as H1.
replace (offset + 0) with offset in H0 by lia.
rewrite H0, H1.
cbn [Result.Option.or_fail Result.op_letdollar].
destruct alignment; left; eexists; split; reflexivity.
Qed.
Lemma mapped_sameDecodeResult {a repr : Set} (m : CT.mapping a repr) s1 s2 offset next_offset :
sameDecodeResult s1 s2 next_offset
(CT.dec m.(CT.mapping.repr) (CT.Input_view.of_storage s1 offset))
(CT.dec m.(CT.mapping.repr) (CT.Input_view.of_storage s2 offset)) →
sameDecodeResult s1 s2 next_offset
(CT.dec (CT.mapped m) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.mapped m) (CT.Input_view.of_storage s2 offset)).
Proof.
intros [[r [H1 H2]] | [err [H1 H2]]];
unfold CT.dec, CT.mapped; cbn [CT.typ.decode];
change (CT.mapping.repr m).(CT.typ.decode) with (CT.dec m.(CT.mapping.repr));
rewrite H1, H2; unfold Result.op_letdollar, Result.bind; cbn.
- destruct (m.(CT.mapping.decode) r) as [v|msg] eqn:Hdec.
+ left. ∃ v. split; reflexivity.
+ right. eexists. split; reflexivity.
- right. ∃ err. split; reflexivity.
Qed.
Lemma cons_sameDecodeParts {A B : Set} (hd : CT.typ A) (tl : CT.tup B)
w_hd s1 s2 offset next_offset tail_offset :
CT.kind_value hd = CT.Static {| CT.kind.Static.word_width := w_hd |} →
sameDecodeResult s1 s2 (offset + w_hd)
(CT.dec hd (CT.Input_view.of_storage s1 offset))
(CT.dec hd (CT.Input_view.of_storage s2 offset)) →
sameDecodeResult s1 s2 tail_offset
(tl.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 (offset + w_hd))
(CT.Input_view.of_storage s1 next_offset))
(tl.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 (offset + w_hd))
(CT.Input_view.of_storage s2 next_offset)) →
sameDecodeResult s1 s2 tail_offset
((CT.cons hd tl).(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 offset)
(CT.Input_view.of_storage s1 next_offset))
((CT.cons hd tl).(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 offset)
(CT.Input_view.of_storage s2 next_offset)).
Proof.
intros Hkind Hhd Htl.
unfold CT.cons. cbn [CT.tup.decode_parts].
rewrite Hkind.
destruct Hhd as [[elt [Hhd1 Hhd2]] | [err [Hhd1 Hhd2]]].
- rewrite Hhd1, Hhd2. unfold Result.op_letdollar, Result.bind. cbn.
destruct Htl as [[elts [Htl1 Htl2]] | [err [Htl1 Htl2]]].
+ rewrite Htl1, Htl2. cbn.
left. ∃ (elt, elts). split; reflexivity.
+ rewrite Htl1, Htl2. cbn.
right. ∃ err. split; reflexivity.
- rewrite Hhd1, Hhd2. unfold Result.op_letdollar, Result.bind. cbn.
right. ∃ err. split; reflexivity.
Qed.
Lemma tup0_sameDecodeParts s1 s2 head_offset tail_offset :
sameDecodeResult s1 s2 tail_offset
(CT.tup0.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 head_offset)
(CT.Input_view.of_storage s1 tail_offset))
(CT.tup0.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 head_offset)
(CT.Input_view.of_storage s2 tail_offset)).
Proof.
left. ∃ tt. split; reflexivity.
Qed.
Lemma map_tuple_sameDecodeParts {A B : Set} (enc_fn : A → B) (dec_fn : B → A) (t : CT.tup B)
s1 s2 head_offset tail_offset res_offset :
sameDecodeResult s1 s2 res_offset
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 head_offset)
(CT.Input_view.of_storage s1 tail_offset))
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 head_offset)
(CT.Input_view.of_storage s2 tail_offset)) →
sameDecodeResult s1 s2 res_offset
((CT.map_tuple enc_fn dec_fn t).(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 head_offset)
(CT.Input_view.of_storage s1 tail_offset))
((CT.map_tuple enc_fn dec_fn t).(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 head_offset)
(CT.Input_view.of_storage s2 tail_offset)).
Proof.
intros [[v [H1 H2]] | [err [H1 H2]]];
unfold CT.map_tuple; cbn [CT.tup.decode_parts];
rewrite H1, H2; cbn [Result.map Result.op_letdollar].
- left. ∃ (dec_fn v). split; reflexivity.
- right. ∃ err. split; reflexivity.
Qed.
Lemma tuple_sameDecodeResult {A : Set} (t : CT.tup A) w s1 s2 offset :
t.(CT.tup.header_words) = w →
0 ≤ w →
0 ≤ offset →
offset + w < 2^256 →
sameDecodeResult s1 s2 (offset + w)
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s1 offset)
(CT.Input_view.of_storage s1 (offset + w)))
(t.(CT.tup.decode_parts)
(CT.Input_view.of_storage s2 offset)
(CT.Input_view.of_storage s2 (offset + w))) →
sameDecodeResult s1 s2 (offset + w)
(CT.dec (CT.tuple t) (CT.Input_view.of_storage s1 offset))
(CT.dec (CT.tuple t) (CT.Input_view.of_storage s2 offset)).
Proof.
intros Hhw Hw Hoff Hbound Hparts.
unfold CT.dec, CT.tuple, CT.dec_tuple.
cbn [CT.typ.decode].
rewrite Hhw.
unfold CT.Input_view.drop, CT.Input_view.of_storage.
replace (Numeric.U256.of_int w) with w
by (symmetry; apply u256_of_int_small; lia).
replace (Numeric.U256.op_plus offset w) with (offset + w)
by (symmetry; apply word_add_small; lia).
exact Hparts.
Qed.
End Storage.
Print Assumptions storage_write_retains_account_fields.
Print Assumptions storage_write_preserves_positive_balance.
Print Assumptions account_storage_update_retains_fields.
Print Assumptions storage_write_retains_account_fields_unconditional.
Print Assumptions storage_clear_retains_account_fields.
Print Assumptions account_storage_update_preserves_supply.