Skip to content

Commit 631ae08

Browse files
committed
Make const fn comparisons in Uint have the same signatures as the ones in Limb
1 parent 13de849 commit 631ae08

File tree

6 files changed

+42
-42
lines changed

6 files changed

+42
-42
lines changed

src/limb.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod sub;
2020
#[cfg(feature = "rand_core")]
2121
mod rand;
2222

23-
use crate::{Bounded, CtChoice, Zero};
23+
use crate::{Bounded, Zero};
2424
use core::fmt;
2525
use subtle::{Choice, ConditionallySelectable};
2626

@@ -90,12 +90,6 @@ impl Limb {
9090
/// Size of the inner integer in bytes.
9191
#[cfg(target_pointer_width = "64")]
9292
pub const BYTES: usize = 8;
93-
94-
/// Return `b` if `c` is truthy, otherwise return `a`.
95-
#[inline]
96-
pub(crate) const fn ct_select(a: Self, b: Self, c: CtChoice) -> Self {
97-
Self(c.select(a.0, b.0))
98-
}
9993
}
10094

10195
impl Bounded for Limb {

src/limb/cmp.rs

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ impl Limb {
2525
self.0 == other.0
2626
}
2727

28+
/// Return `b` if `c` is truthy, otherwise return `a`.
29+
#[inline]
30+
pub(crate) const fn ct_select(a: Self, b: Self, c: CtChoice) -> Self {
31+
Self(c.select(a.0, b.0))
32+
}
33+
2834
/// Returns the truthy value if `self != 0` and the falsy value otherwise.
2935
#[inline]
3036
pub(crate) const fn ct_is_nonzero(&self) -> CtChoice {

src/uint/cmp.rs

+31-31
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ impl<const LIMBS: usize> Uint<LIMBS> {
4949

5050
/// Returns the truthy value if `self == rhs` or the falsy value otherwise.
5151
#[inline]
52-
pub(crate) const fn ct_eq(&self, rhs: &Self) -> CtChoice {
52+
pub(crate) const fn ct_eq(lhs: &Self, rhs: &Self) -> CtChoice {
5353
let mut acc = 0;
5454
let mut i = 0;
5555

5656
while i < LIMBS {
57-
acc |= self.limbs[i].0 ^ rhs.limbs[i].0;
57+
acc |= lhs.limbs[i].0 ^ rhs.limbs[i].0;
5858
i += 1;
5959
}
6060

@@ -64,40 +64,40 @@ impl<const LIMBS: usize> Uint<LIMBS> {
6464

6565
/// Returns the truthy value if `self <= rhs` and the falsy value otherwise.
6666
#[inline]
67-
pub(crate) const fn ct_lt(&self, rhs: &Self) -> CtChoice {
67+
pub(crate) const fn ct_lt(lhs: &Self, rhs: &Self) -> CtChoice {
6868
// We could use the same approach as in Limb::ct_lt(),
6969
// but since we have to use Uint::wrapping_sub(), which calls `sbb()`,
7070
// there are no savings compared to just calling `sbb()` directly.
71-
let (_res, borrow) = self.sbb(rhs, Limb::ZERO);
71+
let (_res, borrow) = lhs.sbb(rhs, Limb::ZERO);
7272
CtChoice::from_mask(borrow.0)
7373
}
7474

7575
/// Returns the truthy value if `self <= rhs` and the falsy value otherwise.
7676
#[inline]
77-
pub(crate) const fn ct_gt(&self, rhs: &Self) -> CtChoice {
78-
let (_res, borrow) = rhs.sbb(self, Limb::ZERO);
77+
pub(crate) const fn ct_gt(lhs: &Self, rhs: &Self) -> CtChoice {
78+
let (_res, borrow) = rhs.sbb(lhs, Limb::ZERO);
7979
CtChoice::from_mask(borrow.0)
8080
}
8181
}
8282

8383
impl<const LIMBS: usize> ConstantTimeEq for Uint<LIMBS> {
8484
#[inline]
8585
fn ct_eq(&self, other: &Self) -> Choice {
86-
self.ct_eq(other).into()
86+
Uint::ct_eq(self, other).into()
8787
}
8888
}
8989

9090
impl<const LIMBS: usize> ConstantTimeGreater for Uint<LIMBS> {
9191
#[inline]
9292
fn ct_gt(&self, other: &Self) -> Choice {
93-
self.ct_gt(other).into()
93+
Uint::ct_gt(self, other).into()
9494
}
9595
}
9696

9797
impl<const LIMBS: usize> ConstantTimeLess for Uint<LIMBS> {
9898
#[inline]
9999
fn ct_lt(&self, other: &Self) -> Choice {
100-
self.ct_lt(other).into()
100+
Uint::ct_lt(self, other).into()
101101
}
102102
}
103103

@@ -154,10 +154,10 @@ mod tests {
154154
let a = U128::ZERO;
155155
let b = U128::MAX;
156156

157-
assert!(bool::from(ConstantTimeEq::ct_eq(&a, &a)));
158-
assert!(!bool::from(ConstantTimeEq::ct_eq(&a, &b)));
159-
assert!(!bool::from(ConstantTimeEq::ct_eq(&b, &a)));
160-
assert!(bool::from(ConstantTimeEq::ct_eq(&b, &b)));
157+
assert!(bool::from(a.ct_eq(&a)));
158+
assert!(!bool::from(a.ct_eq(&b)));
159+
assert!(!bool::from(b.ct_eq(&a)));
160+
assert!(bool::from(b.ct_eq(&b)));
161161
}
162162

163163
#[test]
@@ -166,17 +166,17 @@ mod tests {
166166
let b = U128::ONE;
167167
let c = U128::MAX;
168168

169-
assert!(bool::from(ConstantTimeGreater::ct_gt(&b, &a)));
170-
assert!(bool::from(ConstantTimeGreater::ct_gt(&c, &a)));
171-
assert!(bool::from(ConstantTimeGreater::ct_gt(&c, &b)));
169+
assert!(bool::from(b.ct_gt(&a)));
170+
assert!(bool::from(c.ct_gt(&a)));
171+
assert!(bool::from(c.ct_gt(&b)));
172172

173-
assert!(!bool::from(ConstantTimeGreater::ct_gt(&a, &a)));
174-
assert!(!bool::from(ConstantTimeGreater::ct_gt(&b, &b)));
175-
assert!(!bool::from(ConstantTimeGreater::ct_gt(&c, &c)));
173+
assert!(!bool::from(a.ct_gt(&a)));
174+
assert!(!bool::from(b.ct_gt(&b)));
175+
assert!(!bool::from(c.ct_gt(&c)));
176176

177-
assert!(!bool::from(ConstantTimeGreater::ct_gt(&a, &b)));
178-
assert!(!bool::from(ConstantTimeGreater::ct_gt(&a, &c)));
179-
assert!(!bool::from(ConstantTimeGreater::ct_gt(&b, &c)));
177+
assert!(!bool::from(a.ct_gt(&b)));
178+
assert!(!bool::from(a.ct_gt(&c)));
179+
assert!(!bool::from(b.ct_gt(&c)));
180180
}
181181

182182
#[test]
@@ -185,16 +185,16 @@ mod tests {
185185
let b = U128::ONE;
186186
let c = U128::MAX;
187187

188-
assert!(bool::from(ConstantTimeLess::ct_lt(&a, &b)));
189-
assert!(bool::from(ConstantTimeLess::ct_lt(&a, &c)));
190-
assert!(bool::from(ConstantTimeLess::ct_lt(&b, &c)));
188+
assert!(bool::from(a.ct_lt(&b)));
189+
assert!(bool::from(a.ct_lt(&c)));
190+
assert!(bool::from(b.ct_lt(&c)));
191191

192-
assert!(!bool::from(ConstantTimeLess::ct_lt(&a, &a)));
193-
assert!(!bool::from(ConstantTimeLess::ct_lt(&b, &b)));
194-
assert!(!bool::from(ConstantTimeLess::ct_lt(&c, &c)));
192+
assert!(!bool::from(a.ct_lt(&a)));
193+
assert!(!bool::from(b.ct_lt(&b)));
194+
assert!(!bool::from(c.ct_lt(&c)));
195195

196-
assert!(!bool::from(ConstantTimeLess::ct_lt(&b, &a)));
197-
assert!(!bool::from(ConstantTimeLess::ct_lt(&c, &a)));
198-
assert!(!bool::from(ConstantTimeLess::ct_lt(&c, &b)));
196+
assert!(!bool::from(b.ct_lt(&a)));
197+
assert!(!bool::from(c.ct_lt(&a)));
198+
assert!(!bool::from(c.ct_lt(&b)));
199199
}
200200
}

src/uint/inv_mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
9393

9494
debug_assert!(!a.ct_is_nonzero().is_true_vartime());
9595

96-
(v, b.ct_eq(&Uint::ONE))
96+
(v, Uint::ct_eq(&b, &Uint::ONE))
9797
}
9898

9999
/// Computes the multiplicative inverse of `self` mod `modulus`, where `modulus` is odd.

src/uint/rand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<const LIMBS: usize> RandomMod for Uint<LIMBS> {
4545
}
4646
n.limbs[n_limbs - 1] = n.limbs[n_limbs - 1] & mask;
4747

48-
if ConstantTimeLess::ct_lt(&n, modulus).into() {
48+
if n.ct_lt(modulus).into() {
4949
return n;
5050
}
5151
}

src/uint/sqrt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2121

2222
// If guess increased, the initial guess was low.
2323
// Repeat until reverse course.
24-
while guess.ct_lt(&xn).is_true_vartime() {
24+
while Uint::ct_lt(&guess, &xn).is_true_vartime() {
2525
// Sometimes an increase is too far, especially with large
2626
// powers, and then takes a long time to walk back. The upper
2727
// bound is based on bit size, so saturate on that.
@@ -35,7 +35,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3535
}
3636

3737
// Repeat while guess decreases.
38-
while guess.ct_gt(&xn).is_true_vartime() && xn.ct_is_nonzero().is_true_vartime() {
38+
while Uint::ct_gt(&guess, &xn).is_true_vartime() && xn.ct_is_nonzero().is_true_vartime() {
3939
guess = xn;
4040
xn = {
4141
let q = self.wrapping_div(&guess);

0 commit comments

Comments
 (0)