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(self) -> bool
pub fn to_bool(self) -> bool
Convert Choice into a bool.
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.
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 moreSource§impl CtSelect for Choice
impl CtSelect for Choice
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> 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)