From df112b5e7a46c18683dc5d744daf7271e02b0538 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 6 Jan 2025 15:50:31 -0700 Subject: [PATCH] Impl `num_traits::Num` for `Uint` Also adds the needed `Div` and `Rem` impls to meet the required bounds --- src/uint.rs | 9 +++++++++ src/uint/div.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/uint.rs b/src/uint.rs index 44a1853d..304d2c60 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -258,6 +258,15 @@ impl Integer for Uint { } } +impl num_traits::Num for Uint { + type FromStrRadixErr = crate::DecodeError; + + /// ⚠️ WARNING: `from_str_radix` impl operates in variable-time with respect to the input. + fn from_str_radix(str: &str, radix: u32) -> Result { + Self::from_str_radix_vartime(str, radix) + } +} + impl ConstZero for Uint { const ZERO: Self = Self::ZERO; } diff --git a/src/uint/div.rs b/src/uint/div.rs index f39c3255..95668088 100644 --- a/src/uint/div.rs +++ b/src/uint/div.rs @@ -803,6 +803,24 @@ impl Div<&NonZero>> for Wrapping> { } } +impl Div> for &Uint { + type Output = Uint; + + #[inline] + fn div(self, rhs: Uint) -> Self::Output { + self / NonZero::new(rhs).expect("attempt to divide with a divisor of zero") + } +} + +impl Div> for Uint { + type Output = Uint; + + #[inline] + fn div(self, rhs: Uint) -> Self::Output { + &self / rhs + } +} + impl DivAssign<&NonZero>> for Wrapping> { fn div_assign(&mut self, rhs: &NonZero>) { *self = Wrapping(self.0 / rhs); @@ -847,6 +865,24 @@ impl Rem>> for Uint { } } +impl Rem> for &Uint { + type Output = Uint; + + #[inline] + fn rem(self, rhs: Uint) -> Self::Output { + self % NonZero::new(rhs).expect("attempt to calculate the remainder with a divisor of zero") + } +} + +impl Rem> for Uint { + type Output = Uint; + + #[inline] + fn rem(self, rhs: Uint) -> Self::Output { + &self % rhs + } +} + impl RemAssign<&NonZero>> for Uint { fn rem_assign(&mut self, rhs: &NonZero>) { *self %= *rhs