subtle

Struct Choice

Source
pub struct Choice(/* private fields */);
Expand description

The Choice struct represents a choice for use in conditional assignment.

It is a wrapper around a u8, which should have the value either 1 (true) or 0 (false).

The conversion from u8 to Choice passes the value through an optimization barrier, as a best-effort attempt to prevent the compiler from inferring that the Choice value is a boolean. This strategy is based on Tim Maclean’s work on rust-timing-shield, which attempts to provide a more comprehensive approach for preventing software side-channels in Rust code.

The Choice struct implements operators for AND, OR, XOR, and NOT, to allow combining Choice values. These operations do not short-circuit.

Implementations§

Source§

impl Choice

Source

pub fn unwrap_u8(&self) -> u8

Unwrap the Choice wrapper to reveal the underlying u8.

§Note

This function only exists as an escape hatch for the rare case where it’s not possible to use one of the subtle-provided trait impls.

To convert a Choice to a bool, use the From implementation instead.

Trait Implementations§

Source§

impl BitAnd for Choice

Source§

type Output = Choice

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Choice) -> Choice

Performs the & operation. Read more
Source§

impl BitAndAssign for Choice

Source§

fn bitand_assign(&mut self, rhs: Choice)

Performs the &= operation. Read more
Source§

impl BitOr for Choice

Source§

type Output = Choice

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Choice) -> Choice

Performs the | operation. Read more
Source§

impl BitOrAssign for Choice

Source§

fn bitor_assign(&mut self, rhs: Choice)

Performs the |= operation. Read more
Source§

impl BitXor for Choice

Source§

type Output = Choice

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Choice) -> Choice

Performs the ^ operation. Read more
Source§

impl BitXorAssign for Choice

Source§

fn bitxor_assign(&mut self, rhs: Choice)

Performs the ^= operation. Read more
Source§

impl Clone for Choice

Source§

fn clone(&self) -> Choice

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ConditionallySelectable for Choice

Source§

fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self

Select a or b according to choice. Read more
Source§

fn conditional_assign(&mut self, other: &Self, choice: Choice)

Conditionally assign other to self, according to choice. Read more
Source§

fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)

Conditionally swap self and other if choice == 1; otherwise, reassign both unto themselves. Read more
Source§

impl ConstantTimeEq for Choice

Source§

fn ct_eq(&self, rhs: &Choice) -> Choice

Determine if two items are equal. Read more
Source§

impl Debug for Choice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Choice> for bool

Source§

fn from(source: Choice) -> bool

Convert the Choice wrapper into a bool, depending on whether the underlying u8 was a 0 or a 1.

§Note

This function exists to avoid having higher-level cryptographic protocol implementations duplicating this pattern.

The intended use case for this conversion is at the end of a higher-level primitive implementation: for example, in checking a keyed MAC, where the verification should happen in constant-time (and thus use a Choice) but it is safe to return a bool at the end of the verification.

Source§

impl From<u8> for Choice

Source§

fn from(input: u8) -> Choice

Converts to this type from the input type.
Source§

impl Not for Choice

Source§

type Output = Choice

The resulting type after applying the ! operator.
Source§

fn not(self) -> Choice

Performs the unary ! operation. Read more
Source§

impl Copy for Choice

Auto Trait Implementations§

§

impl Freeze for Choice

§

impl RefUnwindSafe for Choice

§

impl Send for Choice

§

impl Sync for Choice

§

impl Unpin for Choice

§

impl UnwindSafe for Choice

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.