crypto_bigint/
wrapping.rs

1//! Wrapping arithmetic.
2
3use crate::Zero;
4use core::fmt;
5use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
6
7#[cfg(feature = "rand_core")]
8use {crate::Random, rand_core::CryptoRngCore};
9
10#[cfg(feature = "serde")]
11use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};
12
13/// Provides intentionally-wrapped arithmetic on `T`.
14///
15/// This is analogous to [`core::num::Wrapping`] but allows this crate to
16/// define trait impls for this type.
17#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
18pub struct Wrapping<T>(pub T);
19
20impl<T: Zero> Zero for Wrapping<T> {
21    const ZERO: Self = Self(T::ZERO);
22}
23
24impl<T: fmt::Display> fmt::Display for Wrapping<T> {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        self.0.fmt(f)
27    }
28}
29
30impl<T: fmt::Binary> fmt::Binary for Wrapping<T> {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        self.0.fmt(f)
33    }
34}
35
36impl<T: fmt::Octal> fmt::Octal for Wrapping<T> {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        self.0.fmt(f)
39    }
40}
41
42impl<T: fmt::LowerHex> fmt::LowerHex for Wrapping<T> {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        self.0.fmt(f)
45    }
46}
47
48impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        self.0.fmt(f)
51    }
52}
53
54impl<T: ConditionallySelectable> ConditionallySelectable for Wrapping<T> {
55    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
56        Wrapping(T::conditional_select(&a.0, &b.0, choice))
57    }
58}
59
60impl<T: ConstantTimeEq> ConstantTimeEq for Wrapping<T> {
61    fn ct_eq(&self, other: &Self) -> Choice {
62        self.0.ct_eq(&other.0)
63    }
64}
65
66#[cfg(feature = "rand_core")]
67impl<T: Random> Random for Wrapping<T> {
68    fn random(rng: &mut impl CryptoRngCore) -> Self {
69        Wrapping(Random::random(rng))
70    }
71}
72
73#[cfg(feature = "serde")]
74impl<'de, T: Deserialize<'de>> Deserialize<'de> for Wrapping<T> {
75    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
76    where
77        D: Deserializer<'de>,
78    {
79        Ok(Self(T::deserialize(deserializer)?))
80    }
81}
82
83#[cfg(feature = "serde")]
84impl<T: Serialize> Serialize for Wrapping<T> {
85    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
86    where
87        S: Serializer,
88    {
89        self.0.serialize(serializer)
90    }
91}
92
93#[cfg(all(test, feature = "serde"))]
94#[allow(clippy::unwrap_used)]
95mod tests {
96    use crate::{Wrapping, U64};
97
98    #[test]
99    fn serde() {
100        const TEST: Wrapping<U64> = Wrapping(U64::from_u64(0x0011223344556677));
101
102        let serialized = bincode::serialize(&TEST).unwrap();
103        let deserialized: Wrapping<U64> = bincode::deserialize(&serialized).unwrap();
104
105        assert_eq!(TEST, deserialized);
106    }
107
108    #[test]
109    fn serde_owned() {
110        const TEST: Wrapping<U64> = Wrapping(U64::from_u64(0x0011223344556677));
111
112        let serialized = bincode::serialize(&TEST).unwrap();
113        let deserialized: Wrapping<U64> = bincode::deserialize_from(serialized.as_slice()).unwrap();
114
115        assert_eq!(TEST, deserialized);
116    }
117}