Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rand_core = { version = "0.6.4", optional = true }
rlp = { version = "0.5", optional = true, default-features = false }
serdect = { version = "0.2", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }
num-traits = { version = "0.2", optional = true }

[dev-dependencies]
bincode = "1"
Expand Down
89 changes: 89 additions & 0 deletions src/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ use crate::Zero;
use core::fmt;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

#[cfg(feature = "num-traits")]
use crate::{Integer, NonZero};
#[cfg(feature = "num-traits")]
use core::ops::{Add, Div, Mul, Rem, Sub};
#[cfg(feature = "num-traits")]
use num_traits::Num;

#[cfg(feature = "rand_core")]
use {crate::Random, rand_core::CryptoRngCore};

Expand Down Expand Up @@ -63,6 +70,70 @@ impl<T: ConstantTimeEq> ConstantTimeEq for Wrapping<T> {
}
}

#[cfg(feature = "num-traits")]
impl<T: Zero> num_traits::Zero for Wrapping<T>
where
Wrapping<T>: Add<Wrapping<T>, Output = Wrapping<T>> + PartialEq<Wrapping<T>>,
{
fn zero() -> Self {
Self::ZERO
}

fn is_zero(&self) -> bool {
self == &Self::ZERO
}
}

#[cfg(feature = "num-traits")]
impl<T: Integer> num_traits::One for Wrapping<T>
where
Wrapping<T>: Add<Wrapping<T>, Output = Wrapping<T>> + Mul<Wrapping<T>, Output = Wrapping<T>>,
{
fn one() -> Self {
Wrapping(T::ONE)
}
}

#[cfg(feature = "num-traits")]
impl<T: Zero> Div<Wrapping<T>> for Wrapping<T>
where
Wrapping<T>: Div<NonZero<T>, Output = Wrapping<T>>,
{
type Output = Wrapping<T>;

fn div(self, rhs: Wrapping<T>) -> Self::Output {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and rem panics in case of division by zero.
This is the intended behavior of this crate, but I'm not sure that "this is the right panic" - i.e. now we'll panic on unwrap()ping of NonZero on zero, and the panic message might not be ideal; should I instead assert_ne(rhs, T::Zero) instead?

self / NonZero::new(rhs.0).unwrap()
}
}

#[cfg(feature = "num-traits")]
impl<T: Zero> Rem<Wrapping<T>> for Wrapping<T>
where
Wrapping<T>: Rem<NonZero<T>, Output = Wrapping<T>>,
{
type Output = Wrapping<T>;

fn rem(self, rhs: Wrapping<T>) -> Self::Output {
self % NonZero::new(rhs.0).unwrap()
}
}

#[cfg(feature = "num-traits")]
impl<T: Integer + Zero> Num for Wrapping<T>
where
Wrapping<T>: Add<Wrapping<T>, Output = Wrapping<T>>
+ Sub<Wrapping<T>, Output = Wrapping<T>>
+ Mul<Wrapping<T>, Output = Wrapping<T>>
+ Div<NonZero<T>, Output = Wrapping<T>>
+ Rem<NonZero<T>, Output = Wrapping<T>>,
{
type FromStrRadixErr = ();

fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we want to handle this?

Obviously this is unimplemented logic for this crate, and I'm not sure whether we want to support this (or, for example, return an error for any radix other than 2, 10 or 16.)

num-bigint's implementation is complex, but I could copy it if that's the way to go.

My thoughts are to actually to use from_str_radix() implementation of u32 and u64, that is, to parse limb-by-limb and sum them up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#246 and/or #247 are some options, although I took issue with them not being constant-time

todo!()
}
}

#[cfg(feature = "rand_core")]
impl<T: Random> Random for Wrapping<T> {
fn random(rng: &mut impl CryptoRngCore) -> Self {
Expand Down Expand Up @@ -90,6 +161,24 @@ impl<T: Serialize> Serialize for Wrapping<T> {
}
}

#[cfg(all(test, feature = "num-traits"))]
mod tests {
use crate::{Integer, NonZero};
use core::ops::{Add, Div, Mul, Rem, Sub};
use num_traits::{Num, NumOps, One, Zero};

use crate::{Wrapping, U64};

fn assures_num<T: Num>(x: T) {}

#[test]
fn num_traits() {
const TEST: Wrapping<U64> = Wrapping(U64::from_u64(0x0011223344556677));

assures_num(TEST)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My logic here is: Num is implemented for Wrapping<T> where ... so that we're implementing it for the minimally restricted type, but we want to assure that it works for Wrapping<Uint<Limbs>>, so this code assures it.

I'm not sure if there's a better way of checking this.

}
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use crate::{Wrapping, U64};
Expand Down