verif_arithmetic/bounded.rs
1//! Drop-in integer operations, and a refinement-typed integer.
2//!
3//! The `safe_*` functions are the total-function route to overflow: each returns
4//! an `Option`, and [`crate::prove`] carries the proof that the `None` case is
5//! exactly the overflow case.
6//!
7//! [`Bounded`] is the type-level route: a value known at compile time to lie in
8//! `[MIN, MAX]`. The constructors are the only way in, so the invariant cannot
9//! be broken by construction, and [`crate::prove::bounded_sub_is_total`] proves
10//! that [`Bounded::sub`] accepts exactly the in-range cases.
11
12/// `a + b`, or `None` on overflow.
13pub const fn safe_add(a: u32, b: u32) -> Option<u32> {
14 a.checked_add(b)
15}
16
17/// `a - b`, or `None` on underflow.
18pub const fn safe_sub(a: u32, b: u32) -> Option<u32> {
19 a.checked_sub(b)
20}
21
22/// `a * b`, or `None` on overflow.
23pub const fn safe_mul(a: u32, b: u32) -> Option<u32> {
24 a.checked_mul(b)
25}
26
27/// `a / b`, or `None` when `b == 0`. Division is *defined* for every nonzero
28/// divisor, so the only failure mode is the one made explicit here.
29pub const fn safe_div(a: u32, b: u32) -> Option<u32> {
30 a.checked_div(b)
31}
32
33/// `a % b`, or `None` when `b == 0`.
34pub const fn safe_rem(a: u32, b: u32) -> Option<u32> {
35 a.checked_rem(b)
36}
37
38/// A value known at compile time to lie in `[MIN, MAX]`.
39///
40/// The constructor is the only way in, so `Bounded<MIN, MAX>` is a
41/// refinement-typed integer: the invariant cannot be broken by construction.
42///
43/// Both constructors, [`new`](Self::new) and [`clamp`](Self::clamp), refuse an
44/// *empty* range: when `MIN > MAX` no `u64` satisfies `MIN <= v <= MAX`, so
45/// neither can produce a value. `clamp` used to return `MIN` in that case, which
46/// is outside `[LOW, HIGH]` and so broke the very invariant the type exists to
47/// carry.
48#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
49pub struct Bounded<const MIN: u64, const MAX: u64>(u64);
50
51impl<const MIN: u64, const MAX: u64> Bounded<MIN, MAX> {
52 /// The inclusive lower bound.
53 pub const LOW: u64 = MIN;
54 /// The inclusive upper bound.
55 pub const HIGH: u64 = MAX;
56
57 /// Construct, or `None` when the value is outside the range.
58 pub const fn new(v: u64) -> Option<Self> {
59 if MIN <= v && v <= MAX {
60 Some(Bounded(v))
61 } else {
62 None
63 }
64 }
65
66 /// The raw value. Always within `[MIN, MAX]` by construction.
67 pub const fn get(self) -> u64 {
68 self.0
69 }
70
71 /// Clamp an arbitrary value into the range, or `None` when the range is empty.
72 ///
73 /// `MIN > MAX` is an empty range: no `u64` satisfies `MIN <= v <= MAX`, so
74 /// there is no value to return. Returning `MIN` (the old behaviour) produced
75 /// a `Bounded` whose `get()` is outside its own `[LOW, HIGH]`, which breaks
76 /// the invariant the type exists to carry -- `Bounded<100, 10>::clamp(50)`
77 /// gave `100`, and `100 <= 10` is false. [`new`](Self::new) already returns
78 /// `None` for the same range, so the two constructors agree: an empty range
79 /// admits nothing.
80 pub const fn clamp(v: u64) -> Option<Self> {
81 if MIN > MAX {
82 return None;
83 }
84 Some(if v < MIN {
85 Bounded(MIN)
86 } else if v > MAX {
87 Bounded(MAX)
88 } else {
89 Bounded(v)
90 })
91 }
92
93 /// Add, or `None` if the sum leaves the range.
94 pub const fn add(self, other: Self) -> Option<Self> {
95 match self.0.checked_add(other.0) {
96 Some(s) if MIN <= s && s <= MAX => Some(Bounded(s)),
97 _ => None,
98 }
99 }
100
101 /// Subtract, or `None` if the difference leaves the range.
102 ///
103 /// Accepts exactly when `other <= self` **and** the difference is still
104 /// `>= MIN`. Both halves matter: at `MIN == 10`, `self == 11` and
105 /// `other == 10` are in range and `other <= self`, yet the difference `1` is
106 /// below `MIN`, so this returns `None`. The underflow gate is the other
107 /// half -- without it a wrapping difference can land back in range and be
108 /// accepted. [`crate::prove::bounded_sub_is_total`] is the obligation that
109 /// proves this is exactly the condition.
110 pub const fn sub(self, other: Self) -> Option<Self> {
111 match self.0.checked_sub(other.0) {
112 Some(d) if MIN <= d && d <= MAX => Some(Bounded(d)),
113 _ => None,
114 }
115 }
116}