Skip to content

Commit 3094c66

Browse files
authored
Add Zero::set_zero method (#426)
Adds a method to the `Zero` trait for setting its value to zero in-place, subsuming the previous `BoxedUint::set_to_zero` function. It's inspired by a similar method on `num_traits::Zero` (which we don't use so `is_zero` can return `Choice` and use `ConstantTimeEq` to compare values in constant-time). https://docs.rs/num-traits/latest/num_traits/identities/trait.Zero.html#method.set_zero
1 parent 4bf6932 commit 3094c66

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/traits.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,16 @@ pub trait Zero: ConstantTimeEq + Sized {
135135
/// # Returns
136136
///
137137
/// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
138+
#[inline]
138139
fn is_zero(&self) -> Choice {
139140
self.ct_eq(&Self::zero())
140141
}
142+
143+
/// Set `self` to its additive identity, i.e. `Self::zero`.
144+
#[inline]
145+
fn set_zero(&mut self) {
146+
*self = Zero::zero();
147+
}
141148
}
142149

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

151158
impl<T: ZeroConstant> Zero for T {
159+
#[inline(always)]
152160
fn zero() -> T {
153161
Self::ZERO
154162
}

src/uint/boxed.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,8 @@ impl BoxedUint {
231231
limbs.into()
232232
}
233233

234-
/// Set the value of `self` to zero in-place.
235-
pub(crate) fn set_to_zero(&mut self) {
236-
self.limbs.as_mut().fill(Limb::ZERO)
237-
}
238-
239234
/// Set the value of `self` to zero in-place if `choice` is truthy.
240-
pub(crate) fn conditional_set_to_zero(&mut self, choice: Choice) {
235+
pub(crate) fn conditional_set_zero(&mut self, choice: Choice) {
241236
let nlimbs = self.nlimbs();
242237
let limbs = self.limbs.as_mut();
243238
for i in 0..nlimbs {
@@ -402,6 +397,10 @@ impl Zero for BoxedUint {
402397
fn is_zero(&self) -> Choice {
403398
self.is_zero()
404399
}
400+
401+
fn set_zero(&mut self) {
402+
self.limbs.as_mut().fill(Limb::ZERO)
403+
}
405404
}
406405

407406
#[cfg(feature = "zeroize")]

src/uint/boxed/shl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! [`BoxedUint`] bitwise left shift operations.
22
3-
use crate::{BoxedUint, Limb};
3+
use crate::{BoxedUint, Limb, Zero};
44
use core::ops::{Shl, ShlAssign};
55
use subtle::{Choice, ConstantTimeLess};
66

@@ -20,15 +20,15 @@ impl BoxedUint {
2020

2121
for i in 0..shift_bits {
2222
let bit = Choice::from(((shift >> i) & 1) as u8);
23-
temp.set_to_zero();
23+
temp.set_zero();
2424
// Will not overflow by construction
2525
result
2626
.shl_vartime_into(&mut temp, 1 << i)
2727
.expect("shift within range");
2828
result.conditional_assign(&temp, bit);
2929
}
3030

31-
result.conditional_set_to_zero(overflow);
31+
result.conditional_set_zero(overflow);
3232

3333
(result, overflow)
3434
}

src/uint/boxed/shr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! [`BoxedUint`] bitwise right shift operations.
22
3-
use crate::{BoxedUint, Limb};
3+
use crate::{BoxedUint, Limb, Zero};
44
use core::ops::{Shr, ShrAssign};
55
use subtle::{Choice, ConstantTimeLess};
66

@@ -20,15 +20,15 @@ impl BoxedUint {
2020

2121
for i in 0..shift_bits {
2222
let bit = Choice::from(((shift >> i) & 1) as u8);
23-
temp.set_to_zero();
23+
temp.set_zero();
2424
// Will not overflow by construction
2525
result
2626
.shr_vartime_into(&mut temp, 1 << i)
2727
.expect("shift within range");
2828
result.conditional_assign(&temp, bit);
2929
}
3030

31-
result.conditional_set_to_zero(overflow);
31+
result.conditional_set_zero(overflow);
3232

3333
(result, overflow)
3434
}

0 commit comments

Comments
 (0)