Skip to content
Open
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
37 changes: 35 additions & 2 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use core::fmt;
use num_traits::{ConstOne, ConstZero};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

#[cfg(feature = "serde")]
use crate::Encoding;
#[cfg(feature = "serde")]
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};

#[macro_use]
mod macros;

mod add;
mod bit_and;
mod bit_not;
Expand All @@ -34,7 +36,6 @@ mod shl;
mod shr;
mod sign;
mod sub;
pub(crate) mod types;

#[cfg(feature = "rand_core")]
mod rand;
Expand Down Expand Up @@ -346,6 +347,38 @@ impl<const LIMBS: usize> fmt::UpperHex for Int<LIMBS> {
}
}

impl_int_aliases! {
(I64, 64, "64-bit"),
(I128, 128, "128-bit"),
(I192, 192, "192-bit"),
(I256, 256, "256-bit"),
(I320, 320, "320-bit"),
(I384, 384, "384-bit"),
(I448, 448, "448-bit"),
(I512, 512, "512-bit"),
(I576, 576, "576-bit"),
(I640, 640, "640-bit"),
(I704, 704, "704-bit"),
(I768, 768, "768-bit"),
(I832, 832, "832-bit"),
(I896, 896, "896-bit"),
(I960, 960, "960-bit"),
(I1024, 1024, "1024-bit"),
(I1280, 1280, "1280-bit"),
(I1536, 1536, "1536-bit"),
(I1792, 1792, "1792-bit"),
(I2048, 2048, "2048-bit"),
(I3072, 3072, "3072-bit"),
(I3584, 3584, "3584-bit"),
(I4096, 4096, "4096-bit"),
(I4224, 4224, "4224-bit"),
(I4352, 4352, "4352-bit"),
(I6144, 6144, "6144-bit"),
(I8192, 8192, "8192-bit"),
(I16384, 16384, "16384-bit"),
(I32768, 32768, "32768-bit")
}

#[cfg(feature = "serde")]
impl<'de, const LIMBS: usize> Deserialize<'de> for Int<LIMBS>
where
Expand Down
37 changes: 37 additions & 0 deletions src/int/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
use crate::{Int, Uint};

impl<const LIMBS: usize> Int<LIMBS> {
/// Create a new [`Int`] from the provided big endian bytes.
///
/// See [`Uint::from_be_slice`] for more details.
pub const fn from_be_slice(bytes: &[u8]) -> Self {
Self(Uint::from_be_slice(bytes))
}

/// Create a new [`Int`] from the provided big endian hex string.
///
/// Panics if the hex is malformed or not zero-padded accordingly for the size.
Expand All @@ -11,4 +18,34 @@ impl<const LIMBS: usize> Int<LIMBS> {
pub const fn from_be_hex(hex: &str) -> Self {
Self(Uint::from_be_hex(hex))
}

/// Create a new [`Int`] from the provided little endian bytes.
///
/// See [`Uint::from_le_slice`] for more details.
pub const fn from_le_slice(bytes: &[u8]) -> Self {
Self(Uint::from_le_slice(bytes))
}

/// Create a new [`Int`] from the provided little endian hex string.
///
/// Panics if the hex is malformed or not zero-padded accordingly for the size.
///
/// See [`Uint::from_le_hex`] for more details.
pub const fn from_le_hex(hex: &str) -> Self {
Self(Uint::from_le_hex(hex))
}
}

/// Encode an [`Int`] to a big endian byte array of the given size.
pub(crate) const fn int_to_be_bytes<const LIMBS: usize, const BYTES: usize>(
int: &Int<LIMBS>,
) -> [u8; BYTES] {
crate::uint::encoding::uint_to_be_bytes(&int.0)
}

/// Encode an [`Int`] to a little endian byte array of the given size.
pub(crate) const fn int_to_le_bytes<const LIMBS: usize, const BYTES: usize>(
int: &Int<LIMBS>,
) -> [u8; BYTES] {
crate::uint::encoding::uint_to_le_bytes(&int.0)
}
48 changes: 48 additions & 0 deletions src/int/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Macros used to define traits on aliases of `Int`.

// TODO(tarcieri): use `generic_const_exprs` when stable to make generic around bits.
macro_rules! impl_int_aliases {
($(($name:ident, $bits:expr, $doc:expr)),+) => {
$(
#[doc = $doc]
#[doc="unsigned big integer."]
pub type $name = Int<{ nlimbs!($bits) }>;

impl $name {
/// Serialize as big endian bytes.
pub const fn to_be_bytes(&self) -> [u8; $bits / 8] {
encoding::int_to_be_bytes::<{ nlimbs!($bits) }, { $bits / 8 }>(self)
}

/// Serialize as little endian bytes.
pub const fn to_le_bytes(&self) -> [u8; $bits / 8] {
encoding::int_to_le_bytes::<{ nlimbs!($bits) }, { $bits / 8 }>(self)
}
}

impl Encoding for $name {
type Repr = [u8; $bits / 8];

#[inline]
fn from_be_bytes(bytes: Self::Repr) -> Self {
Self::from_be_slice(&bytes)
}

#[inline]
fn from_le_bytes(bytes: Self::Repr) -> Self {
Self::from_le_slice(&bytes)
}

#[inline]
fn to_be_bytes(&self) -> Self::Repr {
encoding::int_to_be_bytes(self)
}

#[inline]
fn to_le_bytes(&self) -> Self::Repr {
encoding::int_to_le_bytes(self)
}
}
)+
};
}
60 changes: 0 additions & 60 deletions src/int/types.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub use crate::uint::boxed::BoxedUint;
pub use crate::{
checked::Checked,
const_choice::{ConstChoice, ConstCtOption},
int::{types::*, *},
int::{*},
jacobi::JacobiSymbol,
limb::{Limb, WideWord, Word},
non_zero::*,
Expand Down
Loading