forked from RustCrypto/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathct_assign.rs
More file actions
213 lines (193 loc) · 5.49 KB
/
ct_assign.rs
File metadata and controls
213 lines (193 loc) · 5.49 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
use crate::Choice;
use cmov::Cmov;
use core::{
cmp,
num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
},
};
#[cfg(feature = "subtle")]
use crate::CtSelect;
#[cfg(doc)]
use core::num::NonZero;
/// Constant-time conditional assignment: assign a given value to another based on a [`Choice`].
///
/// 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`], [`NonZeroI128`]
/// - [`NonZeroU8`], [`NonZeroU16`], [`NonZeroU32`], [`NonZeroU64`], [`NonZeroU128`],, [`NonZeroUsize`]
/// - [`cmp::Ordering`]
/// - [`Choice`]
/// - `[T]` and `[T; N]` where `T` impls [`CtAssignSlice`], which the previously mentioned
/// types all do.
pub trait CtAssign<Rhs: ?Sized = Self> {
/// Conditionally assign `src` to `self` if `choice` is [`Choice::TRUE`].
fn ct_assign(&mut self, src: &Rhs, choice: Choice);
}
/// Implementing this trait enables use of the [`CtAssign`] trait for `[T]` where `T` is the
/// `Self` type implementing the trait, via a blanket impl.
///
/// It needs to be a separate trait from [`CtAssign`] because we need to be able to impl
/// [`CtAssign`] for `[T]` which is `?Sized`.
pub trait CtAssignSlice: CtAssign + Sized {
/// Conditionally assign `src` to `dst` if `choice` is [`Choice::TRUE`], or leave it unchanged
/// for [`Choice::FALSE`].
fn ct_assign_slice(dst: &mut [Self], src: &[Self], choice: Choice) {
assert_eq!(
dst.len(),
src.len(),
"source slice length ({}) does not match destination slice length ({})",
src.len(),
dst.len()
);
for (a, b) in dst.iter_mut().zip(src) {
a.ct_assign(b, choice);
}
}
}
impl<T: CtAssignSlice> CtAssign for [T] {
fn ct_assign(&mut self, src: &[T], choice: Choice) {
T::ct_assign_slice(self, src, choice);
}
}
/// Impl `CtAssign` using the `cmov::Cmov` trait
macro_rules! impl_ct_assign_with_cmov {
( $($ty:ty),+ ) => {
$(
impl CtAssign for $ty {
#[inline]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
self.cmovnz(rhs, choice.into());
}
}
)+
};
}
/// Impl `CtAssign` and `CtAssignSlice` using the `cmov::Cmov` trait
macro_rules! impl_ct_assign_slice_with_cmov {
( $($ty:ty),+ ) => {
$(
impl_ct_assign_with_cmov!($ty);
impl CtAssignSlice for $ty {
#[inline]
fn ct_assign_slice(dst: &mut [Self], src: &[Self], choice: Choice) {
dst.cmovnz(src, choice.into());
}
}
)+
};
}
// NOTE: impls `CtAssign` and `CtAssignSlice`
impl_ct_assign_slice_with_cmov!(
i8,
i16,
i32,
i64,
i128,
u8,
u16,
u32,
u64,
u128,
NonZeroI8,
NonZeroI16,
NonZeroI32,
NonZeroI64,
NonZeroI128,
NonZeroIsize,
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
NonZeroUsize,
cmp::Ordering
);
impl_ct_assign_with_cmov!(isize, usize);
impl CtAssignSlice for isize {}
impl CtAssignSlice for usize {}
impl<T, const N: usize> CtAssign for [T; N]
where
T: CtAssignSlice,
{
#[inline]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
self.as_mut_slice().ct_assign(rhs, choice);
}
}
impl<T, const N: usize> CtAssignSlice for [T; N] where T: CtAssignSlice {}
#[cfg(feature = "subtle")]
impl CtAssign for subtle::Choice {
#[inline]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
*self = Self::ct_select(self, rhs, choice);
}
}
#[cfg(feature = "subtle")]
impl<T> CtAssign for subtle::CtOption<T>
where
T: Default + subtle::ConditionallySelectable,
{
#[inline]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
use subtle::ConditionallySelectable as _;
self.conditional_assign(rhs, choice.into());
}
}
#[cfg(feature = "alloc")]
mod alloc {
use super::{Choice, CtAssign, CtAssignSlice};
use ::alloc::{boxed::Box, vec::Vec};
impl<T> CtAssign for Box<T>
where
T: CtAssign,
{
#[inline]
#[track_caller]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
(**self).ct_assign(rhs, choice);
}
}
impl<T> CtAssign for Box<[T]>
where
T: CtAssignSlice,
{
#[inline]
#[track_caller]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
self.ct_assign(&**rhs, choice);
}
}
impl<T> CtAssign<[T]> for Box<[T]>
where
T: CtAssignSlice,
{
#[inline]
#[track_caller]
fn ct_assign(&mut self, rhs: &[T], choice: Choice) {
(**self).ct_assign(rhs, choice);
}
}
impl<T> CtAssign for Vec<T>
where
T: CtAssignSlice,
{
#[inline]
#[track_caller]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
self.ct_assign(rhs.as_slice(), choice);
}
}
impl<T> CtAssign<[T]> for Vec<T>
where
T: CtAssignSlice,
{
#[inline]
#[track_caller]
fn ct_assign(&mut self, rhs: &[T], choice: Choice) {
self.as_mut_slice().ct_assign(rhs, choice);
}
}
}