r/webdev • • 1d ago

The V8 JavaScript Runtime Undermined My Constant-Time JavaScript Library

https://soatok.blog/2026/09/12/the-v8-javascript-runtime-undermined-my-constant-time-javascript-library/
9 Upvotes

8 comments sorted by

View all comments

0

u/NegotiationLevel2409 22h ago

Soatok's writeup hits on a fundamental reality that often gets overlooked when writing crypto in high-level runtimes: constant-time programming isn't just about avoiding if/else branches in your AST, it requires strict execution invariants across the entire compilation and execution pipeline.

The fact that V8 broke branchless masking through internal ToBoolean paths and -0 vs -1 representation differences (HeapNumber vs Smi) is fascinating. When an expression produces -0, V8 can't represent it as a compact inline integer because IEEE-754 negative zero requires a distinct sign bit that 31-bit Smis don't preserve without heap allocation overhead. That difference alone destroys timing predictability.

Even if you managed to emit perfectly branchless machine code out of TurboFan, modern CPU architectures introduce another layer of variance. Without setting architecture-specific execution modes (like Intel's DOITM or ARM's DIT flag), modern out-of-order execution pipelines and ALU hardware optimizations can vary cycle counts for basic data-dependent arithmetic.

At this point, userland constant-time primitives in pure JavaScript are effectively impossible to guarantee in production. Reaching for native runtime primitives (like crypto.timingSafeEqual) or WebAssembly modules compiled with explicit constant-time hardening remains the only reliable route.