#constant-time #intrinsics #conditional #instructions #move #cpu

no-std cmov

Conditional move CPU intrinsics which are guaranteed 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 CSEL on AArch64

5 unstable releases

0.3.1 Oct 14, 2023
0.3.0 Apr 3, 2023
0.2.0 Feb 26, 2023
0.1.1 Mar 2, 2022
0.0.0 Feb 11, 2022

#2627 in Cryptography

Download history 2/week @ 2024-07-20 58/week @ 2024-07-27 5/week @ 2024-08-03 47/week @ 2024-08-10 39/week @ 2024-08-17 20/week @ 2024-08-24 19/week @ 2024-08-31 23/week @ 2024-09-07 14/week @ 2024-09-14 64/week @ 2024-09-21 103/week @ 2024-09-28 22/week @ 2024-10-05 26/week @ 2024-10-12 22/week @ 2024-10-19 16/week @ 2024-10-26 49/week @ 2024-11-02

115 downloads per month
Used in lockstitch

Apache-2.0 OR MIT

23KB
462 lines

RustCrypto: CMOV (Conditional Move)

Crate Docs Build Status Apache 2.0/MIT Licensed MSRV

Conditional move CPU intrinsics which are guaranteed 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.

Documentation

About

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.

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.

This crate provides wrappers for the CMOV/CSEL instructions implemented using inline assembly as stabilized in Rust 1.59. This 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.

Supported target architectures

This crate provides guaranteed constant-time operation using inline assembly on the following CPU architectures:

  • x86 (CMOVZ, CMOVNZ)
  • x86_64 (CMOVZ, CMOVNZ)
  • aarch64 (CSEL)

On other target architectures, a "best effort" portable fallback implementation based on bitwise arithmetic is used instead. However, we cannot guarantee that this implementation generates branch-free code.

It's possible to extend constant-time guarantees to other CPU architectures. Please open an issue with your desired CPU architecture if this interests you.

Minimum Supported Rust Version

Rust 1.60 or newer.

In the future, we reserve the right to change MSRV (i.e. MSRV is out-of-scope for this crate's SemVer guarantees), however when we do it will be accompanied by a minor version bump.

License

Licensed under either of:

at your option.

Contribution

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.

No runtime deps