#constant-time #utilities #crypto

no-std subtle-ng

Pure-Rust traits and utilities for constant-time cryptographic implementations

3 stable releases

Uses old Rust 2015

2.5.0 Sep 16, 2021
2.4.1 Jan 28, 2021
2.4.0 Jan 14, 2021

#1160 in Cryptography

Download history 57546/week @ 2024-01-23 63214/week @ 2024-01-30 60010/week @ 2024-02-06 63719/week @ 2024-02-13 64272/week @ 2024-02-20 58704/week @ 2024-02-27 54223/week @ 2024-03-05 56924/week @ 2024-03-12 60803/week @ 2024-03-19 51119/week @ 2024-03-26 63636/week @ 2024-04-02 61515/week @ 2024-04-09 58671/week @ 2024-04-16 58707/week @ 2024-04-23 47408/week @ 2024-04-30 39170/week @ 2024-05-07

215,253 downloads per month
Used in 243 crates (9 directly)


377 lines


Pure-Rust traits and utilities for constant-time cryptographic implementations.

It consists of a Choice type, and a collection of traits using Choice instead of bool which are intended to execute in constant-time. The Choice type is a wrapper around a u8 that holds a 0 or 1.

subtle-ng = "2.5"

To switch from the previous subtle crate without changing your code, use the following:

subtle = { package = "subtle-ng", version = "2.4" }

This crate continues the subtle series under a different package name. Unfortunately, one of the maintainers of the previous crate seized control of the dalek-cryptography GitHub organization and the subtle and curve25519-dalek crates by silently removing all other co-maintainers.

This crate represents a “best-effort” attempt, since side-channels are ultimately a property of a deployed cryptographic system including the hardware it runs on, not just of software.

The traits are implemented using bitwise operations, and should execute in constant time provided that a) the bitwise operations are constant-time and b) the bitwise operations are not recognized as a conditional assignment and optimized back into a branch.

For a compiler to recognize that bitwise operations represent a conditional assignment, it needs to know that the value used to generate the bitmasks is really a boolean i1 rather than an i8 byte value. In an attempt to prevent this refinement, the crate tries to hide the value of a Choice's inner u8 by passing it through a volatile read. For more information, see the About section below.

Versions prior to 2.2 recommended use of the nightly feature to enable an optimization barrier; this is not required in versions 2.2 and above.

Note: the subtle crate contains debug_asserts to check invariants during debug builds. These invariant checks involve secret-dependent branches, and are not present when compiled in release mode. This crate is intended to be used in release mode.


Documentation is available here.

Minimum Supported Rust Version

Rust 1.54 or higher.

Minimum supported Rust version can be changed in the future, but it will be done with a minor version bump.


This library aims to be the Rust equivalent of Go’s crypto/subtle module.

The optimization barrier in impl From<u8> for Choice was based on Tim Maclean's work on rust-timing-shield, which attempts to provide a more comprehensive approach for preventing software side-channels in Rust code.

subtle is authored by isis agora lovecruft and Henry de Valence.


This code is a low-level library, intended for specific use-cases implementing cryptographic protocols. It represents a best-effort attempt to protect against some software side-channels. Because side-channel resistance is not a property of software alone, but of software together with hardware, any such effort is fundamentally limited.


No runtime deps