Skip to content

Commit 2d447a4

Browse files
authored
Merge pull request #76 from tarcieri/impl-ct-eq-for-choice
Impl ConstantTimeEq for Choice
2 parents dc7d73d + b54d36b commit 2d447a4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ impl<T: ConstantTimeEq> ConstantTimeEq for [T] {
240240
}
241241
}
242242

243+
impl ConstantTimeEq for Choice {
244+
#[inline]
245+
fn ct_eq(&self, rhs: &Choice) -> Choice {
246+
!(*self ^ *rhs)
247+
}
248+
}
249+
243250
/// Given the bit-width `$bit_width` and the corresponding primitive
244251
/// unsigned and signed types `$t_u` and `$t_i` respectively, generate
245252
/// an `ConstantTimeEq` implementation.

tests/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ fn conditional_select_choice() {
135135
assert_eq!(bool::from(Choice::conditional_select(&f, &t, t)), true);
136136
}
137137

138+
#[test]
139+
fn choice_equal() {
140+
assert!(Choice::from(0).ct_eq(&Choice::from(0)).unwrap_u8() == 1);
141+
assert!(Choice::from(0).ct_eq(&Choice::from(1)).unwrap_u8() == 0);
142+
assert!(Choice::from(1).ct_eq(&Choice::from(0)).unwrap_u8() == 0);
143+
assert!(Choice::from(1).ct_eq(&Choice::from(1)).unwrap_u8() == 1);
144+
}
145+
138146
#[test]
139147
fn test_ctoption() {
140148
let a = CtOption::new(10, Choice::from(1));

0 commit comments

Comments
 (0)