Skip to content

Commit 525a81e

Browse files
committed
Don't use "ct_" prefixes for constant-time methods - it is the default
1 parent 4314261 commit 525a81e

File tree

17 files changed

+69
-63
lines changed

17 files changed

+69
-63
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/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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ where
204204
D: Deserializer<'de>,
205205
{
206206
Uint::<LIMBS>::deserialize(deserializer).and_then(|montgomery_form| {
207-
if Uint::ct_lt(&montgomery_form, &MOD::MODULUS).into() {
207+
if montgomery_form < MOD::MODULUS.0 {
208208
Ok(Self {
209209
montgomery_form,
210210
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3434
let mut done = CtChoice::FALSE;
3535
loop {
3636
let (mut r, borrow) = rem.sbb(&c, Limb::ZERO);
37-
rem = Self::ct_select(&r, &rem, CtChoice::from_word_mask(borrow.0).or(done));
37+
rem = Self::select(&r, &rem, CtChoice::from_word_mask(borrow.0).or(done));
3838
r = quo.bitor(&Self::ONE);
39-
quo = Self::ct_select(&r, &quo, CtChoice::from_word_mask(borrow.0).or(done));
39+
quo = Self::select(&r, &quo, CtChoice::from_word_mask(borrow.0).or(done));
4040
if i == 0 {
4141
break;
4242
}
@@ -45,7 +45,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
4545
// aren't modified further (but do the remaining iterations anyway to be constant-time)
4646
done = CtChoice::from_word_lt(i as Word, mb as Word);
4747
c = c.shr1();
48-
quo = Self::ct_select(&quo.shl1(), &quo, done);
48+
quo = Self::select(&quo.shl1(), &quo, done);
4949
}
5050

5151
(quo, rem)
@@ -68,9 +68,9 @@ impl<const LIMBS: usize> Uint<LIMBS> {
6868

6969
loop {
7070
let (mut r, borrow) = rem.sbb(&c, Limb::ZERO);
71-
rem = Self::ct_select(&r, &rem, CtChoice::from_word_mask(borrow.0));
71+
rem = Self::select(&r, &rem, CtChoice::from_word_mask(borrow.0));
7272
r = quo.bitor(&Self::ONE);
73-
quo = Self::ct_select(&r, &quo, CtChoice::from_word_mask(borrow.0));
73+
quo = Self::select(&r, &quo, CtChoice::from_word_mask(borrow.0));
7474
if bd == 0 {
7575
break;
7676
}
@@ -96,7 +96,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
9696

9797
loop {
9898
let (r, borrow) = rem.sbb(&c, Limb::ZERO);
99-
rem = Self::ct_select(&r, &rem, CtChoice::from_word_mask(borrow.0));
99+
rem = Self::select(&r, &rem, CtChoice::from_word_mask(borrow.0));
100100
if bd == 0 {
101101
break;
102102
}
@@ -129,8 +129,8 @@ impl<const LIMBS: usize> Uint<LIMBS> {
129129
let (lower_sub, borrow) = lower.sbb(&c.0, Limb::ZERO);
130130
let (upper_sub, borrow) = upper.sbb(&c.1, borrow);
131131

132-
lower = Self::ct_select(&lower_sub, &lower, CtChoice::from_word_mask(borrow.0));
133-
upper = Self::ct_select(&upper_sub, &upper, CtChoice::from_word_mask(borrow.0));
132+
lower = Self::select(&lower_sub, &lower, CtChoice::from_word_mask(borrow.0));
133+
upper = Self::select(&upper_sub, &upper, CtChoice::from_word_mask(borrow.0));
134134
if bd == 0 {
135135
break;
136136
}
@@ -156,7 +156,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
156156

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

159-
out.limbs[limb_num] = Limb::ct_select(out.limbs[limb_num], outmask, le);
159+
out.limbs[limb_num] = Limb::select(out.limbs[limb_num], outmask, le);
160160

161161
// TODO: this is not constant-time.
162162
let mut i = limb_num + 1;

src/uint/div_limb.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ pub const fn reciprocal(d: Word) -> Word {
7070

7171
/// Returns `u32::MAX` if `a < b` and `0` otherwise.
7272
#[inline]
73-
const fn ct_lt(a: u32, b: u32) -> u32 {
73+
const fn lt(a: u32, b: u32) -> u32 {
7474
let bit = (((!a) & b) | (((!a) | b) & (a.wrapping_sub(b)))) >> (u32::BITS - 1);
7575
bit.wrapping_neg()
7676
}
7777

7878
/// Returns `a` if `c == 0` and `b` if `c == u32::MAX`.
7979
#[inline(always)]
80-
const fn ct_select(a: u32, b: u32, c: u32) -> u32 {
80+
const fn select(a: u32, b: u32, c: u32) -> u32 {
8181
a ^ (c & (a ^ b))
8282
}
8383

@@ -101,8 +101,8 @@ const fn short_div(dividend: u32, dividend_bits: u32, divisor: u32, divisor_bits
101101

102102
while i > 0 {
103103
i -= 1;
104-
let bit = ct_lt(dividend, divisor);
105-
dividend = ct_select(dividend.wrapping_sub(divisor), dividend, bit);
104+
let bit = lt(dividend, divisor);
105+
dividend = select(dividend.wrapping_sub(divisor), dividend, bit);
106106
divisor >>= 1;
107107
let inv_bit = !bit;
108108
quotient |= (inv_bit >> (u32::BITS - 1)) << i;

src/uint/inv_mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2121
let mut i = 0;
2222

2323
// The inverse exists either if `k` is 0 or if `self` is odd.
24-
let is_some = CtChoice::from_u32_nonzero(k).not().or(self.ct_is_odd());
24+
let is_some = CtChoice::from_u32_nonzero(k).not().or(self.is_odd());
2525

2626
while i < k {
2727
// X_i = b_i mod 2
2828
let x_i = b.limbs[0].0 & 1;
2929
let x_i_choice = CtChoice::from_word_lsb(x_i);
3030
// b_{i+1} = (b_i - a * X_i) / 2
31-
b = Self::ct_select(&b, &b.wrapping_sub(self), x_i_choice).shr1();
31+
b = Self::select(&b, &b.wrapping_sub(self), x_i_choice).shr1();
3232
// Store the X_i bit in the result (x = x | (1 << X_i))
3333
let (shifted, _overflow) = Uint::from_word(x_i).shl_vartime(i);
3434
x = x.bitor(&shifted);
@@ -53,7 +53,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
5353
let mut i = 0;
5454

5555
// The inverse exists either if `k` is 0 or if `self` is odd.
56-
let is_some = CtChoice::from_u32_nonzero(k).not().or(self.ct_is_odd());
56+
let is_some = CtChoice::from_u32_nonzero(k).not().or(self.is_odd());
5757

5858
while i < Self::BITS {
5959
// Only iterations for i = 0..k need to change `x`,
@@ -64,7 +64,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
6464
let x_i = b.limbs[0].0 & 1;
6565
let x_i_choice = CtChoice::from_word_lsb(x_i);
6666
// b_{i+1} = (b_i - self * X_i) / 2
67-
b = Self::ct_select(&b, &b.wrapping_sub(self), x_i_choice).shr1();
67+
b = Self::select(&b, &b.wrapping_sub(self), x_i_choice).shr1();
6868

6969
// Store the X_i bit in the result (x = x | (1 << X_i))
7070
// Don't change the result in dummy iterations.
@@ -106,24 +106,24 @@ impl<const LIMBS: usize> Uint<LIMBS> {
106106

107107
let m1hp = modulus.shr1().wrapping_add(&Uint::ONE);
108108

109-
let modulus_is_odd = modulus.ct_is_odd();
109+
let modulus_is_odd = modulus.is_odd();
110110

111111
let mut i = 0;
112112
while i < bit_size {
113113
// A sanity check that `b` stays odd. Only matters if `modulus` was odd to begin with,
114114
// otherwise this whole thing produces nonsense anyway.
115-
debug_assert!(modulus_is_odd.not().or(b.ct_is_odd()).is_true_vartime());
115+
debug_assert!(modulus_is_odd.not().or(b.is_odd()).is_true_vartime());
116116

117-
let self_odd = a.ct_is_odd();
117+
let self_odd = a.is_odd();
118118

119119
// Set `self -= b` if `self` is odd.
120120
let (new_a, swap) = a.conditional_wrapping_sub(&b, self_odd);
121121
// Set `b += self` if `swap` is true.
122-
b = Uint::ct_select(&b, &b.wrapping_add(&new_a), swap);
122+
b = Uint::select(&b, &b.wrapping_add(&new_a), swap);
123123
// Negate `self` if `swap` is true.
124124
a = new_a.conditional_wrapping_neg(swap);
125125

126-
let (new_u, new_v) = Uint::ct_swap(&u, &v, swap);
126+
let (new_u, new_v) = Uint::swap(&u, &v, swap);
127127
let (new_u, cy) = new_u.conditional_wrapping_sub(&new_v, self_odd);
128128
let (new_u, cyy) = new_u.conditional_wrapping_add(modulus, cy);
129129
debug_assert!(cy.is_true_vartime() == cyy.is_true_vartime());
@@ -143,10 +143,10 @@ impl<const LIMBS: usize> Uint<LIMBS> {
143143

144144
debug_assert!(modulus_is_odd
145145
.not()
146-
.or(a.ct_is_nonzero().not())
146+
.or(a.is_nonzero().not())
147147
.is_true_vartime());
148148

149-
(v, Uint::ct_eq(&b, &Uint::ONE).and(modulus_is_odd))
149+
(v, Uint::eq(&b, &Uint::ONE).and(modulus_is_odd))
150150
}
151151

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

0 commit comments

Comments
 (0)