C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Constant-time selection primitive following memset_explicit precedent

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Tue, 9 Dec 2025 10:58:48 +0100
What you want to avoid is that the optimizer creates two paths:   from v = select_explicit(b, A, B);   to if (b)     v = select_explicit(true, A, B); else     v = select_explicit(false, A, B);   and suddenly the select argument is a compile-time constant and the select_explicit can be eliminated.   Perhaps it wouldn't do it, perhaps it would. b is just a normal bool variable.   -----Ursprüngliche Nachricht----- Von:Shivam Kunwar <shivam.kunwar_at_[hidden]> On 2025-12-09 14:53, Sebastian Wittmeier via Std-Proposals wrote: > How would the optimizer handle select_explicit? > > If it knows the bool, can it skip the computation? > > If it just needs to know, whether it is one of the two results, can it > skip? > > If it does not need the result at all, can it skip? > > If the select_explicit is used twice, can it skip one? > >> -----Ursprüngliche Nachricht--'' 1. If it knows the bool, can it skip the computation? Yes I think so. If the computation is a compile time constant, there's no secret to leak. The timing concern only exists when the condition could be secret at runtime. And this is very much analogous to how the memset_explicit can still be optimized if the compiler can prove the memory is never accessible. 2. If it just needs to know whether it is one of the two results, can it skip? I'd say yes, if the compiler can prove both a and b are the same value, it can return either, no timing information leaks because there's nothing to distinguish. 3. If it does not need the result at all, can it skip? this one is trickier imo, but i still think yes, so if the result is truly dead (never observed), eliminating it shouldn't leak anything, but also i could see an argument for "no" to this , based on the programmer explcitly asked for this operation to happen. for memset_explicit, the standard says the store "is always performed", but that's because the side effect(clearing memory) is the whole point, and for select_explicit, the point is the result, not a side effect, so if nobody observes the result, arguably there's nothing to protect. 4. If the select_explicit is used twice, can it skip one? if it's the exact same operation (same operands, same condition), common subexpression elimination should be fine.

Received on 2025-12-09 10:13:31