forked from RustCrypto/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathct_eq.rs
More file actions
327 lines (290 loc) · 8.14 KB
/
ct_eq.rs
File metadata and controls
327 lines (290 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::Choice;
use cmov::CmovEq;
use core::{
cmp,
num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU128,
},
};
#[cfg(feature = "subtle")]
use crate::CtOption;
/// Constant-time equality: like `(Partial)Eq` with [`Choice`] instead of [`bool`].
///
/// Impl'd for: [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`], [`cmp::Ordering`],
/// [`Choice`], and arrays/slices of any type which also impls [`CtEq`].
///
/// This crate provides built-in implementations for the following types:
/// - [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`isize`]
/// - [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`]
/// - [`NonZeroI8`], [`NonZeroI16`], [`NonZeroI32`], [`NonZeroI64`], [`NonZeroI128`]
/// - [`NonZeroU8`], [`NonZeroU16`], [`NonZeroU32`], [`NonZeroU64`], [`NonZeroU128`]
/// - [`cmp::Ordering`]
/// - [`Choice`]
/// - `[T]` and `[T; N]` where `T` impls [`CtEqSlice`], which the previously mentioned types all do.
pub trait CtEq<Rhs = Self>
where
Rhs: ?Sized,
{
/// Determine if `self` is equal to `other` in constant-time.
#[must_use]
fn ct_eq(&self, other: &Rhs) -> Choice;
/// Determine if `self` is NOT equal to `other` in constant-time.
#[must_use]
fn ct_ne(&self, other: &Rhs) -> Choice {
!self.ct_eq(other)
}
}
/// Implementing this trait enables use of the [`CtEq`] trait for `[T]` where `T` is the
/// `Self` type implementing the trait, via a blanket impl.
///
/// It needs to be a separate trait from [`CtEq`] because we need to be able to impl
/// [`CtEq`] for `[T]` which is `?Sized`.
pub trait CtEqSlice: CtEq + Sized {
/// Determine if `a` is equal to `b` in constant-time.
#[must_use]
fn ct_eq_slice(a: &[Self], b: &[Self]) -> Choice {
let mut ret = a.len().ct_eq(&b.len());
for (a, b) in a.iter().zip(b.iter()) {
ret &= a.ct_eq(b);
}
ret
}
/// Determine if `a` is NOT equal to `b` in constant-time.
#[must_use]
fn ct_ne_slice(a: &[Self], b: &[Self]) -> Choice {
!Self::ct_eq_slice(a, b)
}
}
impl<T: CtEqSlice> CtEq for [T] {
fn ct_eq(&self, other: &Self) -> Choice {
T::ct_eq_slice(self, other)
}
fn ct_ne(&self, other: &Self) -> Choice {
T::ct_ne_slice(self, other)
}
}
/// Impl `CtEq` using the `cmov::CmovEq` trait
macro_rules! impl_ct_eq_with_cmov_eq {
( $($ty:ty),+ ) => {
$(
impl CtEq for $ty {
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
let mut ret = Choice::FALSE;
self.cmoveq(other, 1, &mut ret.0);
ret
}
}
)+
};
}
/// Impl `CtEq` and `CtEqSlice` using the `cmov::CmovEq` trait
macro_rules! impl_ct_eq_slice_with_cmov_eq {
( $($ty:ty),+ ) => {
$(
impl_ct_eq_with_cmov_eq!($ty);
impl CtEqSlice for $ty {
#[inline]
fn ct_eq_slice(a: &[Self], b: &[Self]) -> Choice {
let mut ret = Choice::FALSE;
a.cmoveq(b, 1, &mut ret.0);
ret
}
}
)+
};
}
impl_ct_eq_slice_with_cmov_eq!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
impl_ct_eq_with_cmov_eq!(isize, usize);
impl CtEqSlice for isize {}
impl CtEqSlice for usize {}
/// Impl `CtEq` for `NonZero<T>` by calling `NonZero::get`.
macro_rules! impl_ct_eq_for_nonzero_integer {
( $($ty:ty),+ ) => {
$(
impl CtEq for $ty {
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
self.get().ct_eq(&other.get())
}
}
impl CtEqSlice for $ty {}
)+
};
}
impl_ct_eq_for_nonzero_integer!(
NonZeroI8,
NonZeroI16,
NonZeroI32,
NonZeroI64,
NonZeroI128,
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128
);
impl CtEq for cmp::Ordering {
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
// `Ordering` is `repr(i8)`, which has a `CtEq` impl
(*self as i8).ct_eq(&(*other as i8))
}
}
impl CtEqSlice for cmp::Ordering {}
impl<T, const N: usize> CtEq for [T; N]
where
T: CtEqSlice,
{
#[inline]
fn ct_eq(&self, other: &[T; N]) -> Choice {
self.as_slice().ct_eq(other.as_slice())
}
}
impl<T, const N: usize> CtEqSlice for [T; N] where T: CtEqSlice {}
#[cfg(feature = "subtle")]
impl CtEq for subtle::Choice {
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
self.unwrap_u8().ct_eq(&other.unwrap_u8())
}
}
#[cfg(feature = "subtle")]
impl<T> CtEq for subtle::CtOption<T>
where
T: CtEq + Default + subtle::ConditionallySelectable,
{
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
CtOption::from(*self).ct_eq(&CtOption::from(*other))
}
}
#[cfg(feature = "alloc")]
mod alloc {
use super::{Choice, CtEq, CtEqSlice};
use ::alloc::{boxed::Box, vec::Vec};
impl<T> CtEq for Box<T>
where
T: CtEq,
{
#[inline]
#[track_caller]
fn ct_eq(&self, rhs: &Self) -> Choice {
(**self).ct_eq(rhs)
}
}
impl<T> CtEq for Box<[T]>
where
T: CtEqSlice,
{
#[inline]
#[track_caller]
fn ct_eq(&self, rhs: &Self) -> Choice {
self.ct_eq(&**rhs)
}
}
impl<T> CtEq<[T]> for Box<[T]>
where
T: CtEqSlice,
{
#[inline]
#[track_caller]
fn ct_eq(&self, rhs: &[T]) -> Choice {
(**self).ct_eq(rhs)
}
}
impl<T> CtEq for Vec<T>
where
T: CtEqSlice,
{
#[inline]
#[track_caller]
fn ct_eq(&self, rhs: &Self) -> Choice {
self.ct_eq(rhs.as_slice())
}
}
impl<T> CtEq<[T]> for Vec<T>
where
T: CtEqSlice,
{
#[inline]
#[track_caller]
fn ct_eq(&self, rhs: &[T]) -> Choice {
self.as_slice().ct_eq(rhs)
}
}
}
#[cfg(test)]
mod tests {
use super::CtEq;
use core::cmp::Ordering;
macro_rules! truth_table {
($a:expr, $b:expr, $c:expr) => {
assert!($a.ct_eq(&$b).to_bool());
assert!(!$a.ct_eq(&$c).to_bool());
assert!(!$b.ct_eq(&$c).to_bool());
assert!(!$a.ct_ne(&$b).to_bool());
assert!($a.ct_ne(&$c).to_bool());
assert!($b.ct_ne(&$c).to_bool());
};
}
macro_rules! ct_eq_test_unsigned {
($ty:ty, $name:ident) => {
#[test]
fn $name() {
let a = <$ty>::MAX;
let b = <$ty>::MAX;
let c = <$ty>::MIN;
truth_table!(a, b, c);
}
};
}
macro_rules! ct_eq_test_signed {
($ty:ty, $name:ident) => {
#[test]
fn $name() {
let a = <$ty>::MAX;
let b = <$ty>::MAX;
let c = <$ty>::MIN;
truth_table!(a, b, c);
}
};
}
ct_eq_test_unsigned!(u8, u8_ct_eq);
ct_eq_test_unsigned!(u16, u16_ct_eq);
ct_eq_test_unsigned!(u32, u32_ct_eq);
ct_eq_test_unsigned!(u64, u64_ct_eq);
ct_eq_test_unsigned!(u128, u128_ct_eq);
ct_eq_test_unsigned!(usize, usize_ct_eq);
ct_eq_test_signed!(i8, i8_ct_eq);
ct_eq_test_signed!(i16, i16_ct_eq);
ct_eq_test_signed!(i32, i32_ct_eq);
ct_eq_test_signed!(i64, i64_ct_eq);
ct_eq_test_signed!(i128, i128_ct_eq);
ct_eq_test_signed!(isize, isize_ct_eq);
#[test]
fn array_ct_eq() {
let a = [1u64, 2, 3];
let b = [1u64, 2, 3];
let c = [1u64, 2, 4];
truth_table!(a, b, c);
}
#[test]
fn ordering_ct_eq() {
let a = Ordering::Greater;
let b = Ordering::Greater;
let c = Ordering::Less;
truth_table!(a, b, c);
}
#[test]
fn slice_ct_eq() {
let a: &[u64] = &[1, 2, 3];
let b: &[u64] = &[1, 2, 3];
let c: &[u64] = &[1, 2, 4];
truth_table!(a, b, c);
// Length mismatches
assert!(a.ct_ne(&[]).to_bool());
assert!(a.ct_ne(&[1, 2]).to_bool());
}
}