MCPcopy Create free account
hub / github.com/RustCrypto/utils / CtSelect

Interface CtSelect

ctutils/src/traits/ct_select.rs:24–39  ·  view source on GitHub ↗

Constant-time selection: choose between two values based on a given [`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`], [`No

Source from the content-addressed store, hash-verified

22/// - `[T; N]` where `T` impls [`CtSelectArray`], which the previously mentioned types all do,
23/// as well as any type which impls [`Clone`] + [`CtAssignSlice`] + [`CtSelect`].
24pub trait CtSelect: Sized {
25 /// Select between `self` and `other` based on `choice`, returning a copy of the value.
26 ///
27 /// # Returns
28 /// - `self` if `choice` is [`Choice::FALSE`].
29 /// - `other` if `choice` is [`Choice::TRUE`].
30 #[must_use]
31 fn ct_select(&self, other: &Self, choice: Choice) -> Self;
32
33 /// Conditionally swap `self` and `other` if `choice` is [`Choice::TRUE`].
34 fn ct_swap(&mut self, other: &mut Self, choice: Choice) {
35 let tmp = self.ct_select(other, choice);
36 *other = Self::ct_select(other, self, choice);
37 *self = tmp;
38 }
39}
40
41/// Implementing this trait enables use of the [`CtSelect`] trait to construct `[T; N]` where `T`
42/// is the `Self` type implementing the trait, via a blanket impl.

Callers

nothing calls this directly

Implementers 2

ct_option.rsctutils/src/ct_option.rs
ct_select.rsctutils/src/traits/ct_select.rs

Calls

no outgoing calls

Tested by

no test coverage detected