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
4 changes: 2 additions & 2 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, Zero};
use crate::{Bounded, ZeroConstant};
use core::fmt;
use subtle::{Choice, ConditionallySelectable};

Expand Down Expand Up @@ -105,7 +105,7 @@ impl ConditionallySelectable for Limb {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/modular/residue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod pow;
mod sub;

use super::{div_by_2::div_by_2, reduction::montgomery_reduction, Retrieve};
use crate::{Limb, Uint, Zero};
use crate::{Limb, Uint, ZeroConstant};
use core::{fmt::Debug, marker::PhantomData};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

Expand Down Expand Up @@ -193,7 +193,7 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Default for Residue<MOD, LIM
}
}

impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Zero for Residue<MOD, LIMBS> {
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> ZeroConstant for Residue<MOD, LIMBS> {
const ZERO: Self = Self::ZERO;
}

Expand Down
18 changes: 16 additions & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,29 @@ pub trait Integer:
/// Zero values.
pub trait Zero: ConstantTimeEq + Sized {
/// The value `0`.
const ZERO: Self;
fn zero() -> Self;

/// Determine if this value is equal to zero.
///
/// # Returns
///
/// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
fn is_zero(&self) -> Choice {
self.ct_eq(&Self::ZERO)
self.ct_eq(&Self::zero())
}
}

/// Trait for associating a constant representing zero.
///
/// Types which impl this trait automatically receive a blanket impl of [`Zero`].
pub trait ZeroConstant: Zero {
/// The value `0`.
const ZERO: Self;
}

impl<T: ZeroConstant> Zero for T {
fn zero() -> T {
Self::ZERO
}
}

Expand Down
4 changes: 2 additions & 2 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, Zero};
use crate::{Bounded, Encoding, Integer, Limb, Word, ZeroConstant};
use core::fmt;
use subtle::{Choice, ConditionallySelectable};

Expand Down Expand Up @@ -224,7 +224,7 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
}
}

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

Expand Down
11 changes: 10 additions & 1 deletion src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl BoxedUint {
}

/// Is this [`BoxedUint`] equal to zero?
// TODO(tarcieri): impl the `Zero` trait
pub fn is_zero(&self) -> Choice {
self.limbs
.iter()
Expand Down Expand Up @@ -346,6 +345,16 @@ impl<const LIMBS: usize> From<Uint<LIMBS>> for BoxedUint {
}
}

impl Zero for BoxedUint {
fn zero() -> Self {
Self::zero()
}

fn is_zero(&self) -> Choice {
self.is_zero()
}
}

impl fmt::LowerHex for BoxedUint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.limbs.is_empty() {
Expand Down
4 changes: 3 additions & 1 deletion src/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};
pub struct Wrapping<T>(pub T);

impl<T: Zero> Zero for Wrapping<T> {
const ZERO: Self = Self(T::ZERO);
fn zero() -> Self {
Wrapping(T::zero())
}
}

impl<T: fmt::Display> fmt::Display for Wrapping<T> {
Expand Down