Skip to content
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
15 changes: 10 additions & 5 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod sub;
#[cfg(feature = "rand_core")]
mod rand;

use crate::{Bounded, ZeroConstant};
use crate::{Bounded, Constants, ZeroConstant};
use core::fmt;
use subtle::{Choice, ConditionallySelectable};

Expand Down Expand Up @@ -98,17 +98,22 @@ impl Bounded for Limb {
const BYTES: usize = Self::BYTES;
}

impl Constants for Limb {
const ONE: Self = Self::ONE;
const MAX: Self = Self::MAX;
}

impl ZeroConstant for Limb {
const ZERO: Self = Self::ZERO;
}

impl ConditionallySelectable for Limb {
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
Self(Word::conditional_select(&a.0, &b.0, choice))
}
}

impl ZeroConstant for Limb {
const ZERO: Self = Self::ZERO;
}

impl fmt::Debug for Limb {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Limb(0x{self:X})")
Expand Down
15 changes: 13 additions & 2 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::{CtChoice, Encoding, Integer, Limb, Uint, Zero};
use crate::{Bounded, Constants, CtChoice, Encoding, Limb, Uint, Zero};
use core::{
fmt,
num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8},
Expand Down Expand Up @@ -58,7 +58,18 @@ where

impl<T> NonZero<T>
where
T: Integer,
T: Bounded + Zero,
{
/// Total size of the represented integer in bits.
pub const BITS: usize = T::BITS;

/// Total size of the represented integer in bytes.
pub const BYTES: usize = T::BYTES;
}

impl<T> NonZero<T>
where
T: Constants + Zero,
{
/// The value `1`.
pub const ONE: Self = Self(T::ONE);
Expand Down
52 changes: 32 additions & 20 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ use subtle::{
#[cfg(feature = "rand_core")]
use rand_core::CryptoRngCore;

/// Integer type.
/// Integers whose representation takes a bounded amount of space.
pub trait Bounded {
/// Size of this integer in bits.
const BITS: usize;

/// Size of this integer in bytes.
const BYTES: usize;
}

/// Integer trait: represents common functionality of integer types provided by this crate.
pub trait Integer:
'static
+ AddMod
Expand Down Expand Up @@ -46,19 +55,16 @@ pub trait Integer:
+ Zero
{
/// The value `1`.
const ONE: Self;
fn one() -> Self;

/// Maximum value this integer can express.
const MAX: Self;
/// Precision of this integer in bits.
fn bits_precision(&self) -> usize;

/// Total size of the represented integer in bits.
const BITS: usize;
/// Precision of this integer in bytes.
fn bytes_precision(&self) -> usize;

/// Total size of the represented integer in bytes.
const BYTES: usize;

/// The number of limbs used on this platform.
const LIMBS: usize;
/// Number of limbs in this integer.
fn nlimbs(&self) -> usize;

/// Is this integer value an odd number?
///
Expand Down Expand Up @@ -106,6 +112,21 @@ impl<T: ZeroConstant> Zero for T {
}
}

/// Trait for associating constant values with a type.
pub trait Constants: ZeroConstant {
/// The value `1`.
const ONE: Self;

/// Maximum value this integer can express.
const MAX: Self;
}

/// Constant representing the number of limbs used to represent a type.
pub trait LimbsConstant {
/// The number of limbs used on this platform.
const LIMBS: usize;
}

/// Random number generation support.
#[cfg(feature = "rand_core")]
pub trait Random: Sized {
Expand Down Expand Up @@ -246,15 +267,6 @@ pub trait SplitMixed<Hi, Lo> {
fn split_mixed(&self) -> (Hi, Lo);
}

/// Integers whose representation takes a bounded amount of space.
pub trait Bounded {
/// Size of this integer in bits.
const BITS: usize;

/// Size of this integer in bytes.
const BYTES: usize;
}

/// Encoding support.
pub trait Encoding: Sized {
/// Byte array representation.
Expand Down
39 changes: 29 additions & 10 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) mod boxed;
#[cfg(feature = "rand_core")]
mod rand;

use crate::{Bounded, Encoding, Integer, Limb, Word, ZeroConstant};
use crate::{Bounded, Constants, Encoding, Integer, Limb, LimbsConstant, Word, ZeroConstant};
use core::fmt;
use subtle::{Choice, ConditionallySelectable};

Expand Down Expand Up @@ -210,11 +210,21 @@ impl<const LIMBS: usize> Default for Uint<LIMBS> {
}

impl<const LIMBS: usize> Integer for Uint<LIMBS> {
const ONE: Self = Self::ONE;
const MAX: Self = Self::MAX;
const BITS: usize = Self::BITS;
const BYTES: usize = Self::BYTES;
const LIMBS: usize = Self::LIMBS;
fn one() -> Self {
Self::ONE
}

fn bits_precision(&self) -> usize {
Self::BITS
}

fn bytes_precision(&self) -> usize {
Self::BYTES
}

fn nlimbs(&self) -> usize {
Self::LIMBS
}

fn is_odd(&self) -> Choice {
self.limbs
Expand All @@ -224,15 +234,24 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
}
}

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

impl<const LIMBS: usize> Bounded for Uint<LIMBS> {
const BITS: usize = Self::BITS;
const BYTES: usize = Self::BYTES;
}

impl<const LIMBS: usize> Constants for Uint<LIMBS> {
const ONE: Self = Self::ONE;
const MAX: Self = Self::MAX;
}

impl<const LIMBS: usize> LimbsConstant for Uint<LIMBS> {
const LIMBS: usize = Self::LIMBS;
}

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

impl<const LIMBS: usize> fmt::Debug for Uint<LIMBS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Uint(0x{self:X})")
Expand Down
1 change: 0 additions & 1 deletion src/uint/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ macro_rules! impl_uint_aliases {
pub type $name = Uint<{nlimbs!($bits)}>;

impl Encoding for $name {

type Repr = [u8; $bits / 8];

#[inline]
Expand Down