Library QedkConcrete.admissionnonces
Bounds from public nonce admission
From Stdlib Require Import List String ZArith Bool Lia.
Require Import QedkConcrete.authorizationnoncechecks.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Require Import QedkConcrete.authorizationnoncechecks.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
allowance tx is an upper bound on the nonce increments assigned to a
transaction. upper bounds nonces before txs. Each signed nonce must
fit that bound plus the allowances for the transactions preceding it.
Fixpoint nonceBoundedData (allowance : T.t → Z) (upper : Z) (txs : list T.t) : Prop :=
match txs with
| [] ⇒ True
| tx :: rest ⇒ 0 ≤ T.nonce tx ≤ upper ∧
nonceBoundedData allowance (upper + allowance tx) rest
end.
Lemma unsigned_increment_upper nonce :
0 ≤ nonce → 0 ≤ Numeric.U64.op_plus nonce Numeric.U64.one ≤ nonce + 1.
Proof.
intro Hnonce.
change (0 ≤ Z.land (Z.shiftl (nonce+1) 0) (Z.ones 64) ≤ nonce+1).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
split; [apply Z.mod_pos_bound|apply Z.mod_le]; lia.
Qed.
Section Admission.
Context {F : E.FArgs}.
match txs with
| [] ⇒ True
| tx :: rest ⇒ 0 ≤ T.nonce tx ≤ upper ∧
nonceBoundedData allowance (upper + allowance tx) rest
end.
Lemma unsigned_increment_upper nonce :
0 ≤ nonce → 0 ≤ Numeric.U64.op_plus nonce Numeric.U64.one ≤ nonce + 1.
Proof.
intro Hnonce.
change (0 ≤ Z.land (Z.shiftl (nonce+1) 0) (Z.ones 64) ≤ nonce+1).
rewrite Z.shiftl_0_r, Z.land_ones by lia.
split; [apply Z.mod_pos_bound|apply Z.mod_le]; lia.
Qed.
Section Admission.
Context {F : E.FArgs}.
Sender consumption and authorization increments for a single checked
transaction. eligible comes from the designated snapshot, nonces
from the preceding public checks, and sender from signature recovery.
Definition admittedNoncesAfter eligible nonces sender tx :=
snd (authorizationNonces eligible
([], fun a ⇒ if String.eqb sender a
then Numeric.U64.op_plus (nonces a) Numeric.U64.one else nonces a)
(T.authorization_list tx)).
snd (authorizationNonces eligible
([], fun a ⇒ if String.eqb sender a
then Numeric.U64.op_plus (nonces a) Numeric.U64.one else nonces a)
(T.authorization_list tx)).
eligible is snapshot code eligibility; nonces is the current public
nonce prediction. Every transaction in txs has a recovered sender,
matches that sender's predicted nonce, and updates the prediction before
the next transaction. Fees and all other admission checks are omitted.
Inductive NonceAdmission (eligible : Addr.t → bool) :
(Addr.t → Numeric.U64.t) → list T.t → Prop :=
| nonce_admission_nil nonces : NonceAdmission eligible nonces []
| nonce_admission_cons nonces tx rest sender :
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
T.nonce tx = nonces sender →
NonceAdmission eligible (admittedNoncesAfter eligible nonces sender tx) rest →
NonceAdmission eligible nonces (tx :: rest).
Lemma authorization_step_upper eligible nonces auth query :
0 ≤ nonces query →
0 ≤ snd (authorizationNonceStep eligible nonces auth) query ≤ nonces query + 1.
Proof.
intro Hnonce. unfold authorizationNonceStep.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [cbn; lia|].
destruct (T.Authorization.authority auth) as [authority|]; [|cbn; lia].
match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [|cbn; lia].
cbn beta iota zeta delta [snd].
destruct (String.eqb authority query) eqn:Heq; [|lia].
apply String.eqb_eq in Heq. subst authority. apply unsigned_increment_upper, Hnonce.
Qed.
Lemma authorizations_upper eligible auths applied nonces query :
0 ≤ nonces query →
0 ≤ snd (authorizationNonces eligible (applied, nonces) auths) query ≤
nonces query + Z.of_nat (Stdlib.Lists.List.length auths).
Proof.
revert applied nonces. induction auths as [|auth rest IH]; intros applied nonces Hnonce.
- cbn [authorizationNonces RocqOfOCaml.OCamlList.fold_left snd]. lia.
- pose proof (authorization_step_upper eligible nonces auth query Hnonce) as Hstep.
unfold authorizationNonces. cbn [RocqOfOCaml.OCamlList.fold_left].
destruct (authorizationNonceStep eligible nonces auth) as [authority after].
cbn beta iota zeta delta [fst snd] in Hstep |- ×.
destruct authority as [a|].
1: specialize (IH (a::applied) after (proj1 Hstep)).
2: specialize (IH applied after (proj1 Hstep)).
all: unfold authorizationNonces in IH; cbn [Stdlib.Lists.List.length]; rewrite Nat2Z.inj_succ.
all: cbn beta iota zeta in IH |- ×.
all: match type of IH with 0 ≤ ?n ≤ _ ⇒
change (0 ≤ n ≤ nonces query + Z.succ (Z.of_nat (Stdlib.Lists.List.length rest))) end.
all: lia.
Qed.
Lemma admitted_nonces_after_upper eligible nonces sender tx query :
0 ≤ nonces query →
0 ≤ admittedNoncesAfter eligible nonces sender tx query ≤
nonces query + 1 + Z.of_nat (Stdlib.Lists.List.length (T.authorization_list tx)).
Proof.
intro Hnonce. unfold admittedNoncesAfter.
set (next := fun a ⇒ if String.eqb sender a
then Numeric.U64.op_plus (nonces a) Numeric.U64.one else nonces a).
assert (Hnext : 0 ≤ next query ≤ nonces query + 1).
{ unfold next. destruct (String.eqb sender query); [apply unsigned_increment_upper|]; lia. }
pose proof (authorizations_upper eligible (T.authorization_list tx) [] next query (proj1 Hnext)) as Hafter.
match type of Hafter with 0 ≤ ?n ≤ _ ⇒
change (0 ≤ n ≤ nonces query + 1 + Z.of_nat (Stdlib.Lists.List.length (T.authorization_list tx))) end.
lia.
Qed.
(Addr.t → Numeric.U64.t) → list T.t → Prop :=
| nonce_admission_nil nonces : NonceAdmission eligible nonces []
| nonce_admission_cons nonces tx rest sender :
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
T.nonce tx = nonces sender →
NonceAdmission eligible (admittedNoncesAfter eligible nonces sender tx) rest →
NonceAdmission eligible nonces (tx :: rest).
Lemma authorization_step_upper eligible nonces auth query :
0 ≤ nonces query →
0 ≤ snd (authorizationNonceStep eligible nonces auth) query ≤ nonces query + 1.
Proof.
intro Hnonce. unfold authorizationNonceStep.
match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [cbn; lia|].
destruct (T.Authorization.authority auth) as [authority|]; [|cbn; lia].
match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [|cbn; lia].
cbn beta iota zeta delta [snd].
destruct (String.eqb authority query) eqn:Heq; [|lia].
apply String.eqb_eq in Heq. subst authority. apply unsigned_increment_upper, Hnonce.
Qed.
Lemma authorizations_upper eligible auths applied nonces query :
0 ≤ nonces query →
0 ≤ snd (authorizationNonces eligible (applied, nonces) auths) query ≤
nonces query + Z.of_nat (Stdlib.Lists.List.length auths).
Proof.
revert applied nonces. induction auths as [|auth rest IH]; intros applied nonces Hnonce.
- cbn [authorizationNonces RocqOfOCaml.OCamlList.fold_left snd]. lia.
- pose proof (authorization_step_upper eligible nonces auth query Hnonce) as Hstep.
unfold authorizationNonces. cbn [RocqOfOCaml.OCamlList.fold_left].
destruct (authorizationNonceStep eligible nonces auth) as [authority after].
cbn beta iota zeta delta [fst snd] in Hstep |- ×.
destruct authority as [a|].
1: specialize (IH (a::applied) after (proj1 Hstep)).
2: specialize (IH applied after (proj1 Hstep)).
all: unfold authorizationNonces in IH; cbn [Stdlib.Lists.List.length]; rewrite Nat2Z.inj_succ.
all: cbn beta iota zeta in IH |- ×.
all: match type of IH with 0 ≤ ?n ≤ _ ⇒
change (0 ≤ n ≤ nonces query + Z.succ (Z.of_nat (Stdlib.Lists.List.length rest))) end.
all: lia.
Qed.
Lemma admitted_nonces_after_upper eligible nonces sender tx query :
0 ≤ nonces query →
0 ≤ admittedNoncesAfter eligible nonces sender tx query ≤
nonces query + 1 + Z.of_nat (Stdlib.Lists.List.length (T.authorization_list tx)).
Proof.
intro Hnonce. unfold admittedNoncesAfter.
set (next := fun a ⇒ if String.eqb sender a
then Numeric.U64.op_plus (nonces a) Numeric.U64.one else nonces a).
assert (Hnext : 0 ≤ next query ≤ nonces query + 1).
{ unfold next. destruct (String.eqb sender query); [apply unsigned_increment_upper|]; lia. }
pose proof (authorizations_upper eligible (T.authorization_list tx) [] next query (proj1 Hnext)) as Hafter.
match type of Hafter with 0 ≤ ?n ≤ _ ⇒
change (0 ≤ n ≤ nonces query + 1 + Z.of_nat (Stdlib.Lists.List.length (T.authorization_list tx))) end.
lia.
Qed.
Public nonce checking supplies the signed-data bound; no execution or
validity premise is needed. senders contains all recovered senders,
whose initial predicted nonces are bounded by upper.
Theorem nonce_admission_bounds eligible nonces txs senders upper allowance :
NonceAdmission eligible nonces txs →
Forall (fun tx ⇒ ∀ sender,
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
In sender senders) txs →
(∀ a, In a senders → 0 ≤ nonces a ≤ upper) →
(∀ tx, 1 + Z.of_nat (Stdlib.Lists.List.length (T.authorization_list tx)) ≤ allowance tx) →
nonceBoundedData allowance upper txs.
Proof.
intros Hcheck. revert upper.
induction Hcheck as [nonces|nonces tx rest sender Hsender Hnonce Hrest IH];
intros upper Hsenders Hbound Hallowance; [exact I|].
inversion Hsenders as [|? ? Hin Htail]; subst.
cbn [nonceBoundedData]. split.
- rewrite Hnonce. apply Hbound, Hin, Hsender.
- apply IH; [exact Htail| |exact Hallowance].
intros a Ha. pose proof (Hbound a Ha) as Hbefore.
pose proof (admitted_nonces_after_upper eligible nonces sender tx a (proj1 Hbefore)).
specialize (Hallowance tx). lia.
Qed.
Lemma nonce_admission_senders eligible nonces txs senders :
NonceAdmission eligible nonces txs →
Forall (fun tx ⇒ ∀ sender,
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
In sender senders) txs →
Forall (fun tx ⇒ ∃ sender,
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender ∧
In sender senders) txs.
Proof.
intros Hcheck. induction Hcheck as [nonces|nonces tx rest sender Hsender Hnonce Hrest IH];
intro Hsenders; [constructor|].
inversion Hsenders as [|? ? Hin Htail]; subst. constructor.
- ∃ sender. split; [exact Hsender|exact (Hin sender Hsender)].
- exact (IH Htail).
Qed.
End Admission.
Print Assumptions nonce_admission_bounds.
NonceAdmission eligible nonces txs →
Forall (fun tx ⇒ ∀ sender,
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
In sender senders) txs →
(∀ a, In a senders → 0 ≤ nonces a ≤ upper) →
(∀ tx, 1 + Z.of_nat (Stdlib.Lists.List.length (T.authorization_list tx)) ≤ allowance tx) →
nonceBoundedData allowance upper txs.
Proof.
intros Hcheck. revert upper.
induction Hcheck as [nonces|nonces tx rest sender Hsender Hnonce Hrest IH];
intros upper Hsenders Hbound Hallowance; [exact I|].
inversion Hsenders as [|? ? Hin Htail]; subst.
cbn [nonceBoundedData]. split.
- rewrite Hnonce. apply Hbound, Hin, Hsender.
- apply IH; [exact Htail| |exact Hallowance].
intros a Ha. pose proof (Hbound a Ha) as Hbefore.
pose proof (admitted_nonces_after_upper eligible nonces sender tx a (proj1 Hbefore)).
specialize (Hallowance tx). lia.
Qed.
Lemma nonce_admission_senders eligible nonces txs senders :
NonceAdmission eligible nonces txs →
Forall (fun tx ⇒ ∀ sender,
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
In sender senders) txs →
Forall (fun tx ⇒ ∃ sender,
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender ∧
In sender senders) txs.
Proof.
intros Hcheck. induction Hcheck as [nonces|nonces tx rest sender Hsender Hnonce Hrest IH];
intro Hsenders; [constructor|].
inversion Hsenders as [|? ? Hin Htail]; subst. constructor.
- ∃ sender. split; [exact Hsender|exact (Hin sender Hsender)].
- exact (IH Htail).
Qed.
End Admission.
Print Assumptions nonce_admission_bounds.