logo
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

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

The resulting type after applying the & operator.

Performs the & operation. Read more

Performs the &= operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

Performs the |= operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

Performs the ^= operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Select a or b according to choice. Read more

Conditionally assign other to self, according to choice. Read more

Conditionally swap self and other if choice == 1; otherwise, reassign both unto themselves. Read more

Determine if two items are equal. Read more

Formats the value using the given formatter. Read more

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.

Performs the conversion.

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.