Skip to content

Commit c68d556

Browse files
committed
Replace ConstantTimeEq with CtEq in trait bounds
Changes all of the traits in `traits.rs`, most notably the `Integer`, `Zero`, and `One` traits, to use `ctutils::CtEq` instead of `subtle::ConstantTimeEq`, as part of a larger overall migration to `ctutils` (see also #1040, #1043)
1 parent 39644d8 commit c68d556

19 files changed

Lines changed: 189 additions & 61 deletions

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ harness = false
8686

8787
[profile.dev]
8888
opt-level = 2
89+
90+
[patch.crates-io.ctutils]
91+
path = "../utils/ctutils"

src/checked.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Checked arithmetic.
22
3-
use crate::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub};
3+
use crate::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, ConstChoice, CtEq, CtSelect};
44
use core::ops::{Add, Div, Mul, Sub};
55
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
66

@@ -268,13 +268,36 @@ impl<T: ConditionallySelectable> ConditionallySelectable for Checked<T> {
268268
}
269269
}
270270

271-
impl<T: ConstantTimeEq> ConstantTimeEq for Checked<T> {
271+
impl<T> ConstantTimeEq for Checked<T>
272+
where
273+
T: ConstantTimeEq,
274+
{
272275
#[inline]
273276
fn ct_eq(&self, rhs: &Self) -> Choice {
274277
self.0.ct_eq(&rhs.0)
275278
}
276279
}
277280

281+
impl<T> CtEq for Checked<T>
282+
where
283+
Self: ConstantTimeEq,
284+
{
285+
#[inline]
286+
fn ct_eq(&self, other: &Self) -> ConstChoice {
287+
ConstantTimeEq::ct_eq(self, &other).into()
288+
}
289+
}
290+
291+
impl<T> CtSelect for Checked<T>
292+
where
293+
Self: ConditionallySelectable,
294+
{
295+
#[inline]
296+
fn ct_select(&self, other: &Self, choice: ConstChoice) -> Self {
297+
Self::conditional_select(self, other, choice.into())
298+
}
299+
}
300+
278301
impl<T> Default for Checked<T>
279302
where
280303
T: Default,

src/int/cmp.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
//!
33
//! By default, these are all constant-time.
44
5+
use crate::{ConstChoice, CtEq, Int, Uint};
56
use core::cmp::Ordering;
6-
7-
use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
8-
9-
use crate::{ConstChoice, Int, Uint};
7+
use subtle::{Choice, ConstantTimeGreater, ConstantTimeLess};
108

119
impl<const LIMBS: usize> Int<LIMBS> {
1210
/// Return `b` if `c` is truthy, otherwise return `a`.
@@ -61,10 +59,17 @@ impl<const LIMBS: usize> Int<LIMBS> {
6159
}
6260
}
6361

64-
impl<const LIMBS: usize> ConstantTimeEq for Int<LIMBS> {
62+
impl<const LIMBS: usize> CtEq for Int<LIMBS> {
63+
#[inline]
64+
fn ct_eq(&self, other: &Self) -> ConstChoice {
65+
CtEq::ct_eq(&self.0, &other.0)
66+
}
67+
}
68+
69+
impl<const LIMBS: usize> subtle::ConstantTimeEq for Int<LIMBS> {
6570
#[inline]
6671
fn ct_eq(&self, other: &Self) -> Choice {
67-
Int::eq(self, other).into()
72+
CtEq::ct_eq(self, other).into()
6873
}
6974
}
7075

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub use crate::{
193193
};
194194

195195
// TODO(tarcieri): get rid of `Const*` prefix
196-
pub use ctutils::{Choice as ConstChoice, CtOption as ConstCtOption, CtSelect};
196+
pub use ctutils::{Choice as ConstChoice, CtEq, CtOption as ConstCtOption, CtSelect};
197197

198198
#[cfg(feature = "alloc")]
199199
pub use crate::uint::boxed::BoxedUint;

src/limb.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ mod sub;
2020
mod rand;
2121

2222
use crate::{
23-
Bounded, ConstChoice, ConstCtOption, ConstOne, ConstZero, Constants, NonZero, One, WideWord,
24-
Word, Zero,
23+
Bounded, ConstChoice, ConstCtOption, ConstOne, ConstZero, Constants, CtEq, NonZero, One,
24+
WideWord, Word, Zero,
2525
};
2626
use core::fmt;
2727
use ctutils::CtSelect;
28-
use subtle::{Choice, ConstantTimeEq};
28+
use subtle::Choice;
2929

3030
#[cfg(feature = "serde")]
3131
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};

src/limb/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl CheckedAdd for Limb {
8383
#[inline]
8484
fn checked_add(&self, rhs: &Self) -> CtOption<Self> {
8585
let (result, carry) = self.overflowing_add(*rhs);
86-
CtOption::new(result, carry.is_zero())
86+
CtOption::new(result, carry.is_zero().into())
8787
}
8888
}
8989

src/limb/cmp.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! Limb comparisons
22
3-
use crate::{ConstChoice, Limb, word};
3+
use crate::{ConstChoice, CtEq, Limb, word};
44
use core::cmp::Ordering;
5-
use subtle::{
6-
Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess,
7-
};
5+
use subtle::{Choice, ConditionallySelectable, ConstantTimeGreater, ConstantTimeLess};
86

97
impl Limb {
108
/// Is this limb an odd number?
@@ -48,15 +46,27 @@ impl Limb {
4846
}
4947
}
5048

51-
impl ConstantTimeEq for Limb {
49+
impl CtEq for Limb {
50+
#[inline]
51+
fn ct_eq(&self, other: &Self) -> ConstChoice {
52+
CtEq::ct_eq(&self.0, &other.0)
53+
}
54+
55+
#[inline]
56+
fn ct_ne(&self, other: &Self) -> ConstChoice {
57+
CtEq::ct_ne(&self.0, &other.0)
58+
}
59+
}
60+
61+
impl subtle::ConstantTimeEq for Limb {
5262
#[inline]
5363
fn ct_eq(&self, other: &Self) -> Choice {
54-
self.0.ct_eq(&other.0)
64+
CtEq::ct_eq(self, other).into()
5565
}
5666

5767
#[inline]
5868
fn ct_ne(&self, other: &Self) -> Choice {
59-
self.0.ct_ne(&other.0)
69+
CtEq::ct_ne(self, other).into()
6070
}
6171
}
6272

@@ -79,7 +89,7 @@ impl Eq for Limb {}
7989
impl Ord for Limb {
8090
fn cmp(&self, other: &Self) -> Ordering {
8191
let mut ret = Ordering::Less;
82-
ret.conditional_assign(&Ordering::Equal, self.ct_eq(other));
92+
ret.conditional_assign(&Ordering::Equal, self.ct_eq(other).into());
8393
ret.conditional_assign(&Ordering::Greater, self.ct_gt(other));
8494
debug_assert_eq!(ret == Ordering::Less, bool::from(self.ct_lt(other)));
8595
ret

src/limb/mul.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl CheckedMul for Limb {
4848
#[inline]
4949
fn checked_mul(&self, rhs: &Self) -> CtOption<Self> {
5050
let (lo, hi) = self.widening_mul(*rhs);
51-
CtOption::new(lo, hi.is_zero())
51+
CtOption::new(lo, hi.is_zero().into())
5252
}
5353
}
5454

src/limb/sub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl CheckedSub for Limb {
3636
#[inline]
3737
fn checked_sub(&self, rhs: &Self) -> CtOption<Self> {
3838
let (result, underflow) = self.borrowing_sub(*rhs, Limb::ZERO);
39-
CtOption::new(result, underflow.is_zero())
39+
CtOption::new(result, underflow.is_zero().into())
4040
}
4141
}
4242

0 commit comments

Comments
 (0)