diff --git a/src/non_zero.rs b/src/non_zero.rs index ccee78bc..997dfc2c 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -1,6 +1,6 @@ //! Wrapper type for non-zero integers. -use crate::{Encoding, Integer, Limb, Uint, Zero}; +use crate::{CtChoice, Encoding, Integer, Limb, Uint, Zero}; use core::{ fmt, num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8}, @@ -24,6 +24,22 @@ use serdect::serde::{ #[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)] pub struct NonZero(T); +impl NonZero { + /// Creates a new non-zero limb in a const context. + /// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise. + pub const fn const_new(n: Limb) -> (Self, CtChoice) { + (Self(n), n.ct_is_nonzero()) + } +} + +impl NonZero> { + /// Creates a new non-zero integer in a const context. + /// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise. + pub const fn const_new(n: Uint) -> (Self, CtChoice) { + (Self(n), n.ct_is_nonzero()) + } +} + impl NonZero where T: Zero,