Skip to content

Commit 29bb99b

Browse files
Merge pull request #104 from tarcieri/ct_ne
Add `ConstantTimeEq::ct_ne`
2 parents b4b070c + bb7e5d2 commit 29bb99b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ pub trait ConstantTimeEq {
259259
/// * `Choice(0u8)` if `self != other`.
260260
#[inline]
261261
fn ct_eq(&self, other: &Self) -> Choice;
262+
263+
/// Determine if two items are NOT equal.
264+
///
265+
/// The `ct_ne` function should execute in constant time.
266+
///
267+
/// # Returns
268+
///
269+
/// * `Choice(0u8)` if `self == other`;
270+
/// * `Choice(1u8)` if `self != other`.
271+
#[inline]
272+
fn ct_ne(&self, other: &Self) -> Choice {
273+
!self.ct_eq(other)
274+
}
262275
}
263276

264277
impl<T: ConstantTimeEq> ConstantTimeEq for [T] {

tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ macro_rules! generate_integer_equal_tests {
105105

106106
assert_eq!(x.ct_eq(&y).unwrap_u8(), 0);
107107
assert_eq!(x.ct_eq(&z).unwrap_u8(), 1);
108+
assert_eq!(x.ct_ne(&y).unwrap_u8(), 1);
109+
assert_eq!(x.ct_ne(&z).unwrap_u8(), 0);
108110
)*)
109111
}
110112

0 commit comments

Comments
 (0)