Skip to content

constant_time_equal reads 64-bit words, its accumulator a register - #4

Merged
pinhopro merged 1 commit into
mainfrom
secure/ct-equal-words
Dec 4, 2026
Merged

pinhopro merged 1 commit into
mainfrom
secure/ct-equal-words

Conversation

@pinhopro

@pinhopro pinhopro commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

constant_time_equal now reads 8-byte words through memcpy at any alignment and ORs their XOR into a 64-bit register through value_barrier at each word; the tail goes byte by byte and the 64 bits fold to one byte before the branchless reduction.

Why: crypto's constant-time lane on wasip2 failed on crypto#24 (run 47265884663) and on webcpp main (run 47116123227) with constant_time_equal at |t| 90 to 150. Probes on hosted runners placed it on AMD EPYC 9V74 hosts under wasmtime 50.0.1 alone, in the comparison's own reads (not the harness layout, the input pattern or the verdict); every other CPU and native read below 4. d7bbab4 (register accumulator, byte by byte) narrowed it to 5.5 to 10.0 on the 9V74; this commit reads 0.8 to 4.0 over 18 runs on three 9V74 runners.

Tests: a new unit test covers every length 0 to 40 at every offset 0 to 7 against every one-bit flip. Local: wasip2 and native constant-time lanes pass, unit tests native/no-exceptions/wasip2, macOS clang-tidy and Linux lint clean.

🤖 Generated with Claude Code

…ister

crypto's constant-time lane on wasip2 failed on crypto#24 (run 47265884663): constant_time_equal
read |t| 115.04, 120.80 and 146.54, on secure 44b1557, which webcpp's main still pins. Probes on
hosted runners (2026-12-04, wasmtime 50.0.1) placed it on the AMD EPYC 9V74 alone, under wasmtime
alone, and in the comparison's own reads, not in the harness: on six 9V74 runners the case read
|t| 30 to 150 whatever the layout (the CI's offsets, 64-byte aligned, a cache line split at 1 or 8
bytes), for zeros or a nonzero pattern against random bytes, for a pair that differs only in its
last or first byte against random bytes, and with the verdict discarded; its A/A control and a run
that writes the classes' bytes without comparing them read 1 to 3. Natively, and on the Xeon
6975P-C, Platinum 8370C and 8573C, EPYC 7763 and 9V45, every variant read below 4.

secure d7bbab4 (the accumulator a register through value_barrier, byte by byte) brought the 9V74
down to 5.5 to 10.0 in the probe and 2.4 to 9.9 in dudect_bearssl's case: below the threshold,
not by much. Reading 64-bit words instead, the accumulator a register through value_barrier at
each word, read 1.4 to 4.2 on three 9V74 runners (nine runs, its A/A control 1.2 to 2.9), and
dudect_bearssl's case on this commit read 0.8 to 4.0 in eighteen runs on three 9V74 runners.

The comparison now reads eight bytes of each sequence at a time through memcpy, at any
alignment, ORs their XOR into a 64-bit register through constant_time::value_barrier, then the
bytes past the last whole word one at a time, and folds the 64 bits into one byte before the
branchless reduction. A test compares every length from 0 to 40 at every offset from 0 to 7
against each one-bit flip of every byte.
@pinhopro
pinhopro merged commit a88fc50 into main Dec 4, 2026
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant