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
//! Contracts for constant-time comparison and conditional updates.
use crateChoice;
/// Selects without branches or addresses depending on the choice or values.
///
/// Implementations must preserve this contract for both possible choices.
/// Composite types can select each field through its own implementation.
///
/// ```
/// use tc_constant_time::{Choice, ConditionallySelectable};
///
/// #[derive(Debug, PartialEq)]
/// struct Pair(u32, u64);
///
/// impl ConditionallySelectable for Pair {
/// fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
/// Self(
/// u32::conditional_select(&a.0, &b.0, choice),
/// u64::conditional_select(&a.1, &b.1, choice),
/// )
/// }
/// }
///
/// let a = Pair(3, 5);
/// let b = Pair(7, 11);
/// assert_eq!(Pair::conditional_select(&a, &b, Choice::from_lsb(0)), a);
/// assert_eq!(Pair::conditional_select(&a, &b, Choice::from_lsb(1)), b);
/// ```
/// Equality without early exits on secret values.
///
/// Implementations must avoid input-dependent branches and memory addresses.
/// For composite values, compare every field and combine the resulting choices
/// with `&`; do not reveal a result to short-circuit the remaining comparisons.
/// Slice lengths are public: mismatched lengths return zero immediately.
///
/// ```
/// use tc_constant_time::{Choice, ConstantTimeEq};
///
/// struct Pair(u32, u64);
///
/// impl ConstantTimeEq for Pair {
/// fn ct_eq(&self, rhs: &Self) -> Choice {
/// self.0.ct_eq(&rhs.0) & self.1.ct_eq(&rhs.1)
/// }
/// }
///
/// let value = Pair(3, 5);
/// assert_eq!(value.ct_eq(&Pair(3, 5)).unwrap_u8(), 1);
/// assert_eq!(value.ct_eq(&Pair(3, 7)).unwrap_u8(), 0);
/// ```
/// Conditionally negates a value with wrapping arithmetic.
///
/// Integer implementations leave the value unchanged for zero and negate it
/// modulo its bit width for one. Signed minimum values remain unchanged when
/// negated. Arrays apply the operation to every element.
///
/// ```
/// use tc_constant_time::{Choice, ConditionallyNegatable};
/// let mut value = 3_u8;
/// value.conditional_negate(Choice::from_lsb(1));
/// assert_eq!(value, 253);
/// let mut minimum = i32::MIN;
/// minimum.conditional_negate(Choice::from_lsb(1));
/// assert_eq!(minimum, i32::MIN);
/// ```
/// Numeric ordering without value-dependent branches or addresses.
///
/// Implemented for all primitive signed and unsigned integer types, following
/// each type's numeric order. Arrays and slices have no ordering implementation
/// in this crate.
///
/// ```
/// use tc_constant_time::ConstantTimeOrd;
/// assert_eq!(0_u128.ct_lt(&u128::MAX).unwrap_u8(), 1);
/// assert_eq!(u128::MAX.ct_gt(&0).unwrap_u8(), 1);
/// assert_eq!(7_u16.ct_le(&7).unwrap_u8(), 1);
/// assert_eq!(7_u16.ct_ge(&8).unwrap_u8(), 0);
/// ```
///
/// Signed comparisons order negative values before zero and positive values,
/// including the minimum and maximum values without arithmetic overflow.
///
/// ```
/// use tc_constant_time::ConstantTimeOrd;
/// assert_eq!(i8::MIN.ct_lt(&i8::MAX).unwrap_u8(), 1);
/// assert_eq!((-1_i16).ct_lt(&0).unwrap_u8(), 1);
/// assert_eq!(0_i32.ct_gt(&-1).unwrap_u8(), 1);
/// assert_eq!((-7_i64).ct_le(&-7).unwrap_u8(), 1);
/// assert_eq!(i128::MAX.ct_ge(&i128::MIN).unwrap_u8(), 1);
/// assert_eq!(isize::MIN.ct_lt(&0).unwrap_u8(), 1);
/// ```