ctutils/lib.rs
1#![no_std]
2#![doc = include_str!("../README.md")]
3#![doc(
4 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
5 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
6)]
7#![deny(unsafe_code)]
8#![warn(
9 clippy::mod_module_files,
10 missing_docs,
11 missing_debug_implementations,
12 missing_copy_implementations,
13 rust_2018_idioms,
14 trivial_casts,
15 trivial_numeric_casts,
16 unused_qualifications
17)]
18#![cfg_attr(not(test), warn(clippy::unwrap_used))]
19
20//! # API Design
21//!
22//! ## [`Choice`]: constant-time analogue for [`bool`]
23//! Values of this type are one of either [`Choice::FALSE`] or [`Choice::TRUE`].
24//!
25//! To achieve constant-time operation, `Choice` is ultimately used in combination with special
26//! CPU-specific constant-time predication instructions implemented by the [`cmov`] crate
27//! (with a portable "best effort" fallback that cannot provide guarantees).
28//!
29//! It additionally uses various methods to hint to the compiler that it should avoid inserting
30//! branches based on its value where it otherwise would if `bool` were used instead, but cannot
31//! provide guarantees in this regard.
32//!
33//! ## [`CtOption`]: constant-time analogue for [`Option`]
34//! The core `Option` type is typically great for representing the conditional absence or presence
35//! of a value, and provides a number of handy combinators for operating on them.
36//!
37//! However, it has a rather fundamental flaw when constant-time is desirable: its combinators are
38//! lazily evaluated. To ensure constant-time operation, all combinators must be eagerly evaluated
39//! so they aren't conditionally executed based on the value's presence.
40//!
41//! `CtOption` instead carries a `Choice` along with a value, which makes it possible to do
42//! something it isn't with `Option`: evaluate combinators eagerly instead of lazily, running the
43//! same functions regardless of the value's effective presence or absence.
44//!
45//! ## [`CtEq`]: constant-time analogue for [`PartialEq`]/[`Eq`]
46//! Equality testing often short circuits for performance reasons, but when comparing values in
47//! constant-time such short-circuiting is forbidden.
48//!
49//! The `CtEq` trait is a replacement for these scenarios. It's impl'd for several core types
50//! including unsigned and signed integers as well as slices and arrays. It returns a `Choice`
51//! as opposed to a `bool`], following the standard practice in this crate.
52//!
53//! *NOTE: for `subtle` users, this is the equivalent of the `ConstantTimeEq` trait*
54//!
55//! ## [`CtSelect`]: constant-time [predication]
56//! Predication in computer architecture describes methods for conditionally modifying state
57//! using non-branch instructions which perform conditional modifications based on a *predicate*
58//! or boolean value, in the design of this library a `Choice`.
59//!
60//! The `CtSelect` trait provides methods for performing conditional moves, either conditionally
61//! modifying a value in-place, or selecting from two different inputs and returning a new one.
62//!
63//! *NOTE: for `subtle` users, this is the equivalent of the `ConditionallySelectable` trait*
64//!
65//! [predication]: https://en.wikipedia.org/wiki/Predication_(computer_architecture)
66//!
67//! # [`subtle`] interop
68//!
69//! When the `subtle` feature of this crate is enabled, bidirectional [`From`] impls are available
70//! for the following types:
71//!
72//! - [`Choice`] <=> [`subtle::Choice`]
73//! - [`CtOption`] <=> [`subtle::CtOption`]
74//!
75//! This makes it possible to use `ctutils` in a codebase where other dependencies are using
76//! `subtle`.
77
78mod choice;
79mod ct_option;
80mod traits;
81
82pub use choice::Choice;
83pub use ct_option::CtOption;
84pub use traits::{ct_eq::CtEq, ct_gt::CtGt, ct_lt::CtLt, ct_select::CtSelect};