logo
pub struct CtOption<T> { /* private fields */ }
Expand description

The CtOption<T> type represents an optional value similar to the Option<T> type but is intended for use in constant time APIs.

Any given CtOption<T> is either Some or None, but unlike Option<T> these variants are not exposed. The is_some() method is used to determine if the value is Some, and unwrap_or() and unwrap_or_else() methods are provided to access the underlying value. The value can also be obtained with unwrap() but this will panic if it is None.

Functions that are intended to be constant time may not produce valid results for all inputs, such as square root and inversion operations in finite field arithmetic. Returning an Option<T> from these functions makes it difficult for the caller to reason about the result in constant time, and returning an incorrect value burdens the caller and increases the chance of bugs.

Implementations

This method is used to construct a new CtOption<T> and takes a value of type T, and a Choice that determines whether the optional value should be Some or not. If is_some is false, the value will still be stored but its value is never exposed.

This returns the underlying value but panics if it is not Some.

This returns the underlying value if it is Some or the provided value otherwise.

This returns the underlying value if it is Some or the value produced by the provided closure otherwise.

Returns a true Choice if this value is Some.

Returns a true Choice if this value is None.

Returns a None value if the option is None, otherwise returns a CtOption enclosing the value of the provided closure. The closure is given the enclosed value or, if the option is None, it is provided a dummy value computed using Default::default().

This operates in constant time, because the provided closure is always called.

Returns a None value if the option is None, otherwise returns the result of the provided closure. The closure is given the enclosed value or, if the option is None, it is provided a dummy value computed using Default::default().

This operates in constant time, because the provided closure is always called.

Returns self if it contains a value, and otherwise returns the result of calling f. The provided function f is always called.

Trait Implementations

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

Two CtOption<T>s are equal if they are both Some and their values are equal, or both None.

Formats the value using the given formatter. Read more

Convert the CtOption<T> wrapper into an Option<T>, depending on whether the underlying is_some Choice was a 0 or a 1 once unwrapped.

Note

This function exists to avoid ending up with ugly, verbose and/or bad handled conversions from the CtOption<T> wraps to an Option<T> or Result<T, E>. This implementation doesn’t intend to be constant-time nor try to protect the leakage of the T since the Option<T> will do it anyways.

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.