Library QedkConcrete.stakingtiming
Checked U64 arithmetic used by staking's withdrawal schedule.
Successful additions of 1 or 2 do not wrap. A request's activation epoch
is the request epoch plus 1 (plus 2 during the delay period); withdrawal
adds another 1. These lemmas establish the arithmetic lower bound, not
that every staking handler or consensus epoch transition respects it.
They use the generated numeric implementation, not a replacement model.
From Stdlib Require Import ZArith Lia Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Numeric.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module U := Numeric.U64.
Lemma checked_add_one_success number next :
U.Checked.op_plus number (U.of_int 1) = inl next →
U.to_z next = U.to_z number + 1.
Proof.
change ((match (if negb (Z.geb (number + 1) 0 &&
Z.leb (number + 1) (2^64-1)) then None else Some (number + 1)) with
| Some result ⇒ inl result
| None ⇒ inr Numeric.Overflow end) = inl next → next = number + 1).
destruct (negb _) eqn:Hrange; [discriminate|].
intro Hnext. inversion Hnext; reflexivity.
Qed.
Lemma checked_add_two_success number next :
U.Checked.op_plus number (U.of_int 2) = inl next →
U.to_z next = U.to_z number + 2.
Proof.
change ((match (if negb (Z.geb (number + 2) 0 &&
Z.leb (number + 2) (2^64-1)) then None else Some (number + 2)) with
| Some result ⇒ inl result
| None ⇒ inr Numeric.Overflow end) = inl next → next = number + 2).
destruct (negb _) eqn:Hrange; [discriminate|].
intro Hnext. inversion Hnext; reflexivity.
Qed.
Theorem checked_withdrawal_delay_lower_bound request (delayed : bool) activation ready current :
U.Checked.op_plus request (U.of_int (if delayed then 2 else 1)) = inl activation →
U.Checked.op_plus activation (U.of_int 1) = inl ready →
U.op_lteq ready current = true →
U.to_z request + (if delayed then 3 else 2) ≤ U.to_z current.
Proof.
intros Hactivation Hready Hcurrent.
apply checked_add_one_success in Hready.
change (Z.leb (match Z.compare ready current with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hcurrent.
assert (Hle : ready ≤ current).
{ destruct (Z.compare ready current) eqn:Hcompare.
- apply Z.compare_eq_iff in Hcompare. lia.
- exact (Z.lt_le_incl _ _ (proj1 (Z.compare_lt_iff _ _) Hcompare)).
- discriminate. }
destruct delayed.
- apply checked_add_two_success in Hactivation.
change (ready = activation + 1) in Hready.
change (activation = request + 2) in Hactivation.
change (request + 3 ≤ current). lia.
- apply checked_add_one_success in Hactivation.
change (ready = activation + 1) in Hready.
change (activation = request + 1) in Hactivation.
change (request + 2 ≤ current). lia.
Qed.
Example max_epoch_addition_fails :
U.Checked.op_plus U.max_t (U.of_int 1) = inr Numeric.Overflow.
Proof. vm_compute. reflexivity. Qed.
Example penultimate_epoch_add_two_fails :
U.Checked.op_plus (U.op_minus U.max_t U.one) (U.of_int 2) = inr Numeric.Overflow.
Proof. vm_compute. reflexivity. Qed.
Print Assumptions checked_add_one_success.
Print Assumptions checked_add_two_success.
Print Assumptions checked_withdrawal_delay_lower_bound.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Numeric.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module U := Numeric.U64.
Lemma checked_add_one_success number next :
U.Checked.op_plus number (U.of_int 1) = inl next →
U.to_z next = U.to_z number + 1.
Proof.
change ((match (if negb (Z.geb (number + 1) 0 &&
Z.leb (number + 1) (2^64-1)) then None else Some (number + 1)) with
| Some result ⇒ inl result
| None ⇒ inr Numeric.Overflow end) = inl next → next = number + 1).
destruct (negb _) eqn:Hrange; [discriminate|].
intro Hnext. inversion Hnext; reflexivity.
Qed.
Lemma checked_add_two_success number next :
U.Checked.op_plus number (U.of_int 2) = inl next →
U.to_z next = U.to_z number + 2.
Proof.
change ((match (if negb (Z.geb (number + 2) 0 &&
Z.leb (number + 2) (2^64-1)) then None else Some (number + 2)) with
| Some result ⇒ inl result
| None ⇒ inr Numeric.Overflow end) = inl next → next = number + 2).
destruct (negb _) eqn:Hrange; [discriminate|].
intro Hnext. inversion Hnext; reflexivity.
Qed.
Theorem checked_withdrawal_delay_lower_bound request (delayed : bool) activation ready current :
U.Checked.op_plus request (U.of_int (if delayed then 2 else 1)) = inl activation →
U.Checked.op_plus activation (U.of_int 1) = inl ready →
U.op_lteq ready current = true →
U.to_z request + (if delayed then 3 else 2) ≤ U.to_z current.
Proof.
intros Hactivation Hready Hcurrent.
apply checked_add_one_success in Hready.
change (Z.leb (match Z.compare ready current with
| Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hcurrent.
assert (Hle : ready ≤ current).
{ destruct (Z.compare ready current) eqn:Hcompare.
- apply Z.compare_eq_iff in Hcompare. lia.
- exact (Z.lt_le_incl _ _ (proj1 (Z.compare_lt_iff _ _) Hcompare)).
- discriminate. }
destruct delayed.
- apply checked_add_two_success in Hactivation.
change (ready = activation + 1) in Hready.
change (activation = request + 2) in Hactivation.
change (request + 3 ≤ current). lia.
- apply checked_add_one_success in Hactivation.
change (ready = activation + 1) in Hready.
change (activation = request + 1) in Hactivation.
change (request + 2 ≤ current). lia.
Qed.
Example max_epoch_addition_fails :
U.Checked.op_plus U.max_t (U.of_int 1) = inr Numeric.Overflow.
Proof. vm_compute. reflexivity. Qed.
Example penultimate_epoch_add_two_fails :
U.Checked.op_plus (U.op_minus U.max_t U.one) (U.of_int 2) = inr Numeric.Overflow.
Proof. vm_compute. reflexivity. Qed.
Print Assumptions checked_add_one_success.
Print Assumptions checked_add_two_success.
Print Assumptions checked_withdrawal_delay_lower_bound.