Library QedkConcrete.protectedstorageboundarycore
Reserve storage at transaction boundaries
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.protectedstorage QedkConcrete.worldaccounts
QedkConcrete.hostframe QedkConcrete.authorization QedkConcrete.authorizationcodecore
QedkConcrete.transactionfinalize QedkConcrete.emptyinghistory.
Set Default Goal Selector "!".
Module E := Execution.Make.
Module B := State.BlockState.
Section World.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Definition worldProtectedStorage query (P : list (string × string) → Prop) world : Prop :=
stringmaps.WellFormed (W.t.accounts world) ∧
A.t.nonce (readAccount world query) ≠ 0%Z ∧
A.t.code (readAccount world query) = Byte_string.Bytes.empty ∧
P (A.t.storage (readAccount world query)).
Lemma protected_storage_world query P s :
@protectedStorage query P s →
worldProtectedStorage query (P (storageBlock s)) (S.t.world_state s).
Proof. intros [Hmap Hnonce Hcode Hcontents Hcreated Hdestroyed]. repeat split; assumption. Qed.
Lemma world_protected_storage_same query P before after :
stringmaps.WellFormed (W.t.accounts after) →
readAccount after query = readAccount before query →
worldProtectedStorage query P before → worldProtectedStorage query P after.
Proof.
intros Hmap Haccount [Hbefore [Hnonce [Hcode Hcontents]]].
split; [exact Hmap|]. cbv beta in Haccount. unfold worldProtectedStorage in ×.
rewrite Haccount. repeat split; assumption.
Qed.
Lemma world_protected_storage_write query P world keep addr value :
worldProtectedStorage query P world →
(addr = query → A.t.nonce value ≠ 0%Z ∧
A.t.code value = Byte_string.Bytes.empty ∧ P (A.t.storage value)) →
worldProtectedStorage query P
(State.op_pointcaretlparenrparenltminus world (@W.account (Some keep) addr) value).
Proof.
intros [Hmap [Hnonce [Hcode Hcontents]]] Hvalue. split.
- apply account_write_wellformed. exact Hmap.
- cbv beta. rewrite account_read_after_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq; [|repeat split; assumption].
apply String.eqb_eq in Heq. specialize (Hvalue Heq).
destruct keep; [exact Hvalue|].
rewrite reservestorage.nonzero_nonce_not_empty by exact (proj1 Hvalue). exact Hvalue.
Qed.
Lemma world_protected_storage_balance query P world keep addr balance :
worldProtectedStorage query P world →
worldProtectedStorage query P (State.op_pointcaretlparenrparenltminus world
(@W.account (Some keep) addr)
(A.t.with_balance balance (readAccount world addr))).
Proof.
intro Hbefore. apply world_protected_storage_write; [exact Hbefore|].
intro Heq. subst addr. exact (proj2 Hbefore).
Qed.
Lemma delete_accounts_protected_storage query P block addresses :
worldProtectedStorage query P (B.t.world_state block) →
¬ List.In query addresses →
worldProtectedStorage query P (B.t.world_state (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) block (OCamlSeq.of_list addresses))).
Proof.
intros Hbefore Habsent.
destruct (delete_accounts_preserves_other block addresses query (proj1 Hbefore) Habsent)
as [Hmap Haccount].
eapply world_protected_storage_same; [exact Hmap|exact Haccount|exact Hbefore].
Qed.
End World.
Section Authorization.
Context {F : E.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation protected := (@protectedStorage).
Lemma authorization_preserves_protected_storage query P s auth :
protected query P s → fst (@E.process_authorization F s auth) ≠ Some query →
protected query P (snd (@E.process_authorization F s auth)).
Proof.
intro Hbefore. unfold E.process_authorization.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
{ intros _. exact Hbefore. }
match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct a as [authority|] end.
2: intros _; exact Hbefore.
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
2: intros _; eapply protected_storage_same_fields with (before := s);
[reflexivity|reflexivity|reflexivity|reflexivity|exact Hbefore].
match goal with |- context [State.op_pointcaretlparenrparenltminus _
(@S.account None authority) ?value] ⇒ set (updated := value) end.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
all: intro Habsent;
eapply (@protected_storage_same_fields query P
(S.t.with_world_state (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(@W.account (Some false) authority) updated) s));
[reflexivity|reflexivity|reflexivity|reflexivity|].
all: apply protected_storage_write; [exact Hbefore|];
intro Heq; subst authority; exfalso; apply Habsent; reflexivity.
Qed.
Lemma authorizations_preserve_protected_storage query P auths authorities s :
protected query P s →
let result := Result.List.fold_left
(fun '(applied, state) auth ⇒
match @E.process_authorization F state auth with
| (None, next) ⇒ (applied, next)
| (Some authority, next) ⇒ (authority :: applied, next)
end) (authorities, s) auths in
¬ List.In query (fst result) → protected query P (snd result).
Proof.
revert authorities s. induction auths as [|auth auths IH]; intros authorities s Hbefore.
- cbn. intros _. exact Hbefore.
- change (@Result.List.fold_left) with (@OCamlList.fold_left) in ×.
cbn [OCamlList.fold_left].
pose proof (authorization_preserves_protected_storage query P s auth Hbefore) as Hnext.
destruct (@E.process_authorization F s auth) as [[authority|] next] eqn:Hstep;
cbn [fst snd] in Hnext; intro Habsent; (apply IH; [|exact Habsent]).
+ apply Hnext. intro Heq. injection Heq as Heq. subst authority.
apply Habsent. apply authorizations_retain_authorities. left. reflexivity.
+ apply Hnext. discriminate.
Qed.
Lemma bump_counters_protected_storage query P s addresses :
protected query P s → protected query P (@E.bump_emptying_transaction_counters F addresses s).
Proof.
intro Hbefore. apply protected_storage_same_fields with (before := s).
- unfold E.bump_emptying_transaction_counters.
cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
apply counter_writes_accounts.
- reflexivity.
- reflexivity.
- reflexivity.
- exact Hbefore.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.protectedstorage QedkConcrete.worldaccounts
QedkConcrete.hostframe QedkConcrete.authorization QedkConcrete.authorizationcodecore
QedkConcrete.transactionfinalize QedkConcrete.emptyinghistory.
Set Default Goal Selector "!".
Module E := Execution.Make.
Module B := State.BlockState.
Section World.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Definition worldProtectedStorage query (P : list (string × string) → Prop) world : Prop :=
stringmaps.WellFormed (W.t.accounts world) ∧
A.t.nonce (readAccount world query) ≠ 0%Z ∧
A.t.code (readAccount world query) = Byte_string.Bytes.empty ∧
P (A.t.storage (readAccount world query)).
Lemma protected_storage_world query P s :
@protectedStorage query P s →
worldProtectedStorage query (P (storageBlock s)) (S.t.world_state s).
Proof. intros [Hmap Hnonce Hcode Hcontents Hcreated Hdestroyed]. repeat split; assumption. Qed.
Lemma world_protected_storage_same query P before after :
stringmaps.WellFormed (W.t.accounts after) →
readAccount after query = readAccount before query →
worldProtectedStorage query P before → worldProtectedStorage query P after.
Proof.
intros Hmap Haccount [Hbefore [Hnonce [Hcode Hcontents]]].
split; [exact Hmap|]. cbv beta in Haccount. unfold worldProtectedStorage in ×.
rewrite Haccount. repeat split; assumption.
Qed.
Lemma world_protected_storage_write query P world keep addr value :
worldProtectedStorage query P world →
(addr = query → A.t.nonce value ≠ 0%Z ∧
A.t.code value = Byte_string.Bytes.empty ∧ P (A.t.storage value)) →
worldProtectedStorage query P
(State.op_pointcaretlparenrparenltminus world (@W.account (Some keep) addr) value).
Proof.
intros [Hmap [Hnonce [Hcode Hcontents]]] Hvalue. split.
- apply account_write_wellformed. exact Hmap.
- cbv beta. rewrite account_read_after_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq; [|repeat split; assumption].
apply String.eqb_eq in Heq. specialize (Hvalue Heq).
destruct keep; [exact Hvalue|].
rewrite reservestorage.nonzero_nonce_not_empty by exact (proj1 Hvalue). exact Hvalue.
Qed.
Lemma world_protected_storage_balance query P world keep addr balance :
worldProtectedStorage query P world →
worldProtectedStorage query P (State.op_pointcaretlparenrparenltminus world
(@W.account (Some keep) addr)
(A.t.with_balance balance (readAccount world addr))).
Proof.
intro Hbefore. apply world_protected_storage_write; [exact Hbefore|].
intro Heq. subst addr. exact (proj2 Hbefore).
Qed.
Lemma delete_accounts_protected_storage query P block addresses :
worldProtectedStorage query P (B.t.world_state block) →
¬ List.In query addresses →
worldProtectedStorage query P (B.t.world_state (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) block (OCamlSeq.of_list addresses))).
Proof.
intros Hbefore Habsent.
destruct (delete_accounts_preserves_other block addresses query (proj1 Hbefore) Habsent)
as [Hmap Haccount].
eapply world_protected_storage_same; [exact Hmap|exact Haccount|exact Hbefore].
Qed.
End World.
Section Authorization.
Context {F : E.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation protected := (@protectedStorage).
Lemma authorization_preserves_protected_storage query P s auth :
protected query P s → fst (@E.process_authorization F s auth) ≠ Some query →
protected query P (snd (@E.process_authorization F s auth)).
Proof.
intro Hbefore. unfold E.process_authorization.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
{ intros _. exact Hbefore. }
match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct a as [authority|] end.
2: intros _; exact Hbefore.
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
2: intros _; eapply protected_storage_same_fields with (before := s);
[reflexivity|reflexivity|reflexivity|reflexivity|exact Hbefore].
match goal with |- context [State.op_pointcaretlparenrparenltminus _
(@S.account None authority) ?value] ⇒ set (updated := value) end.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
all: intro Habsent;
eapply (@protected_storage_same_fields query P
(S.t.with_world_state (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(@W.account (Some false) authority) updated) s));
[reflexivity|reflexivity|reflexivity|reflexivity|].
all: apply protected_storage_write; [exact Hbefore|];
intro Heq; subst authority; exfalso; apply Habsent; reflexivity.
Qed.
Lemma authorizations_preserve_protected_storage query P auths authorities s :
protected query P s →
let result := Result.List.fold_left
(fun '(applied, state) auth ⇒
match @E.process_authorization F state auth with
| (None, next) ⇒ (applied, next)
| (Some authority, next) ⇒ (authority :: applied, next)
end) (authorities, s) auths in
¬ List.In query (fst result) → protected query P (snd result).
Proof.
revert authorities s. induction auths as [|auth auths IH]; intros authorities s Hbefore.
- cbn. intros _. exact Hbefore.
- change (@Result.List.fold_left) with (@OCamlList.fold_left) in ×.
cbn [OCamlList.fold_left].
pose proof (authorization_preserves_protected_storage query P s auth Hbefore) as Hnext.
destruct (@E.process_authorization F s auth) as [[authority|] next] eqn:Hstep;
cbn [fst snd] in Hnext; intro Habsent; (apply IH; [|exact Habsent]).
+ apply Hnext. intro Heq. injection Heq as Heq. subst authority.
apply Habsent. apply authorizations_retain_authorities. left. reflexivity.
+ apply Hnext. discriminate.
Qed.
Lemma bump_counters_protected_storage query P s addresses :
protected query P s → protected query P (@E.bump_emptying_transaction_counters F addresses s).
Proof.
intro Hbefore. apply protected_storage_same_fields with (before := s).
- unfold E.bump_emptying_transaction_counters.
cbn beta iota zeta delta [S.t.with_world_state S.t.world_state].
apply counter_writes_accounts.
- reflexivity.
- reflexivity.
- reflexivity.
- exact Hbefore.
Qed.
Signature recovery is independent of execution state. These predicates
expose the reserved-address cryptographic condition at the inputs;
they do not assert that every transaction satisfies it.
Local Abbreviation recoveredSender := (@Chain.Ethereum.Transaction.sender (@E.Params F).(Execution.Make_Params_signature.chain_id)).
Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).
Definition signaturesAvoidAddress tx query : Prop :=
recoveredSender tx ≠ Some query ∧
List.Forall (fun auth ⇒ recoveredAuthority auth ≠ Some query)
(Chain.Ethereum.Transaction.authorization_list tx).
Local Lemma validation_bind_success {A B Error : Set}
(first : A + Error) (next : A → B + Error) value :
Result.op_letdollar first next = inl value →
∃ intermediate, first = inl intermediate ∧ next intermediate = inl value.
Proof.
destruct first; cbn; [intro H; eexists; split; [reflexivity|exact H]|discriminate].
Qed.
Lemma validated_sender_recovered block tx validation :
@E.validate_transaction F block tx = inl validation →
recoveredSender tx = Some (E.transaction_validation.sender validation).
Proof.
intro Hvalidated. unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
repeat (lazymatch type of Hvalidated with Result.op_letdollar _ _ = _ ⇒ idtac end;
apply validation_bind_success in Hvalidated;
let step := fresh "step" in let Hstep := fresh "Hstep" in
destruct Hvalidated as [step [Hstep Hvalidated]];
cbn beta iota zeta in Hvalidated).
injection Hvalidated as Heq. subst validation.
match goal with
| H : (match ?recovery with Some _ ⇒ _ | None ⇒ _ end) = inl ?sender |- _ ⇒
destruct recovery as [addr|] eqn:Hrecovery;
[cbn in H; injection H as <-; reflexivity|discriminate H]
end.
Qed.
Lemma authorization_recovery_excludes query s auth :
recoveredAuthority auth ≠ Some query →
fst (@E.process_authorization F s auth) ≠ Some query.
Proof.
intro Hexcluded. unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
{ discriminate. }
destruct (recoveredAuthority auth) as [addr|] eqn:Hrecovered.
2: discriminate.
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
2: discriminate.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; exact Hexcluded.
Qed.
Lemma authorizations_recovery_excludes query auths authorities s :
List.Forall (fun auth ⇒ recoveredAuthority auth ≠ Some query) auths →
¬ List.In query authorities →
¬ List.In query (fst (Result.List.fold_left
(fun '(applied, state) auth ⇒
match @E.process_authorization F state auth with
| (None, next) ⇒ (applied, next)
| (Some authority, next) ⇒ (authority :: applied, next)
end) (authorities, s) auths)).
Proof.
intro Hauths. revert authorities s. induction Hauths as [|auth auths Hauth Hrest IH];
intros authorities s Habsent.
- exact Habsent.
- change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
pose proof (authorization_recovery_excludes query s auth Hauth) as Hnext.
destruct (@E.process_authorization F s auth) as [[authority|] next]; cbn [fst] in Hnext.
+ apply IH. intros [Heq|Hin]; [subst authority; apply Hnext; reflexivity|exact (Habsent Hin)].
+ apply IH. exact Habsent.
Qed.
End Authorization.
Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).
Definition signaturesAvoidAddress tx query : Prop :=
recoveredSender tx ≠ Some query ∧
List.Forall (fun auth ⇒ recoveredAuthority auth ≠ Some query)
(Chain.Ethereum.Transaction.authorization_list tx).
Local Lemma validation_bind_success {A B Error : Set}
(first : A + Error) (next : A → B + Error) value :
Result.op_letdollar first next = inl value →
∃ intermediate, first = inl intermediate ∧ next intermediate = inl value.
Proof.
destruct first; cbn; [intro H; eexists; split; [reflexivity|exact H]|discriminate].
Qed.
Lemma validated_sender_recovered block tx validation :
@E.validate_transaction F block tx = inl validation →
recoveredSender tx = Some (E.transaction_validation.sender validation).
Proof.
intro Hvalidated. unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
repeat (lazymatch type of Hvalidated with Result.op_letdollar _ _ = _ ⇒ idtac end;
apply validation_bind_success in Hvalidated;
let step := fresh "step" in let Hstep := fresh "Hstep" in
destruct Hvalidated as [step [Hstep Hvalidated]];
cbn beta iota zeta in Hvalidated).
injection Hvalidated as Heq. subst validation.
match goal with
| H : (match ?recovery with Some _ ⇒ _ | None ⇒ _ end) = inl ?sender |- _ ⇒
destruct recovery as [addr|] eqn:Hrecovery;
[cbn in H; injection H as <-; reflexivity|discriminate H]
end.
Qed.
Lemma authorization_recovery_excludes query s auth :
recoveredAuthority auth ≠ Some query →
fst (@E.process_authorization F s auth) ≠ Some query.
Proof.
intro Hexcluded. unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
{ discriminate. }
destruct (recoveredAuthority auth) as [addr|] eqn:Hrecovered.
2: discriminate.
cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
2: discriminate.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; exact Hexcluded.
Qed.
Lemma authorizations_recovery_excludes query auths authorities s :
List.Forall (fun auth ⇒ recoveredAuthority auth ≠ Some query) auths →
¬ List.In query authorities →
¬ List.In query (fst (Result.List.fold_left
(fun '(applied, state) auth ⇒
match @E.process_authorization F state auth with
| (None, next) ⇒ (applied, next)
| (Some authority, next) ⇒ (authority :: applied, next)
end) (authorities, s) auths)).
Proof.
intro Hauths. revert authorities s. induction Hauths as [|auth auths Hauth Hrest IH];
intros authorities s Habsent.
- exact Habsent.
- change (@Result.List.fold_left) with (@OCamlList.fold_left).
cbn [OCamlList.fold_left].
pose proof (authorization_recovery_excludes query s auth Hauth) as Hnext.
destruct (@E.process_authorization F s auth) as [[authority|] next]; cbn [fst] in Hnext.
+ apply IH. intros [Heq|Hin]; [subst authority; apply Hnext; reflexivity|exact (Habsent Hin)].
+ apply IH. exact Habsent.
Qed.
End Authorization.