RustCrypto: Constant-Time Utilities
Constant-time utility library with selection and equality testing support targeting cryptographic
applications. Supports const fn where appropriate. Built on the cmov crate which provides
architecture-specific predication intrinsics. Heavily inspired by the subtle crate.
This crate contains constant-time equivalents of the bool and Option types (Choice and
CtOption respectively), along with traits that can be used in combination with them.
The CtOption type notably provides eagerly evaluated combinator methods (as opposed to the lazily
evaluated combinators on Option) which make it possible to write constant-time code using
an idiomatic Rust style.
This is an experimental next-generation constant-time library inspired by subtle, but for now we
recommend you continue to stick with subtle. We may attempt to get some of the changes in this
library incorporated into subtle for a potential v3.0.
- Pervasive
const fnsupport- Almost all constructors/methods on
Choiceareconst fn Choicecan be constructed using variousconst fnpredicates on integer types, enabling writing constant-timeconst fnlogicCtOptionsupportsconst fnconstructors and*_copiedmethods to access the inner value when it's aCopytype- Macros to act as
CtOptionpseudo-combinators:map!andunwrap_or! - Expanded selection of
CtOptioncombinators that more closely mirrorsstd::option::Option
- Almost all constructors/methods on
- Guaranteed constant-time equality testing and conditional selection on
x86(_64)andaarch64usingasm!implementations in thecmovcrate which call special constant-time CPU instructions with a portable "best effort" fallback on other platforms using bitwise arithmetic andblack_box - No
Copybounds, which means all functionality can work with heap-allocated types in addition to stack-allocated - Expanded selection of traits:
CtFindandCtLookupfor arrays and slices
Many features of this crate are extractions from the crypto-bigint crate, where we implement all
core logic as const fn and needed solutions for implementing constant-time code despite the
unique constraints it imposes.
This crate is designed to operate in constant-time on the following CPU architectures:
arm(32-bit)aarch64riscv32✅️riscv64️thumbv6✅thumbv7(with checks forthumbv7em✅)x86️x86_64✅wasm32
✅: indicates architectures where we use [cargo checkct] / [BINSEC] in CI to
ensure that, for functions with checkct assertions, changes do not introduce
variable-time operations.
Please file a security advisory if you notice non-constant-time CPU instructions (e.g. branches, secret-dependent address calculations) being generated on any of the architectures.
Other architectures are officially unsupported, however a use-at-your-own-risk
portable fallback implementation based on bitwise arithmetic and black_box is
provided without constant-time codegen guarantees. Please do not open issues
about codegen problems on unsupported CPU architectures: they will be closed
noting they're unsupported. If you would like such problems addressed, please
open a pull request that addresses the issue instead.
You can also open an issue to request first-class support for native predication instructions on other architectures we don't currently support.
The implementation contained in this crate has never been independently audited!
USE AT YOUR OWN RISK!
Below are security issues this crate has experienced in the past:
- RUSTSEC-2026-0385: Non-constant-time mask generation and
u128comparisons
MSRV increases are not considered breaking changes and can happen in patch releases.
The crate MSRV accounts for all supported targets and crate feature combinations.
Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.