Skip to content

Commit 76a284a

Browse files
authored
change select to mimic ct_select_u32 behaviour
Before this patch, it was not explicit that `a` was returned when `bit` was zero and `b` was returned when `bit` was one. This patch make `select` behave consistently with respect to `ct_select_u32` by returning `a` when `bit` is zero and `b` otherwise.
1 parent fb22bb5 commit 76a284a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,20 @@ Secret-dependent loop bounds are a special case of this problem.
283283
Timing leaks may be mitigated by introducing dummy operations in branches of the program in order to ensure a constant execution time. It is however more reliable to avoid branchings altogether, for example by implementing the conditional operation as a straight-line program. To select between two inputs `a` and `b` depending on a selection bit `bit`, this can be achieved with the following code:
284284
<!-- from E. Kasper's ECC code, listing 1 in http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en//pubs/archive/37376.pdf -->
285285
<!-- Changed int to unsigned. The C standard guarantees that negation of an n-bit unsigned x is 2^n - x; signed integers may have other interpretations, e.g. one's complement -->
286+
<!-- Changed to return a when bit is non-zero, b otherwise. -->
287+
286288

287289
```C
288-
unsigned select (unsigned a, unsigned b, unsigned bit)
290+
/* Conditionally return a or b depending on whether bit is set */
291+
/* Equivalent to: return bit ? a : b */
292+
unsigned select (unsigned a, unsigned b, unsigned bit)
289293
{
290-
/* -0 = 0, -1 = 0xff....ff */
291-
unsigned mask = - bit;
292-
unsigned ret = mask & (a^b);
293-
ret = ret ^ a;
294-
return ret;
294+
unsigned isnonzero = (bit | -bit) >> (sizeof(unsigned) * 8 - 1);
295+
/* -0 = 0, -1 = 0xff....ff */
296+
unsigned mask = -isnonzero;
297+
unsigned ret = mask & (b^a);
298+
ret = ret ^ b;
299+
return ret;
295300
}
296301
```
297302

0 commit comments

Comments
 (0)