constant-time-select ( flag a b -- a/b )
Cryptographic utilities

Prev:constant-time-zero? ( seq -- ? )


Vocabulary
crypto.utils

Inputs
flag0 or 1
ainteger
binteger


Outputs
a/ba if flag=1, b if flag=0


Word description
Selects between two integers in constant time without branching. If flag is 1, returns a. If flag is 0, returns b. Uses arithmetic masking to avoid conditional branches.

Warning
Flag MUST be exactly 0 or 1. Other values produce undefined results.


Examples
USING: crypto.utils ; 1 42 99 constant-time-select
! => 42

USING: crypto.utils ; 0 42 99 constant-time-select
! => 99


Definition


:: constant-time-select ( flag a b -- a/b )
flag neg :> mask a mask bitand b mask bitnot bitand bitor ;