Choice

Struct Choice 

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

Constant-time analogue of bool providing a “best effort” optimization barrier.

Attempts to hint to the compiler and its codegen backends that optimizations should not be applied which depend on a value.

This is used as a “belt-and-suspenders” defense in addition to mechanisms like constant-time predication intrinsics provided by the cmov crate, and is never expected to be the only line of defense.

Implementations§

Source§

impl Choice

Source

pub const FALSE: Self

Equivalent of false.

Source

pub const TRUE: Self

Equivalent of true.

Source

pub const fn new(value: u8) -> Self

Create a new Choice from the given u8 value, which should be either 0 or 1.

Source

pub fn to_bool(self) -> bool

Convert Choice into a bool.

Security Warning

Using this function will introduce timing variability, since computing this at all currently requires a branch.

This is intended to be used as either the one and only branch at the end of a constant-time operation to e.g. differentiate between success and failure, or in contexts where constant-time doesn’t matter, e.g. variable-time code that operates on “maybe secret” types which aren’t secrets in a particular context.

If you are trying to use this in the context of a constant-time operation, be warned that the small amount of timing variability it introduces can potentially be exploited. Whenever possible, prefer fully constant-time approaches instead.

Source

pub fn to_u8(self) -> u8

Convert Choice to a u8, attempting to apply a “best effort” optimization barrier.

Source

pub const fn and(self, rhs: Choice) -> Choice

Apply an and conditional to the given Choices.

Source

pub const fn or(self, rhs: Choice) -> Choice

Apply an or conditional to the given Choices.

Source

pub const fn xor(self, rhs: Choice) -> Choice

Apply an xor conditional to the given Choices.

Source

pub const fn not(self) -> Choice

Compute the boolean inverse of self.

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 duplicate of the value. Read more
1.0.0§

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

Performs copy-assignment from source. Read more
Source§

impl CtEq for Choice

Source§

fn ct_eq(&self, other: &Self) -> Self

Equality
Source§

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

Inequality
Source§

impl CtSelect for Choice

Source§

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

Select between self and other based on choice, returning a copy of the value. Read more
Source§

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

Conditionally assign other to self if choice is Choice::TRUE.
Source§

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

Conditionally swap self and other if choice is Choice::TRUE.
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 Choice

Available on crate feature subtle only.
Source§

fn from(choice: Choice) -> Choice

Converts to this type from the input type.
Source§

impl From<Choice> for Choice

Available on crate feature subtle only.
Source§

fn from(choice: Choice) -> Choice

Converts to this type from the input type.
Source§

impl From<Choice> for bool

Source§

fn from(choice: Choice) -> bool

Converts to this type from the input type.
Source§

impl From<Choice> for u8

Source§

fn from(choice: Choice) -> u8

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§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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.
§

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

Performs the conversion.