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
8 changes: 8 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,16 @@ pub trait Zero: ConstantTimeEq + Sized {
/// # Returns
///
/// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
#[inline]
fn is_zero(&self) -> Choice {
self.ct_eq(&Self::zero())
}

/// Set `self` to its additive identity, i.e. `Self::zero`.
#[inline]
fn set_zero(&mut self) {
*self = Zero::zero();
}
}

/// Trait for associating a constant representing zero.
Expand All @@ -149,6 +156,7 @@ pub trait ZeroConstant: Zero {
}

impl<T: ZeroConstant> Zero for T {
#[inline(always)]
fn zero() -> T {
Self::ZERO
}
Expand Down
11 changes: 5 additions & 6 deletions src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,8 @@ impl BoxedUint {
limbs.into()
}

/// Set the value of `self` to zero in-place.
pub(crate) fn set_to_zero(&mut self) {
self.limbs.as_mut().fill(Limb::ZERO)
}

/// Set the value of `self` to zero in-place if `choice` is truthy.
pub(crate) fn conditional_set_to_zero(&mut self, choice: Choice) {
pub(crate) fn conditional_set_zero(&mut self, choice: Choice) {
let nlimbs = self.nlimbs();
let limbs = self.limbs.as_mut();
for i in 0..nlimbs {
Expand Down Expand Up @@ -402,6 +397,10 @@ impl Zero for BoxedUint {
fn is_zero(&self) -> Choice {
self.is_zero()
}

fn set_zero(&mut self) {
self.limbs.as_mut().fill(Limb::ZERO)
}
}

#[cfg(feature = "zeroize")]
Expand Down
6 changes: 3 additions & 3 deletions src/uint/boxed/shl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! [`BoxedUint`] bitwise left shift operations.

use crate::{BoxedUint, Limb};
use crate::{BoxedUint, Limb, Zero};
use core::ops::{Shl, ShlAssign};
use subtle::{Choice, ConstantTimeLess};

Expand All @@ -20,15 +20,15 @@ impl BoxedUint {

for i in 0..shift_bits {
let bit = Choice::from(((shift >> i) & 1) as u8);
temp.set_to_zero();
temp.set_zero();
// Will not overflow by construction
result
.shl_vartime_into(&mut temp, 1 << i)
.expect("shift within range");
result.conditional_assign(&temp, bit);
}

result.conditional_set_to_zero(overflow);
result.conditional_set_zero(overflow);

(result, overflow)
}
Expand Down
6 changes: 3 additions & 3 deletions src/uint/boxed/shr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! [`BoxedUint`] bitwise right shift operations.

use crate::{BoxedUint, Limb};
use crate::{BoxedUint, Limb, Zero};
use core::ops::{Shr, ShrAssign};
use subtle::{Choice, ConstantTimeLess};

Expand All @@ -20,15 +20,15 @@ impl BoxedUint {

for i in 0..shift_bits {
let bit = Choice::from(((shift >> i) & 1) as u8);
temp.set_to_zero();
temp.set_zero();
// Will not overflow by construction
result
.shr_vartime_into(&mut temp, 1 << i)
.expect("shift within range");
result.conditional_assign(&temp, bit);
}

result.conditional_set_to_zero(overflow);
result.conditional_set_zero(overflow);

(result, overflow)
}
Expand Down