RustCrypto: CMOV (Conditional Move)
Conditional move CPU intrinsics which are guaranteed on major platforms to execute in constant-time and not be rewritten as branches by the compiler.
Provides wrappers for the CMOV family of instructions on x86/x86_64 and the CSEL
instruction on aarch64 CPUs, along with a portable fallback implementation for other CPU
architectures.
Conditional move intrinsics provide predication which allows selection of one or more values without using branch instructions, thus making the selection constant-time with respect to the values, and not subject to CPU execution features which might introduce timing or other microarchitectural sidechannels introduced by branch prediction or other speculative execution features.
This crate provides wrappers for the CMOV/CSEL instructions implemented using
inline asm!, which means the implementation is a black box that will not be
rewritten by e.g. LLVM's architecture-specific lowerings, such as the
x86-cmov-conversion pass.
This is a low level crate who's API is designed to make it easy to build portable abstractions across various CPU architectures and is not intended for general purpose use.
See the ctutils crate for a higher-level API build on top of cmov.
This crate is designed to operate in constant-time on the following CPU architectures:
arm(32-bit) ⚙️aarch64⚙️riscv32⚙️✅riscv64⚙️thumbv6⚙️✅thumbv7⚙️(with checks forthumbv7em✅)x86⚙️x86_64⚙️✅wasm32
⚙️: indicates platforms with asm!-backed constant-time operations
✅: 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.
Intel has confirmed that all extant CPUs implement the CMOV family of instructions in constant-time, and that this property will hold for future Intel CPUs as well.
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-0003: Non-constant-time code generation on ARM32 targets
- GHSA-3rjw-m598-pq24:
Cmov/CmovEqon ARM64 can produce wrong results if high-bits of registers are set
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, excluding explicitly unstable features.
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.