File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -240,6 +240,13 @@ impl<T: ConstantTimeEq> ConstantTimeEq for [T] {
240
240
}
241
241
}
242
242
243
+ impl ConstantTimeEq for Choice {
244
+ #[ inline]
245
+ fn ct_eq ( & self , rhs : & Choice ) -> Choice {
246
+ !( * self ^ * rhs)
247
+ }
248
+ }
249
+
243
250
/// Given the bit-width `$bit_width` and the corresponding primitive
244
251
/// unsigned and signed types `$t_u` and `$t_i` respectively, generate
245
252
/// an `ConstantTimeEq` implementation.
Original file line number Diff line number Diff line change @@ -135,6 +135,14 @@ fn conditional_select_choice() {
135
135
assert_eq ! ( bool :: from( Choice :: conditional_select( & f, & t, t) ) , true ) ;
136
136
}
137
137
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
+
138
146
#[ test]
139
147
fn test_ctoption ( ) {
140
148
let a = CtOption :: new ( 10 , Choice :: from ( 1 ) ) ;
You can’t perform that action at this time.
0 commit comments