Skip to content

Commit 152179e

Browse files
committed
Remove ConstChoice::is_true_vartime
The `to_bool_vartime` and `is_true_vartime` methods duplicate each other, performing the same function. I picked `to_bool_vartime` to consolidate on.
1 parent 5216e23 commit 152179e

File tree

14 files changed

+48
-53
lines changed

14 files changed

+48
-53
lines changed

src/const_choice.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,16 @@ impl ConstChoice {
241241
x & self.as_u32_mask()
242242
}
243243

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

249250
#[inline]
250251
pub(crate) const fn to_u8(self) -> u8 {
251252
(self.0 as u8) & 1
252253
}
253-
254-
/// WARNING: this method should only be used in contexts that aren't constant-time critical!
255-
#[inline]
256-
pub(crate) const fn to_bool_vartime(self) -> bool {
257-
self.to_u8() != 0
258-
}
259254
}
260255

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

285280
impl From<ConstChoice> for bool {
286281
fn from(choice: ConstChoice) -> Self {
287-
choice.is_true_vartime()
282+
choice.to_bool_vartime()
288283
}
289284
}
290285

@@ -351,7 +346,7 @@ impl<T> ConstCtOption<T> {
351346
#[track_caller]
352347
pub fn unwrap(self) -> T {
353348
assert!(
354-
self.is_some.is_true_vartime(),
349+
self.is_some.to_bool_vartime(),
355350
"called `ConstCtOption::unwrap()` on a `None` value"
356351
);
357352
self.value
@@ -403,7 +398,7 @@ impl<const LIMBS: usize> ConstCtOption<Uint<LIMBS>> {
403398
#[inline]
404399
#[track_caller]
405400
pub const fn expect(self, msg: &str) -> Uint<LIMBS> {
406-
assert!(self.is_some.is_true_vartime(), "{}", msg);
401+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
407402
self.value
408403
}
409404

@@ -424,7 +419,7 @@ impl<const LIMBS: usize> ConstCtOption<(Uint<LIMBS>, Uint<LIMBS>)> {
424419
#[inline]
425420
#[track_caller]
426421
pub const fn expect(self, msg: &str) -> (Uint<LIMBS>, Uint<LIMBS>) {
427-
assert!(self.is_some.is_true_vartime(), "{}", msg);
422+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
428423
self.value
429424
}
430425
}
@@ -439,7 +434,7 @@ impl<const LIMBS: usize> ConstCtOption<NonZero<Uint<LIMBS>>> {
439434
#[inline]
440435
#[track_caller]
441436
pub const fn expect(self, msg: &str) -> NonZero<Uint<LIMBS>> {
442-
assert!(self.is_some.is_true_vartime(), "{}", msg);
437+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
443438
self.value
444439
}
445440
}
@@ -454,7 +449,7 @@ impl<const LIMBS: usize> ConstCtOption<Odd<Uint<LIMBS>>> {
454449
#[inline]
455450
#[track_caller]
456451
pub const fn expect(self, msg: &str) -> Odd<Uint<LIMBS>> {
457-
assert!(self.is_some.is_true_vartime(), "{}", msg);
452+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
458453
self.value
459454
}
460455
}
@@ -475,7 +470,7 @@ impl<const LIMBS: usize> ConstCtOption<Int<LIMBS>> {
475470
#[inline]
476471
#[track_caller]
477472
pub const fn expect(self, msg: &str) -> Int<LIMBS> {
478-
assert!(self.is_some.is_true_vartime(), "{}", msg);
473+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
479474
self.value
480475
}
481476
}
@@ -490,7 +485,7 @@ impl<const LIMBS: usize> ConstCtOption<NonZeroInt<LIMBS>> {
490485
#[inline]
491486
#[track_caller]
492487
pub const fn expect(self, msg: &str) -> NonZeroInt<LIMBS> {
493-
assert!(self.is_some.is_true_vartime(), "{}", msg);
488+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
494489
self.value
495490
}
496491
}
@@ -505,7 +500,7 @@ impl<const LIMBS: usize> ConstCtOption<OddInt<LIMBS>> {
505500
#[inline]
506501
#[track_caller]
507502
pub const fn expect(self, msg: &str) -> OddInt<LIMBS> {
508-
assert!(self.is_some.is_true_vartime(), "{}", msg);
503+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
509504
self.value
510505
}
511506
}
@@ -520,7 +515,7 @@ impl ConstCtOption<NonZero<Limb>> {
520515
#[inline]
521516
#[track_caller]
522517
pub const fn expect(self, msg: &str) -> NonZero<Limb> {
523-
assert!(self.is_some.is_true_vartime(), "{}", msg);
518+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
524519
self.value
525520
}
526521
}
@@ -535,7 +530,7 @@ impl<const LIMBS: usize> ConstCtOption<SafeGcdInverter<LIMBS>> {
535530
#[inline]
536531
#[track_caller]
537532
pub const fn expect(self, msg: &str) -> SafeGcdInverter<LIMBS> {
538-
assert!(self.is_some.is_true_vartime(), "{}", msg);
533+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
539534
self.value
540535
}
541536
}
@@ -555,7 +550,7 @@ impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> ConstCtOption<ConstMontyF
555550
#[inline]
556551
#[track_caller]
557552
pub const fn expect(self, msg: &str) -> ConstMontyForm<MOD, LIMBS> {
558-
assert!(self.is_some.is_true_vartime(), "{}", msg);
553+
assert!(self.is_some.to_bool_vartime(), "{}", msg);
559554
self.value
560555
}
561556
}

src/int/mod_symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
2525
let (abs, sign) = self.abs_sign();
2626
let jacobi = abs.jacobi_symbol_vartime(rhs);
2727
JacobiSymbol::from_i8(
28-
if sign.is_true_vartime() && rhs.as_ref().limbs[0].0 & 3 == 3 {
28+
if sign.to_bool_vartime() && rhs.as_ref().limbs[0].0 & 3 == 3 {
2929
-(jacobi as i8)
3030
} else {
3131
jacobi as i8

src/int/shl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ mod tests {
153153

154154
#[test]
155155
fn shl256_const() {
156-
assert!(N.overflowing_shl(256).is_none().is_true_vartime());
157-
assert!(N.overflowing_shl_vartime(256).is_none().is_true_vartime());
156+
assert!(N.overflowing_shl(256).is_none().to_bool_vartime());
157+
assert!(N.overflowing_shl_vartime(256).is_none().to_bool_vartime());
158158
}
159159

160160
#[test]

src/int/shr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ mod tests {
227227

228228
#[test]
229229
fn shr256_const() {
230-
assert!(N.overflowing_shr(256).is_none().is_true_vartime());
231-
assert!(N.overflowing_shr_vartime(256).is_none().is_true_vartime());
230+
assert!(N.overflowing_shr(256).is_none().to_bool_vartime());
231+
assert!(N.overflowing_shr_vartime(256).is_none().to_bool_vartime());
232232
}
233233

234234
#[test]

src/modular/reduction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub const fn montgomery_retrieve<const LIMBS: usize>(
104104
modulus: &Odd<Uint<LIMBS>>,
105105
mod_neg_inv: Limb,
106106
) -> Uint<LIMBS> {
107-
debug_assert!(Uint::lt(montgomery_form, modulus.as_ref()).is_true_vartime());
107+
debug_assert!(Uint::lt(montgomery_form, modulus.as_ref()).to_bool_vartime());
108108
let mut res = Uint::ZERO;
109109
montgomery_retrieve_inner(
110110
montgomery_form.as_limbs(),

src/modular/safegcd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl<const LIMBS: usize> fmt::Debug for SignedInt<LIMBS> {
508508
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
509509
f.write_fmt(format_args!(
510510
"{}0x{}",
511-
if self.sign.is_true_vartime() {
511+
if self.sign.to_bool_vartime() {
512512
"-"
513513
} else {
514514
"+"
@@ -534,7 +534,7 @@ impl<const LIMBS: usize> ConstCtOption<Odd<SignedInt<LIMBS>>> {
534534
#[inline]
535535
#[track_caller]
536536
pub const fn expect(self, msg: &str) -> Odd<SignedInt<LIMBS>> {
537-
assert!(self.is_some().is_true_vartime(), "{}", msg);
537+
assert!(self.is_some().to_bool_vartime(), "{}", msg);
538538
*self.components_ref().0
539539
}
540540
}

src/modular/safegcd/boxed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl fmt::Debug for SignedBoxedInt {
417417
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
418418
f.write_fmt(format_args!(
419419
"{}0x{}",
420-
if self.sign.is_true_vartime() {
420+
if self.sign.to_bool_vartime() {
421421
"-"
422422
} else {
423423
"+"
@@ -443,7 +443,7 @@ impl ConstCtOption<Odd<SignedBoxedInt>> {
443443
#[inline]
444444
#[track_caller]
445445
pub fn expect(self, msg: &str) -> Odd<SignedBoxedInt> {
446-
assert!(self.is_some().is_true_vartime(), "{}", msg);
446+
assert!(self.is_some().to_bool_vartime(), "{}", msg);
447447
self.components_ref().0.clone()
448448
}
449449
}

src/non_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl NonZero<Limb> {
152152
/// `NonZero::new(…).unwrap()`
153153
// TODO: Remove when `Self::new` and `CtOption::unwrap` support `const fn`
154154
pub const fn new_unwrap(n: Limb) -> Self {
155-
if n.is_nonzero().is_true_vartime() {
155+
if n.is_nonzero().to_bool_vartime() {
156156
Self(n)
157157
} else {
158158
panic!("Invalid value: zero")
@@ -195,7 +195,7 @@ impl<const LIMBS: usize> NonZeroUint<LIMBS> {
195195
/// - if the value is zero.
196196
// TODO: Remove when `Self::new` and `CtOption::unwrap` support `const fn`
197197
pub const fn new_unwrap(n: Uint<LIMBS>) -> Self {
198-
if n.is_nonzero().is_true_vartime() {
198+
if n.is_nonzero().to_bool_vartime() {
199199
Self(n)
200200
} else {
201201
panic!("Invalid value: zero")

src/odd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<const LIMBS: usize> Odd<Uint<LIMBS>> {
9191
/// Panics if the hex is malformed or not zero-padded accordingly for the size, or if the value is even.
9292
pub const fn from_be_hex(hex: &str) -> Self {
9393
let uint = Uint::<LIMBS>::from_be_hex(hex);
94-
assert!(uint.is_odd().is_true_vartime(), "number must be odd");
94+
assert!(uint.is_odd().to_bool_vartime(), "number must be odd");
9595
Odd(uint)
9696
}
9797

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

src/uint/bits.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ mod tests {
162162
#[test]
163163
fn bit() {
164164
let u = uint_with_bits_at(&[16, 48, 112, 127, 255]);
165-
assert!(!u.bit(0).is_true_vartime());
166-
assert!(!u.bit(1).is_true_vartime());
167-
assert!(u.bit(16).is_true_vartime());
168-
assert!(u.bit(127).is_true_vartime());
169-
assert!(u.bit(255).is_true_vartime());
170-
assert!(!u.bit(256).is_true_vartime());
171-
assert!(!u.bit(260).is_true_vartime());
165+
assert!(!u.bit(0).to_bool_vartime());
166+
assert!(!u.bit(1).to_bool_vartime());
167+
assert!(u.bit(16).to_bool_vartime());
168+
assert!(u.bit(127).to_bool_vartime());
169+
assert!(u.bit(255).to_bool_vartime());
170+
assert!(!u.bit(256).to_bool_vartime());
171+
assert!(!u.bit(260).to_bool_vartime());
172172
}
173173

174174
#[test]

0 commit comments

Comments
 (0)