ruint/
cmp.rs

1use crate::Uint;
2use core::cmp::Ordering;
3
4impl<const BITS: usize, const LIMBS: usize> PartialOrd for Uint<BITS, LIMBS> {
5    #[inline]
6    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
7        Some(self.cmp(other))
8    }
9}
10
11impl<const BITS: usize, const LIMBS: usize> Ord for Uint<BITS, LIMBS> {
12    #[cfg(not(target_os = "zkvm"))]
13    #[inline]
14    fn cmp(&self, rhs: &Self) -> Ordering {
15        crate::algorithms::cmp(self.as_limbs(), rhs.as_limbs())
16    }
17
18    #[cfg(target_os = "zkvm")]
19    #[inline]
20    fn cmp(&self, rhs: &Self) -> Ordering {
21        use crate::support::zkvm::zkvm_u256_cmp_impl;
22        if BITS == 256 {
23            return unsafe {
24                zkvm_u256_cmp_impl(
25                    self.limbs.as_ptr() as *const u8,
26                    rhs.limbs.as_ptr() as *const u8,
27                )
28            };
29        }
30        self.cmp(rhs)
31    }
32}
33
34impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
35    /// Returns true if the value is zero.
36    #[inline]
37    #[must_use]
38    pub fn is_zero(&self) -> bool {
39        *self == Self::ZERO
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::Uint;
46
47    #[test]
48    fn test_is_zero() {
49        assert!(Uint::<0, 0>::ZERO.is_zero());
50        assert!(Uint::<1, 1>::ZERO.is_zero());
51        assert!(Uint::<7, 1>::ZERO.is_zero());
52        assert!(Uint::<64, 1>::ZERO.is_zero());
53
54        assert!(!Uint::<1, 1>::from_limbs([1]).is_zero());
55        assert!(!Uint::<7, 1>::from_limbs([1]).is_zero());
56        assert!(!Uint::<64, 1>::from_limbs([1]).is_zero());
57    }
58}