Skip to main content

crypto_bigint/uint/
cmp.rs

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