Skip to main content

crypto_bigint/
wrapping.rs

1//! Wrapping arithmetic.
2
3use core::fmt;
4use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
5
6/// Provides intentionally-wrapped arithmetic on `T`.
7///
8/// This is analogous to [`core::num::Wrapping`] but allows this crate to
9/// define trait impls for this type.
10#[derive(Copy, Clone, Default, Eq, PartialEq, PartialOrd, Ord)]
11pub struct Wrapping<T>(pub T);
12
13impl<T: fmt::Display> fmt::Display for Wrapping<T> {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        self.0.fmt(f)
16    }
17}
18
19impl<T: fmt::Binary> fmt::Binary for Wrapping<T> {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        self.0.fmt(f)
22    }
23}
24
25impl<T: fmt::Octal> fmt::Octal for Wrapping<T> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        self.0.fmt(f)
28    }
29}
30
31impl<T: fmt::LowerHex> fmt::LowerHex for Wrapping<T> {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        self.0.fmt(f)
34    }
35}
36
37impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        self.0.fmt(f)
40    }
41}
42
43impl<T: ConditionallySelectable> ConditionallySelectable for Wrapping<T> {
44    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
45        Wrapping(T::conditional_select(&a.0, &b.0, choice))
46    }
47}
48
49impl<T: ConstantTimeEq> ConstantTimeEq for Wrapping<T> {
50    fn ct_eq(&self, other: &Self) -> Choice {
51        self.0.ct_eq(&other.0)
52    }
53}