utils ct: replace black_box with a volatile optimization barrier in ct_eq_bytes and friends - #131
Open
ounsworth wants to merge 1 commit into
Open
utils ct: replace black_box with a volatile optimization barrier in ct_eq_bytes and friends#131ounsworth wants to merge 1 commit into
ounsworth wants to merge 1 commit into
Conversation
…t_eq_bytes and friends Survey of other libraries (from their current upstream sources): libsodium's `sodium_memcmp` uses a `volatile` accumulator and volatile input pointers, and subtle's `Choice::from` is an `#[inline(never)]` `read_volatile`, so both place the barrier inside the loop per byte. `constant_time_eq` and RustCrypto's `cmov` chunk into machine words and apply an inline-asm barrier (or `cmov`/`csel`) per word, which is the shape adopted here. Graviola writes the whole byte loop in inline asm on x86_64 and aarch64. OpenSSL's `CRYPTO_memcmp` reads through volatile pointers only, and BoringSSL's uses no barrier at all. Assembly was inspected by Claude from release builds on x86_64, i686, thumbv7em (Cortex-M4), riscv32imac, wasm32, msp430 (16-bit) and avr-none (8-bit): in every case the loop is load, xor, or, one native-width store to a stack slot and one reload, with a byte-wise tail, no `bcmp`/`memcmp` call and no data-dependent branch, and the final `== 0` tests the volatile-loaded value. Integration tests now sweep every length from 0 to 40 with a single bit flipped at every position, covering the word and tail paths on both sides of every boundary for 2, 4 and 8-byte words. Co-Authored-By: Claude Fable 5
ounsworth
force-pushed
the
feature/ct_eq
branch
from
September 12, 2026 01:28
d3ebc7c to
e89d8fd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Survey of other libraries (from their current upstream sources): libsodium's
sodium_memcmpuses avolatileaccumulator and volatile input pointers, and subtle'sChoice::fromis an#[inline(never)]read_volatile, so both place the barrier inside the loop per byte.constant_time_eqand RustCrypto'scmovchunk into machine words and apply an inline-asm barrier (orcmov/csel) per word, which is the shape adopted here. Graviola writes the whole byte loop in inline asm on x86_64 and aarch64. OpenSSL'sCRYPTO_memcmpreads through volatile pointers only, and BoringSSL's uses no barrier at all.Assembly was inspected by Claude from release builds on x86_64, i686, thumbv7em (Cortex-M4), riscv32imac, wasm32, msp430 (16-bit) and avr-none (8-bit): in every case the loop is load, xor, or, one native-width store to a stack slot and one reload, with a byte-wise tail, no
bcmp/memcmpcall and no data-dependent branch, and the final== 0tests the volatile-loaded value.Integration tests now sweep every length from 0 to 40 with a single bit flipped at every position, covering the word and tail paths on both sides of every boundary for 2, 4 and 8-byte words.
Co-Authored-By: Claude Fable 5
Closes #128