Skip to content

Commit 350d86c

Browse files
committed
Don't use "ct_" prefixes for constant-time methods - it is the default
1 parent 90e472a commit 350d86c

File tree

18 files changed

+85
-76
lines changed

18 files changed

+85
-76
lines changed

src/limb/cmp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ impl Limb {
2828

2929
/// Return `b` if `c` is truthy, otherwise return `a`.
3030
#[inline]
31-
pub(crate) const fn ct_select(a: Self, b: Self, c: CtChoice) -> Self {
31+
pub(crate) const fn select(a: Self, b: Self, c: CtChoice) -> Self {
3232
Self(c.select_word(a.0, b.0))
3333
}
3434

3535
/// Returns the truthy value if `self != 0` and the falsy value otherwise.
3636
#[inline]
37-
pub(crate) const fn ct_is_nonzero(&self) -> CtChoice {
37+
pub(crate) const fn is_nonzero(&self) -> CtChoice {
3838
CtChoice::from_word_nonzero(self.0)
3939
}
4040
}

src/modular/div_by_2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ pub(crate) fn div_by_2<const LIMBS: usize>(a: &Uint<LIMBS>, modulus: &Uint<LIMBS
2626
.wrapping_add(&half_modulus)
2727
.wrapping_add(&Uint::<LIMBS>::ONE);
2828

29-
Uint::<LIMBS>::ct_select(&if_even, &if_odd, is_odd)
29+
Uint::<LIMBS>::select(&if_even, &if_odd, is_odd)
3030
}

src/modular/dyn_residue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<const LIMBS: usize> DynResidueParams<LIMBS> {
5656
mod_neg_inv,
5757
};
5858

59-
CtOption::new(params, modulus.is_odd())
59+
CtOption::new(params, <Uint<LIMBS> as Integer>::is_odd(modulus))
6060
}
6161

6262
/// Returns the modulus which was used to initialize these parameters.

src/modular/pow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const fn multi_exponentiate_montgomery_form_internal<const LIMBS: usize, const R
164164
let mut j = 1;
165165
while j < 1 << WINDOW {
166166
let choice = CtChoice::from_word_eq(j, idx);
167-
power = Uint::<LIMBS>::ct_select(&power, &powers[j as usize], choice);
167+
power = Uint::<LIMBS>::select(&power, &powers[j as usize], choice);
168168
j += 1;
169169
}
170170

src/modular/residue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
100100
/// [`new_checked`][`Residue::new_checked`] if you want to be able to detect an invalid modulus.
101101
pub const fn new(integer: &Uint<LIMBS>) -> Self {
102102
// A valid modulus must be odd
103-
if MOD::MODULUS.ct_is_odd().to_u8() == 0 {
103+
if MOD::MODULUS.is_odd().to_u8() == 0 {
104104
panic!("modulus must be odd");
105105
}
106106

@@ -117,7 +117,7 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
117117
// A valid modulus must be odd.
118118
CtOption::new(
119119
Self::generate_residue(integer),
120-
MOD::MODULUS.ct_is_odd().into(),
120+
MOD::MODULUS.is_odd().into(),
121121
)
122122
}
123123

@@ -226,7 +226,7 @@ where
226226
D: Deserializer<'de>,
227227
{
228228
Uint::<LIMBS>::deserialize(deserializer).and_then(|montgomery_form| {
229-
if Uint::ct_lt(&montgomery_form, &MOD::MODULUS).into() {
229+
if montgomery_form < MOD::MODULUS {
230230
Ok(Self {
231231
montgomery_form,
232232
phantom: PhantomData,

src/non_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ impl NonZero<Limb> {
2828
/// Creates a new non-zero limb in a const context.
2929
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
3030
pub const fn const_new(n: Limb) -> (Self, CtChoice) {
31-
(Self(n), n.ct_is_nonzero())
31+
(Self(n), n.is_nonzero())
3232
}
3333
}
3434

3535
impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
3636
/// Creates a new non-zero integer in a const context.
3737
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
3838
pub const fn const_new(n: Uint<LIMBS>) -> (Self, CtChoice) {
39-
(Self(n), n.ct_is_nonzero())
39+
(Self(n), n.is_nonzero())
4040
}
4141
}
4242

src/uint/add.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2424
/// Perform saturating addition, returning `MAX` on overflow.
2525
pub const fn saturating_add(&self, rhs: &Self) -> Self {
2626
let (res, overflow) = self.adc(rhs, Limb::ZERO);
27-
Self::ct_select(&res, &Self::MAX, CtChoice::from_word_lsb(overflow.0))
27+
Self::select(&res, &Self::MAX, CtChoice::from_word_lsb(overflow.0))
2828
}
2929

3030
/// Perform wrapping addition, discarding overflow.
@@ -39,7 +39,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3939
rhs: &Self,
4040
choice: CtChoice,
4141
) -> (Self, CtChoice) {
42-
let actual_rhs = Uint::ct_select(&Uint::ZERO, rhs, choice);
42+
let actual_rhs = Uint::select(&Uint::ZERO, rhs, choice);
4343
let (sum, carry) = self.adc(&actual_rhs, Limb::ZERO);
4444
(sum, CtChoice::from_word_lsb(carry.0))
4545
}

src/uint/cmp.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,46 @@ use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
1010
impl<const LIMBS: usize> Uint<LIMBS> {
1111
/// Return `b` if `c` is truthy, otherwise return `a`.
1212
#[inline]
13-
pub(crate) const fn ct_select(a: &Self, b: &Self, c: CtChoice) -> Self {
13+
pub(crate) const fn select(a: &Self, b: &Self, c: CtChoice) -> Self {
1414
let mut limbs = [Limb::ZERO; LIMBS];
1515

1616
let mut i = 0;
1717
while i < LIMBS {
18-
limbs[i] = Limb::ct_select(a.limbs[i], b.limbs[i], c);
18+
limbs[i] = Limb::select(a.limbs[i], b.limbs[i], c);
1919
i += 1;
2020
}
2121

2222
Uint { limbs }
2323
}
2424

2525
#[inline]
26-
pub(crate) const fn ct_swap(a: &Self, b: &Self, c: CtChoice) -> (Self, Self) {
27-
let new_a = Self::ct_select(a, b, c);
28-
let new_b = Self::ct_select(b, a, c);
26+
pub(crate) const fn swap(a: &Self, b: &Self, c: CtChoice) -> (Self, Self) {
27+
let new_a = Self::select(a, b, c);
28+
let new_b = Self::select(b, a, c);
2929

3030
(new_a, new_b)
3131
}
3232

3333
/// Returns the truthy value if `self`!=0 or the falsy value otherwise.
3434
#[inline]
35-
pub(crate) const fn ct_is_nonzero(&self) -> CtChoice {
35+
pub(crate) const fn is_nonzero(&self) -> CtChoice {
3636
let mut b = 0;
3737
let mut i = 0;
3838
while i < LIMBS {
3939
b |= self.limbs[i].0;
4040
i += 1;
4141
}
42-
Limb(b).ct_is_nonzero()
42+
Limb(b).is_nonzero()
4343
}
4444

4545
/// Returns the truthy value if `self` is odd or the falsy value otherwise.
46-
pub(crate) const fn ct_is_odd(&self) -> CtChoice {
46+
pub(crate) const fn is_odd(&self) -> CtChoice {
4747
CtChoice::from_word_lsb(self.limbs[0].0 & 1)
4848
}
4949

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

@@ -59,12 +59,12 @@ impl<const LIMBS: usize> Uint<LIMBS> {
5959
}
6060

6161
// acc == 0 if and only if self == rhs
62-
Limb(acc).ct_is_nonzero().not()
62+
Limb(acc).is_nonzero().not()
6363
}
6464

6565
/// Returns the truthy value if `self <= rhs` and the falsy value otherwise.
6666
#[inline]
67-
pub(crate) const fn ct_lt(lhs: &Self, rhs: &Self) -> CtChoice {
67+
pub(crate) const fn 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.
@@ -74,7 +74,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
7474

7575
/// Returns the truthy value if `self >= rhs` and the falsy value otherwise.
7676
#[inline]
77-
pub(crate) const fn ct_gt(lhs: &Self, rhs: &Self) -> CtChoice {
77+
pub(crate) const fn gt(lhs: &Self, rhs: &Self) -> CtChoice {
7878
let (_res, borrow) = rhs.sbb(lhs, Limb::ZERO);
7979
CtChoice::from_word_mask(borrow.0)
8080
}
@@ -85,7 +85,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
8585
/// 0 is Equal
8686
/// 1 is Greater
8787
#[inline]
88-
pub(crate) const fn ct_cmp(lhs: &Self, rhs: &Self) -> i8 {
88+
pub(crate) const fn cmp(lhs: &Self, rhs: &Self) -> i8 {
8989
let mut i = 0;
9090
let mut borrow = Limb::ZERO;
9191
let mut diff = Limb::ZERO;
@@ -97,7 +97,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
9797
i += 1;
9898
}
9999
let sgn = ((borrow.0 & 2) as i8) - 1;
100-
(diff.ct_is_nonzero().to_u8() as i8) * sgn
100+
(diff.is_nonzero().to_u8() as i8) * sgn
101101
}
102102

103103
/// Returns the Ordering between `self` and `rhs` in variable time.
@@ -123,29 +123,29 @@ impl<const LIMBS: usize> Uint<LIMBS> {
123123
impl<const LIMBS: usize> ConstantTimeEq for Uint<LIMBS> {
124124
#[inline]
125125
fn ct_eq(&self, other: &Self) -> Choice {
126-
Uint::ct_eq(self, other).into()
126+
Uint::eq(self, other).into()
127127
}
128128
}
129129

130130
impl<const LIMBS: usize> ConstantTimeGreater for Uint<LIMBS> {
131131
#[inline]
132132
fn ct_gt(&self, other: &Self) -> Choice {
133-
Uint::ct_gt(self, other).into()
133+
Uint::gt(self, other).into()
134134
}
135135
}
136136

137137
impl<const LIMBS: usize> ConstantTimeLess for Uint<LIMBS> {
138138
#[inline]
139139
fn ct_lt(&self, other: &Self) -> Choice {
140-
Uint::ct_lt(self, other).into()
140+
Uint::lt(self, other).into()
141141
}
142142
}
143143

144144
impl<const LIMBS: usize> Eq for Uint<LIMBS> {}
145145

146146
impl<const LIMBS: usize> Ord for Uint<LIMBS> {
147147
fn cmp(&self, other: &Self) -> Ordering {
148-
let c = Self::ct_cmp(self, other);
148+
let c = Self::cmp(self, other);
149149
match c {
150150
-1 => Ordering::Less,
151151
0 => Ordering::Equal,
@@ -181,9 +181,15 @@ mod tests {
181181

182182
#[test]
183183
fn is_odd() {
184+
// inherent methods
184185
assert!(!bool::from(U128::ZERO.is_odd()));
185186
assert!(bool::from(U128::ONE.is_odd()));
186187
assert!(bool::from(U128::MAX.is_odd()));
188+
189+
// `Integer` methods
190+
assert!(!bool::from(<U128 as Integer>::is_odd(&U128::ZERO)));
191+
assert!(bool::from(<U128 as Integer>::is_odd(&U128::ONE)));
192+
assert!(bool::from(<U128 as Integer>::is_odd(&U128::MAX)));
187193
}
188194

189195
#[test]

src/uint/div.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ impl<const LIMBS: usize> Uint<LIMBS> {
99
/// Computes `self` / `rhs` using a pre-made reciprocal,
1010
/// returns the quotient (q) and remainder (r).
1111
#[inline(always)]
12-
pub const fn ct_div_rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> (Self, Limb) {
12+
pub const fn const_div_rem_limb_with_reciprocal(
13+
&self,
14+
reciprocal: &Reciprocal,
15+
) -> (Self, Limb) {
1316
div_rem_limb_with_reciprocal(self, reciprocal)
1417
}
1518

@@ -27,8 +30,8 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2730
/// Returns the truthy value as the third element of the tuple if `rhs != 0`,
2831
/// and the falsy value otherwise.
2932
#[inline(always)]
30-
pub(crate) const fn ct_div_rem_limb(&self, rhs: Limb) -> (Self, Limb, CtChoice) {
31-
let (reciprocal, is_some) = Reciprocal::ct_new(rhs);
33+
pub(crate) const fn const_div_rem_limb(&self, rhs: Limb) -> (Self, Limb, CtChoice) {
34+
let (reciprocal, is_some) = Reciprocal::const_new(rhs);
3235
let (quo, rem) = div_rem_limb_with_reciprocal(self, &reciprocal);
3336
(quo, rem, is_some)
3437
}
@@ -37,7 +40,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3740
#[inline(always)]
3841
pub fn div_rem_limb(&self, rhs: NonZero<Limb>) -> (Self, Limb) {
3942
// Guaranteed to succeed since `rhs` is nonzero.
40-
let (quo, rem, _is_some) = self.ct_div_rem_limb(*rhs);
43+
let (quo, rem, _is_some) = self.const_div_rem_limb(*rhs);
4144
(quo, rem)
4245
}
4346

@@ -59,9 +62,9 @@ impl<const LIMBS: usize> Uint<LIMBS> {
5962
let mut done = CtChoice::FALSE;
6063
loop {
6164
let (mut r, borrow) = rem.sbb(&c, Limb::ZERO);
62-
rem = Self::ct_select(&r, &rem, CtChoice::from_word_mask(borrow.0).or(done));
65+
rem = Self::select(&r, &rem, CtChoice::from_word_mask(borrow.0).or(done));
6366
r = quo.bitor(&Self::ONE);
64-
quo = Self::ct_select(&r, &quo, CtChoice::from_word_mask(borrow.0).or(done));
67+
quo = Self::select(&r, &quo, CtChoice::from_word_mask(borrow.0).or(done));
6568
if i == 0 {
6669
break;
6770
}
@@ -70,11 +73,11 @@ impl<const LIMBS: usize> Uint<LIMBS> {
7073
// aren't modified further (but do the remaining iterations anyway to be constant-time)
7174
done = CtChoice::from_word_lt(i as Word, mb as Word);
7275
c = c.shr1();
73-
quo = Self::ct_select(&quo.shl1(), &quo, done);
76+
quo = Self::select(&quo.shl1(), &quo, done);
7477
}
7578

76-
let is_some = Limb(mb as Word).ct_is_nonzero();
77-
quo = Self::ct_select(&Self::ZERO, &quo, is_some);
79+
let is_some = Limb(mb as Word).is_nonzero();
80+
quo = Self::select(&Self::ZERO, &quo, is_some);
7881
(quo, rem, is_some)
7982
}
8083

@@ -97,9 +100,9 @@ impl<const LIMBS: usize> Uint<LIMBS> {
97100

98101
loop {
99102
let (mut r, borrow) = rem.sbb(&c, Limb::ZERO);
100-
rem = Self::ct_select(&r, &rem, CtChoice::from_word_mask(borrow.0));
103+
rem = Self::select(&r, &rem, CtChoice::from_word_mask(borrow.0));
101104
r = quo.bitor(&Self::ONE);
102-
quo = Self::ct_select(&r, &quo, CtChoice::from_word_mask(borrow.0));
105+
quo = Self::select(&r, &quo, CtChoice::from_word_mask(borrow.0));
103106
if bd == 0 {
104107
break;
105108
}
@@ -109,7 +112,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
109112
}
110113

111114
let is_some = CtChoice::from_u32_nonzero(mb);
112-
quo = Self::ct_select(&Self::ZERO, &quo, is_some);
115+
quo = Self::select(&Self::ZERO, &quo, is_some);
113116
(quo, rem, is_some)
114117
}
115118

@@ -129,7 +132,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
129132

130133
loop {
131134
let (r, borrow) = rem.sbb(&c, Limb::ZERO);
132-
rem = Self::ct_select(&r, &rem, CtChoice::from_word_mask(borrow.0));
135+
rem = Self::select(&r, &rem, CtChoice::from_word_mask(borrow.0));
133136
if bd == 0 {
134137
break;
135138
}
@@ -164,8 +167,8 @@ impl<const LIMBS: usize> Uint<LIMBS> {
164167
let (lower_sub, borrow) = lower.sbb(&c.0, Limb::ZERO);
165168
let (upper_sub, borrow) = upper.sbb(&c.1, borrow);
166169

167-
lower = Self::ct_select(&lower_sub, &lower, CtChoice::from_word_mask(borrow.0));
168-
upper = Self::ct_select(&upper_sub, &upper, CtChoice::from_word_mask(borrow.0));
170+
lower = Self::select(&lower_sub, &lower, CtChoice::from_word_mask(borrow.0));
171+
upper = Self::select(&upper_sub, &upper, CtChoice::from_word_mask(borrow.0));
169172
if bd == 0 {
170173
break;
171174
}
@@ -191,7 +194,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
191194

192195
let outmask = Limb(out.limbs[limb_num].0 & mask);
193196

194-
out.limbs[limb_num] = Limb::ct_select(out.limbs[limb_num], outmask, le);
197+
out.limbs[limb_num] = Limb::select(out.limbs[limb_num], outmask, le);
195198

196199
// TODO: this is not constant-time.
197200
let mut i = limb_num + 1;
@@ -305,7 +308,7 @@ impl<const LIMBS: usize> Div<NonZero<Limb>> for Uint<LIMBS> {
305308
type Output = Uint<LIMBS>;
306309

307310
fn div(self, rhs: NonZero<Limb>) -> Self::Output {
308-
let (q, _, _) = self.ct_div_rem_limb(*rhs);
311+
let (q, _, _) = self.const_div_rem_limb(*rhs);
309312
q
310313
}
311314
}
@@ -394,7 +397,7 @@ impl<const LIMBS: usize> Rem<NonZero<Limb>> for Uint<LIMBS> {
394397
type Output = Limb;
395398

396399
fn rem(self, rhs: NonZero<Limb>) -> Self::Output {
397-
let (_, r, _) = self.ct_div_rem_limb(*rhs);
400+
let (_, r, _) = self.const_div_rem_limb(*rhs);
398401
r
399402
}
400403
}

0 commit comments

Comments
 (0)