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
6 changes: 0 additions & 6 deletions src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn overflowing_add(mut self, rhs: Self) -> (Self, bool) {
if BITS == 0 {
return (Self::ZERO, false);
}
let mut carry = false;
let mut i = 0;
while i < LIMBS {
Expand Down Expand Up @@ -92,9 +89,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn overflowing_sub(mut self, rhs: Self) -> (Self, bool) {
if BITS == 0 {
return (Self::ZERO, false);
}
let mut borrow = false;
let mut i = 0;
while i < LIMBS {
Expand Down
3 changes: 0 additions & 3 deletions src/algorithms/gcd/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ impl Matrix {
a: &mut Uint<BITS, LIMBS>,
b: &mut Uint<BITS, LIMBS>,
) {
if BITS == 0 {
return;
}
// OPT: We can avoid the temporary if we implement a dedicated matrix
// multiplication.
let (c, d) = if self.4 {
Expand Down
5 changes: 1 addition & 4 deletions src/algorithms/gcd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ pub fn gcd_extended<const BITS: usize, const LIMBS: usize>(
Uint<BITS, LIMBS>,
bool,
) {
if BITS == 0 {
return (Uint::ZERO, Uint::ZERO, Uint::ZERO, false);
}
let swapped = a < b;
if swapped {
swap(&mut a, &mut b);
Expand Down Expand Up @@ -142,7 +139,7 @@ pub fn inv_mod<const BITS: usize, const LIMBS: usize>(
num: Uint<BITS, LIMBS>,
modulus: Uint<BITS, LIMBS>,
) -> Option<Uint<BITS, LIMBS>> {
if BITS == 0 || modulus.is_zero() {
if modulus.is_zero() {
return None;
}
let mut a = modulus;
Expand Down
3 changes: 1 addition & 2 deletions src/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{Bits, Uint};

/// [`Uint`] for `0` bits. Always zero. Similar to `()`.
#[deprecated = "do not use"]
pub type U0 = Uint<0, 0>;

/// [`Uint`] for `1` bit. Similar to [`bool`].
Expand Down Expand Up @@ -30,7 +31,6 @@ macro_rules! bit_alias {
}

bit_alias! {
B0(0, 0);
B1(1, 1);
B8(8, 1);
B16(16, 1);
Expand Down Expand Up @@ -70,7 +70,6 @@ mod tests {

#[test]
const fn instantiate_consts() {
let _ = (U0::ZERO, U0::MAX, B0::ZERO);
let _ = (U1::ZERO, U1::MAX, B1::ZERO);
let _ = (U8::ZERO, U8::MAX, B8::ZERO);
let _ = (U16::ZERO, U16::MAX, B16::ZERO);
Expand Down
23 changes: 0 additions & 23 deletions src/base_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
if base < 2 {
return Err(BaseConvertError::InvalidBase(base));
}
if BITS == 0 {
for digit in digits {
if digit >= base {
return Err(BaseConvertError::InvalidDigit(digit, base));
}
if digit != 0 {
return Err(BaseConvertError::Overflow);
}
}
return Ok(Self::ZERO);
}

let mut iter = digits.into_iter();
let mut result = Self::ZERO;
Expand Down Expand Up @@ -533,18 +522,6 @@ mod tests {

#[test]
fn test_from_base_be_overflow() {
assert_eq!(
Uint::<0, 0>::from_base_be(10, core::iter::empty()),
Ok(Uint::<0, 0>::ZERO)
);
assert_eq!(
Uint::<0, 0>::from_base_be(10, core::iter::once(0)),
Ok(Uint::<0, 0>::ZERO)
);
assert_eq!(
Uint::<0, 0>::from_base_be(10, core::iter::once(1)),
Err(BaseConvertError::Overflow)
);
assert_eq!(
Uint::<1, 1>::from_base_be(10, [1, 0, 0].into_iter()),
Err(BaseConvertError::Overflow)
Expand Down
20 changes: 0 additions & 20 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn not(mut self) -> Self {
if BITS == 0 {
return Self::ZERO;
}
let mut i = 0;
while i < LIMBS {
self.limbs[i] = !self.limbs[i];
Expand Down Expand Up @@ -350,9 +347,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// See [`overflowing_shl`](Self::overflowing_shl) for details.
#[inline]
pub(crate) fn overflowing_shl_big(self, rhs: Self) -> (Self, bool) {
if BITS == 0 {
return (Self::ZERO, false);
}
let Ok(rhs) = u64::try_from(rhs) else {
return (Self::ZERO, true);
};
Expand Down Expand Up @@ -433,9 +427,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// See [`overflowing_shr`](Self::overflowing_shr) for details.
#[inline]
pub(crate) fn overflowing_shr_big(self, rhs: Self) -> (Self, bool) {
if BITS == 0 {
return (Self::ZERO, false);
}
let Ok(rhs) = u64::try_from(rhs) else {
return (Self::ZERO, true);
};
Expand Down Expand Up @@ -465,9 +456,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn arithmetic_shr(self, rhs: usize) -> Self {
if BITS == 0 {
return Self::ZERO;
}
let sign = self.bit(BITS - 1);
let mut r = self.wrapping_shr(rhs);
if sign {
Expand All @@ -482,9 +470,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn rotate_left(self, rhs: usize) -> Self {
if BITS == 0 {
return Self::ZERO;
}
let rhs = rhs % BITS;
// (self << rhs) | (self >> (BITS - rhs))
self.wrapping_shl(rhs).bitor(self.wrapping_shr(BITS - rhs))
Expand All @@ -495,9 +480,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline(always)]
#[must_use]
pub const fn rotate_right(self, rhs: usize) -> Self {
if BITS == 0 {
return Self::ZERO;
}
let rhs = rhs % BITS;
self.rotate_left(BITS - rhs)
}
Expand Down Expand Up @@ -764,7 +746,6 @@ mod tests {

#[test]
fn test_leading_zeros() {
assert_eq!(Uint::<0, 0>::ZERO.leading_zeros(), 0);
assert_eq!(Uint::<1, 1>::ZERO.leading_zeros(), 1);
assert_eq!(Uint::<1, 1>::ONE.leading_zeros(), 0);
const_for!(BITS in NON_ZERO {
Expand Down Expand Up @@ -802,7 +783,6 @@ mod tests {

#[test]
fn test_leading_ones() {
assert_eq!(Uint::<0, 0>::ZERO.leading_ones(), 0);
assert_eq!(Uint::<1, 1>::ZERO.leading_ones(), 0);
assert_eq!(Uint::<1, 1>::ONE.leading_ones(), 1);
}
Expand Down
7 changes: 0 additions & 7 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ mod tests {
assert!(matches!(K.to_be_bytes::<{ KBE.len() }>(), KBE));
assert!(matches!(K.to_le_bytes::<{ KLE.len() }>(), KLE));

assert!(matches!(Uint::<0, 0>::ZERO.to_be_bytes::<0>(), []));
assert!(matches!(Uint::<1, 1>::ZERO.to_be_bytes::<1>(), [0]));
assert!(matches!(
Uint::<1, 1>::from_limbs([1]).to_be_bytes::<1>(),
Expand All @@ -516,8 +515,6 @@ mod tests {
[0x12, 0x34]
));

assert!(matches!(Uint::<0, 0>::ZERO.to_be_bytes::<0>(), []));
assert!(matches!(Uint::<0, 0>::ZERO.to_le_bytes::<0>(), []));
assert!(matches!(Uint::<1, 1>::ZERO.to_be_bytes::<1>(), [0]));
assert!(matches!(Uint::<1, 1>::ZERO.to_le_bytes::<1>(), [0]));
assert!(matches!(
Expand Down Expand Up @@ -549,8 +546,6 @@ mod tests {

#[test]
fn test_from_bytes() {
assert_eq!(Uint::<0, 0>::from_be_bytes([]), Uint::ZERO);
assert_eq!(Uint::<0, 0>::from_le_bytes([]), Uint::ZERO);
assert_eq!(
Uint::<12, 1>::from_be_bytes([0x01, 0x23]),
Uint::from(0x0123)
Expand Down Expand Up @@ -597,8 +592,6 @@ mod tests {

#[test]
fn test_to_bytes() {
assert_eq!(Uint::<0, 0>::ZERO.to_le_bytes(), [0_u8; 0]);
assert_eq!(Uint::<0, 0>::ZERO.to_be_bytes(), [0_u8; 0]);
assert_eq!(Uint::<12, 1>::from(0x0123_u64).to_le_bytes(), [0x23, 0x01]);
assert_eq!(Uint::<12, 1>::from(0x0123_u64).to_be_bytes(), [0x01, 0x23]);
assert_eq!(Uint::<16, 1>::from(0x1234_u64).to_le_bytes(), [0x34, 0x12]);
Expand Down
1 change: 0 additions & 1 deletion src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ mod tests {

#[test]
fn test_is_zero() {
assert!(Uint::<0, 0>::ZERO.is_zero());
assert!(Uint::<1, 1>::ZERO.is_zero());
assert!(Uint::<7, 1>::ZERO.is_zero());
assert!(Uint::<64, 1>::ZERO.is_zero());
Expand Down
9 changes: 3 additions & 6 deletions src/const_for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// ```rust
/// # use ruint::{const_for, nlimbs, Uint};
/// const_for!(BITS in [0, 10, 100] {
/// const_for!(BITS in [10, 100] {
/// const LIMBS: usize = nlimbs(BITS);
/// println!("{:?}", Uint::<BITS, LIMBS>::MAX);
/// });
Expand All @@ -14,19 +14,17 @@
///
/// ```rust
/// # use ruint::{const_for, Uint};
/// println!("{:?}", Uint::<0, 0>::MAX);
/// println!("{:?}", Uint::<10, 1>::MAX);
/// println!("{:?}", Uint::<100, 2>::MAX);
/// ```
///
/// It comes with two built-in lists: `NON_ZERO` which is equivalent to
/// It comes with two built-in lists, `SIZES` and `NON_ZERO` (which are
/// currently equivalent), defined as
///
/// ```text
/// [1, 2, 63, 64, 65, 127, 128, 129, 256, 384, 512, 4096]
/// ```
///
/// and `SIZES` which is the same but also has `0` as a value.
///
/// In combination with [`proptest!`][proptest::proptest] this allows for
/// testing over a large range of [`Uint`][crate::Uint] types and values:
///
Expand All @@ -49,7 +47,6 @@ macro_rules! const_for {
})*
};
($C:ident in SIZES $x:block) => {
$crate::const_for!($C in [0] $x);
$crate::const_for!($C in NON_ZERO $x);
};
($C:ident in NON_ZERO $x:block) => {
Expand Down
28 changes: 4 additions & 24 deletions src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// Saturates at the maximum value of the [`Uint`] if the value is too
/// large.
pub(crate) const fn const_from_u64(x: u64) -> Self {
if BITS == 0 || (BITS < 64 && x >= 1 << BITS) {
if BITS < 64 && x >= 1 << BITS {
return Self::MAX;
}
let mut limbs = [0; LIMBS];
Expand Down Expand Up @@ -603,12 +603,8 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<f64> for Uint<BITS, LIMBS> {

// Positive and exponent indicates |value| < 1
if exponent < exponent_bias {
return if BITS == 0 {
Err(ToUintError::ValueTooLarge(BITS, Self::ZERO))
} else {
// We already handled value < 0.5 above; here 0.5 <= value < 1.0 → 1.
Ok(Self::ONE)
};
// We already handled value < 0.5 above; here 0.5 <= value < 1.0 → 1.
return Ok(Self::ONE);
}
exponent -= exponent_bias;

Expand Down Expand Up @@ -636,11 +632,7 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<f64> for Uint<BITS, LIMBS> {

// Match old impl: if rounding bumps us across 2^BITS (only possible when
// exponent == BITS - 1), report ValueTooLarge with the wrapped payload.
if sign == Sign::Positive
&& fixint_bits > 0
&& exponent == fixint_bits - 1
&& r == Self::ZERO
{
if sign == Sign::Positive && exponent == fixint_bits - 1 && r == Self::ZERO {
return Err(ToUintError::ValueTooLarge(BITS, r));
}

Expand Down Expand Up @@ -687,9 +679,6 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<&Uint<BITS, LIMBS>> for bool

#[inline]
fn try_from(value: &Uint<BITS, LIMBS>) -> Result<Self, Self::Error> {
if BITS == 0 {
return Ok(false);
}
if value.gt_u64_max() || value.limbs[0] > 1 {
return Err(Self::Error::Overflow(BITS, value.bit(0), true));
}
Expand All @@ -707,9 +696,6 @@ macro_rules! to_int {
#[inline]
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
fn try_from(value: &Uint<BITS, LIMBS>) -> Result<Self, Self::Error> {
if BITS == 0 {
return Ok(0);
}
if value.gt_u64_max() || value.limbs[0] > (Self::MAX as u64) {
return Err(Self::Error::Overflow(
BITS,
Expand Down Expand Up @@ -876,11 +862,6 @@ mod test {

#[test]
fn test_u64() {
assert_eq!(Uint::<0, 0>::try_from(0_u64), Ok(Uint::ZERO));
assert_eq!(
Uint::<0, 0>::try_from(1_u64),
Err(ToUintError::ValueTooLarge(0, Uint::ZERO))
);
const_for!(BITS in NON_ZERO {
const LIMBS: usize = nlimbs(BITS);
assert_eq!(Uint::<BITS, LIMBS>::try_from(0_u64), Ok(Uint::ZERO));
Expand Down Expand Up @@ -929,7 +910,6 @@ mod test {

#[test]
fn test_f64() {
assert_eq!(Uint::<0, 0>::try_from(0.0_f64), Ok(Uint::ZERO));
const_for!(BITS in NON_ZERO {
const LIMBS: usize = nlimbs(BITS);
assert_eq!(Uint::<BITS, LIMBS>::try_from(0.0_f64), Ok(Uint::ZERO));
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub struct Uint<const BITS: usize, const LIMBS: usize> {
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// The size of this integer type in 64-bit limbs.
pub const LIMBS: usize = {
assert!(BITS > 0, "Can not construct Uint<0, 0>");
let limbs = nlimbs(BITS);
assert!(
LIMBS == limbs,
Expand All @@ -165,7 +166,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// Bit mask for the last limb.
pub const MASK: u64 = mask(BITS);

const SHOULD_MASK: bool = BITS > 0 && Self::MASK != u64::MAX;
const SHOULD_MASK: bool = Self::MASK != u64::MAX;

/// The size of this integer type in bits.
pub const BITS: usize = BITS;
Expand All @@ -175,8 +176,6 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
pub const ZERO: Self = Self::from_limbs([0; LIMBS]);

/// The value one. This is useful to have as a constant for use in const fn.
///
/// Zero if `BITS` is zero.
pub const ONE: Self = Self::const_from_u64(1);

/// The smallest value that can be represented by this integer type.
Expand Down Expand Up @@ -380,7 +379,6 @@ mod test {

#[test]
fn test_max() {
assert_eq!(Uint::<0, 0>::MAX, Uint::ZERO);
assert_eq!(Uint::<1, 1>::MAX, Uint::from_limbs([1]));
assert_eq!(Uint::<7, 1>::MAX, Uint::from_limbs([127]));
assert_eq!(Uint::<64, 1>::MAX, Uint::from_limbs([u64::MAX]));
Expand Down
Loading
Loading