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
impl Choice
Sourcepub const fn new(value: u8) -> Self
pub const fn new(value: u8) -> Self
Create a new Choice from the given u8 value, which should be either 0 or 1.
Sourcepub fn to_bool_vartime(self) -> bool
pub fn to_bool_vartime(self) -> bool
Convert Choice into a bool.
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. vartime code that operates on “maybe secret” types which aren’t secrets in a particular context.
Trait Implementations§
Source§impl BitAndAssign for Choice
impl BitAndAssign for Choice
Source§fn bitand_assign(&mut self, rhs: Choice)
fn bitand_assign(&mut self, rhs: Choice)
&= operation. Read moreSource§impl BitOrAssign for Choice
impl BitOrAssign for Choice
Source§fn bitor_assign(&mut self, rhs: Choice)
fn bitor_assign(&mut self, rhs: Choice)
|= operation. Read moreSource§impl BitXorAssign for Choice
impl BitXorAssign for Choice
Source§fn bitxor_assign(&mut self, rhs: Choice)
fn bitxor_assign(&mut self, rhs: Choice)
^= operation. Read moreimpl 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)