pub struct Choice(_);
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
Trait Implementations
sourceimpl BitAndAssign<Choice> for Choice
impl BitAndAssign<Choice> for Choice
sourcefn bitand_assign(&mut self, rhs: Choice)
fn bitand_assign(&mut self, rhs: Choice)
Performs the &=
operation. Read more
sourceimpl BitOrAssign<Choice> for Choice
impl BitOrAssign<Choice> for Choice
sourcefn bitor_assign(&mut self, rhs: Choice)
fn bitor_assign(&mut self, rhs: Choice)
Performs the |=
operation. Read more
sourceimpl BitXorAssign<Choice> for Choice
impl BitXorAssign<Choice> for Choice
sourcefn bitxor_assign(&mut self, rhs: Choice)
fn bitxor_assign(&mut self, rhs: Choice)
Performs the ^=
operation. Read more
sourceimpl ConditionallySelectable for Choice
impl ConditionallySelectable for Choice
sourceimpl ConstantTimeEq for Choice
impl ConstantTimeEq for Choice
sourceimpl From<Choice> for bool
impl From<Choice> for bool
sourcefn from(source: Choice) -> bool
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.
impl Copy for Choice
Auto Trait Implementations
impl RefUnwindSafe for Choice
impl Send for Choice
impl Sync for Choice
impl Unpin for Choice
impl UnwindSafe for Choice
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more