Skip to main content

crypto_bigint/limb/
cmp.rs

1//! Limb comparisons
2
3use super::Limb;
4use core::cmp::Ordering;
5use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
6
7impl Limb {
8    /// Is this limb equal to zero?
9    #[inline]
10    pub fn is_zero(&self) -> Choice {
11        self.ct_eq(&Self::ZERO)
12    }
13
14    /// Is this limb an odd number?
15    #[inline]
16    pub fn is_odd(&self) -> Choice {
17        Choice::from(self.0 as u8 & 1)
18    }
19
20    /// Perform a comparison of the inner value in variable-time.
21    ///
22    /// Note that the [`PartialOrd`] and [`Ord`] impls wrap constant-time
23    /// comparisons using the `subtle` crate.
24    pub fn cmp_vartime(&self, other: &Self) -> Ordering {
25        self.0.cmp(&other.0)
26    }
27
28    /// Performs an equality check in variable-time.
29    pub const fn eq_vartime(&self, other: &Self) -> bool {
30        self.0 == other.0
31    }
32}
33
34impl ConstantTimeEq for Limb {
35    #[inline]
36    fn ct_eq(&self, other: &Self) -> Choice {
37        self.0.ct_eq(&other.0)
38    }
39}
40
41impl ConstantTimeGreater for Limb {
42    #[inline]
43    fn ct_gt(&self, other: &Self) -> Choice {
44        self.0.ct_gt(&other.0)
45    }
46}
47
48impl ConstantTimeLess for Limb {
49    #[inline]
50    fn ct_lt(&self, other: &Self) -> Choice {
51        self.0.ct_lt(&other.0)
52    }
53}
54
55impl Eq for Limb {}
56
57impl Ord for Limb {
58    fn cmp(&self, other: &Self) -> Ordering {
59        let mut n = 0i8;
60        n -= self.ct_lt(other).unwrap_u8() as i8;
61        n += self.ct_gt(other).unwrap_u8() as i8;
62
63        match n {
64            -1 => Ordering::Less,
65            1 => Ordering::Greater,
66            _ => {
67                debug_assert_eq!(n, 0);
68                debug_assert!(bool::from(self.ct_eq(other)));
69                Ordering::Equal
70            }
71        }
72    }
73}
74
75impl PartialOrd for Limb {
76    #[inline]
77    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
78        Some(self.cmp(other))
79    }
80}
81
82impl PartialEq for Limb {
83    #[inline]
84    fn eq(&self, other: &Self) -> bool {
85        self.ct_eq(other).into()
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use crate::Limb;
92    use core::cmp::Ordering;
93    use subtle::{ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
94
95    #[test]
96    fn is_zero() {
97        assert!(bool::from(Limb::ZERO.is_zero()));
98        assert!(!bool::from(Limb::ONE.is_zero()));
99        assert!(!bool::from(Limb::MAX.is_zero()));
100    }
101
102    #[test]
103    fn is_odd() {
104        assert!(!bool::from(Limb::ZERO.is_odd()));
105        assert!(bool::from(Limb::ONE.is_odd()));
106        assert!(bool::from(Limb::MAX.is_odd()));
107    }
108
109    #[test]
110    fn ct_eq() {
111        let a = Limb::ZERO;
112        let b = Limb::MAX;
113
114        assert!(bool::from(a.ct_eq(&a)));
115        assert!(!bool::from(a.ct_eq(&b)));
116        assert!(!bool::from(b.ct_eq(&a)));
117        assert!(bool::from(b.ct_eq(&b)));
118    }
119
120    #[test]
121    fn ct_gt() {
122        let a = Limb::ZERO;
123        let b = Limb::ONE;
124        let c = Limb::MAX;
125
126        assert!(bool::from(b.ct_gt(&a)));
127        assert!(bool::from(c.ct_gt(&a)));
128        assert!(bool::from(c.ct_gt(&b)));
129
130        assert!(!bool::from(a.ct_gt(&a)));
131        assert!(!bool::from(b.ct_gt(&b)));
132        assert!(!bool::from(c.ct_gt(&c)));
133
134        assert!(!bool::from(a.ct_gt(&b)));
135        assert!(!bool::from(a.ct_gt(&c)));
136        assert!(!bool::from(b.ct_gt(&c)));
137    }
138
139    #[test]
140    fn ct_lt() {
141        let a = Limb::ZERO;
142        let b = Limb::ONE;
143        let c = Limb::MAX;
144
145        assert!(bool::from(a.ct_lt(&b)));
146        assert!(bool::from(a.ct_lt(&c)));
147        assert!(bool::from(b.ct_lt(&c)));
148
149        assert!(!bool::from(a.ct_lt(&a)));
150        assert!(!bool::from(b.ct_lt(&b)));
151        assert!(!bool::from(c.ct_lt(&c)));
152
153        assert!(!bool::from(b.ct_lt(&a)));
154        assert!(!bool::from(c.ct_lt(&a)));
155        assert!(!bool::from(c.ct_lt(&b)));
156    }
157
158    #[test]
159    fn cmp() {
160        assert_eq!(Limb::ZERO.cmp(&Limb::ONE), Ordering::Less);
161        assert_eq!(Limb::ONE.cmp(&Limb::ONE), Ordering::Equal);
162        assert_eq!(Limb::MAX.cmp(&Limb::ONE), Ordering::Greater);
163    }
164}