Library SetcodeConcrete.admissionnonceagreement

Nonce prediction for one still-eligible sender

Migration of another account may invalidate its predicted nonce and authorization eligibility. Neither affects this account's prediction: an authorization increments only its own recovered authority.
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml QedkConcrete.admissionnonces
  QedkConcrete.authorizationnoncechecks QedkConcrete.authorizationnoncecore.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Agreement.
Context {F : E.FArgs}.
Lemma authorization_step_pointwise eligible nonces otherEligible otherNonces auth query :
  eligible query = otherEligible query → nonces query = otherNonces query →
  snd (authorizationNonceStep eligible nonces auth) query =
  snd (authorizationNonceStep otherEligible otherNonces auth) query.
Proof.
  intros Heligible Hnonce. unfold authorizationNonceStep.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
    [exact Hnonce|].
  destruct (T.Authorization.authority auth) as [authority|]; [|exact Hnonce].
  destruct (String.eqb authority query) eqn:Heq.
  - apply String.eqb_eq in Heq. subst authority. rewrite Heligible, Hnonce.
    match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
      cbn; rewrite ?String.eqb_refl, ?Hnonce; reflexivity.
  - repeat match goal with |- context [if ?test then _ else _] ⇒
      destruct test end; cbn; rewrite ?Heq; exact Hnonce.
Qed.

Lemma authorizations_pointwise eligible nonces otherEligible otherNonces
    auths applied otherApplied query :
  eligible query = otherEligible query → nonces query = otherNonces query →
  snd (authorizationNonces eligible (applied, nonces) auths) query =
  snd (authorizationNonces otherEligible (otherApplied, otherNonces) auths) query.
Proof.
  revert nonces otherNonces applied otherApplied.
  induction auths as [|auth rest IH]; intros nonces otherNonces applied otherApplied He Hn;
    [exact Hn|].
  pose proof (authorization_step_pointwise eligible nonces otherEligible otherNonces auth query He Hn) as Hstep.
  unfold authorizationNonces. cbn [OCamlList.fold_left].
  destruct (authorizationNonceStep eligible nonces auth) as [a ns].
  destruct (authorizationNonceStep otherEligible otherNonces auth) as [b ms].
  cbn [snd] in Hstep. destruct a, b; apply IH; assumption.
Qed.

Lemma admitted_nonces_pointwise eligible nonces otherEligible otherNonces sender tx query :
  eligible query = otherEligible query → nonces query = otherNonces query →
  admittedNoncesAfter eligible nonces sender tx query =
  admittedNoncesAfter otherEligible otherNonces sender tx query.
Proof.
  intros He Hn. unfold admittedNoncesAfter. apply authorizations_pointwise; [exact He|].
  destruct (String.eqb sender query); rewrite Hn; reflexivity.
Qed.
Only this account's code classification and nonce must agree. The actual and predicted applied-authority lists may differ elsewhere.
Lemma actual_authorizations_pointwise eligible nonces auths applied predictedApplied s query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) auths →
  negb (A.is_smart_contract (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query))) = eligible query →
  A.t.nonce (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None query)) = nonces query →
  let actual := Result.List.fold_left
    (fun '(previous, state) auth ⇒
      match E.process_authorization state auth with
      | (Some a, next) ⇒ (a :: previous, next)
      | (None, next) ⇒ (previous, next) end) (applied, s) auths in
  negb (A.is_smart_contract (State.op_pointcaretlparenrparen
    (S.t.world_state (snd actual)) (W.account None query))) = eligible query ∧
  A.t.nonce (State.op_pointcaretlparenrparen
    (S.t.world_state (snd actual)) (W.account None query)) =
    snd (authorizationNonces eligible (predictedApplied, nonces) auths) query.
Proof.
  intros Hmap Hwidth He Hn actual.
  set (ownEligible := fun a ⇒ negb (A.is_smart_contract
    (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None a)))).
  set (ownNonces := fun a ⇒ A.t.nonce
    (State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None a))).
  pose proof (authorizations_nonce_projection ownEligible auths applied ownNonces s
    Hmap Hwidth (fun a ⇒ eq_refl) (fun a ⇒ eq_refl)) as [_ Hprojection].
  destruct (Hprojection query) as [Hcode Hnonce]. split.
  - exact (eq_trans Hcode He).
  - etransitivity; [exact Hnonce|]. apply authorizations_pointwise; assumption.
Qed.
End Agreement.