Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename UInt => Uint #143

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
//!
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions src/non_zero.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -237,9 +237,9 @@ impl From<NonZeroU64> for NonZero<Limb> {
}
}

impl<const LIMBS: usize> NonZero<UInt<LIMBS>> {
/// Create a [`NonZero<UInt>`] from a [`UInt`] (const-friendly)
pub const fn from_uint(n: UInt<LIMBS>) -> Self {
impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
/// Create a [`NonZero<Uint>`] from a [`Uint`] (const-friendly)
pub const fn from_uint(n: Uint<LIMBS>) -> Self {
let mut i = 0;
let mut found_non_zero = false;
while i < LIMBS {
Expand All @@ -252,62 +252,62 @@ impl<const LIMBS: usize> NonZero<UInt<LIMBS>> {
Self(n)
}

/// Create a [`NonZero<UInt>`] from a [`NonZeroU8`] (const-friendly)
/// Create a [`NonZero<Uint>`] from a [`NonZeroU8`] (const-friendly)
// TODO(tarcieri): replace with `const impl From<NonZeroU8>` 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<UInt>`] from a [`NonZeroU16`] (const-friendly)
/// Create a [`NonZero<Uint>`] from a [`NonZeroU16`] (const-friendly)
// TODO(tarcieri): replace with `const impl From<NonZeroU16>` 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<UInt>`] from a [`NonZeroU32`] (const-friendly)
/// Create a [`NonZero<Uint>`] from a [`NonZeroU32`] (const-friendly)
// TODO(tarcieri): replace with `const impl From<NonZeroU32>` 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<UInt>`] from a [`NonZeroU64`] (const-friendly)
/// Create a [`NonZero<Uint>`] from a [`NonZeroU64`] (const-friendly)
// TODO(tarcieri): replace with `const impl From<NonZeroU64>` 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<UInt>`] from a [`NonZeroU128`] (const-friendly)
/// Create a [`NonZero<Uint>`] from a [`NonZeroU128`] (const-friendly)
// TODO(tarcieri): replace with `const impl From<NonZeroU128>` when stable
pub const fn from_u128(n: NonZeroU128) -> Self {
Self(UInt::from_u128(n.get()))
Self(Uint::from_u128(n.get()))
}
}

impl<const LIMBS: usize> From<NonZeroU8> for NonZero<UInt<LIMBS>> {
impl<const LIMBS: usize> From<NonZeroU8> for NonZero<Uint<LIMBS>> {
fn from(integer: NonZeroU8) -> Self {
Self::from_u8(integer)
}
}

impl<const LIMBS: usize> From<NonZeroU16> for NonZero<UInt<LIMBS>> {
impl<const LIMBS: usize> From<NonZeroU16> for NonZero<Uint<LIMBS>> {
fn from(integer: NonZeroU16) -> Self {
Self::from_u16(integer)
}
}

impl<const LIMBS: usize> From<NonZeroU32> for NonZero<UInt<LIMBS>> {
impl<const LIMBS: usize> From<NonZeroU32> for NonZero<Uint<LIMBS>> {
fn from(integer: NonZeroU32) -> Self {
Self::from_u32(integer)
}
}

impl<const LIMBS: usize> From<NonZeroU64> for NonZero<UInt<LIMBS>> {
impl<const LIMBS: usize> From<NonZeroU64> for NonZero<Uint<LIMBS>> {
fn from(integer: NonZeroU64) -> Self {
Self::from_u64(integer)
}
}

impl<const LIMBS: usize> From<NonZeroU128> for NonZero<UInt<LIMBS>> {
impl<const LIMBS: usize> From<NonZeroU128> for NonZero<Uint<LIMBS>> {
fn from(integer: NonZeroU128) -> Self {
Self::from_u128(integer)
}
Expand Down
58 changes: 29 additions & 29 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8, N>` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which
/// can be used to `GenericArray<u8, N>` as [`UInt`].
/// [`Uint`] as `GenericArray<u8, N>` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which
/// can be used to `GenericArray<u8, N>` 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<const LIMBS: usize> {
pub struct Uint<const LIMBS: usize> {
/// Inner limb array. Stored from least significant to most significant.
limbs: [Limb; LIMBS],
}

impl<const LIMBS: usize> UInt<LIMBS> {
impl<const LIMBS: usize> Uint<LIMBS> {
/// The value `0`.
pub const ZERO: Self = Self::from_u8(0);

Expand All @@ -86,17 +86,17 @@ impl<const LIMBS: usize> UInt<LIMBS> {
/// 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 {
Expand All @@ -112,7 +112,7 @@ impl<const LIMBS: usize> UInt<LIMBS> {
}

/// 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];
Expand Down Expand Up @@ -145,52 +145,52 @@ impl<const LIMBS: usize> UInt<LIMBS> {
}
}

/// 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<const LIMBS: usize> AsRef<[Word; LIMBS]> for UInt<LIMBS> {
impl<const LIMBS: usize> AsRef<[Word; LIMBS]> for Uint<LIMBS> {
fn as_ref(&self) -> &[Word; LIMBS] {
self.as_words()
}
}

impl<const LIMBS: usize> AsMut<[Word; LIMBS]> for UInt<LIMBS> {
impl<const LIMBS: usize> AsMut<[Word; LIMBS]> for Uint<LIMBS> {
fn as_mut(&mut self) -> &mut [Word; LIMBS] {
self.as_words_mut()
}
}

// TODO(tarcieri): eventually phase this out in favor of `limbs()`?
impl<const LIMBS: usize> AsRef<[Limb]> for UInt<LIMBS> {
impl<const LIMBS: usize> AsRef<[Limb]> for Uint<LIMBS> {
fn as_ref(&self) -> &[Limb] {
self.limbs()
}
}

// TODO(tarcieri): eventually phase this out in favor of `limbs_mut()`?
impl<const LIMBS: usize> AsMut<[Limb]> for UInt<LIMBS> {
impl<const LIMBS: usize> AsMut<[Limb]> for Uint<LIMBS> {
fn as_mut(&mut self) -> &mut [Limb] {
self.limbs_mut()
}
}

impl<const LIMBS: usize> ConditionallySelectable for UInt<LIMBS> {
impl<const LIMBS: usize> ConditionallySelectable for Uint<LIMBS> {
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
let mut limbs = [Limb::ZERO; LIMBS];

Expand All @@ -202,13 +202,13 @@ impl<const LIMBS: usize> ConditionallySelectable for UInt<LIMBS> {
}
}

impl<const LIMBS: usize> Default for UInt<LIMBS> {
impl<const LIMBS: usize> Default for Uint<LIMBS> {
fn default() -> Self {
Self::ZERO
}
}

impl<const LIMBS: usize> Integer for UInt<LIMBS> {
impl<const LIMBS: usize> Integer for Uint<LIMBS> {
const ONE: Self = Self::ONE;
const MAX: Self = Self::MAX;

Expand All @@ -220,17 +220,17 @@ impl<const LIMBS: usize> Integer for UInt<LIMBS> {
}
}

impl<const LIMBS: usize> Zero for UInt<LIMBS> {
impl<const LIMBS: usize> Zero for Uint<LIMBS> {
const ZERO: Self = Self::ZERO;
}

impl<const LIMBS: usize> fmt::Display for UInt<LIMBS> {
impl<const LIMBS: usize> fmt::Display for Uint<LIMBS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::UpperHex::fmt(self, f)
}
}

impl<const LIMBS: usize> fmt::LowerHex for UInt<LIMBS> {
impl<const LIMBS: usize> fmt::LowerHex for Uint<LIMBS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for limb in self.limbs.iter().rev() {
fmt::LowerHex::fmt(limb, f)?;
Expand All @@ -239,7 +239,7 @@ impl<const LIMBS: usize> fmt::LowerHex for UInt<LIMBS> {
}
}

impl<const LIMBS: usize> fmt::UpperHex for UInt<LIMBS> {
impl<const LIMBS: usize> fmt::UpperHex for Uint<LIMBS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for limb in self.limbs.iter().rev() {
fmt::UpperHex::fmt(limb, f)?;
Expand All @@ -250,9 +250,9 @@ impl<const LIMBS: usize> fmt::UpperHex for UInt<LIMBS> {

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de, const LIMBS: usize> Deserialize<'de> for UInt<LIMBS>
impl<'de, const LIMBS: usize> Deserialize<'de> for Uint<LIMBS>
where
UInt<LIMBS>: Encoding,
Uint<LIMBS>: Encoding,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -267,9 +267,9 @@ where

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de, const LIMBS: usize> Serialize for UInt<LIMBS>
impl<'de, const LIMBS: usize> Serialize for Uint<LIMBS>
where
UInt<LIMBS>: Encoding,
Uint<LIMBS>: Encoding,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -281,15 +281,15 @@ where

#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<const LIMBS: usize> DefaultIsZeroes for UInt<LIMBS> {}
impl<const LIMBS: usize> DefaultIsZeroes for Uint<LIMBS> {}

// TODO(tarcieri): use `const_evaluatable_checked` when stable to make generic around bits.
macro_rules! impl_uint_aliases {
($(($name:ident, $bits:expr, $doc:expr)),+) => {
$(
#[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;
Expand Down
Loading