From d0b94ad731d04ea5177c5760025e410c7ef6e013 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 8 Dec 2022 18:50:25 -0700 Subject: [PATCH] Rename `UInt` => `Uint` From https://rust-lang.github.io/api-guidelines/naming.html > In UpperCamelCase, acronyms and contractions of compound words count > as one word: use Uuid rather than UUID, Usize rather than USize or > Stdin rather than StdIn. Based on the `Usize` example, it's pretty clear we should be using `Uint` rather than `UInt`. This is also consistent with `num-bigint`. --- src/lib.rs | 6 +- src/non_zero.rs | 38 ++--- src/uint.rs | 58 +++---- src/uint/add.rs | 62 ++++---- src/uint/add_mod.rs | 34 ++-- src/uint/array.rs | 26 +-- src/uint/bit_and.rs | 58 +++---- src/uint/bit_not.rs | 10 +- src/uint/bit_or.rs | 58 +++---- src/uint/bit_xor.rs | 58 +++---- src/uint/bits.rs | 4 +- src/uint/cmp.rs | 26 +-- src/uint/concat.rs | 10 +- src/uint/div.rs | 168 ++++++++++---------- src/uint/encoding.rs | 46 +++--- src/uint/encoding/der.rs | 30 ++-- src/uint/encoding/rlp.rs | 6 +- src/uint/from.rs | 58 +++---- src/uint/inv_mod.rs | 22 +-- src/uint/modular.rs | 18 +-- src/uint/modular/add.rs | 10 +- src/uint/modular/constant_mod.rs | 24 +-- src/uint/modular/constant_mod/const_add.rs | 6 +- src/uint/modular/constant_mod/const_pow.rs | 8 +- src/uint/modular/constant_mod/macros.rs | 18 +-- src/uint/modular/inv.rs | 10 +- src/uint/modular/mul.rs | 16 +- src/uint/modular/pow.rs | 28 ++-- src/uint/modular/reduction.rs | 10 +- src/uint/modular/runtime_mod.rs | 28 ++-- src/uint/modular/runtime_mod/runtime_add.rs | 6 +- src/uint/modular/runtime_mod/runtime_pow.rs | 6 +- src/uint/mul.rs | 60 +++---- src/uint/mul_mod.rs | 44 ++--- src/uint/neg.rs | 10 +- src/uint/neg_mod.rs | 8 +- src/uint/rand.rs | 10 +- src/uint/resize.rs | 10 +- src/uint/shl.rs | 30 ++-- src/uint/shr.rs | 30 ++-- src/uint/split.rs | 10 +- src/uint/sqrt.rs | 6 +- src/uint/sub.rs | 62 ++++---- src/uint/sub_mod.rs | 48 +++--- 44 files changed, 647 insertions(+), 647 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4b57d3c5..735a6dd5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ //! ## Usage //! -//! This crate defines a [`UInt`] type which is const generic around an inner +//! This crate defines a [`Uint`] type which is const generic around an inner //! [`Limb`] array, where a [`Limb`] is a newtype for a word-sized integer. //! Thus large integers are represented as a arrays of smaller integers which //! are sized appropriately for the CPU, giving us some assurances of how @@ -33,7 +33,7 @@ //! //! ### `const fn` usage //! -//! The [`UInt`] type provides a number of `const fn` inherent methods which +//! The [`Uint`] type provides a number of `const fn` inherent methods which //! can be used for initializing and performing arithmetic on big integers in //! const contexts: //! @@ -59,7 +59,7 @@ //! //! ### Trait-based usage //! -//! The [`UInt`] type itself does not implement the standard arithmetic traits +//! The [`Uint`] type itself does not implement the standard arithmetic traits //! such as [`Add`], [`Sub`], [`Mul`], and [`Div`]. //! //! To use these traits you must first pick a wrapper type which determines diff --git a/src/non_zero.rs b/src/non_zero.rs index fc3c9a84..10346654 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -1,6 +1,6 @@ //! Wrapper type for non-zero integers. -use crate::{Encoding, Integer, Limb, UInt, Zero}; +use crate::{Encoding, Integer, Limb, Uint, Zero}; use core::{ fmt, num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8}, @@ -237,9 +237,9 @@ impl From for NonZero { } } -impl NonZero> { - /// Create a [`NonZero`] from a [`UInt`] (const-friendly) - pub const fn from_uint(n: UInt) -> Self { +impl NonZero> { + /// Create a [`NonZero`] from a [`Uint`] (const-friendly) + pub const fn from_uint(n: Uint) -> Self { let mut i = 0; let mut found_non_zero = false; while i < LIMBS { @@ -252,62 +252,62 @@ impl NonZero> { Self(n) } - /// Create a [`NonZero`] from a [`NonZeroU8`] (const-friendly) + /// Create a [`NonZero`] from a [`NonZeroU8`] (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u8(n: NonZeroU8) -> Self { - Self(UInt::from_u8(n.get())) + Self(Uint::from_u8(n.get())) } - /// Create a [`NonZero`] from a [`NonZeroU16`] (const-friendly) + /// Create a [`NonZero`] from a [`NonZeroU16`] (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u16(n: NonZeroU16) -> Self { - Self(UInt::from_u16(n.get())) + Self(Uint::from_u16(n.get())) } - /// Create a [`NonZero`] from a [`NonZeroU32`] (const-friendly) + /// Create a [`NonZero`] from a [`NonZeroU32`] (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u32(n: NonZeroU32) -> Self { - Self(UInt::from_u32(n.get())) + Self(Uint::from_u32(n.get())) } - /// Create a [`NonZero`] from a [`NonZeroU64`] (const-friendly) + /// Create a [`NonZero`] from a [`NonZeroU64`] (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u64(n: NonZeroU64) -> Self { - Self(UInt::from_u64(n.get())) + Self(Uint::from_u64(n.get())) } - /// Create a [`NonZero`] from a [`NonZeroU128`] (const-friendly) + /// Create a [`NonZero`] from a [`NonZeroU128`] (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u128(n: NonZeroU128) -> Self { - Self(UInt::from_u128(n.get())) + Self(Uint::from_u128(n.get())) } } -impl From for NonZero> { +impl From for NonZero> { fn from(integer: NonZeroU8) -> Self { Self::from_u8(integer) } } -impl From for NonZero> { +impl From for NonZero> { fn from(integer: NonZeroU16) -> Self { Self::from_u16(integer) } } -impl From for NonZero> { +impl From for NonZero> { fn from(integer: NonZeroU32) -> Self { Self::from_u32(integer) } } -impl From for NonZero> { +impl From for NonZero> { fn from(integer: NonZeroU64) -> Self { Self::from_u64(integer) } } -impl From for NonZero> { +impl From for NonZero> { fn from(integer: NonZeroU128) -> Self { Self::from_u128(integer) } diff --git a/src/uint.rs b/src/uint.rs index 7e371866..49520f6a 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -60,23 +60,23 @@ use zeroize::DefaultIsZeroes; /// # Encoding support /// This type supports many different types of encodings, either via the /// [`Encoding`][`crate::Encoding`] trait or various `const fn` decoding and -/// encoding functions that can be used with [`UInt`] constants. +/// encoding functions that can be used with [`Uint`] constants. /// /// Optional crate features for encoding (off-by-default): /// - `generic-array`: enables [`ArrayEncoding`][`crate::ArrayEncoding`] trait which can be used to -/// [`UInt`] as `GenericArray` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which -/// can be used to `GenericArray` as [`UInt`]. +/// [`Uint`] as `GenericArray` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which +/// can be used to `GenericArray` as [`Uint`]. /// - `rlp`: support for [Recursive Length Prefix (RLP)][RLP] encoding. /// /// [RLP]: https://eth.wiki/fundamentals/rlp // TODO(tarcieri): make generic around a specified number of bits. #[derive(Copy, Clone, Debug, Hash)] -pub struct UInt { +pub struct Uint { /// Inner limb array. Stored from least significant to most significant. limbs: [Limb; LIMBS], } -impl UInt { +impl Uint { /// The value `0`. pub const ZERO: Self = Self::from_u8(0); @@ -86,17 +86,17 @@ impl UInt { /// The number of limbs used on this platform. pub const LIMBS: usize = LIMBS; - /// Maximum value this [`UInt`] can express. + /// Maximum value this [`Uint`] can express. pub const MAX: Self = Self { limbs: [Limb::MAX; LIMBS], }; - /// Const-friendly [`UInt`] constructor. + /// Const-friendly [`Uint`] constructor. pub const fn new(limbs: [Limb; LIMBS]) -> Self { Self { limbs } } - /// Create a [`UInt`] from an array of [`Word`]s (i.e. word-sized unsigned + /// Create a [`Uint`] from an array of [`Word`]s (i.e. word-sized unsigned /// integers). #[inline] pub const fn from_words(arr: [Word; LIMBS]) -> Self { @@ -112,7 +112,7 @@ impl UInt { } /// Create an array of [`Word`]s (i.e. word-sized unsigned integers) from - /// a [`UInt`]. + /// a [`Uint`]. #[inline] pub const fn to_words(self) -> [Word; LIMBS] { let mut arr = [0; LIMBS]; @@ -145,52 +145,52 @@ impl UInt { } } - /// Borrow the limbs of this [`UInt`]. + /// Borrow the limbs of this [`Uint`]. // TODO(tarcieri): rename to `as_limbs` for consistency with `as_words` pub const fn limbs(&self) -> &[Limb; LIMBS] { &self.limbs } - /// Borrow the limbs of this [`UInt`] mutably. + /// Borrow the limbs of this [`Uint`] mutably. // TODO(tarcieri): rename to `as_limbs_mut` for consistency with `as_words_mut` pub fn limbs_mut(&mut self) -> &mut [Limb; LIMBS] { &mut self.limbs } - /// Convert this [`UInt`] into its inner limbs. + /// Convert this [`Uint`] into its inner limbs. // TODO(tarcieri): rename to `to_limbs` for consistency with `to_words` pub const fn into_limbs(self) -> [Limb; LIMBS] { self.limbs } } -impl AsRef<[Word; LIMBS]> for UInt { +impl AsRef<[Word; LIMBS]> for Uint { fn as_ref(&self) -> &[Word; LIMBS] { self.as_words() } } -impl AsMut<[Word; LIMBS]> for UInt { +impl AsMut<[Word; LIMBS]> for Uint { fn as_mut(&mut self) -> &mut [Word; LIMBS] { self.as_words_mut() } } // TODO(tarcieri): eventually phase this out in favor of `limbs()`? -impl AsRef<[Limb]> for UInt { +impl AsRef<[Limb]> for Uint { fn as_ref(&self) -> &[Limb] { self.limbs() } } // TODO(tarcieri): eventually phase this out in favor of `limbs_mut()`? -impl AsMut<[Limb]> for UInt { +impl AsMut<[Limb]> for Uint { fn as_mut(&mut self) -> &mut [Limb] { self.limbs_mut() } } -impl ConditionallySelectable for UInt { +impl ConditionallySelectable for Uint { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { let mut limbs = [Limb::ZERO; LIMBS]; @@ -202,13 +202,13 @@ impl ConditionallySelectable for UInt { } } -impl Default for UInt { +impl Default for Uint { fn default() -> Self { Self::ZERO } } -impl Integer for UInt { +impl Integer for Uint { const ONE: Self = Self::ONE; const MAX: Self = Self::MAX; @@ -220,17 +220,17 @@ impl Integer for UInt { } } -impl Zero for UInt { +impl Zero for Uint { const ZERO: Self = Self::ZERO; } -impl fmt::Display for UInt { +impl fmt::Display for Uint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::UpperHex::fmt(self, f) } } -impl fmt::LowerHex for UInt { +impl fmt::LowerHex for Uint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for limb in self.limbs.iter().rev() { fmt::LowerHex::fmt(limb, f)?; @@ -239,7 +239,7 @@ impl fmt::LowerHex for UInt { } } -impl fmt::UpperHex for UInt { +impl fmt::UpperHex for Uint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for limb in self.limbs.iter().rev() { fmt::UpperHex::fmt(limb, f)?; @@ -250,9 +250,9 @@ impl fmt::UpperHex for UInt { #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] -impl<'de, const LIMBS: usize> Deserialize<'de> for UInt +impl<'de, const LIMBS: usize> Deserialize<'de> for Uint where - UInt: Encoding, + Uint: Encoding, { fn deserialize(deserializer: D) -> Result where @@ -267,9 +267,9 @@ where #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] -impl<'de, const LIMBS: usize> Serialize for UInt +impl<'de, const LIMBS: usize> Serialize for Uint where - UInt: Encoding, + Uint: Encoding, { fn serialize(&self, serializer: S) -> Result where @@ -281,7 +281,7 @@ where #[cfg(feature = "zeroize")] #[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))] -impl DefaultIsZeroes for UInt {} +impl DefaultIsZeroes for Uint {} // TODO(tarcieri): use `const_evaluatable_checked` when stable to make generic around bits. macro_rules! impl_uint_aliases { @@ -289,7 +289,7 @@ macro_rules! impl_uint_aliases { $( #[doc = $doc] #[doc="unsigned big integer."] - pub type $name = UInt<{nlimbs!($bits)}>; + pub type $name = Uint<{nlimbs!($bits)}>; impl Encoding for $name { const BIT_SIZE: usize = $bits; diff --git a/src/uint/add.rs b/src/uint/add.rs index 8a7f929c..a105127e 100644 --- a/src/uint/add.rs +++ b/src/uint/add.rs @@ -1,10 +1,10 @@ -//! [`UInt`] addition operations. +//! [`Uint`] addition operations. -use crate::{Checked, CheckedAdd, Limb, UInt, Word, Wrapping, Zero}; +use crate::{Checked, CheckedAdd, Limb, Uint, Word, Wrapping, Zero}; use core::ops::{Add, AddAssign}; use subtle::CtOption; -impl UInt { +impl Uint { /// Computes `a + b + carry`, returning the result along with the new carry. #[inline(always)] pub const fn adc(&self, rhs: &Self, mut carry: Limb) -> (Self, Limb) { @@ -39,14 +39,14 @@ impl UInt { /// Perform wrapping addition, returning the overflow bit as a `Word` that is either 0...0 or 1...1. pub(crate) const fn conditional_wrapping_add(&self, rhs: &Self, choice: Word) -> (Self, Word) { - let actual_rhs = UInt::ct_select(UInt::ZERO, *rhs, choice); + let actual_rhs = Uint::ct_select(Uint::ZERO, *rhs, choice); let (sum, carry) = self.adc(&actual_rhs, Limb::ZERO); (sum, carry.0.wrapping_mul(Word::MAX)) } } -impl CheckedAdd<&UInt> for UInt { +impl CheckedAdd<&Uint> for Uint { type Output = Self; fn checked_add(&self, rhs: &Self) -> CtOption { @@ -55,54 +55,54 @@ impl CheckedAdd<&UInt> for UInt { } } -impl Add for Wrapping> { +impl Add for Wrapping> { type Output = Self; - fn add(self, rhs: Self) -> Wrapping> { + fn add(self, rhs: Self) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl Add<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl Add<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn add(self, rhs: &Wrapping>) -> Wrapping> { + fn add(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl Add>> for &Wrapping> { - type Output = Wrapping>; +impl Add>> for &Wrapping> { + type Output = Wrapping>; - fn add(self, rhs: Wrapping>) -> Wrapping> { + fn add(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl Add<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl Add<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn add(self, rhs: &Wrapping>) -> Wrapping> { + fn add(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl AddAssign for Wrapping> { +impl AddAssign for Wrapping> { fn add_assign(&mut self, other: Self) { *self = *self + other; } } -impl AddAssign<&Wrapping>> for Wrapping> { +impl AddAssign<&Wrapping>> for Wrapping> { fn add_assign(&mut self, other: &Self) { *self = *self + other; } } -impl Add for Checked> { +impl Add for Checked> { type Output = Self; - fn add(self, rhs: Self) -> Checked> { + fn add(self, rhs: Self) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -110,10 +110,10 @@ impl Add for Checked> { } } -impl Add<&Checked>> for Checked> { - type Output = Checked>; +impl Add<&Checked>> for Checked> { + type Output = Checked>; - fn add(self, rhs: &Checked>) -> Checked> { + fn add(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -121,10 +121,10 @@ impl Add<&Checked>> for Checked> { } } -impl Add>> for &Checked> { - type Output = Checked>; +impl Add>> for &Checked> { + type Output = Checked>; - fn add(self, rhs: Checked>) -> Checked> { + fn add(self, rhs: Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -132,10 +132,10 @@ impl Add>> for &Checked> { } } -impl Add<&Checked>> for &Checked> { - type Output = Checked>; +impl Add<&Checked>> for &Checked> { + type Output = Checked>; - fn add(self, rhs: &Checked>) -> Checked> { + fn add(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -143,13 +143,13 @@ impl Add<&Checked>> for &Checked> { } } -impl AddAssign for Checked> { +impl AddAssign for Checked> { fn add_assign(&mut self, other: Self) { *self = *self + other; } } -impl AddAssign<&Checked>> for Checked> { +impl AddAssign<&Checked>> for Checked> { fn add_assign(&mut self, other: &Self) { *self = *self + other; } diff --git a/src/uint/add_mod.rs b/src/uint/add_mod.rs index 3486a0a5..bfdda6ff 100644 --- a/src/uint/add_mod.rs +++ b/src/uint/add_mod.rs @@ -1,12 +1,12 @@ -//! [`UInt`] addition modulus operations. +//! [`Uint`] addition modulus operations. -use crate::{AddMod, Limb, UInt}; +use crate::{AddMod, Limb, Uint}; -impl UInt { +impl Uint { /// Computes `self + rhs mod p` in constant time. /// /// Assumes `self + rhs` as unbounded integer is `< 2p`. - pub const fn add_mod(&self, rhs: &UInt, p: &UInt) -> UInt { + pub const fn add_mod(&self, rhs: &Uint, p: &Uint) -> Uint { let (w, carry) = self.adc(rhs, Limb::ZERO); // Attempt to subtract the modulus, to ensure the result is in the field. @@ -36,19 +36,19 @@ impl UInt { /// /// Assumes `self + rhs` as unbounded integer is `< 2p`. pub const fn add_mod_special(&self, rhs: &Self, c: Limb) -> Self { - // `UInt::adc` also works with a carry greater than 1. + // `Uint::adc` also works with a carry greater than 1. let (out, carry) = self.adc(rhs, c); // If overflow occurred, then above addition of `c` already accounts // for the overflow. Otherwise, we need to subtract `c` again, which // in that case cannot underflow. let l = carry.0.wrapping_sub(1) & c.0; - let (out, _) = out.sbb(&UInt::from_word(l), Limb::ZERO); + let (out, _) = out.sbb(&Uint::from_word(l), Limb::ZERO); out } } -impl AddMod for UInt { +impl AddMod for Uint { type Output = Self; fn add_mod(&self, rhs: &Self, p: &Self) -> Self { @@ -60,7 +60,7 @@ impl AddMod for UInt { #[cfg(all(test, feature = "rand"))] mod tests { - use crate::{Limb, NonZero, Random, RandomMod, UInt, U256}; + use crate::{Limb, NonZero, Random, RandomMod, Uint, U256}; use rand_core::SeedableRng; // TODO(tarcieri): additional tests + proptests @@ -92,17 +92,17 @@ mod tests { ]; for special in &moduli { - let p = &NonZero::new(UInt::ZERO.wrapping_sub(&UInt::from_word(special.0))) + let p = &NonZero::new(Uint::ZERO.wrapping_sub(&Uint::from_word(special.0))) .unwrap(); - let minus_one = p.wrapping_sub(&UInt::ONE); + let minus_one = p.wrapping_sub(&Uint::ONE); let base_cases = [ - (UInt::ZERO, UInt::ZERO, UInt::ZERO), - (UInt::ONE, UInt::ZERO, UInt::ONE), - (UInt::ZERO, UInt::ONE, UInt::ONE), - (minus_one, UInt::ONE, UInt::ZERO), - (UInt::ONE, minus_one, UInt::ZERO), + (Uint::ZERO, Uint::ZERO, Uint::ZERO), + (Uint::ONE, Uint::ZERO, Uint::ONE), + (Uint::ZERO, Uint::ONE, Uint::ONE), + (minus_one, Uint::ONE, Uint::ZERO), + (Uint::ONE, minus_one, Uint::ZERO), ]; for (a, b, c) in &base_cases { let x = a.add_mod_special(b, *special.as_ref()); @@ -110,8 +110,8 @@ mod tests { } for _i in 0..100 { - let a = UInt::<$size>::random_mod(&mut rng, p); - let b = UInt::<$size>::random_mod(&mut rng, p); + let a = Uint::<$size>::random_mod(&mut rng, p); + let b = Uint::<$size>::random_mod(&mut rng, p); let c = a.add_mod_special(&b, *special.as_ref()); assert!(c < **p, "not reduced: {} >= {} ", c, p); diff --git a/src/uint/array.rs b/src/uint/array.rs index cba2b371..53a3571b 100644 --- a/src/uint/array.rs +++ b/src/uint/array.rs @@ -1,4 +1,4 @@ -//! `generic-array` integration with `UInt`. +//! `generic-array` integration with `Uint`. // TODO(tarcieri): completely phase out `generic-array` when const generics are powerful enough use crate::{ArrayDecoding, ArrayEncoding, ByteArray}; @@ -81,25 +81,25 @@ mod tests { use hex_literal::hex; #[cfg(target_pointer_width = "32")] - use crate::U64 as UIntEx; + use crate::U64 as UintEx; #[cfg(target_pointer_width = "64")] - use crate::U128 as UIntEx; + use crate::U128 as UintEx; - /// Byte array that corresponds to `UIntEx` - type ByteArray = crate::ByteArray; + /// Byte array that corresponds to `UintEx` + type ByteArray = crate::ByteArray; #[test] #[cfg(target_pointer_width = "32")] fn from_be_byte_array() { - let n = UIntEx::from_be_byte_array(hex!("0011223344556677").into()); + let n = UintEx::from_be_byte_array(hex!("0011223344556677").into()); assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_be_byte_array() { - let n = UIntEx::from_be_byte_array(hex!("00112233445566778899aabbccddeeff").into()); + let n = UintEx::from_be_byte_array(hex!("00112233445566778899aabbccddeeff").into()); assert_eq!( n.limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] @@ -109,14 +109,14 @@ mod tests { #[test] #[cfg(target_pointer_width = "32")] fn from_le_byte_array() { - let n = UIntEx::from_le_byte_array(hex!("7766554433221100").into()); + let n = UintEx::from_le_byte_array(hex!("7766554433221100").into()); assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_le_byte_array() { - let n = UIntEx::from_le_byte_array(hex!("ffeeddccbbaa99887766554433221100").into()); + let n = UintEx::from_le_byte_array(hex!("ffeeddccbbaa99887766554433221100").into()); assert_eq!( n.limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] @@ -127,7 +127,7 @@ mod tests { #[cfg(target_pointer_width = "32")] fn to_be_byte_array() { let expected_bytes = ByteArray::from(hex!("0011223344556677")); - let actual_bytes = UIntEx::from_be_byte_array(expected_bytes).to_be_byte_array(); + let actual_bytes = UintEx::from_be_byte_array(expected_bytes).to_be_byte_array(); assert_eq!(expected_bytes, actual_bytes); } @@ -135,7 +135,7 @@ mod tests { #[cfg(target_pointer_width = "64")] fn to_be_byte_array() { let expected_bytes = ByteArray::from(hex!("00112233445566778899aabbccddeeff")); - let actual_bytes = UIntEx::from_be_byte_array(expected_bytes).to_be_byte_array(); + let actual_bytes = UintEx::from_be_byte_array(expected_bytes).to_be_byte_array(); assert_eq!(expected_bytes, actual_bytes); } @@ -143,7 +143,7 @@ mod tests { #[cfg(target_pointer_width = "32")] fn to_le_byte_array() { let expected_bytes = ByteArray::from(hex!("7766554433221100")); - let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array(); + let actual_bytes = UintEx::from_le_byte_array(expected_bytes).to_le_byte_array(); assert_eq!(expected_bytes, actual_bytes); } @@ -151,7 +151,7 @@ mod tests { #[cfg(target_pointer_width = "64")] fn to_le_byte_array() { let expected_bytes = ByteArray::from(hex!("ffeeddccbbaa99887766554433221100")); - let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array(); + let actual_bytes = UintEx::from_le_byte_array(expected_bytes).to_le_byte_array(); assert_eq!(expected_bytes, actual_bytes); } diff --git a/src/uint/bit_and.rs b/src/uint/bit_and.rs index e6eab65f..18186fb8 100644 --- a/src/uint/bit_and.rs +++ b/src/uint/bit_and.rs @@ -1,11 +1,11 @@ -//! [`UInt`] bitwise and operations. +//! [`Uint`] bitwise and operations. -use super::UInt; +use super::Uint; use crate::{Limb, Wrapping}; use core::ops::{BitAnd, BitAndAssign}; use subtle::{Choice, CtOption}; -impl UInt { +impl Uint { /// Computes bitwise `a & b`. #[inline(always)] pub const fn bitand(&self, rhs: &Self) -> Self { @@ -35,93 +35,93 @@ impl UInt { } } -impl BitAnd for UInt { +impl BitAnd for Uint { type Output = Self; - fn bitand(self, rhs: Self) -> UInt { + fn bitand(self, rhs: Self) -> Uint { self.bitand(&rhs) } } -impl BitAnd<&UInt> for UInt { - type Output = UInt; +impl BitAnd<&Uint> for Uint { + type Output = Uint; #[allow(clippy::needless_borrow)] - fn bitand(self, rhs: &UInt) -> UInt { + fn bitand(self, rhs: &Uint) -> Uint { (&self).bitand(rhs) } } -impl BitAnd> for &UInt { - type Output = UInt; +impl BitAnd> for &Uint { + type Output = Uint; - fn bitand(self, rhs: UInt) -> UInt { + fn bitand(self, rhs: Uint) -> Uint { self.bitand(&rhs) } } -impl BitAnd<&UInt> for &UInt { - type Output = UInt; +impl BitAnd<&Uint> for &Uint { + type Output = Uint; - fn bitand(self, rhs: &UInt) -> UInt { + fn bitand(self, rhs: &Uint) -> Uint { self.bitand(rhs) } } -impl BitAndAssign for UInt { +impl BitAndAssign for Uint { #[allow(clippy::assign_op_pattern)] fn bitand_assign(&mut self, other: Self) { *self = *self & other; } } -impl BitAndAssign<&UInt> for UInt { +impl BitAndAssign<&Uint> for Uint { #[allow(clippy::assign_op_pattern)] fn bitand_assign(&mut self, other: &Self) { *self = *self & other; } } -impl BitAnd for Wrapping> { +impl BitAnd for Wrapping> { type Output = Self; - fn bitand(self, rhs: Self) -> Wrapping> { + fn bitand(self, rhs: Self) -> Wrapping> { Wrapping(self.0.bitand(&rhs.0)) } } -impl BitAnd<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl BitAnd<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn bitand(self, rhs: &Wrapping>) -> Wrapping> { + fn bitand(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.bitand(&rhs.0)) } } -impl BitAnd>> for &Wrapping> { - type Output = Wrapping>; +impl BitAnd>> for &Wrapping> { + type Output = Wrapping>; - fn bitand(self, rhs: Wrapping>) -> Wrapping> { + fn bitand(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.bitand(&rhs.0)) } } -impl BitAnd<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl BitAnd<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn bitand(self, rhs: &Wrapping>) -> Wrapping> { + fn bitand(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.bitand(&rhs.0)) } } -impl BitAndAssign for Wrapping> { +impl BitAndAssign for Wrapping> { #[allow(clippy::assign_op_pattern)] fn bitand_assign(&mut self, other: Self) { *self = *self & other; } } -impl BitAndAssign<&Wrapping>> for Wrapping> { +impl BitAndAssign<&Wrapping>> for Wrapping> { #[allow(clippy::assign_op_pattern)] fn bitand_assign(&mut self, other: &Self) { *self = *self & other; diff --git a/src/uint/bit_not.rs b/src/uint/bit_not.rs index 914774a8..52fea5fd 100644 --- a/src/uint/bit_not.rs +++ b/src/uint/bit_not.rs @@ -1,10 +1,10 @@ -//! [`UInt`] bitwise not operations. +//! [`Uint`] bitwise not operations. -use super::UInt; +use super::Uint; use crate::{Limb, Wrapping}; use core::ops::Not; -impl UInt { +impl Uint { /// Computes bitwise `!a`. #[inline(always)] pub const fn not(&self) -> Self { @@ -20,7 +20,7 @@ impl UInt { } } -impl Not for UInt { +impl Not for Uint { type Output = Self; #[allow(clippy::needless_borrow)] @@ -29,7 +29,7 @@ impl Not for UInt { } } -impl Not for Wrapping> { +impl Not for Wrapping> { type Output = Self; fn not(self) -> ::Output { diff --git a/src/uint/bit_or.rs b/src/uint/bit_or.rs index 8f6b84db..9a78e366 100644 --- a/src/uint/bit_or.rs +++ b/src/uint/bit_or.rs @@ -1,11 +1,11 @@ -//! [`UInt`] bitwise or operations. +//! [`Uint`] bitwise or operations. -use super::UInt; +use super::Uint; use crate::{Limb, Wrapping}; use core::ops::{BitOr, BitOrAssign}; use subtle::{Choice, CtOption}; -impl UInt { +impl Uint { /// Computes bitwise `a & b`. #[inline(always)] pub const fn bitor(&self, rhs: &Self) -> Self { @@ -35,90 +35,90 @@ impl UInt { } } -impl BitOr for UInt { +impl BitOr for Uint { type Output = Self; - fn bitor(self, rhs: Self) -> UInt { + fn bitor(self, rhs: Self) -> Uint { self.bitor(&rhs) } } -impl BitOr<&UInt> for UInt { - type Output = UInt; +impl BitOr<&Uint> for Uint { + type Output = Uint; #[allow(clippy::needless_borrow)] - fn bitor(self, rhs: &UInt) -> UInt { + fn bitor(self, rhs: &Uint) -> Uint { (&self).bitor(rhs) } } -impl BitOr> for &UInt { - type Output = UInt; +impl BitOr> for &Uint { + type Output = Uint; - fn bitor(self, rhs: UInt) -> UInt { + fn bitor(self, rhs: Uint) -> Uint { self.bitor(&rhs) } } -impl BitOr<&UInt> for &UInt { - type Output = UInt; +impl BitOr<&Uint> for &Uint { + type Output = Uint; - fn bitor(self, rhs: &UInt) -> UInt { + fn bitor(self, rhs: &Uint) -> Uint { self.bitor(rhs) } } -impl BitOrAssign for UInt { +impl BitOrAssign for Uint { fn bitor_assign(&mut self, other: Self) { *self = *self | other; } } -impl BitOrAssign<&UInt> for UInt { +impl BitOrAssign<&Uint> for Uint { fn bitor_assign(&mut self, other: &Self) { *self = *self | other; } } -impl BitOr for Wrapping> { +impl BitOr for Wrapping> { type Output = Self; - fn bitor(self, rhs: Self) -> Wrapping> { + fn bitor(self, rhs: Self) -> Wrapping> { Wrapping(self.0.bitor(&rhs.0)) } } -impl BitOr<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl BitOr<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn bitor(self, rhs: &Wrapping>) -> Wrapping> { + fn bitor(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.bitor(&rhs.0)) } } -impl BitOr>> for &Wrapping> { - type Output = Wrapping>; +impl BitOr>> for &Wrapping> { + type Output = Wrapping>; - fn bitor(self, rhs: Wrapping>) -> Wrapping> { + fn bitor(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.bitor(&rhs.0)) } } -impl BitOr<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl BitOr<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn bitor(self, rhs: &Wrapping>) -> Wrapping> { + fn bitor(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.bitor(&rhs.0)) } } -impl BitOrAssign for Wrapping> { +impl BitOrAssign for Wrapping> { fn bitor_assign(&mut self, other: Self) { *self = *self | other; } } -impl BitOrAssign<&Wrapping>> for Wrapping> { +impl BitOrAssign<&Wrapping>> for Wrapping> { fn bitor_assign(&mut self, other: &Self) { *self = *self | other; } diff --git a/src/uint/bit_xor.rs b/src/uint/bit_xor.rs index 0e886ca2..91121d23 100644 --- a/src/uint/bit_xor.rs +++ b/src/uint/bit_xor.rs @@ -1,11 +1,11 @@ -//! [`UInt`] bitwise xor operations. +//! [`Uint`] bitwise xor operations. -use super::UInt; +use super::Uint; use crate::{Limb, Wrapping}; use core::ops::{BitXor, BitXorAssign}; use subtle::{Choice, CtOption}; -impl UInt { +impl Uint { /// Computes bitwise `a ^ b`. #[inline(always)] pub const fn bitxor(&self, rhs: &Self) -> Self { @@ -35,90 +35,90 @@ impl UInt { } } -impl BitXor for UInt { +impl BitXor for Uint { type Output = Self; - fn bitxor(self, rhs: Self) -> UInt { + fn bitxor(self, rhs: Self) -> Uint { self.bitxor(&rhs) } } -impl BitXor<&UInt> for UInt { - type Output = UInt; +impl BitXor<&Uint> for Uint { + type Output = Uint; #[allow(clippy::needless_borrow)] - fn bitxor(self, rhs: &UInt) -> UInt { + fn bitxor(self, rhs: &Uint) -> Uint { (&self).bitxor(rhs) } } -impl BitXor> for &UInt { - type Output = UInt; +impl BitXor> for &Uint { + type Output = Uint; - fn bitxor(self, rhs: UInt) -> UInt { + fn bitxor(self, rhs: Uint) -> Uint { self.bitxor(&rhs) } } -impl BitXor<&UInt> for &UInt { - type Output = UInt; +impl BitXor<&Uint> for &Uint { + type Output = Uint; - fn bitxor(self, rhs: &UInt) -> UInt { + fn bitxor(self, rhs: &Uint) -> Uint { self.bitxor(rhs) } } -impl BitXorAssign for UInt { +impl BitXorAssign for Uint { fn bitxor_assign(&mut self, other: Self) { *self = *self ^ other; } } -impl BitXorAssign<&UInt> for UInt { +impl BitXorAssign<&Uint> for Uint { fn bitxor_assign(&mut self, other: &Self) { *self = *self ^ other; } } -impl BitXor for Wrapping> { +impl BitXor for Wrapping> { type Output = Self; - fn bitxor(self, rhs: Self) -> Wrapping> { + fn bitxor(self, rhs: Self) -> Wrapping> { Wrapping(self.0.bitxor(&rhs.0)) } } -impl BitXor<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl BitXor<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn bitxor(self, rhs: &Wrapping>) -> Wrapping> { + fn bitxor(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.bitxor(&rhs.0)) } } -impl BitXor>> for &Wrapping> { - type Output = Wrapping>; +impl BitXor>> for &Wrapping> { + type Output = Wrapping>; - fn bitxor(self, rhs: Wrapping>) -> Wrapping> { + fn bitxor(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.bitxor(&rhs.0)) } } -impl BitXor<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl BitXor<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn bitxor(self, rhs: &Wrapping>) -> Wrapping> { + fn bitxor(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.bitxor(&rhs.0)) } } -impl BitXorAssign for Wrapping> { +impl BitXorAssign for Wrapping> { fn bitxor_assign(&mut self, other: Self) { *self = *self ^ other; } } -impl BitXorAssign<&Wrapping>> for Wrapping> { +impl BitXorAssign<&Wrapping>> for Wrapping> { fn bitxor_assign(&mut self, other: &Self) { *self = *self ^ other; } diff --git a/src/uint/bits.rs b/src/uint/bits.rs index 25b4c425..c3def76a 100644 --- a/src/uint/bits.rs +++ b/src/uint/bits.rs @@ -1,6 +1,6 @@ -use crate::{Limb, UInt, Word}; +use crate::{Limb, Uint, Word}; -impl UInt { +impl Uint { /// Get the value of the bit at position `index`, as a 0- or 1-valued Word. /// Returns 0 for indices out of range. #[inline(always)] diff --git a/src/uint/cmp.rs b/src/uint/cmp.rs index 676622ce..81ef70cb 100644 --- a/src/uint/cmp.rs +++ b/src/uint/cmp.rs @@ -1,18 +1,18 @@ -//! [`UInt`] comparisons. +//! [`Uint`] comparisons. //! //! By default these are all constant-time and use the `subtle` crate. -use super::UInt; +use super::Uint; use crate::{limb::HI_BIT, Limb, SignedWord, WideSignedWord, Word, Zero}; use core::cmp::Ordering; use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess}; -impl UInt { +impl Uint { /// Return `a` if `c`==0 or `b` if `c`==`Word::MAX`. /// /// Const-friendly: we can't yet use `subtle` in `const fn` contexts. #[inline] - pub(crate) const fn ct_select(a: UInt, b: UInt, c: Word) -> Self { + pub(crate) const fn ct_select(a: Uint, b: Uint, c: Word) -> Self { let mut limbs = [Limb::ZERO; LIMBS]; let mut i = 0; @@ -21,11 +21,11 @@ impl UInt { i += 1; } - UInt { limbs } + Uint { limbs } } #[inline] - pub(crate) const fn ct_swap(a: UInt, b: UInt, c: Word) -> (Self, Self) { + pub(crate) const fn ct_swap(a: Uint, b: Uint, c: Word) -> (Self, Self) { let new_a = Self::ct_select(a, b, c); let new_b = Self::ct_select(b, a, c); @@ -87,14 +87,14 @@ impl UInt { } } -impl ConstantTimeEq for UInt { +impl ConstantTimeEq for Uint { #[inline] fn ct_eq(&self, other: &Self) -> Choice { Choice::from((!self.ct_not_eq(other) as u8) & 1) } } -impl ConstantTimeGreater for UInt { +impl ConstantTimeGreater for Uint { #[inline] fn ct_gt(&self, other: &Self) -> Choice { let underflow = other.sbb(self, Limb::ZERO).1; @@ -102,7 +102,7 @@ impl ConstantTimeGreater for UInt { } } -impl ConstantTimeLess for UInt { +impl ConstantTimeLess for Uint { #[inline] fn ct_lt(&self, other: &Self) -> Choice { let underflow = self.sbb(other, Limb::ZERO).1; @@ -110,9 +110,9 @@ impl ConstantTimeLess for UInt { } } -impl Eq for UInt {} +impl Eq for Uint {} -impl Ord for UInt { +impl Ord for Uint { fn cmp(&self, other: &Self) -> Ordering { match self.ct_cmp(other) { -1 => Ordering::Less, @@ -126,13 +126,13 @@ impl Ord for UInt { } } -impl PartialOrd for UInt { +impl PartialOrd for Uint { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl PartialEq for UInt { +impl PartialEq for Uint { fn eq(&self, other: &Self) -> bool { self.ct_eq(other).into() } diff --git a/src/uint/concat.rs b/src/uint/concat.rs index e92960da..906bb2ec 100644 --- a/src/uint/concat.rs +++ b/src/uint/concat.rs @@ -5,7 +5,7 @@ macro_rules! impl_concat { impl $name { /// Concatenate the two values, with `self` as most significant and `rhs` /// as the least significant. - pub const fn concat(&self, rhs: &Self) -> UInt<{nlimbs!($bits) * 2}> { + pub const fn concat(&self, rhs: &Self) -> Uint<{nlimbs!($bits) * 2}> { let mut limbs = [Limb::ZERO; nlimbs!($bits) * 2]; let mut i = 0; let mut j = 0; @@ -23,20 +23,20 @@ macro_rules! impl_concat { j += 1; } - UInt { limbs } + Uint { limbs } } } impl Concat for $name { - type Output = UInt<{nlimbs!($bits) * 2}>; + type Output = Uint<{nlimbs!($bits) * 2}>; fn concat(&self, rhs: &Self) -> Self::Output { self.concat(rhs) } } - impl From<($name, $name)> for UInt<{nlimbs!($bits) * 2}> { - fn from(nums: ($name, $name)) -> UInt<{nlimbs!($bits) * 2}> { + impl From<($name, $name)> for Uint<{nlimbs!($bits) * 2}> { + fn from(nums: ($name, $name)) -> Uint<{nlimbs!($bits) * 2}> { nums.0.concat(&nums.1) } } diff --git a/src/uint/div.rs b/src/uint/div.rs index 828b9f50..89f94a22 100644 --- a/src/uint/div.rs +++ b/src/uint/div.rs @@ -1,12 +1,12 @@ -//! [`UInt`] division operations. +//! [`Uint`] division operations. -use super::UInt; +use super::Uint; use crate::limb::Word; use crate::{Integer, Limb, NonZero, Wrapping}; use core::ops::{Div, DivAssign, Rem, RemAssign}; use subtle::{Choice, CtOption}; -impl UInt { +impl Uint { /// Computes `self` / `rhs`, returns the quotient (q), remainder (r) /// and 1 for is_some or 0 for is_none. The results can be wrapped in [`CtOption`]. /// NOTE: Use only if you need to access const fn. Otherwise use `div_rem` because @@ -86,7 +86,7 @@ impl UInt { let (mut lower, mut upper) = lower_upper; // Factor of the modulus, split into two halves - let mut c = Self::shl_vartime_wide((*rhs, UInt::ZERO), bd); + let mut c = Self::shl_vartime_wide((*rhs, Uint::ZERO), bd); loop { let (lower_sub, borrow) = lower.sbb(&c.0, Limb::ZERO); @@ -106,7 +106,7 @@ impl UInt { } /// Computes `self` % 2^k. Faster than reduce since its a power of 2. - /// Limited to 2^16-1 since UInt doesn't support higher. + /// Limited to 2^16-1 since Uint doesn't support higher. pub const fn reduce2k(&self, k: usize) -> Self { let highest = (LIMBS - 1) as u32; let index = k as u32 / (Limb::BIT_SIZE as u32); @@ -178,218 +178,218 @@ impl UInt { } } -impl Div<&NonZero>> for &UInt +impl Div<&NonZero>> for &Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn div(self, rhs: &NonZero>) -> Self::Output { + fn div(self, rhs: &NonZero>) -> Self::Output { *self / *rhs } } -impl Div<&NonZero>> for UInt +impl Div<&NonZero>> for Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn div(self, rhs: &NonZero>) -> Self::Output { + fn div(self, rhs: &NonZero>) -> Self::Output { self / *rhs } } -impl Div>> for &UInt +impl Div>> for &Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn div(self, rhs: NonZero>) -> Self::Output { + fn div(self, rhs: NonZero>) -> Self::Output { *self / rhs } } -impl Div>> for UInt +impl Div>> for Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn div(self, rhs: NonZero>) -> Self::Output { + fn div(self, rhs: NonZero>) -> Self::Output { let (q, _, _) = self.ct_div_rem(&rhs); q } } -impl DivAssign<&NonZero>> for UInt +impl DivAssign<&NonZero>> for Uint where - UInt: Integer, + Uint: Integer, { - fn div_assign(&mut self, rhs: &NonZero>) { + fn div_assign(&mut self, rhs: &NonZero>) { let (q, _, _) = self.ct_div_rem(rhs); *self = q } } -impl DivAssign>> for UInt +impl DivAssign>> for Uint where - UInt: Integer, + Uint: Integer, { - fn div_assign(&mut self, rhs: NonZero>) { + fn div_assign(&mut self, rhs: NonZero>) { *self /= &rhs; } } -impl Div>> for Wrapping> { - type Output = Wrapping>; +impl Div>> for Wrapping> { + type Output = Wrapping>; - fn div(self, rhs: NonZero>) -> Self::Output { + fn div(self, rhs: NonZero>) -> Self::Output { Wrapping(self.0.wrapping_div(rhs.as_ref())) } } -impl Div>> for &Wrapping> { - type Output = Wrapping>; +impl Div>> for &Wrapping> { + type Output = Wrapping>; - fn div(self, rhs: NonZero>) -> Self::Output { + fn div(self, rhs: NonZero>) -> Self::Output { *self / rhs } } -impl Div<&NonZero>> for &Wrapping> { - type Output = Wrapping>; +impl Div<&NonZero>> for &Wrapping> { + type Output = Wrapping>; - fn div(self, rhs: &NonZero>) -> Self::Output { + fn div(self, rhs: &NonZero>) -> Self::Output { *self / *rhs } } -impl Div<&NonZero>> for Wrapping> { - type Output = Wrapping>; +impl Div<&NonZero>> for Wrapping> { + type Output = Wrapping>; - fn div(self, rhs: &NonZero>) -> Self::Output { + fn div(self, rhs: &NonZero>) -> Self::Output { self / *rhs } } -impl DivAssign<&NonZero>> for Wrapping> { - fn div_assign(&mut self, rhs: &NonZero>) { +impl DivAssign<&NonZero>> for Wrapping> { + fn div_assign(&mut self, rhs: &NonZero>) { *self = Wrapping(self.0.wrapping_div(rhs.as_ref())) } } -impl DivAssign>> for Wrapping> { - fn div_assign(&mut self, rhs: NonZero>) { +impl DivAssign>> for Wrapping> { + fn div_assign(&mut self, rhs: NonZero>) { *self /= &rhs; } } -impl Rem<&NonZero>> for &UInt +impl Rem<&NonZero>> for &Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn rem(self, rhs: &NonZero>) -> Self::Output { + fn rem(self, rhs: &NonZero>) -> Self::Output { *self % *rhs } } -impl Rem<&NonZero>> for UInt +impl Rem<&NonZero>> for Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn rem(self, rhs: &NonZero>) -> Self::Output { + fn rem(self, rhs: &NonZero>) -> Self::Output { self % *rhs } } -impl Rem>> for &UInt +impl Rem>> for &Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn rem(self, rhs: NonZero>) -> Self::Output { + fn rem(self, rhs: NonZero>) -> Self::Output { *self % rhs } } -impl Rem>> for UInt +impl Rem>> for Uint where - UInt: Integer, + Uint: Integer, { - type Output = UInt; + type Output = Uint; - fn rem(self, rhs: NonZero>) -> Self::Output { + fn rem(self, rhs: NonZero>) -> Self::Output { let (r, _) = self.ct_reduce(&rhs); r } } -impl RemAssign<&NonZero>> for UInt +impl RemAssign<&NonZero>> for Uint where - UInt: Integer, + Uint: Integer, { - fn rem_assign(&mut self, rhs: &NonZero>) { + fn rem_assign(&mut self, rhs: &NonZero>) { let (r, _) = self.ct_reduce(rhs); *self = r } } -impl RemAssign>> for UInt +impl RemAssign>> for Uint where - UInt: Integer, + Uint: Integer, { - fn rem_assign(&mut self, rhs: NonZero>) { + fn rem_assign(&mut self, rhs: NonZero>) { *self %= &rhs; } } -impl Rem>> for Wrapping> { - type Output = Wrapping>; +impl Rem>> for Wrapping> { + type Output = Wrapping>; - fn rem(self, rhs: NonZero>) -> Self::Output { + fn rem(self, rhs: NonZero>) -> Self::Output { Wrapping(self.0.wrapping_rem(rhs.as_ref())) } } -impl Rem>> for &Wrapping> { - type Output = Wrapping>; +impl Rem>> for &Wrapping> { + type Output = Wrapping>; - fn rem(self, rhs: NonZero>) -> Self::Output { + fn rem(self, rhs: NonZero>) -> Self::Output { *self % rhs } } -impl Rem<&NonZero>> for &Wrapping> { - type Output = Wrapping>; +impl Rem<&NonZero>> for &Wrapping> { + type Output = Wrapping>; - fn rem(self, rhs: &NonZero>) -> Self::Output { + fn rem(self, rhs: &NonZero>) -> Self::Output { *self % *rhs } } -impl Rem<&NonZero>> for Wrapping> { - type Output = Wrapping>; +impl Rem<&NonZero>> for Wrapping> { + type Output = Wrapping>; - fn rem(self, rhs: &NonZero>) -> Self::Output { + fn rem(self, rhs: &NonZero>) -> Self::Output { self % *rhs } } -impl RemAssign>> for Wrapping> { - fn rem_assign(&mut self, rhs: NonZero>) { +impl RemAssign>> for Wrapping> { + fn rem_assign(&mut self, rhs: NonZero>) { *self %= &rhs; } } -impl RemAssign<&NonZero>> for Wrapping> { - fn rem_assign(&mut self, rhs: &NonZero>) { +impl RemAssign<&NonZero>> for Wrapping> { + fn rem_assign(&mut self, rhs: &NonZero>) { *self = Wrapping(self.0.wrapping_rem(rhs.as_ref())) } } @@ -452,11 +452,11 @@ mod tests { let mut b = U256::ZERO; b.limbs[b.limbs.len() - 1] = Limb(Word::MAX); let q = a.wrapping_div(&b); - assert_eq!(q, UInt::ZERO); + assert_eq!(q, Uint::ZERO); a.limbs[a.limbs.len() - 1] = Limb(1 << (HI_BIT - 7)); b.limbs[b.limbs.len() - 1] = Limb(0x82 << (HI_BIT - 7)); let q = a.wrapping_div(&b); - assert_eq!(q, UInt::ZERO); + assert_eq!(q, Uint::ZERO); } #[test] @@ -522,7 +522,7 @@ mod tests { let mut b = U256::ZERO; b.limbs[b.limbs.len() - 1] = Limb(Word::MAX); let r = a.wrapping_rem(&b); - assert_eq!(r, UInt::ZERO); + assert_eq!(r, Uint::ZERO); a.limbs[a.limbs.len() - 1] = Limb(1 << (HI_BIT - 7)); b.limbs[b.limbs.len() - 1] = Limb(0x82 << (HI_BIT - 7)); let r = a.wrapping_rem(&b); diff --git a/src/uint/encoding.rs b/src/uint/encoding.rs index a8397623..2ec545e8 100644 --- a/src/uint/encoding.rs +++ b/src/uint/encoding.rs @@ -1,4 +1,4 @@ -//! Const-friendly decoding operations for [`UInt`] +//! Const-friendly decoding operations for [`Uint`] #[cfg(all(feature = "der", feature = "generic-array"))] mod der; @@ -6,11 +6,11 @@ mod der; #[cfg(feature = "rlp")] mod rlp; -use super::UInt; +use super::Uint; use crate::{Encoding, Limb, Word}; -impl UInt { - /// Create a new [`UInt`] from the provided big endian bytes. +impl Uint { + /// Create a new [`Uint`] from the provided big endian bytes. pub const fn from_be_slice(bytes: &[u8]) -> Self { assert!( bytes.len() == Limb::BYTE_SIZE * LIMBS, @@ -31,10 +31,10 @@ impl UInt { i += 1; } - UInt::new(res) + Uint::new(res) } - /// Create a new [`UInt`] from the provided big endian hex string. + /// Create a new [`Uint`] from the provided big endian hex string. pub const fn from_be_hex(hex: &str) -> Self { let bytes = hex.as_bytes(); @@ -58,10 +58,10 @@ impl UInt { i += 1; } - UInt::new(res) + Uint::new(res) } - /// Create a new [`UInt`] from the provided little endian bytes. + /// Create a new [`Uint`] from the provided little endian bytes. pub const fn from_le_slice(bytes: &[u8]) -> Self { assert!( bytes.len() == Limb::BYTE_SIZE * LIMBS, @@ -82,10 +82,10 @@ impl UInt { i += 1; } - UInt::new(res) + Uint::new(res) } - /// Create a new [`UInt`] from the provided little endian hex string. + /// Create a new [`Uint`] from the provided little endian hex string. pub const fn from_le_hex(hex: &str) -> Self { let bytes = hex.as_bytes(); @@ -109,10 +109,10 @@ impl UInt { i += 1; } - UInt::new(res) + Uint::new(res) } - /// Serialize this [`UInt`] as big-endian, writing it into the provided + /// Serialize this [`Uint`] as big-endian, writing it into the provided /// byte slice. #[inline] #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))] @@ -130,7 +130,7 @@ impl UInt { } } - /// Serialize this [`UInt`] as little-endian, writing it into the provided + /// Serialize this [`Uint`] as little-endian, writing it into the provided /// byte slice. #[inline] #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))] @@ -183,16 +183,16 @@ mod tests { use {crate::U128, alloc::format}; #[cfg(target_pointer_width = "32")] - use crate::U64 as UIntEx; + use crate::U64 as UintEx; #[cfg(target_pointer_width = "64")] - use crate::U128 as UIntEx; + use crate::U128 as UintEx; #[test] #[cfg(target_pointer_width = "32")] fn from_be_slice() { let bytes = hex!("0011223344556677"); - let n = UIntEx::from_be_slice(&bytes); + let n = UintEx::from_be_slice(&bytes); assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } @@ -200,7 +200,7 @@ mod tests { #[cfg(target_pointer_width = "64")] fn from_be_slice() { let bytes = hex!("00112233445566778899aabbccddeeff"); - let n = UIntEx::from_be_slice(&bytes); + let n = UintEx::from_be_slice(&bytes); assert_eq!( n.limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] @@ -211,7 +211,7 @@ mod tests { #[cfg(target_pointer_width = "32")] fn from_le_slice() { let bytes = hex!("7766554433221100"); - let n = UIntEx::from_le_slice(&bytes); + let n = UintEx::from_le_slice(&bytes); assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } @@ -219,7 +219,7 @@ mod tests { #[cfg(target_pointer_width = "64")] fn from_le_slice() { let bytes = hex!("ffeeddccbbaa99887766554433221100"); - let n = UIntEx::from_le_slice(&bytes); + let n = UintEx::from_le_slice(&bytes); assert_eq!( n.limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] @@ -229,14 +229,14 @@ mod tests { #[test] #[cfg(target_pointer_width = "32")] fn from_be_hex() { - let n = UIntEx::from_be_hex("0011223344556677"); + let n = UintEx::from_be_hex("0011223344556677"); assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_be_hex() { - let n = UIntEx::from_be_hex("00112233445566778899aabbccddeeff"); + let n = UintEx::from_be_hex("00112233445566778899aabbccddeeff"); assert_eq!( n.limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] @@ -246,14 +246,14 @@ mod tests { #[test] #[cfg(target_pointer_width = "32")] fn from_le_hex() { - let n = UIntEx::from_le_hex("7766554433221100"); + let n = UintEx::from_le_hex("7766554433221100"); assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_le_hex() { - let n = UIntEx::from_le_hex("ffeeddccbbaa99887766554433221100"); + let n = UintEx::from_le_hex("ffeeddccbbaa99887766554433221100"); assert_eq!( n.limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] diff --git a/src/uint/encoding/der.rs b/src/uint/encoding/der.rs index cf1b9c31..1b971f0c 100644 --- a/src/uint/encoding/der.rs +++ b/src/uint/encoding/der.rs @@ -1,42 +1,42 @@ -//! Support for decoding/encoding [`UInt`] as an ASN.1 DER `INTEGER`. +//! Support for decoding/encoding [`Uint`] as an ASN.1 DER `INTEGER`. -use crate::{generic_array::GenericArray, ArrayEncoding, UInt}; +use crate::{generic_array::GenericArray, ArrayEncoding, Uint}; use ::der::{ asn1::{AnyRef, UIntRef}, DecodeValue, EncodeValue, FixedTag, Length, Tag, }; #[cfg_attr(docsrs, doc(cfg(feature = "der")))] -impl<'a, const LIMBS: usize> TryFrom> for UInt +impl<'a, const LIMBS: usize> TryFrom> for Uint where - UInt: ArrayEncoding, + Uint: ArrayEncoding, { type Error = der::Error; - fn try_from(any: AnyRef<'a>) -> der::Result> { + fn try_from(any: AnyRef<'a>) -> der::Result> { UIntRef::try_from(any)?.try_into() } } #[cfg_attr(docsrs, doc(cfg(feature = "der")))] -impl<'a, const LIMBS: usize> TryFrom> for UInt +impl<'a, const LIMBS: usize> TryFrom> for Uint where - UInt: ArrayEncoding, + Uint: ArrayEncoding, { type Error = der::Error; - fn try_from(bytes: UIntRef<'a>) -> der::Result> { + fn try_from(bytes: UIntRef<'a>) -> der::Result> { let mut array = GenericArray::default(); let offset = array.len().saturating_sub(bytes.len().try_into()?); array[offset..].copy_from_slice(bytes.as_bytes()); - Ok(UInt::from_be_byte_array(array)) + Ok(Uint::from_be_byte_array(array)) } } #[cfg_attr(docsrs, doc(cfg(feature = "der")))] -impl<'a, const LIMBS: usize> DecodeValue<'a> for UInt +impl<'a, const LIMBS: usize> DecodeValue<'a> for Uint where - UInt: ArrayEncoding, + Uint: ArrayEncoding, { fn decode_value>(reader: &mut R, header: der::Header) -> der::Result { UIntRef::decode_value(reader, header)?.try_into() @@ -44,9 +44,9 @@ where } #[cfg_attr(docsrs, doc(cfg(feature = "der")))] -impl EncodeValue for UInt +impl EncodeValue for Uint where - UInt: ArrayEncoding, + Uint: ArrayEncoding, { fn value_len(&self) -> der::Result { // TODO(tarcieri): more efficient length calculation @@ -61,9 +61,9 @@ where } #[cfg_attr(docsrs, doc(cfg(feature = "der")))] -impl FixedTag for UInt +impl FixedTag for Uint where - UInt: ArrayEncoding, + Uint: ArrayEncoding, { const TAG: Tag = Tag::Integer; } diff --git a/src/uint/encoding/rlp.rs b/src/uint/encoding/rlp.rs index 8a10170d..478ef4f8 100644 --- a/src/uint/encoding/rlp.rs +++ b/src/uint/encoding/rlp.rs @@ -1,10 +1,10 @@ //! Recursive Length Prefix (RLP) encoding support. -use crate::{Encoding, UInt}; +use crate::{Encoding, Uint}; use rlp::{DecoderError, Rlp, RlpStream}; #[cfg_attr(docsrs, doc(cfg(feature = "rlp")))] -impl rlp::Encodable for UInt +impl rlp::Encodable for Uint where Self: Encoding, { @@ -21,7 +21,7 @@ where } #[cfg_attr(docsrs, doc(cfg(feature = "rlp")))] -impl rlp::Decodable for UInt +impl rlp::Decodable for Uint where Self: Encoding, ::Repr: Default, diff --git a/src/uint/from.rs b/src/uint/from.rs index daa5b709..6c651045 100644 --- a/src/uint/from.rs +++ b/src/uint/from.rs @@ -1,9 +1,9 @@ -//! `From`-like conversions for [`UInt`]. +//! `From`-like conversions for [`Uint`]. -use crate::{Limb, UInt, WideWord, Word, U128, U64}; +use crate::{Limb, Uint, WideWord, Word, U128, U64}; -impl UInt { - /// Create a [`UInt`] from a `u8` (const-friendly) +impl Uint { + /// Create a [`Uint`] from a `u8` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u8(n: u8) -> Self { assert!(LIMBS >= 1, "number of limbs must be greater than zero"); @@ -12,7 +12,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `u16` (const-friendly) + /// Create a [`Uint`] from a `u16` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u16(n: u16) -> Self { assert!(LIMBS >= 1, "number of limbs must be greater than zero"); @@ -21,7 +21,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `u32` (const-friendly) + /// Create a [`Uint`] from a `u32` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable #[allow(trivial_numeric_casts)] pub const fn from_u32(n: u32) -> Self { @@ -31,7 +31,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `u64` (const-friendly) + /// Create a [`Uint`] from a `u64` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable #[cfg(target_pointer_width = "32")] pub const fn from_u64(n: u64) -> Self { @@ -42,7 +42,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `u64` (const-friendly) + /// Create a [`Uint`] from a `u64` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable #[cfg(target_pointer_width = "64")] pub const fn from_u64(n: u64) -> Self { @@ -52,7 +52,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `u128` (const-friendly) + /// Create a [`Uint`] from a `u128` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_u128(n: u128) -> Self { assert!( @@ -80,7 +80,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `Word` (const-friendly) + /// Create a [`Uint`] from a `Word` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_word(n: Word) -> Self { assert!(LIMBS >= 1, "number of limbs must be greater than zero"); @@ -89,7 +89,7 @@ impl UInt { Self { limbs } } - /// Create a [`UInt`] from a `WideWord` (const-friendly) + /// Create a [`Uint`] from a `WideWord` (const-friendly) // TODO(tarcieri): replace with `const impl From` when stable pub const fn from_wide_word(n: WideWord) -> Self { assert!(LIMBS >= 2, "number of limbs must be two or greater"); @@ -100,7 +100,7 @@ impl UInt { } } -impl From for UInt { +impl From for Uint { fn from(n: u8) -> Self { // TODO(tarcieri): const where clause when possible debug_assert!(LIMBS > 0, "limbs must be non-zero"); @@ -108,7 +108,7 @@ impl From for UInt { } } -impl From for UInt { +impl From for Uint { fn from(n: u16) -> Self { // TODO(tarcieri): const where clause when possible debug_assert!(LIMBS > 0, "limbs must be non-zero"); @@ -116,7 +116,7 @@ impl From for UInt { } } -impl From for UInt { +impl From for Uint { fn from(n: u32) -> Self { // TODO(tarcieri): const where clause when possible debug_assert!(LIMBS > 0, "limbs must be non-zero"); @@ -124,7 +124,7 @@ impl From for UInt { } } -impl From for UInt { +impl From for Uint { fn from(n: u64) -> Self { // TODO(tarcieri): const where clause when possible debug_assert!(LIMBS >= (64 / Limb::BIT_SIZE), "not enough limbs"); @@ -132,7 +132,7 @@ impl From for UInt { } } -impl From for UInt { +impl From for Uint { fn from(n: u128) -> Self { // TODO(tarcieri): const where clause when possible debug_assert!(LIMBS >= (128 / Limb::BIT_SIZE), "not enough limbs"); @@ -163,31 +163,31 @@ impl From for u128 { } } -impl From<[Word; LIMBS]> for UInt { +impl From<[Word; LIMBS]> for Uint { fn from(arr: [Word; LIMBS]) -> Self { Self::from_words(arr) } } -impl From> for [Word; LIMBS] { - fn from(n: UInt) -> [Word; LIMBS] { +impl From> for [Word; LIMBS] { + fn from(n: Uint) -> [Word; LIMBS] { *n.as_ref() } } -impl From<[Limb; LIMBS]> for UInt { +impl From<[Limb; LIMBS]> for Uint { fn from(limbs: [Limb; LIMBS]) -> Self { Self { limbs } } } -impl From> for [Limb; LIMBS] { - fn from(n: UInt) -> [Limb; LIMBS] { +impl From> for [Limb; LIMBS] { + fn from(n: Uint) -> [Limb; LIMBS] { n.limbs } } -impl From for UInt { +impl From for Uint { fn from(limb: Limb) -> Self { limb.0.into() } @@ -198,26 +198,26 @@ mod tests { use crate::{Limb, Word, U128}; #[cfg(target_pointer_width = "32")] - use crate::U64 as UIntEx; + use crate::U64 as UintEx; #[cfg(target_pointer_width = "64")] - use crate::U128 as UIntEx; + use crate::U128 as UintEx; #[test] fn from_u8() { - let n = UIntEx::from(42u8); + let n = UintEx::from(42u8); assert_eq!(n.limbs(), &[Limb(42), Limb(0)]); } #[test] fn from_u16() { - let n = UIntEx::from(42u16); + let n = UintEx::from(42u16); assert_eq!(n.limbs(), &[Limb(42), Limb(0)]); } #[test] fn from_u64() { - let n = UIntEx::from(42u64); + let n = UintEx::from(42u64); assert_eq!(n.limbs(), &[Limb(42), Limb(0)]); } @@ -231,7 +231,7 @@ mod tests { #[test] fn array_round_trip() { let arr1 = [1, 2]; - let n = UIntEx::from(arr1); + let n = UintEx::from(arr1); let arr2: [Word; 2] = n.into(); assert_eq!(arr1, arr2); } diff --git a/src/uint/inv_mod.rs b/src/uint/inv_mod.rs index 911fbc4e..4e0e650d 100644 --- a/src/uint/inv_mod.rs +++ b/src/uint/inv_mod.rs @@ -1,9 +1,9 @@ use subtle::{Choice, CtOption}; -use super::UInt; +use super::Uint; use crate::{Limb, Word}; -impl UInt { +impl Uint { /// Computes 1/`self` mod 2^k as specified in Algorithm 4 from /// A Secure Algorithm for Inversion Modulo 2k by /// Sadiel de la Fe and Carles Ferrer. See @@ -29,13 +29,13 @@ impl UInt { } /// Computes the multiplicative inverse of `self` mod `modulus`. In other words `self^-1 mod modulus`. Returns `(inverse, 1...1)` if an inverse exists, otherwise `(undefined, 0...0)`. The algorithm is the same as in GMP 6.2.1's `mpn_sec_invert`. - pub const fn inv_odd_mod(self, modulus: UInt) -> (Self, Word) { + pub const fn inv_odd_mod(self, modulus: Uint) -> (Self, Word) { debug_assert!(modulus.ct_is_odd() == Word::MAX); let mut a = self; - let mut u = UInt::ONE; - let mut v = UInt::ZERO; + let mut u = Uint::ONE; + let mut v = Uint::ZERO; let mut b = modulus; @@ -45,7 +45,7 @@ impl UInt { let mut m1hp = modulus; let (m1hp_new, carry) = m1hp.shr_1(); debug_assert!(carry == Word::MAX); - m1hp = m1hp_new.wrapping_add(&UInt::ONE); + m1hp = m1hp_new.wrapping_add(&Uint::ONE); let mut i = 0; while i < bit_size { @@ -56,11 +56,11 @@ impl UInt { // Set `self -= b` if `self` is odd. let (new_a, swap) = a.conditional_wrapping_sub(&b, self_odd); // Set `b += self` if `swap` is true. - b = UInt::ct_select(b, b.wrapping_add(&new_a), swap); + b = Uint::ct_select(b, b.wrapping_add(&new_a), swap); // Negate `self` if `swap` is true. a = new_a.conditional_wrapping_neg(swap); - let (new_u, new_v) = UInt::ct_swap(u, v, swap); + let (new_u, new_v) = Uint::ct_swap(u, v, swap); let (new_u, cy) = new_u.conditional_wrapping_sub(&new_v, self_odd); let (new_u, cyy) = new_u.conditional_wrapping_add(&modulus, cy); debug_assert!(cy == cyy); @@ -78,13 +78,13 @@ impl UInt { i += 1; } - debug_assert!(a.ct_cmp(&UInt::ZERO) == 0); + debug_assert!(a.ct_cmp(&Uint::ZERO) == 0); - (v, b.ct_not_eq(&UInt::ONE) ^ Word::MAX) + (v, b.ct_not_eq(&Uint::ONE) ^ Word::MAX) } /// Computes the multiplicative inverse of `self` mod `modulus`. In other words `self^-1 mod modulus`. Returns `None` if the inverse does not exist. The algorithm is the same as in GMP 6.2.1's `mpn_sec_invert`. - pub fn inv_odd_mod_option(self, modulus: UInt) -> CtOption { + pub fn inv_odd_mod_option(self, modulus: Uint) -> CtOption { let (inverse, exists) = self.inv_odd_mod(modulus); CtOption::new(inverse, Choice::from((exists == Word::MAX) as u8)) } diff --git a/src/uint/modular.rs b/src/uint/modular.rs index 8a553991..8bea02ef 100644 --- a/src/uint/modular.rs +++ b/src/uint/modular.rs @@ -1,6 +1,6 @@ use subtle::CtOption; -use crate::{UInt, Word}; +use crate::{Uint, Word}; mod reduction; @@ -40,12 +40,12 @@ where Self: Sized, { /// Computes the (reduced) exponentiation of a residue. - fn pow(self, exponent: &UInt) -> Self { + fn pow(self, exponent: &Uint) -> Self { self.pow_specific(exponent, LIMBS * Word::BITS as usize) } /// Computes the (reduced) exponentiation of a residue, here `exponent_bits` represents the number of bits to take into account for the exponent. Note that this value is leaked in the time pattern. - fn pow_specific(self, exponent: &UInt, exponent_bits: usize) -> Self; + fn pow_specific(self, exponent: &Uint, exponent_bits: usize) -> Self; } /// Provides a consistent interface to invert a residue. @@ -62,7 +62,7 @@ pub trait GenericResidue: AddResidue + MulResidue + PowResidue + InvResidue { /// Retrieves the integer currently encoded in this `Residue`, guaranteed to be reduced. - fn retrieve(&self) -> UInt; + fn retrieve(&self) -> Uint; } #[cfg(test)] @@ -73,7 +73,7 @@ mod tests { constant_mod::Residue, constant_mod::ResidueParams, reduction::montgomery_reduction, }, traits::Encoding, - UInt, U256, U64, + Uint, U256, U64, }; impl_modulus!( @@ -109,11 +109,11 @@ mod tests { // Divide the value R by R, which should equal 1 assert_eq!( montgomery_reduction::<{ Modulus2::LIMBS }>( - (Modulus2::R, UInt::ZERO), + (Modulus2::R, Uint::ZERO), Modulus2::MODULUS, Modulus2::MOD_NEG_INV ), - UInt::ONE + Uint::ONE ); } @@ -122,7 +122,7 @@ mod tests { // Divide the value R^2 by R, which should equal R assert_eq!( montgomery_reduction::<{ Modulus2::LIMBS }>( - (Modulus2::R2, UInt::ZERO), + (Modulus2::R2, Uint::ZERO), Modulus2::MODULUS, Modulus2::MOD_NEG_INV ), @@ -172,7 +172,7 @@ mod tests { let c = hi.concat(&lo); let red = c.reduce(&U256::ZERO.concat(&Modulus2::MODULUS)).unwrap(); let (hi, lo) = red.split(); - assert_eq!(hi, UInt::ZERO); + assert_eq!(hi, Uint::ZERO); assert_eq!( montgomery_reduction::<{ Modulus2::LIMBS }>( diff --git a/src/uint/modular/add.rs b/src/uint/modular/add.rs index 92da3530..f4d0f79d 100644 --- a/src/uint/modular/add.rs +++ b/src/uint/modular/add.rs @@ -1,9 +1,9 @@ -use crate::UInt; +use crate::Uint; pub(crate) const fn add_montgomery_form( - a: &UInt, - b: &UInt, - modulus: &UInt, -) -> UInt { + a: &Uint, + b: &Uint, + modulus: &Uint, +) -> Uint { a.add_mod(b, modulus) } diff --git a/src/uint/modular/constant_mod.rs b/src/uint/modular/constant_mod.rs index fa6bf2d9..d815f8c5 100644 --- a/src/uint/modular/constant_mod.rs +++ b/src/uint/modular/constant_mod.rs @@ -2,7 +2,7 @@ use core::marker::PhantomData; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; -use crate::{Limb, UInt, Zero}; +use crate::{Limb, Uint, Zero}; use super::{reduction::montgomery_reduction, GenericResidue}; @@ -27,13 +27,13 @@ pub trait ResidueParams: Copy { const LIMBS: usize; /// The constant modulus - const MODULUS: UInt; + const MODULUS: Uint; /// Parameter used in Montgomery reduction - const R: UInt; + const R: Uint; /// R^2, used to move into Montgomery form - const R2: UInt; + const R2: Uint; /// R^3, used to perform a multiplicative inverse - const R3: UInt; + const R3: Uint; /// The lowest limbs of -(MODULUS^-1) mod R // We only need the LSB because during reduction this value is multiplied modulo 2**64. const MOD_NEG_INV: Limb; @@ -45,14 +45,14 @@ pub struct Residue where MOD: ResidueParams, { - montgomery_form: UInt, + montgomery_form: Uint, phantom: PhantomData, } impl, const LIMBS: usize> Residue { /// The representation of 0 mod `MOD`. pub const ZERO: Self = Self { - montgomery_form: UInt::::ZERO, + montgomery_form: Uint::::ZERO, phantom: PhantomData, }; @@ -63,7 +63,7 @@ impl, const LIMBS: usize> Residue { }; /// Instantiates a new `Residue` that represents this `integer` mod `MOD`. - pub const fn new(integer: UInt) -> Self { + pub const fn new(integer: Uint) -> Self { let mut modular_integer = Residue { montgomery_form: integer, phantom: PhantomData, @@ -77,9 +77,9 @@ impl, const LIMBS: usize> Residue { } /// Retrieves the integer currently encoded in this `Residue`, guaranteed to be reduced. - pub const fn retrieve(&self) -> UInt { + pub const fn retrieve(&self) -> Uint { montgomery_reduction::( - (self.montgomery_form, UInt::ZERO), + (self.montgomery_form, Uint::ZERO), MOD::MODULUS, MOD::MOD_NEG_INV, ) @@ -87,7 +87,7 @@ impl, const LIMBS: usize> Residue { } impl, const LIMBS: usize> GenericResidue for Residue { - fn retrieve(&self) -> UInt { + fn retrieve(&self) -> Uint { self.retrieve() } } @@ -97,7 +97,7 @@ impl + Copy, const LIMBS: usize> ConditionallySelectab { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Residue { - montgomery_form: UInt::conditional_select( + montgomery_form: Uint::conditional_select( &a.montgomery_form, &b.montgomery_form, choice, diff --git a/src/uint/modular/constant_mod/const_add.rs b/src/uint/modular/constant_mod/const_add.rs index 3c3ecbdf..d19ce6fe 100644 --- a/src/uint/modular/constant_mod/const_add.rs +++ b/src/uint/modular/constant_mod/const_add.rs @@ -2,7 +2,7 @@ use core::ops::AddAssign; use crate::{ modular::{add::add_montgomery_form, AddResidue}, - UInt, + Uint, }; use super::{Residue, ResidueParams}; @@ -27,10 +27,10 @@ impl, const LIMBS: usize> Residue { } } -impl, const LIMBS: usize> AddAssign<&UInt> +impl, const LIMBS: usize> AddAssign<&Uint> for Residue { - fn add_assign(&mut self, rhs: &UInt) { + fn add_assign(&mut self, rhs: &Uint) { *self += &Residue::new(*rhs); } } diff --git a/src/uint/modular/constant_mod/const_pow.rs b/src/uint/modular/constant_mod/const_pow.rs index fc1e63c1..b2d0dac9 100644 --- a/src/uint/modular/constant_mod/const_pow.rs +++ b/src/uint/modular/constant_mod/const_pow.rs @@ -1,26 +1,26 @@ use crate::{ modular::{pow::pow_montgomery_form, PowResidue}, - UInt, Word, + Uint, Word, }; use super::{Residue, ResidueParams}; impl, const LIMBS: usize> PowResidue for Residue { - fn pow_specific(self, exponent: &UInt, exponent_bits: usize) -> Self { + fn pow_specific(self, exponent: &Uint, exponent_bits: usize) -> Self { self.pow_specific(exponent, exponent_bits) } } impl, const LIMBS: usize> Residue { /// Performs modular exponentiation using Montgomery's ladder. - pub const fn pow(self, exponent: &UInt) -> Residue { + pub const fn pow(self, exponent: &Uint) -> Residue { self.pow_specific(exponent, LIMBS * Word::BITS as usize) } /// Performs modular exponentiation using Montgomery's ladder. `exponent_bits` represents the number of bits to take into account for the exponent. Note that this value is leaked in the time pattern. pub const fn pow_specific( self, - exponent: &UInt, + exponent: &Uint, exponent_bits: usize, ) -> Residue { Self { diff --git a/src/uint/modular/constant_mod/macros.rs b/src/uint/modular/constant_mod/macros.rs index 8c23fb57..69187d16 100644 --- a/src/uint/modular/constant_mod/macros.rs +++ b/src/uint/modular/constant_mod/macros.rs @@ -8,24 +8,24 @@ macro_rules! impl_modulus { pub struct $name {} impl ResidueParams<{ nlimbs!(<$uint_type>::BIT_SIZE) }> for $name where - $crate::UInt<{ nlimbs!(<$uint_type>::BIT_SIZE) }>: - $crate::traits::Concat>, - $crate::UInt: $crate::traits::Split, + $crate::Uint<{ nlimbs!(<$uint_type>::BIT_SIZE) }>: + $crate::traits::Concat>, + $crate::Uint: $crate::traits::Split, { const LIMBS: usize = { nlimbs!(<$uint_type>::BIT_SIZE) }; - const MODULUS: $crate::UInt<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = + const MODULUS: $crate::Uint<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = <$uint_type>::from_be_hex($value); - const R: $crate::UInt<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = $crate::UInt::MAX + const R: $crate::Uint<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = $crate::Uint::MAX .ct_reduce(&Self::MODULUS) .0 - .wrapping_add(&$crate::UInt::ONE); - const R2: $crate::UInt<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = - $crate::UInt::ct_reduce_wide(Self::R.square_wide(), &Self::MODULUS).0; + .wrapping_add(&$crate::Uint::ONE); + const R2: $crate::Uint<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = + $crate::Uint::ct_reduce_wide(Self::R.square_wide(), &Self::MODULUS).0; const MOD_NEG_INV: $crate::Limb = $crate::Limb( $crate::Word::MIN .wrapping_sub(Self::MODULUS.inv_mod2k($crate::Word::BITS as usize).limbs[0].0), ); - const R3: $crate::UInt<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = + const R3: $crate::Uint<{ nlimbs!(<$uint_type>::BIT_SIZE) }> = $crate::uint::modular::reduction::montgomery_reduction( Self::R2.square_wide(), Self::MODULUS, diff --git a/src/uint/modular/inv.rs b/src/uint/modular/inv.rs index 86c2b7f3..7fb6e282 100644 --- a/src/uint/modular/inv.rs +++ b/src/uint/modular/inv.rs @@ -1,11 +1,11 @@ -use crate::{modular::reduction::montgomery_reduction, Limb, UInt, Word}; +use crate::{modular::reduction::montgomery_reduction, Limb, Uint, Word}; pub const fn inv_montgomery_form( - x: UInt, - modulus: UInt, - r3: &UInt, + x: Uint, + modulus: Uint, + r3: &Uint, mod_neg_inv: Limb, -) -> (UInt, Word) { +) -> (Uint, Word) { let (inverse, error) = x.inv_odd_mod(modulus); ( montgomery_reduction(inverse.mul_wide(r3), modulus, mod_neg_inv), diff --git a/src/uint/modular/mul.rs b/src/uint/modular/mul.rs index 37f4c986..74da6b04 100644 --- a/src/uint/modular/mul.rs +++ b/src/uint/modular/mul.rs @@ -1,22 +1,22 @@ -use crate::{Limb, UInt}; +use crate::{Limb, Uint}; use super::reduction::montgomery_reduction; pub(crate) const fn mul_montgomery_form( - a: &UInt, - b: &UInt, - modulus: UInt, + a: &Uint, + b: &Uint, + modulus: Uint, mod_neg_inv: Limb, -) -> UInt { +) -> Uint { let product = a.mul_wide(b); montgomery_reduction::(product, modulus, mod_neg_inv) } pub(crate) const fn square_montgomery_form( - a: &UInt, - modulus: UInt, + a: &Uint, + modulus: Uint, mod_neg_inv: Limb, -) -> UInt { +) -> Uint { let product = a.square_wide(); montgomery_reduction::(product, modulus, mod_neg_inv) } diff --git a/src/uint/modular/pow.rs b/src/uint/modular/pow.rs index e910fb92..17dd17a8 100644 --- a/src/uint/modular/pow.rs +++ b/src/uint/modular/pow.rs @@ -1,21 +1,21 @@ -use crate::{Limb, UInt, Word}; +use crate::{Limb, Uint, Word}; use super::mul::{mul_montgomery_form, square_montgomery_form}; /// Performs modular exponentiation using Montgomery's ladder. `exponent_bits` represents the number of bits to take into account for the exponent. Note that this value is leaked in the time pattern. pub const fn pow_montgomery_form( - x: UInt, - exponent: &UInt, + x: Uint, + exponent: &Uint, exponent_bits: usize, - modulus: UInt, - r: UInt, + modulus: Uint, + r: Uint, mod_neg_inv: Limb, -) -> UInt { - let mut x1: UInt = r; - let mut x2: UInt = x; +) -> Uint { + let mut x1: Uint = r; + let mut x2: Uint = x; - // Shift the exponent all the way to the left so the leftmost bit is the MSB of the `UInt` - let mut n: UInt = exponent.shl_vartime((LIMBS * Word::BITS as usize) - exponent_bits); + // Shift the exponent all the way to the left so the leftmost bit is the MSB of the `Uint` + let mut n: Uint = exponent.shl_vartime((LIMBS * Word::BITS as usize) - exponent_bits); let mut i = 0; while i < exponent_bits { @@ -23,14 +23,14 @@ pub const fn pow_montgomery_form( let (next_n, overflow) = n.shl_1(); n = next_n; - let mut product: UInt = x1; + let mut product: Uint = x1; product = mul_montgomery_form(&product, &x2, modulus, mod_neg_inv); - let mut square = UInt::ct_select(x1, x2, overflow); + let mut square = Uint::ct_select(x1, x2, overflow); square = square_montgomery_form(&square, modulus, mod_neg_inv); - x1 = UInt::::ct_select(square, product, overflow); - x2 = UInt::::ct_select(product, square, overflow); + x1 = Uint::::ct_select(square, product, overflow); + x2 = Uint::::ct_select(product, square, overflow); i += 1; } diff --git a/src/uint/modular/reduction.rs b/src/uint/modular/reduction.rs index da4895a2..cd967a35 100644 --- a/src/uint/modular/reduction.rs +++ b/src/uint/modular/reduction.rs @@ -1,11 +1,11 @@ -use crate::{Limb, UInt, WideWord, Word}; +use crate::{Limb, Uint, WideWord, Word}; /// Algorithm 14.32 in Handbook of Applied Cryptography (https://cacr.uwaterloo.ca/hac/about/chap14.pdf) pub(crate) const fn montgomery_reduction( - lower_upper: (UInt, UInt), - modulus: UInt, + lower_upper: (Uint, Uint), + modulus: Uint, mod_neg_inv: Limb, -) -> UInt { +) -> Uint { let (mut lower, mut upper) = lower_upper; let mut meta_carry = 0; @@ -51,7 +51,7 @@ pub(crate) const fn montgomery_reduction( // Final reduction (at this point, the value is at most 2 * modulus) let must_reduce = (meta_carry as Word).saturating_mul(Word::MAX) | ((upper.ct_cmp(&modulus) != -1) as Word).saturating_mul(Word::MAX); - upper = upper.wrapping_sub(&UInt::ct_select(UInt::ZERO, modulus, must_reduce)); + upper = upper.wrapping_sub(&Uint::ct_select(Uint::ZERO, modulus, must_reduce)); upper } diff --git a/src/uint/modular/runtime_mod.rs b/src/uint/modular/runtime_mod.rs index 474af1e2..8ed1db1c 100644 --- a/src/uint/modular/runtime_mod.rs +++ b/src/uint/modular/runtime_mod.rs @@ -1,4 +1,4 @@ -use crate::{Limb, UInt, Word}; +use crate::{Limb, Uint, Word}; use super::{reduction::montgomery_reduction, GenericResidue}; @@ -15,13 +15,13 @@ mod runtime_pow; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DynResidueParams { // The constant modulus - modulus: UInt, + modulus: Uint, // Parameter used in Montgomery reduction - r: UInt, + r: Uint, // R^2, used to move into Montgomery form - r2: UInt, + r2: Uint, // R^3, used to compute the multiplicative inverse - r3: UInt, + r3: Uint, // The lowest limbs of -(MODULUS^-1) mod R // We only need the LSB because during reduction this value is multiplied modulo 2**64. mod_neg_inv: Limb, @@ -29,9 +29,9 @@ pub struct DynResidueParams { impl DynResidueParams { /// Instantiates a new set of `ResidueParams` representing the given `modulus`. - pub fn new(modulus: UInt) -> Self { - let r = UInt::MAX.ct_reduce(&modulus).0.wrapping_add(&UInt::ONE); - let r2 = UInt::ct_reduce_wide(r.square_wide(), &modulus).0; + pub fn new(modulus: Uint) -> Self { + let r = Uint::MAX.ct_reduce(&modulus).0.wrapping_add(&Uint::ONE); + let r2 = Uint::ct_reduce_wide(r.square_wide(), &modulus).0; let mod_neg_inv = Limb(Word::MIN.wrapping_sub(modulus.inv_mod2k(Word::BITS as usize).limbs[0].0)); let r3 = montgomery_reduction(r2.square_wide(), modulus, mod_neg_inv); @@ -49,13 +49,13 @@ impl DynResidueParams { /// A residue represented using `LIMBS` limbs. The odd modulus of this residue is set at runtime. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DynResidue { - montgomery_form: UInt, + montgomery_form: Uint, residue_params: DynResidueParams, } impl DynResidue { /// Instantiates a new `Residue` that represents this `integer` mod `MOD`. - pub const fn new(integer: UInt, residue_params: DynResidueParams) -> Self { + pub const fn new(integer: Uint, residue_params: DynResidueParams) -> Self { let mut modular_integer = Self { montgomery_form: integer, residue_params, @@ -69,9 +69,9 @@ impl DynResidue { } /// Retrieves the integer currently encoded in this `Residue`, guaranteed to be reduced. - pub const fn retrieve(&self) -> UInt { + pub const fn retrieve(&self) -> Uint { montgomery_reduction( - (self.montgomery_form, UInt::ZERO), + (self.montgomery_form, Uint::ZERO), self.residue_params.modulus, self.residue_params.mod_neg_inv, ) @@ -80,14 +80,14 @@ impl DynResidue { /// Instantiates a new `Residue` that represents zero. pub const fn zero(residue_params: DynResidueParams) -> Self { Self { - montgomery_form: UInt::::ZERO, + montgomery_form: Uint::::ZERO, residue_params, } } } impl GenericResidue for DynResidue { - fn retrieve(&self) -> UInt { + fn retrieve(&self) -> Uint { self.retrieve() } } diff --git a/src/uint/modular/runtime_mod/runtime_add.rs b/src/uint/modular/runtime_mod/runtime_add.rs index 4d294ded..98cc9344 100644 --- a/src/uint/modular/runtime_mod/runtime_add.rs +++ b/src/uint/modular/runtime_mod/runtime_add.rs @@ -2,7 +2,7 @@ use core::ops::{Add, AddAssign}; use crate::{ modular::{add::add_montgomery_form, AddResidue}, - UInt, + Uint, }; use super::DynResidue; @@ -31,8 +31,8 @@ impl AddAssign for DynResidue { } } -impl AddAssign> for DynResidue { - fn add_assign(&mut self, rhs: UInt) { +impl AddAssign> for DynResidue { + fn add_assign(&mut self, rhs: Uint) { self.montgomery_form = add_montgomery_form( &self.montgomery_form, &DynResidue::new(rhs, self.residue_params).montgomery_form, diff --git a/src/uint/modular/runtime_mod/runtime_pow.rs b/src/uint/modular/runtime_mod/runtime_pow.rs index dea06945..52b215c2 100644 --- a/src/uint/modular/runtime_mod/runtime_pow.rs +++ b/src/uint/modular/runtime_mod/runtime_pow.rs @@ -1,19 +1,19 @@ use crate::{ modular::{pow::pow_montgomery_form, PowResidue}, - UInt, + Uint, }; use super::DynResidue; impl PowResidue for DynResidue { - fn pow_specific(self, exponent: &UInt, exponent_bits: usize) -> Self { + fn pow_specific(self, exponent: &Uint, exponent_bits: usize) -> Self { self.pow_specific(exponent, exponent_bits) } } impl DynResidue { /// Computes the (reduced) exponentiation of a residue, here `exponent_bits` represents the number of bits to take into account for the exponent. Note that this value is leaked in the time pattern. - pub const fn pow_specific(self, exponent: &UInt, exponent_bits: usize) -> Self { + pub const fn pow_specific(self, exponent: &Uint, exponent_bits: usize) -> Self { Self { montgomery_form: pow_montgomery_form( self.montgomery_form, diff --git a/src/uint/mul.rs b/src/uint/mul.rs index 3600aa55..507e2706 100644 --- a/src/uint/mul.rs +++ b/src/uint/mul.rs @@ -1,10 +1,10 @@ -//! [`UInt`] addition operations. +//! [`Uint`] addition operations. -use crate::{Checked, CheckedMul, Concat, Limb, UInt, Wrapping, Zero}; +use crate::{Checked, CheckedMul, Concat, Limb, Uint, Wrapping, Zero}; use core::ops::{Mul, MulAssign}; use subtle::CtOption; -impl UInt { +impl Uint { /// Compute "wide" multiplication, with a product twice the size of the input. /// /// Returns a tuple containing the `(lo, hi)` components of the product. @@ -90,7 +90,7 @@ impl UInt { } } -impl CheckedMul<&UInt> for UInt { +impl CheckedMul<&Uint> for Uint { type Output = Self; fn checked_mul(&self, rhs: &Self) -> CtOption { @@ -99,89 +99,89 @@ impl CheckedMul<&UInt> for UInt { } } -impl Mul for Wrapping> { +impl Mul for Wrapping> { type Output = Self; - fn mul(self, rhs: Self) -> Wrapping> { + fn mul(self, rhs: Self) -> Wrapping> { Wrapping(self.0.wrapping_mul(&rhs.0)) } } -impl Mul<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl Mul<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn mul(self, rhs: &Wrapping>) -> Wrapping> { + fn mul(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_mul(&rhs.0)) } } -impl Mul>> for &Wrapping> { - type Output = Wrapping>; +impl Mul>> for &Wrapping> { + type Output = Wrapping>; - fn mul(self, rhs: Wrapping>) -> Wrapping> { + fn mul(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_mul(&rhs.0)) } } -impl Mul<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl Mul<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn mul(self, rhs: &Wrapping>) -> Wrapping> { + fn mul(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_mul(&rhs.0)) } } -impl MulAssign for Wrapping> { +impl MulAssign for Wrapping> { fn mul_assign(&mut self, other: Self) { *self = *self * other; } } -impl MulAssign<&Wrapping>> for Wrapping> { +impl MulAssign<&Wrapping>> for Wrapping> { fn mul_assign(&mut self, other: &Self) { *self = *self * other; } } -impl Mul for Checked> { +impl Mul for Checked> { type Output = Self; - fn mul(self, rhs: Self) -> Checked> { + fn mul(self, rhs: Self) -> Checked> { Checked(self.0.and_then(|a| rhs.0.and_then(|b| a.checked_mul(&b)))) } } -impl Mul<&Checked>> for Checked> { - type Output = Checked>; +impl Mul<&Checked>> for Checked> { + type Output = Checked>; - fn mul(self, rhs: &Checked>) -> Checked> { + fn mul(self, rhs: &Checked>) -> Checked> { Checked(self.0.and_then(|a| rhs.0.and_then(|b| a.checked_mul(&b)))) } } -impl Mul>> for &Checked> { - type Output = Checked>; +impl Mul>> for &Checked> { + type Output = Checked>; - fn mul(self, rhs: Checked>) -> Checked> { + fn mul(self, rhs: Checked>) -> Checked> { Checked(self.0.and_then(|a| rhs.0.and_then(|b| a.checked_mul(&b)))) } } -impl Mul<&Checked>> for &Checked> { - type Output = Checked>; +impl Mul<&Checked>> for &Checked> { + type Output = Checked>; - fn mul(self, rhs: &Checked>) -> Checked> { + fn mul(self, rhs: &Checked>) -> Checked> { Checked(self.0.and_then(|a| rhs.0.and_then(|b| a.checked_mul(&b)))) } } -impl MulAssign for Checked> { +impl MulAssign for Checked> { fn mul_assign(&mut self, other: Self) { *self = *self * other; } } -impl MulAssign<&Checked>> for Checked> { +impl MulAssign<&Checked>> for Checked> { fn mul_assign(&mut self, other: &Self) { *self = *self * other; } diff --git a/src/uint/mul_mod.rs b/src/uint/mul_mod.rs index 1e9c053e..e91f1451 100644 --- a/src/uint/mul_mod.rs +++ b/src/uint/mul_mod.rs @@ -1,15 +1,15 @@ -//! [`UInt`] multiplication modulus operations. +//! [`Uint`] multiplication modulus operations. -use crate::{Limb, UInt, WideWord, Word}; +use crate::{Limb, Uint, WideWord, Word}; -impl UInt { +impl Uint { /// Computes `self * rhs mod p` in constant time for the special modulus /// `p = MAX+1-c` where `c` is small enough to fit in a single [`Limb`]. /// For the modulus reduction, this function implements Algorithm 14.47 from /// the "Handbook of Applied Cryptography", by A. Menezes, P. van Oorschot, /// and S. Vanstone, CRC Press, 1996. pub const fn mul_mod_special(&self, rhs: &Self, c: Limb) -> Self { - // We implicitly assume `LIMBS > 0`, because `UInt<0>` doesn't compile. + // We implicitly assume `LIMBS > 0`, because `Uint<0>` doesn't compile. // Still the case `LIMBS == 1` needs special handling. if LIMBS == 1 { let prod = self.limbs[0].0 as WideWord * rhs.limbs[0].0 as WideWord; @@ -38,11 +38,11 @@ impl UInt { /// Computes `a + (b * c) + carry`, returning the result along with the new carry. const fn mac_by_limb( - mut a: UInt, - b: UInt, + mut a: Uint, + b: Uint, c: Limb, mut carry: Limb, -) -> (UInt, Limb) { +) -> (Uint, Limb) { let mut i = 0; while i < LIMBS { @@ -57,7 +57,7 @@ const fn mac_by_limb( #[cfg(all(test, feature = "rand"))] mod tests { - use crate::{Limb, NonZero, Random, RandomMod, UInt}; + use crate::{Limb, NonZero, Random, RandomMod, Uint}; use rand_core::SeedableRng; macro_rules! test_mul_mod_special { @@ -71,19 +71,19 @@ mod tests { ]; for special in &moduli { - let p = &NonZero::new(UInt::ZERO.wrapping_sub(&UInt::from_word(special.0))) + let p = &NonZero::new(Uint::ZERO.wrapping_sub(&Uint::from_word(special.0))) .unwrap(); - let minus_one = p.wrapping_sub(&UInt::ONE); + let minus_one = p.wrapping_sub(&Uint::ONE); let base_cases = [ - (UInt::ZERO, UInt::ZERO, UInt::ZERO), - (UInt::ONE, UInt::ZERO, UInt::ZERO), - (UInt::ZERO, UInt::ONE, UInt::ZERO), - (UInt::ONE, UInt::ONE, UInt::ONE), - (minus_one, minus_one, UInt::ONE), - (minus_one, UInt::ONE, minus_one), - (UInt::ONE, minus_one, minus_one), + (Uint::ZERO, Uint::ZERO, Uint::ZERO), + (Uint::ONE, Uint::ZERO, Uint::ZERO), + (Uint::ZERO, Uint::ONE, Uint::ZERO), + (Uint::ONE, Uint::ONE, Uint::ONE), + (minus_one, minus_one, Uint::ONE), + (minus_one, Uint::ONE, minus_one), + (Uint::ONE, minus_one, minus_one), ]; for (a, b, c) in &base_cases { let x = a.mul_mod_special(&b, *special.as_ref()); @@ -91,21 +91,21 @@ mod tests { } for _i in 0..100 { - let a = UInt::<$size>::random_mod(&mut rng, p); - let b = UInt::<$size>::random_mod(&mut rng, p); + let a = Uint::<$size>::random_mod(&mut rng, p); + let b = Uint::<$size>::random_mod(&mut rng, p); let c = a.mul_mod_special(&b, *special.as_ref()); assert!(c < **p, "not reduced: {} >= {} ", c, p); let expected = { let (lo, hi) = a.mul_wide(&b); - let mut prod = UInt::<{ 2 * $size }>::ZERO; + let mut prod = Uint::<{ 2 * $size }>::ZERO; prod.limbs[..$size].clone_from_slice(&lo.limbs); prod.limbs[$size..].clone_from_slice(&hi.limbs); - let mut modulus = UInt::ZERO; + let mut modulus = Uint::ZERO; modulus.limbs[..$size].clone_from_slice(&p.as_ref().limbs); let reduced = prod.reduce(&modulus).unwrap(); - let mut expected = UInt::ZERO; + let mut expected = Uint::ZERO; expected.limbs[..].clone_from_slice(&reduced.limbs[..$size]); expected }; diff --git a/src/uint/neg.rs b/src/uint/neg.rs index 0af1601c..6ea16a14 100644 --- a/src/uint/neg.rs +++ b/src/uint/neg.rs @@ -1,8 +1,8 @@ use core::ops::Neg; -use crate::{UInt, Word, Wrapping}; +use crate::{Uint, Word, Wrapping}; -impl Neg for Wrapping> { +impl Neg for Wrapping> { type Output = Self; fn neg(self) -> Self::Output { @@ -11,12 +11,12 @@ impl Neg for Wrapping> { } } -impl UInt { +impl Uint { /// Negates based on `choice` by wrapping the integer. - pub(crate) const fn conditional_wrapping_neg(self, choice: Word) -> UInt { + pub(crate) const fn conditional_wrapping_neg(self, choice: Word) -> Uint { let (shifted, _) = self.shl_1(); let negated_self = self.wrapping_sub(&shifted); - UInt::ct_select(self, negated_self, choice) + Uint::ct_select(self, negated_self, choice) } } diff --git a/src/uint/neg_mod.rs b/src/uint/neg_mod.rs index 0a1dc033..7472a558 100644 --- a/src/uint/neg_mod.rs +++ b/src/uint/neg_mod.rs @@ -1,8 +1,8 @@ -//! [`UInt`] negation modulus operations. +//! [`Uint`] negation modulus operations. -use crate::{Limb, NegMod, UInt}; +use crate::{Limb, NegMod, Uint}; -impl UInt { +impl Uint { /// Computes `-a mod p` in constant time. /// Assumes `self` is in `[0, p)`. pub const fn neg_mod(&self, p: &Self) -> Self { @@ -25,7 +25,7 @@ impl UInt { } } -impl NegMod for UInt { +impl NegMod for Uint { type Output = Self; fn neg_mod(&self, p: &Self) -> Self { diff --git a/src/uint/rand.rs b/src/uint/rand.rs index df551c71..74197cba 100644 --- a/src/uint/rand.rs +++ b/src/uint/rand.rs @@ -1,13 +1,13 @@ //! Random number generator support -use super::UInt; +use super::Uint; use crate::{Limb, NonZero, Random, RandomMod}; use rand_core::{CryptoRng, RngCore}; use subtle::ConstantTimeLess; #[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))] -impl Random for UInt { - /// Generate a cryptographically secure random [`UInt`]. +impl Random for Uint { + /// Generate a cryptographically secure random [`Uint`]. fn random(mut rng: impl CryptoRng + RngCore) -> Self { let mut limbs = [Limb::ZERO; LIMBS]; @@ -20,8 +20,8 @@ impl Random for UInt { } #[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))] -impl RandomMod for UInt { - /// Generate a cryptographically secure random [`UInt`] which is less than +impl RandomMod for Uint { + /// Generate a cryptographically secure random [`Uint`] which is less than /// a given `modulus`. /// /// This function uses rejection sampling, a method which produces an diff --git a/src/uint/resize.rs b/src/uint/resize.rs index 5a5ec7ee..2c80b895 100644 --- a/src/uint/resize.rs +++ b/src/uint/resize.rs @@ -1,12 +1,12 @@ -use super::UInt; +use super::Uint; -impl UInt { - /// Construct a `UInt` from the unsigned integer value, +impl Uint { + /// Construct a `Uint` from the unsigned integer value, /// truncating the upper bits if the value is too large to be /// represented. #[inline(always)] - pub const fn resize(&self) -> UInt { - let mut res = UInt::ZERO; + pub const fn resize(&self) -> Uint { + let mut res = Uint::ZERO; let mut i = 0; let dim = if T < LIMBS { T } else { LIMBS }; while i < dim { diff --git a/src/uint/shl.rs b/src/uint/shl.rs index 1dbe0e79..2f67966b 100644 --- a/src/uint/shl.rs +++ b/src/uint/shl.rs @@ -1,9 +1,9 @@ -//! [`UInt`] bitwise left shift operations. +//! [`Uint`] bitwise left shift operations. -use crate::{limb::HI_BIT, Limb, UInt, Word}; +use crate::{limb::HI_BIT, Limb, Uint, Word}; use core::ops::{Shl, ShlAssign}; -impl UInt { +impl Uint { /// Computes `self << 1` in constant-time, returning the overflowing bit as a `Word` that is either 0...0 or 1...1. pub(crate) const fn shl_1(&self) -> (Self, Word) { let mut shifted_bits = [0; LIMBS]; @@ -30,7 +30,7 @@ impl UInt { } ( - UInt::new(limbs), + Uint::new(limbs), carry_bits[LIMBS - 1].wrapping_mul(Word::MAX), ) } @@ -89,31 +89,31 @@ impl UInt { } } -impl Shl for UInt { - type Output = UInt; +impl Shl for Uint { + type Output = Uint; /// NOTE: this operation is variable time with respect to `rhs` *ONLY*. /// /// When used with a fixed `rhs`, this function is constant-time with respect /// to `self`. - fn shl(self, rhs: usize) -> UInt { + fn shl(self, rhs: usize) -> Uint { self.shl_vartime(rhs) } } -impl Shl for &UInt { - type Output = UInt; +impl Shl for &Uint { + type Output = Uint; /// NOTE: this operation is variable time with respect to `rhs` *ONLY*. /// /// When used with a fixed `rhs`, this function is constant-time with respect /// to `self`. - fn shl(self, rhs: usize) -> UInt { + fn shl(self, rhs: usize) -> Uint { self.shl_vartime(rhs) } } -impl ShlAssign for UInt { +impl ShlAssign for Uint { /// NOTE: this operation is variable time with respect to `rhs` *ONLY*. /// /// When used with a fixed `rhs`, this function is constant-time with respect @@ -125,7 +125,7 @@ impl ShlAssign for UInt { #[cfg(test)] mod tests { - use crate::{Limb, UInt, U128, U256}; + use crate::{Limb, Uint, U128, U256}; const N: U256 = U256::from_be_hex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"); @@ -186,7 +186,7 @@ mod tests { #[test] fn shl_wide_1_1_128() { assert_eq!( - UInt::shl_vartime_wide((U128::ONE, U128::ONE), 128), + Uint::shl_vartime_wide((U128::ONE, U128::ONE), 128), (U128::ZERO, U128::ONE) ); } @@ -194,7 +194,7 @@ mod tests { #[test] fn shl_wide_max_0_1() { assert_eq!( - UInt::shl_vartime_wide((U128::MAX, U128::ZERO), 1), + Uint::shl_vartime_wide((U128::MAX, U128::ZERO), 1), (U128::MAX.sbb(&U128::ONE, Limb::ZERO).0, U128::ONE) ); } @@ -202,7 +202,7 @@ mod tests { #[test] fn shl_wide_max_max_256() { assert_eq!( - UInt::shl_vartime_wide((U128::MAX, U128::MAX), 256), + Uint::shl_vartime_wide((U128::MAX, U128::MAX), 256), (U128::ZERO, U128::ZERO) ); } diff --git a/src/uint/shr.rs b/src/uint/shr.rs index f24dea46..058baf25 100644 --- a/src/uint/shr.rs +++ b/src/uint/shr.rs @@ -1,10 +1,10 @@ -//! [`UInt`] bitwise right shift operations. +//! [`Uint`] bitwise right shift operations. -use super::UInt; +use super::Uint; use crate::{limb::HI_BIT, Limb, Word}; use core::ops::{Shr, ShrAssign}; -impl UInt { +impl Uint { /// Computes `self >> 1` in constant-time, returning the overflowing bit as a `Word` that is either 0...0 or 1...1. pub(crate) const fn shr_1(&self) -> (Self, Word) { let mut shifted_bits = [0; LIMBS]; @@ -31,7 +31,7 @@ impl UInt { limbs[LIMBS - 1] = Limb(shifted_bits[LIMBS - 1]); ( - UInt::new(limbs), + Uint::new(limbs), (carry_bits[0] >> HI_BIT).wrapping_mul(Word::MAX), ) } @@ -97,31 +97,31 @@ impl UInt { } } -impl Shr for UInt { - type Output = UInt; +impl Shr for Uint { + type Output = Uint; /// NOTE: this operation is variable time with respect to `rhs` *ONLY*. /// /// When used with a fixed `rhs`, this function is constant-time with respect /// to `self`. - fn shr(self, rhs: usize) -> UInt { + fn shr(self, rhs: usize) -> Uint { self.shr_vartime(rhs) } } -impl Shr for &UInt { - type Output = UInt; +impl Shr for &Uint { + type Output = Uint; /// NOTE: this operation is variable time with respect to `rhs` *ONLY*. /// /// When used with a fixed `rhs`, this function is constant-time with respect /// to `self`. - fn shr(self, rhs: usize) -> UInt { + fn shr(self, rhs: usize) -> Uint { self.shr_vartime(rhs) } } -impl ShrAssign for UInt { +impl ShrAssign for Uint { fn shr_assign(&mut self, rhs: usize) { *self = self.shr_vartime(rhs); } @@ -129,7 +129,7 @@ impl ShrAssign for UInt { #[cfg(test)] mod tests { - use crate::{UInt, U128, U256}; + use crate::{Uint, U128, U256}; const N: U256 = U256::from_be_hex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"); @@ -145,7 +145,7 @@ mod tests { #[test] fn shr_wide_1_1_128() { assert_eq!( - UInt::shr_vartime_wide((U128::ONE, U128::ONE), 128), + Uint::shr_vartime_wide((U128::ONE, U128::ONE), 128), (U128::ONE, U128::ZERO) ); } @@ -153,7 +153,7 @@ mod tests { #[test] fn shr_wide_0_max_1() { assert_eq!( - UInt::shr_vartime_wide((U128::ZERO, U128::MAX), 1), + Uint::shr_vartime_wide((U128::ZERO, U128::MAX), 1), (U128::ONE << 127, U128::MAX >> 1) ); } @@ -161,7 +161,7 @@ mod tests { #[test] fn shr_wide_max_max_256() { assert_eq!( - UInt::shr_vartime_wide((U128::MAX, U128::MAX), 256), + Uint::shr_vartime_wide((U128::MAX, U128::MAX), 256), (U128::ZERO, U128::ZERO) ); } diff --git a/src/uint/split.rs b/src/uint/split.rs index ecff9d6d..b681a615 100644 --- a/src/uint/split.rs +++ b/src/uint/split.rs @@ -5,7 +5,7 @@ macro_rules! impl_split { impl $name { /// Split this number in half, returning its high and low components /// respectively. - pub const fn split(&self) -> (UInt<{nlimbs!($bits) / 2}>, UInt<{nlimbs!($bits) / 2}>) { + pub const fn split(&self) -> (Uint<{nlimbs!($bits) / 2}>, Uint<{nlimbs!($bits) / 2}>) { let mut lo = [Limb::ZERO; nlimbs!($bits) / 2]; let mut hi = [Limb::ZERO; nlimbs!($bits) / 2]; let mut i = 0; @@ -24,20 +24,20 @@ macro_rules! impl_split { j += 1; } - (UInt { limbs: hi }, UInt { limbs: lo }) + (Uint { limbs: hi }, Uint { limbs: lo }) } } impl Split for $name { - type Output = UInt<{nlimbs!($bits) / 2}>; + type Output = Uint<{nlimbs!($bits) / 2}>; fn split(&self) -> (Self::Output, Self::Output) { self.split() } } - impl From<$name> for (UInt<{nlimbs!($bits) / 2}>, UInt<{nlimbs!($bits) / 2}>) { - fn from(num: $name) -> (UInt<{nlimbs!($bits) / 2}>, UInt<{nlimbs!($bits) / 2}>) { + impl From<$name> for (Uint<{nlimbs!($bits) / 2}>, Uint<{nlimbs!($bits) / 2}>) { + fn from(num: $name) -> (Uint<{nlimbs!($bits) / 2}>, Uint<{nlimbs!($bits) / 2}>) { num.split() } } diff --git a/src/uint/sqrt.rs b/src/uint/sqrt.rs index 4a9f26a6..26d39410 100644 --- a/src/uint/sqrt.rs +++ b/src/uint/sqrt.rs @@ -1,10 +1,10 @@ -//! [`UInt`] square root operations. +//! [`Uint`] square root operations. -use super::UInt; +use super::Uint; use crate::{Limb, Word}; use subtle::{ConstantTimeEq, CtOption}; -impl UInt { +impl Uint { /// Computes √(`self`) /// Uses Brent & Zimmermann, Modern Computer Arithmetic, v0.5.9, Algorithm 1.13 /// diff --git a/src/uint/sub.rs b/src/uint/sub.rs index b0d1f49c..aafbc3f9 100644 --- a/src/uint/sub.rs +++ b/src/uint/sub.rs @@ -1,11 +1,11 @@ -//! [`UInt`] addition operations. +//! [`Uint`] addition operations. -use super::UInt; +use super::Uint; use crate::{Checked, CheckedSub, Limb, Word, Wrapping, Zero}; use core::ops::{Sub, SubAssign}; use subtle::CtOption; -impl UInt { +impl Uint { /// Computes `a - (b + borrow)`, returning the result along with the new borrow. #[inline(always)] pub const fn sbb(&self, rhs: &Self, mut borrow: Limb) -> (Self, Limb) { @@ -41,7 +41,7 @@ impl UInt { /// Perform wrapping subtraction, returning the underflow bit as a `Word` that is either 0...0 or 1...1. pub(crate) const fn conditional_wrapping_sub(&self, rhs: &Self, choice: Word) -> (Self, Word) { - let actual_rhs = UInt::ct_select(UInt::ZERO, *rhs, choice); + let actual_rhs = Uint::ct_select(Uint::ZERO, *rhs, choice); let (res, borrow) = self.sbb(&actual_rhs, Limb::ZERO); // Here we use a saturating multiplication to get the result to 0...0 or 1...1 @@ -49,7 +49,7 @@ impl UInt { } } -impl CheckedSub<&UInt> for UInt { +impl CheckedSub<&Uint> for Uint { type Output = Self; fn checked_sub(&self, rhs: &Self) -> CtOption { @@ -58,54 +58,54 @@ impl CheckedSub<&UInt> for UInt { } } -impl Sub for Wrapping> { +impl Sub for Wrapping> { type Output = Self; - fn sub(self, rhs: Self) -> Wrapping> { + fn sub(self, rhs: Self) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl Sub<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl Sub<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn sub(self, rhs: &Wrapping>) -> Wrapping> { + fn sub(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl Sub>> for &Wrapping> { - type Output = Wrapping>; +impl Sub>> for &Wrapping> { + type Output = Wrapping>; - fn sub(self, rhs: Wrapping>) -> Wrapping> { + fn sub(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl Sub<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl Sub<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn sub(self, rhs: &Wrapping>) -> Wrapping> { + fn sub(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl SubAssign for Wrapping> { +impl SubAssign for Wrapping> { fn sub_assign(&mut self, other: Self) { *self = *self - other; } } -impl SubAssign<&Wrapping>> for Wrapping> { +impl SubAssign<&Wrapping>> for Wrapping> { fn sub_assign(&mut self, other: &Self) { *self = *self - other; } } -impl Sub for Checked> { +impl Sub for Checked> { type Output = Self; - fn sub(self, rhs: Self) -> Checked> { + fn sub(self, rhs: Self) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -113,10 +113,10 @@ impl Sub for Checked> { } } -impl Sub<&Checked>> for Checked> { - type Output = Checked>; +impl Sub<&Checked>> for Checked> { + type Output = Checked>; - fn sub(self, rhs: &Checked>) -> Checked> { + fn sub(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -124,10 +124,10 @@ impl Sub<&Checked>> for Checked> { } } -impl Sub>> for &Checked> { - type Output = Checked>; +impl Sub>> for &Checked> { + type Output = Checked>; - fn sub(self, rhs: Checked>) -> Checked> { + fn sub(self, rhs: Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -135,10 +135,10 @@ impl Sub>> for &Checked> { } } -impl Sub<&Checked>> for &Checked> { - type Output = Checked>; +impl Sub<&Checked>> for &Checked> { + type Output = Checked>; - fn sub(self, rhs: &Checked>) -> Checked> { + fn sub(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -146,13 +146,13 @@ impl Sub<&Checked>> for &Checked> { } } -impl SubAssign for Checked> { +impl SubAssign for Checked> { fn sub_assign(&mut self, other: Self) { *self = *self - other; } } -impl SubAssign<&Checked>> for Checked> { +impl SubAssign<&Checked>> for Checked> { fn sub_assign(&mut self, other: &Self) { *self = *self - other; } diff --git a/src/uint/sub_mod.rs b/src/uint/sub_mod.rs index f699f66e..728a9276 100644 --- a/src/uint/sub_mod.rs +++ b/src/uint/sub_mod.rs @@ -1,12 +1,12 @@ -//! [`UInt`] subtraction modulus operations. +//! [`Uint`] subtraction modulus operations. -use crate::{Limb, SubMod, UInt}; +use crate::{Limb, SubMod, Uint}; -impl UInt { +impl Uint { /// Computes `self - rhs mod p` in constant time. /// /// Assumes `self - rhs` as unbounded signed integer is in `[-p, p)`. - pub const fn sub_mod(&self, rhs: &UInt, p: &UInt) -> UInt { + pub const fn sub_mod(&self, rhs: &Uint, p: &Uint) -> Uint { let (mut out, borrow) = self.sbb(rhs, Limb::ZERO); // If underflow occurred on the final limb, borrow = 0xfff...fff, otherwise @@ -35,12 +35,12 @@ impl UInt { // the underflow. This cannot underflow due to the assumption // `self - rhs >= -p`. let l = borrow.0 & c.0; - let (out, _) = out.sbb(&UInt::from_word(l), Limb::ZERO); + let (out, _) = out.sbb(&Uint::from_word(l), Limb::ZERO); out } } -impl SubMod for UInt { +impl SubMod for Uint { type Output = Self; fn sub_mod(&self, rhs: &Self, p: &Self) -> Self { @@ -52,7 +52,7 @@ impl SubMod for UInt { #[cfg(all(test, feature = "rand"))] mod tests { - use crate::{Limb, NonZero, Random, RandomMod, UInt}; + use crate::{Limb, NonZero, Random, RandomMod, Uint}; use rand_core::SeedableRng; macro_rules! test_sub_mod { @@ -61,8 +61,8 @@ mod tests { fn $test_name() { let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1); let moduli = [ - NonZero::>::random(&mut rng), - NonZero::>::random(&mut rng), + NonZero::>::random(&mut rng), + NonZero::>::random(&mut rng), ]; for p in &moduli { @@ -72,8 +72,8 @@ mod tests { (0, 0, 0u64.into()), ]; for (a, b, c) in &base_cases { - let a: UInt<$size> = (*a).into(); - let b: UInt<$size> = (*b).into(); + let a: Uint<$size> = (*a).into(); + let b: Uint<$size> = (*b).into(); let x = a.sub_mod(&b, p); assert_eq!(*c, x, "{} - {} mod {} = {} != {}", a, b, p, x, c); @@ -81,8 +81,8 @@ mod tests { if $size > 1 { for _i in 0..100 { - let a: UInt<$size> = Limb::random(&mut rng).into(); - let b: UInt<$size> = Limb::random(&mut rng).into(); + let a: Uint<$size> = Limb::random(&mut rng).into(); + let b: Uint<$size> = Limb::random(&mut rng).into(); let (a, b) = if a < b { (b, a) } else { (a, b) }; let c = a.sub_mod(&b, p); @@ -92,8 +92,8 @@ mod tests { } for _i in 0..100 { - let a = UInt::<$size>::random_mod(&mut rng, p); - let b = UInt::<$size>::random_mod(&mut rng, p); + let a = Uint::<$size>::random_mod(&mut rng, p); + let b = Uint::<$size>::random_mod(&mut rng, p); let c = a.sub_mod(&b, p); assert!(c < **p, "not reduced: {} >= {} ", c, p); @@ -119,17 +119,17 @@ mod tests { ]; for special in &moduli { - let p = &NonZero::new(UInt::ZERO.wrapping_sub(&UInt::from_word(special.0))) + let p = &NonZero::new(Uint::ZERO.wrapping_sub(&Uint::from_word(special.0))) .unwrap(); - let minus_one = p.wrapping_sub(&UInt::ONE); + let minus_one = p.wrapping_sub(&Uint::ONE); let base_cases = [ - (UInt::ZERO, UInt::ZERO, UInt::ZERO), - (UInt::ONE, UInt::ZERO, UInt::ONE), - (UInt::ZERO, UInt::ONE, minus_one), - (minus_one, minus_one, UInt::ZERO), - (UInt::ZERO, minus_one, UInt::ONE), + (Uint::ZERO, Uint::ZERO, Uint::ZERO), + (Uint::ONE, Uint::ZERO, Uint::ONE), + (Uint::ZERO, Uint::ONE, minus_one), + (minus_one, minus_one, Uint::ZERO), + (Uint::ZERO, minus_one, Uint::ONE), ]; for (a, b, c) in &base_cases { let x = a.sub_mod_special(&b, *special.as_ref()); @@ -137,8 +137,8 @@ mod tests { } for _i in 0..100 { - let a = UInt::<$size>::random_mod(&mut rng, p); - let b = UInt::<$size>::random_mod(&mut rng, p); + let a = Uint::<$size>::random_mod(&mut rng, p); + let b = Uint::<$size>::random_mod(&mut rng, p); let c = a.sub_mod_special(&b, *special.as_ref()); assert!(c < **p, "not reduced: {} >= {} ", c, p);