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
39 changes: 31 additions & 8 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait Integer:
+ for<'a> CheckedSub<&'a Self, Output = Self>
+ for<'a> CheckedMul<&'a Self, Output = Self>
+ Copy
+ ConditionallySelectable
// + ConditionallySelectable (see dalek-cryptography/subtle#94)
+ ConstantTimeEq
+ ConstantTimeGreater
+ ConstantTimeLess
Expand Down Expand Up @@ -71,7 +71,12 @@ pub trait Integer:
/// # Returns
///
/// If odd, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
fn is_odd(&self) -> Choice;
fn is_odd(&self) -> Choice {
self.as_ref()
.first()
.map(|limb| limb.is_odd())
.unwrap_or_else(|| Choice::from(0))
}

/// Is this integer value an even number?
///
Expand All @@ -83,6 +88,30 @@ pub trait Integer:
}
}

/// Fixed-width integers.
pub trait FixedInteger: Bounded + Constants + ConditionallySelectable + Integer {
/// The number of limbs used on this platform.
const LIMBS: usize;
}

impl<T: FixedInteger> Integer for T {
fn one() -> Self {
T::ONE
}

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

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

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

/// Zero values.
pub trait Zero: ConstantTimeEq + Sized {
/// The value `0`.
Expand Down Expand Up @@ -121,12 +150,6 @@ pub trait Constants: ZeroConstant {
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
31 changes: 3 additions & 28 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, Constants, Encoding, Integer, Limb, LimbsConstant, Word, ZeroConstant};
use crate::{Bounded, Constants, Encoding, FixedInteger, Limb, Word, ZeroConstant};
use core::fmt;
use subtle::{Choice, ConditionallySelectable};

Expand Down Expand Up @@ -209,29 +209,8 @@ impl<const LIMBS: usize> Default for Uint<LIMBS> {
}
}

impl<const LIMBS: usize> Integer for Uint<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
.first()
.map(|limb| limb.is_odd())
.unwrap_or_else(|| Choice::from(0))
}
impl<const LIMBS: usize> FixedInteger for Uint<LIMBS> {
const LIMBS: usize = LIMBS;
}

impl<const LIMBS: usize> Bounded for Uint<LIMBS> {
Expand All @@ -244,10 +223,6 @@ impl<const LIMBS: usize> Constants for Uint<LIMBS> {
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;
}
Expand Down