Skip to content

Commit eb85103

Browse files
authored
Use {Limb, Uint}::to_nz to convert to NonZero (#484)
Replaces the previous type-specific `NonZero::const_new` methods with more idiomatic `Limb::to_nz`/`Uint::to_nz` methods which can be called on a value and return `ConstCtChoice<NonZero<_>>`.
1 parent 4041078 commit eb85103

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

src/limb.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod sub;
1919
#[cfg(feature = "rand_core")]
2020
mod rand;
2121

22-
use crate::{Bounded, Constants, ZeroConstant};
22+
use crate::{Bounded, ConstCtOption, Constants, NonZero, ZeroConstant};
2323
use core::fmt;
2424
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
2525

@@ -91,6 +91,13 @@ impl Limb {
9191
/// Size of the inner integer in bytes.
9292
#[cfg(target_pointer_width = "64")]
9393
pub const BYTES: usize = 8;
94+
95+
/// Convert to a [`NonZero<Limb>`].
96+
///
97+
/// Returns some if the original value is non-zero, and false otherwise.
98+
pub const fn to_nz(self) -> ConstCtOption<NonZero<Self>> {
99+
ConstCtOption::new(NonZero(self), self.is_nonzero())
100+
}
94101
}
95102

96103
impl Bounded for Limb {

src/modular/residue/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ macro_rules! impl_modulus {
2828
panic!("modulus must be odd");
2929
}
3030

31-
// Can unwrap `NonZero::const_new()` here since `res` was asserted to be odd.
32-
$crate::NonZero::<$uint_type>::const_new(res).expect("modulus ensured non-zero")
31+
// Can unwrap here since `res` was asserted to be odd.
32+
res.to_nz().expect("modulus ensured non-zero")
3333
};
3434

3535
const R: $uint_type = $crate::Uint::MAX

src/non_zero.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Wrapper type for non-zero integers.
22
3-
use crate::{Bounded, ConstCtOption, Constants, Encoding, Limb, Uint, Zero};
3+
use crate::{Bounded, Constants, Encoding, Limb, Uint, Zero};
44
use core::{
55
fmt,
66
num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8},
@@ -25,22 +25,6 @@ use serdect::serde::{
2525
#[repr(transparent)]
2626
pub struct NonZero<T>(pub(crate) T);
2727

28-
impl NonZero<Limb> {
29-
/// Creates a new non-zero limb in a const context.
30-
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
31-
pub const fn const_new(n: Limb) -> ConstCtOption<Self> {
32-
ConstCtOption::new(Self(n), n.is_nonzero())
33-
}
34-
}
35-
36-
impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
37-
/// Creates a new non-zero integer in a const context.
38-
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
39-
pub const fn const_new(n: Uint<LIMBS>) -> ConstCtOption<Self> {
40-
ConstCtOption::new(Self(n), n.is_nonzero())
41-
}
42-
}
43-
4428
impl<T> NonZero<T> {
4529
/// Create a new non-zero integer.
4630
pub fn new(n: T) -> CtOption<Self>

src/uint.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub(crate) mod boxed;
4040
mod rand;
4141

4242
use crate::{
43-
modular::BernsteinYangInverter, Bounded, Constants, Encoding, FixedInteger, Integer, Limb,
44-
PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
43+
modular::BernsteinYangInverter, Bounded, ConstCtOption, Constants, Encoding, FixedInteger,
44+
Integer, Limb, NonZero, PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
4545
};
4646
use core::fmt;
4747
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
@@ -169,6 +169,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {
169169
pub const fn to_limbs(self) -> [Limb; LIMBS] {
170170
self.limbs
171171
}
172+
173+
/// Convert to a [`NonZero<Limb>`].
174+
///
175+
/// Returns some if the original value is non-zero, and false otherwise.
176+
pub const fn to_nz(self) -> ConstCtOption<NonZero<Self>> {
177+
ConstCtOption::new(NonZero(self), self.is_nonzero())
178+
}
172179
}
173180

174181
impl<const LIMBS: usize> AsRef<[Word; LIMBS]> for Uint<LIMBS> {

src/uint/div.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
204204
///
205205
/// Panics if `rhs == 0`.
206206
pub const fn wrapping_rem(&self, rhs: &Self) -> Self {
207-
let nz_rhs = NonZero::<Self>::const_new(*rhs).expect("non-zero divisor");
207+
let nz_rhs = rhs.to_nz().expect("non-zero divisor");
208208
self.rem_vartime(&nz_rhs)
209209
}
210210

src/uint/sqrt.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! [`Uint`] square root operations.
22
3+
use crate::Uint;
34
use subtle::{ConstantTimeEq, CtOption};
45

5-
use crate::{NonZero, Uint};
6-
76
impl<const LIMBS: usize> Uint<LIMBS> {
87
/// Computes √(`self`) in constant time.
98
///
@@ -30,8 +29,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3029
x_prev = x;
3130

3231
// Calculate `x_{i+1} = floor((x_i + self / x_i) / 2)`
33-
34-
let maybe_nz_x = NonZero::<Self>::const_new(x);
32+
let maybe_nz_x = x.to_nz();
3533
let (nz_x, is_some) = maybe_nz_x.components_ref();
3634
let (q, _) = self.div_rem(nz_x);
3735

@@ -63,8 +61,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
6361
// Stop right away if `x` is zero to avoid divizion by zero.
6462
while !x.cmp_vartime(&Self::ZERO).is_eq() {
6563
// Calculate `x_{i+1} = floor((x_i + self / x_i) / 2)`
66-
let q = self
67-
.wrapping_div_vartime(&NonZero::<Self>::const_new(x).expect("ensured non-zero"));
64+
let q = self.wrapping_div_vartime(&x.to_nz().expect("ensured non-zero"));
6865
let t = x.wrapping_add(&q);
6966
let next_x = t.shr1();
7067

0 commit comments

Comments
 (0)