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
35 changes: 15 additions & 20 deletions src/const_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,16 @@ impl ConstChoice {
x & self.as_u32_mask()
}

/// WARNING: this method should only be used in contexts that aren't constant-time critical!
#[inline]
pub(crate) const fn is_true_vartime(&self) -> bool {
self.0 == ConstChoice::TRUE.0
pub(crate) const fn to_bool_vartime(self) -> bool {
self.0 != 0
}

#[inline]
pub(crate) const fn to_u8(self) -> u8 {
(self.0 as u8) & 1
}

/// WARNING: this method should only be used in contexts that aren't constant-time critical!
#[inline]
pub(crate) const fn to_bool_vartime(self) -> bool {
self.to_u8() != 0
}
}

/// `const` equivalent of `u32::max(a, b)`.
Expand Down Expand Up @@ -284,7 +279,7 @@ impl From<Choice> for ConstChoice {

impl From<ConstChoice> for bool {
fn from(choice: ConstChoice) -> Self {
choice.is_true_vartime()
choice.to_bool_vartime()
}
}

Expand Down Expand Up @@ -351,7 +346,7 @@ impl<T> ConstCtOption<T> {
#[track_caller]
pub fn unwrap(self) -> T {
assert!(
self.is_some.is_true_vartime(),
self.is_some.to_bool_vartime(),
"called `ConstCtOption::unwrap()` on a `None` value"
);
self.value
Expand Down Expand Up @@ -403,7 +398,7 @@ impl<const LIMBS: usize> ConstCtOption<Uint<LIMBS>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> Uint<LIMBS> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}

Expand All @@ -424,7 +419,7 @@ impl<const LIMBS: usize> ConstCtOption<(Uint<LIMBS>, Uint<LIMBS>)> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> (Uint<LIMBS>, Uint<LIMBS>) {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -439,7 +434,7 @@ impl<const LIMBS: usize> ConstCtOption<NonZero<Uint<LIMBS>>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> NonZero<Uint<LIMBS>> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -454,7 +449,7 @@ impl<const LIMBS: usize> ConstCtOption<Odd<Uint<LIMBS>>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> Odd<Uint<LIMBS>> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -475,7 +470,7 @@ impl<const LIMBS: usize> ConstCtOption<Int<LIMBS>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> Int<LIMBS> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -490,7 +485,7 @@ impl<const LIMBS: usize> ConstCtOption<NonZeroInt<LIMBS>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> NonZeroInt<LIMBS> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -505,7 +500,7 @@ impl<const LIMBS: usize> ConstCtOption<OddInt<LIMBS>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> OddInt<LIMBS> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -520,7 +515,7 @@ impl ConstCtOption<NonZero<Limb>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> NonZero<Limb> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -535,7 +530,7 @@ impl<const LIMBS: usize> ConstCtOption<SafeGcdInverter<LIMBS>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> SafeGcdInverter<LIMBS> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand All @@ -555,7 +550,7 @@ impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> ConstCtOption<ConstMontyF
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> ConstMontyForm<MOD, LIMBS> {
assert!(self.is_some.is_true_vartime(), "{}", msg);
assert!(self.is_some.to_bool_vartime(), "{}", msg);
self.value
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/int/mod_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
let (abs, sign) = self.abs_sign();
let jacobi = abs.jacobi_symbol_vartime(rhs);
JacobiSymbol::from_i8(
if sign.is_true_vartime() && rhs.as_ref().limbs[0].0 & 3 == 3 {
if sign.to_bool_vartime() && rhs.as_ref().limbs[0].0 & 3 == 3 {
-(jacobi as i8)
} else {
jacobi as i8
Expand Down
4 changes: 2 additions & 2 deletions src/int/shl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ mod tests {

#[test]
fn shl256_const() {
assert!(N.overflowing_shl(256).is_none().is_true_vartime());
assert!(N.overflowing_shl_vartime(256).is_none().is_true_vartime());
assert!(N.overflowing_shl(256).is_none().to_bool_vartime());
assert!(N.overflowing_shl_vartime(256).is_none().to_bool_vartime());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/int/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ mod tests {

#[test]
fn shr256_const() {
assert!(N.overflowing_shr(256).is_none().is_true_vartime());
assert!(N.overflowing_shr_vartime(256).is_none().is_true_vartime());
assert!(N.overflowing_shr(256).is_none().to_bool_vartime());
assert!(N.overflowing_shr_vartime(256).is_none().to_bool_vartime());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/modular/reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub const fn montgomery_retrieve<const LIMBS: usize>(
modulus: &Odd<Uint<LIMBS>>,
mod_neg_inv: Limb,
) -> Uint<LIMBS> {
debug_assert!(Uint::lt(montgomery_form, modulus.as_ref()).is_true_vartime());
debug_assert!(Uint::lt(montgomery_form, modulus.as_ref()).to_bool_vartime());
let mut res = Uint::ZERO;
montgomery_retrieve_inner(
montgomery_form.as_limbs(),
Expand Down
4 changes: 2 additions & 2 deletions src/modular/safegcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ impl<const LIMBS: usize> fmt::Debug for SignedInt<LIMBS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"{}0x{}",
if self.sign.is_true_vartime() {
if self.sign.to_bool_vartime() {
"-"
} else {
"+"
Expand All @@ -534,7 +534,7 @@ impl<const LIMBS: usize> ConstCtOption<Odd<SignedInt<LIMBS>>> {
#[inline]
#[track_caller]
pub const fn expect(self, msg: &str) -> Odd<SignedInt<LIMBS>> {
assert!(self.is_some().is_true_vartime(), "{}", msg);
assert!(self.is_some().to_bool_vartime(), "{}", msg);
*self.components_ref().0
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/modular/safegcd/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl fmt::Debug for SignedBoxedInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"{}0x{}",
if self.sign.is_true_vartime() {
if self.sign.to_bool_vartime() {
"-"
} else {
"+"
Expand All @@ -443,7 +443,7 @@ impl ConstCtOption<Odd<SignedBoxedInt>> {
#[inline]
#[track_caller]
pub fn expect(self, msg: &str) -> Odd<SignedBoxedInt> {
assert!(self.is_some().is_true_vartime(), "{}", msg);
assert!(self.is_some().to_bool_vartime(), "{}", msg);
self.components_ref().0.clone()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/non_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl NonZero<Limb> {
/// `NonZero::new(…).unwrap()`
// TODO: Remove when `Self::new` and `CtOption::unwrap` support `const fn`
pub const fn new_unwrap(n: Limb) -> Self {
if n.is_nonzero().is_true_vartime() {
if n.is_nonzero().to_bool_vartime() {
Self(n)
} else {
panic!("Invalid value: zero")
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<const LIMBS: usize> NonZeroUint<LIMBS> {
/// - if the value is zero.
// TODO: Remove when `Self::new` and `CtOption::unwrap` support `const fn`
pub const fn new_unwrap(n: Uint<LIMBS>) -> Self {
if n.is_nonzero().is_true_vartime() {
if n.is_nonzero().to_bool_vartime() {
Self(n)
} else {
panic!("Invalid value: zero")
Expand Down
4 changes: 2 additions & 2 deletions src/odd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<const LIMBS: usize> Odd<Uint<LIMBS>> {
/// Panics if the hex is malformed or not zero-padded accordingly for the size, or if the value is even.
pub const fn from_be_hex(hex: &str) -> Self {
let uint = Uint::<LIMBS>::from_be_hex(hex);
assert!(uint.is_odd().is_true_vartime(), "number must be odd");
assert!(uint.is_odd().to_bool_vartime(), "number must be odd");
Odd(uint)
}

Expand All @@ -100,7 +100,7 @@ impl<const LIMBS: usize> Odd<Uint<LIMBS>> {
/// Panics if the hex is malformed or not zero-padded accordingly for the size, or if the value is even.
pub const fn from_le_hex(hex: &str) -> Self {
let uint = Uint::<LIMBS>::from_be_hex(hex);
assert!(uint.is_odd().is_true_vartime(), "number must be odd");
assert!(uint.is_odd().to_bool_vartime(), "number must be odd");
Odd(uint)
}

Expand Down
14 changes: 7 additions & 7 deletions src/uint/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ mod tests {
#[test]
fn bit() {
let u = uint_with_bits_at(&[16, 48, 112, 127, 255]);
assert!(!u.bit(0).is_true_vartime());
assert!(!u.bit(1).is_true_vartime());
assert!(u.bit(16).is_true_vartime());
assert!(u.bit(127).is_true_vartime());
assert!(u.bit(255).is_true_vartime());
assert!(!u.bit(256).is_true_vartime());
assert!(!u.bit(260).is_true_vartime());
assert!(!u.bit(0).to_bool_vartime());
assert!(!u.bit(1).to_bool_vartime());
assert!(u.bit(16).to_bool_vartime());
assert!(u.bit(127).to_bool_vartime());
assert!(u.bit(255).to_bool_vartime());
assert!(!u.bit(256).to_bool_vartime());
assert!(!u.bit(260).to_bool_vartime());
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/uint/invert_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ mod tests {
// An inverse of an even number does not exist.

let a = U256::from(10u64).invert_mod2k(4);
assert!(a.is_none().is_true_vartime());
assert!(a.is_none().to_bool_vartime());

let a = U256::from(10u64).invert_mod2k_vartime(4);
assert!(a.is_none().is_true_vartime());
assert!(a.is_none().to_bool_vartime());

// A degenerate case. An inverse mod 2^0 == 1 always exists even for even numbers.

Expand Down Expand Up @@ -346,7 +346,7 @@ mod tests {

// `m` is a multiple of `p1`, so no inverse exists
let res = p1.invert_odd_mod(&m);
assert!(res.is_none().is_true_vartime());
assert!(res.is_none().to_bool_vartime());
}

#[test]
Expand Down Expand Up @@ -391,7 +391,7 @@ mod tests {
let m = U64::from(49u64).to_odd().unwrap();

let res = a.invert_odd_mod(&m);
assert!(res.is_none().is_true_vartime());
assert!(res.is_none().to_bool_vartime());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/uint/ref_type/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl UintRef {

// This loop is a no-op once xi is smaller than the number of words in the divisor
let done = ConstChoice::from_u32_lt(xi as u32, ywords - 1);
if vartime.and(done).is_true_vartime() {
if vartime.and(done).to_bool_vartime() {
break;
}
quo = done.select_word(quo, 0);
Expand Down Expand Up @@ -450,7 +450,7 @@ impl UintRef {

// This loop is a no-op once xi is smaller than the number of words in the divisor
let done = ConstChoice::from_u32_lt(xi as u32, ywords - 1);
if vartime.and(done).is_true_vartime() {
if vartime.and(done).to_bool_vartime() {
break;
}
quo = done.select_word(quo, 0);
Expand Down
6 changes: 3 additions & 3 deletions src/uint/shl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ mod tests {

#[test]
fn shl256_const() {
assert!(N.overflowing_shl(256).is_none().is_true_vartime());
assert!(N.overflowing_shl_vartime(256).is_none().is_true_vartime());
assert!(N.overflowing_shl(256).is_none().to_bool_vartime());
assert!(N.overflowing_shl_vartime(256).is_none().to_bool_vartime());
}

#[test]
Expand Down Expand Up @@ -361,7 +361,7 @@ mod tests {
assert!(
Uint::overflowing_shl_vartime_wide((U128::MAX, U128::MAX), 256)
.is_none()
.is_true_vartime(),
.to_bool_vartime(),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/uint/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ mod tests {

#[test]
fn shr256_const() {
assert!(N.overflowing_shr(256).is_none().is_true_vartime());
assert!(N.overflowing_shr_vartime(256).is_none().is_true_vartime());
assert!(N.overflowing_shr(256).is_none().to_bool_vartime());
assert!(N.overflowing_shr_vartime(256).is_none().to_bool_vartime());
}

#[test]
Expand Down Expand Up @@ -337,7 +337,7 @@ mod tests {
assert!(
Uint::overflowing_shr_vartime_wide((U128::MAX, U128::MAX), 256)
.is_none()
.is_true_vartime()
.to_bool_vartime()
);
}

Expand Down