Struct subtle::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

§

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

§

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

§

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§

fn ct_ne(&self, other: &Self) -> Choice

Determine if two items are NOT 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

§

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> 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>,

§

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>,

§

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.