pub trait ConditionallySelectable: Copy {
// Required method
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self;
// Provided methods
fn conditional_assign(&mut self, other: &Self, choice: Choice) { ... }
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice) { ... }
}Expand description
A type which can be conditionally selected in constant time.
This trait also provides generic implementations of conditional assignment and conditional swaps.
Required Methods§
Sourcefn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
Select a or b according to choice.
§Returns
aifchoice == Choice(0);bifchoice == Choice(1).
This function should execute in constant time.
§Example
use subtle::ConditionallySelectable;
let x: u8 = 13;
let y: u8 = 42;
let z = u8::conditional_select(&x, &y, 0.into());
assert_eq!(z, x);
let z = u8::conditional_select(&x, &y, 1.into());
assert_eq!(z, y);Provided Methods§
Sourcefn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
Conditionally assign other to self, according to choice.
This function should execute in constant time.
§Example
use subtle::ConditionallySelectable;
let mut x: u8 = 13;
let mut y: u8 = 42;
x.conditional_assign(&y, 0.into());
assert_eq!(x, 13);
x.conditional_assign(&y, 1.into());
assert_eq!(x, 42);Sourcefn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
Conditionally swap self and other if choice == 1; otherwise,
reassign both unto themselves.
This function should execute in constant time.
§Example
use subtle::ConditionallySelectable;
let mut x: u8 = 13;
let mut y: u8 = 42;
u8::conditional_swap(&mut x, &mut y, 0.into());
assert_eq!(x, 13);
assert_eq!(y, 42);
u8::conditional_swap(&mut x, &mut y, 1.into());
assert_eq!(x, 42);
assert_eq!(y, 13);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl ConditionallySelectable for Ordering
Ordering is #[repr(i8)] where:
impl ConditionallySelectable for Ordering
Ordering is #[repr(i8)] where:
Less=> -1Equal=> 0Greater=> 1
Given this, it’s possible to operate on orderings as if they’re integers, which allows leveraging conditional masking for predication.
Source§impl ConditionallySelectable for i8
impl ConditionallySelectable for i8
Source§impl ConditionallySelectable for i16
impl ConditionallySelectable for i16
Source§impl ConditionallySelectable for i32
impl ConditionallySelectable for i32
Source§impl ConditionallySelectable for i64
impl ConditionallySelectable for i64
Source§impl ConditionallySelectable for i128
impl ConditionallySelectable for i128
Source§impl ConditionallySelectable for u8
impl ConditionallySelectable for u8
Source§impl ConditionallySelectable for u16
impl ConditionallySelectable for u16
Source§impl ConditionallySelectable for u32
impl ConditionallySelectable for u32
Source§impl ConditionallySelectable for u64
impl ConditionallySelectable for u64
Source§impl ConditionallySelectable for u128
impl ConditionallySelectable for u128
§impl<C> ConditionallySelectable for AffinePoint<C>where
C: PrimeCurveParams,
impl<C> ConditionallySelectable for AffinePoint<C>where
C: PrimeCurveParams,
fn conditional_select( a: &AffinePoint<C>, b: &AffinePoint<C>, choice: Choice, ) -> AffinePoint<C>
§impl<C> ConditionallySelectable for ProjectivePoint<C>where
C: PrimeCurveParams,
impl<C> ConditionallySelectable for ProjectivePoint<C>where
C: PrimeCurveParams,
fn conditional_select( a: &ProjectivePoint<C>, b: &ProjectivePoint<C>, choice: Choice, ) -> ProjectivePoint<C>
§impl<Size> ConditionallySelectable for EncodedPoint<Size>where
Size: ModulusSize,
<<Size as ModulusSize>::UncompressedPointSize as ArrayLength<u8>>::ArrayType: Copy,
Available on crate feature subtle only.
impl<Size> ConditionallySelectable for EncodedPoint<Size>where
Size: ModulusSize,
<<Size as ModulusSize>::UncompressedPointSize as ArrayLength<u8>>::ArrayType: Copy,
subtle only.