Library QedkConcrete.consensushistoryproofs
Localized authorization and activity projection
From Stdlib Require Import List String ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.authorizationnonces
QedkConcrete.publictransaction QedkConcrete.transactioncode QedkConcrete.consensushistory.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module A := Chain.Ethereum.Account.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Section History.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.authorizationnonces
QedkConcrete.publictransaction QedkConcrete.transactioncode QedkConcrete.consensushistory.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module A := Chain.Ethereum.Account.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Section History.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).
Projection proofs
Lemma authorization_nonce_step_on (tracked : Addr.t → Prop)
eligible nonces otherEligible otherNonces auth :
(∀ a, tracked a → eligible a = otherEligible a ∧ nonces a = otherNonces a) →
let actual := @authorizationNonceStep F eligible nonces auth in
let predicted := @authorizationNonceStep F otherEligible otherNonces auth in
∀ a, tracked a →
(match fst actual with Some b ⇒ String.eqb b a | None ⇒ false end) =
(match fst predicted with Some b ⇒ String.eqb b a | None ⇒ false end) ∧
snd actual a = snd predicted a.
Proof.
cbn zeta. intros Hsame a Ha. destruct (Hsame a Ha) as [Heligible Hnonce].
unfold authorizationNonceStep.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [auto|].
destruct (recoveredAuthority auth) as [authority|]; [|auto].
destruct (String.eqb authority a) eqn:Heq.
- apply String.eqb_eq in Heq; subst authority. rewrite Heligible, Hnonce.
destruct (andb _ _); cbn [fst snd]; rewrite ?String.eqb_refl; auto.
- destruct (andb _ _), (andb _ _); cbn [fst snd]; rewrite ?Heq; auto.
Qed.
Lemma authorizations_nonce_on (tracked : Addr.t → Prop)
auths eligible nonces otherEligible otherNonces authorities otherAuthorities :
(∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) authorities =
existsb (fun b ⇒ String.eqb b a) otherAuthorities) →
(∀ a, tracked a → eligible a = otherEligible a ∧ nonces a = otherNonces a) →
let actual := @authorizationNonces F eligible (authorities, nonces) auths in
let predicted := @authorizationNonces F otherEligible (otherAuthorities, otherNonces) auths in
(∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) (fst actual) =
existsb (fun b ⇒ String.eqb b a) (fst predicted)) ∧
∀ a, tracked a → snd actual a = snd predicted a.
Proof.
revert nonces otherNonces authorities otherAuthorities.
induction auths as [|auth rest IH]; intros nonces otherNonces authorities otherAuthorities Hactivity Hsame.
- split; [exact Hactivity|]. intros a Ha. exact (proj2 (Hsame a Ha)).
- pose proof (authorization_nonce_step_on tracked eligible nonces otherEligible otherNonces auth
Hsame) as Hstep.
unfold authorizationNonces. cbn [OCamlList.fold_left].
destruct (@authorizationNonceStep F eligible nonces auth) as [actual next].
destruct (@authorizationNonceStep F otherEligible otherNonces auth) as [predicted nextOther].
cbn beta iota zeta delta [fst snd] in Hstep.
destruct actual, predicted; apply IH.
all: intros a Ha; specialize (Hstep a Ha); cbn [existsb] in ×.
all: first [exact (Hactivity a Ha) |rewrite (Hactivity a Ha), (proj1 Hstep); reflexivity
|rewrite (Hactivity a Ha), <- (proj1 Hstep); reflexivity
|split; [exact (proj1 (Hsame a Ha))|exact (proj2 Hstep)]].
Qed.
eligible nonces otherEligible otherNonces auth :
(∀ a, tracked a → eligible a = otherEligible a ∧ nonces a = otherNonces a) →
let actual := @authorizationNonceStep F eligible nonces auth in
let predicted := @authorizationNonceStep F otherEligible otherNonces auth in
∀ a, tracked a →
(match fst actual with Some b ⇒ String.eqb b a | None ⇒ false end) =
(match fst predicted with Some b ⇒ String.eqb b a | None ⇒ false end) ∧
snd actual a = snd predicted a.
Proof.
cbn zeta. intros Hsame a Ha. destruct (Hsame a Ha) as [Heligible Hnonce].
unfold authorizationNonceStep.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [auto|].
destruct (recoveredAuthority auth) as [authority|]; [|auto].
destruct (String.eqb authority a) eqn:Heq.
- apply String.eqb_eq in Heq; subst authority. rewrite Heligible, Hnonce.
destruct (andb _ _); cbn [fst snd]; rewrite ?String.eqb_refl; auto.
- destruct (andb _ _), (andb _ _); cbn [fst snd]; rewrite ?Heq; auto.
Qed.
Lemma authorizations_nonce_on (tracked : Addr.t → Prop)
auths eligible nonces otherEligible otherNonces authorities otherAuthorities :
(∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) authorities =
existsb (fun b ⇒ String.eqb b a) otherAuthorities) →
(∀ a, tracked a → eligible a = otherEligible a ∧ nonces a = otherNonces a) →
let actual := @authorizationNonces F eligible (authorities, nonces) auths in
let predicted := @authorizationNonces F otherEligible (otherAuthorities, otherNonces) auths in
(∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) (fst actual) =
existsb (fun b ⇒ String.eqb b a) (fst predicted)) ∧
∀ a, tracked a → snd actual a = snd predicted a.
Proof.
revert nonces otherNonces authorities otherAuthorities.
induction auths as [|auth rest IH]; intros nonces otherNonces authorities otherAuthorities Hactivity Hsame.
- split; [exact Hactivity|]. intros a Ha. exact (proj2 (Hsame a Ha)).
- pose proof (authorization_nonce_step_on tracked eligible nonces otherEligible otherNonces auth
Hsame) as Hstep.
unfold authorizationNonces. cbn [OCamlList.fold_left].
destruct (@authorizationNonceStep F eligible nonces auth) as [actual next].
destruct (@authorizationNonceStep F otherEligible otherNonces auth) as [predicted nextOther].
cbn beta iota zeta delta [fst snd] in Hstep.
destruct actual, predicted; apply IH.
all: intros a Ha; specialize (Hstep a Ha); cbn [existsb] in ×.
all: first [exact (Hactivity a Ha) |rewrite (Hactivity a Ha), (proj1 Hstep); reflexivity
|rewrite (Hactivity a Ha), <- (proj1 Hstep); reflexivity
|split; [exact (proj1 (Hsame a Ha))|exact (proj2 Hstep)]].
Qed.
Predict only the senders' nonce and applied-authorization membership.
No agreement or creation exclusion is needed for other authorities.
Theorem advance_history_projection (tracked : Addr.t → Prop) snapshot history world sender tx :
(∀ a, tracked a →
negb (@A.is_smart_contract (readAccount world a)) =
negb (@A.is_smart_contract (readAccount snapshot a)) ∧
A.t.nonce (readAccount world a) = historyNonces history a) →
let actual := @transactionAuthorizationNonces F world sender tx in
let predicted := advanceHistory snapshot history sender tx in
(∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) (fst actual) =
existsb (fun b ⇒ String.eqb b a) (fst predicted)) ∧
∀ a, tracked a → snd actual a = historyNonces (snd predicted) a.
Proof.
intros Hsame.
pose proof (authorizations_nonce_on tracked (T.authorization_list tx)
(fun a ⇒ negb (@A.is_smart_contract (readAccount world a)))
(fun a ⇒ if String.eqb sender a
then @Numeric.U64.op_plus (A.t.nonce (readAccount world a)) (@Numeric.U64.one)
else A.t.nonce (readAccount world a))
(fun a ⇒ negb (@A.is_smart_contract (readAccount snapshot a)))
(fun a ⇒ if String.eqb sender a
then @Numeric.U64.op_plus (historyNonces history a) (@Numeric.U64.one)
else historyNonces history a) [] [] ltac:(reflexivity)) as Hprojection.
specialize (Hprojection ltac:(intros a Ha; split; [exact (proj1 (Hsame a Ha))|];
rewrite (proj2 (Hsame a Ha)); reflexivity)).
unfold transactionAuthorizationNonces, advanceHistory.
cbv beta iota zeta in Hprojection |- ×.
match goal with
| |- (∀ a, tracked a → existsb _ (fst ?actual) =
existsb _ (fst (let '(authorities, nonces) := ?predicted in _))) ∧ _ ⇒
change ((∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) (fst actual) =
existsb (fun b ⇒ String.eqb b a) (fst predicted)) ∧
∀ a, tracked a → snd actual a = snd predicted a) in Hprojection;
destruct predicted as [authorities nonces] in Hprojection |- ×
end.
exact Hprojection.
Qed.
Lemma initial_history_emptying snapshot a block :
historyAllowsEmptying snapshot (initialHistory snapshot) a block =
@emptyingEligible snapshot a block.
Proof. unfold historyAllowsEmptying. cbn [initialHistory historyActivity]. apply Bool.andb_true_r. Qed.
Lemma advance_history_emptying snapshot history sender tx a block :
historyAllowsEmptying snapshot (snd (advanceHistory snapshot history sender tx)) a block =
historyAllowsEmptying snapshot history a block &&
negb (existsb (fun addr ⇒ String.eqb addr a)
(sender :: fst (advanceHistory snapshot history sender tx))).
Proof.
unfold advanceHistory, historyAllowsEmptying.
destruct (@authorizationNonces F _ _ _) as [authorities nonces].
cbn beta iota zeta delta [fst snd historyActivity existsb].
rewrite existsb_app.
change ((@emptyingEligible snapshot a block &&
negb (String.eqb sender a ||
(existsb (fun addr : Addr.t ⇒ String.eqb addr a) authorities ||
existsb (fun addr : Addr.t ⇒ String.eqb addr a) (historyActivity history)))) =
((@emptyingEligible snapshot a block &&
negb (existsb (fun addr : Addr.t ⇒ String.eqb addr a) (historyActivity history))) &&
negb (String.eqb sender a || existsb (fun addr : Addr.t ⇒ String.eqb addr a) authorities))).
destruct (@emptyingEligible snapshot a block), (String.eqb sender a),
(existsb (fun addr : Addr.t ⇒ String.eqb addr a) authorities),
(existsb (fun addr : Addr.t ⇒ String.eqb addr a) (historyActivity history)); reflexivity.
Qed.
End History.
(∀ a, tracked a →
negb (@A.is_smart_contract (readAccount world a)) =
negb (@A.is_smart_contract (readAccount snapshot a)) ∧
A.t.nonce (readAccount world a) = historyNonces history a) →
let actual := @transactionAuthorizationNonces F world sender tx in
let predicted := advanceHistory snapshot history sender tx in
(∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) (fst actual) =
existsb (fun b ⇒ String.eqb b a) (fst predicted)) ∧
∀ a, tracked a → snd actual a = historyNonces (snd predicted) a.
Proof.
intros Hsame.
pose proof (authorizations_nonce_on tracked (T.authorization_list tx)
(fun a ⇒ negb (@A.is_smart_contract (readAccount world a)))
(fun a ⇒ if String.eqb sender a
then @Numeric.U64.op_plus (A.t.nonce (readAccount world a)) (@Numeric.U64.one)
else A.t.nonce (readAccount world a))
(fun a ⇒ negb (@A.is_smart_contract (readAccount snapshot a)))
(fun a ⇒ if String.eqb sender a
then @Numeric.U64.op_plus (historyNonces history a) (@Numeric.U64.one)
else historyNonces history a) [] [] ltac:(reflexivity)) as Hprojection.
specialize (Hprojection ltac:(intros a Ha; split; [exact (proj1 (Hsame a Ha))|];
rewrite (proj2 (Hsame a Ha)); reflexivity)).
unfold transactionAuthorizationNonces, advanceHistory.
cbv beta iota zeta in Hprojection |- ×.
match goal with
| |- (∀ a, tracked a → existsb _ (fst ?actual) =
existsb _ (fst (let '(authorities, nonces) := ?predicted in _))) ∧ _ ⇒
change ((∀ a, tracked a →
existsb (fun b ⇒ String.eqb b a) (fst actual) =
existsb (fun b ⇒ String.eqb b a) (fst predicted)) ∧
∀ a, tracked a → snd actual a = snd predicted a) in Hprojection;
destruct predicted as [authorities nonces] in Hprojection |- ×
end.
exact Hprojection.
Qed.
Lemma initial_history_emptying snapshot a block :
historyAllowsEmptying snapshot (initialHistory snapshot) a block =
@emptyingEligible snapshot a block.
Proof. unfold historyAllowsEmptying. cbn [initialHistory historyActivity]. apply Bool.andb_true_r. Qed.
Lemma advance_history_emptying snapshot history sender tx a block :
historyAllowsEmptying snapshot (snd (advanceHistory snapshot history sender tx)) a block =
historyAllowsEmptying snapshot history a block &&
negb (existsb (fun addr ⇒ String.eqb addr a)
(sender :: fst (advanceHistory snapshot history sender tx))).
Proof.
unfold advanceHistory, historyAllowsEmptying.
destruct (@authorizationNonces F _ _ _) as [authorities nonces].
cbn beta iota zeta delta [fst snd historyActivity existsb].
rewrite existsb_app.
change ((@emptyingEligible snapshot a block &&
negb (String.eqb sender a ||
(existsb (fun addr : Addr.t ⇒ String.eqb addr a) authorities ||
existsb (fun addr : Addr.t ⇒ String.eqb addr a) (historyActivity history)))) =
((@emptyingEligible snapshot a block &&
negb (existsb (fun addr : Addr.t ⇒ String.eqb addr a) (historyActivity history))) &&
negb (String.eqb sender a || existsb (fun addr : Addr.t ⇒ String.eqb addr a) authorities))).
destruct (@emptyingEligible snapshot a block), (String.eqb sender a),
(existsb (fun addr : Addr.t ⇒ String.eqb addr a) authorities),
(existsb (fun addr : Addr.t ⇒ String.eqb addr a) (historyActivity history)); reflexivity.
Qed.
End History.