Botan 1.10.17
Botan::CT Namespace Reference

Functions

template<typename T>
void cond_zero_mem (T cond, T *array, size_t elems)
template<typename T>
void conditional_copy_mem (T value, T *to, const T *from0, const T *from1, size_t elems)
template<typename T>
expand_mask (T x)
template<typename T>
expand_top_bit (T a)
template<typename T>
is_equal (T x, T y)
template<typename T>
is_less (T x, T y)
template<typename T>
is_lte (T x, T y)
template<typename T>
is_zero (T x)
template<typename T>
max (T a, T b)
template<typename T>
min (T a, T b)
template<typename T>
select (T mask, T from0, T from1)
template<typename PredT, typename ValT>
ValT val_or_zero (PredT pred_val, ValT val)

Function Documentation

◆ cond_zero_mem()

template<typename T>
void Botan::CT::cond_zero_mem ( T cond,
T * array,
size_t elems )
inline

Definition at line 100 of file ct_utils.h.

103 {
104 const T mask = CT::expand_mask(cond);
105 const T zero(0);
106
107 for(size_t i = 0; i != elems; ++i)
108 {
109 array[i] = CT::select(mask, zero, array[i]);
110 }
111 }
T expand_mask(T x)
Definition ct_utils.h:33
T select(T mask, T from0, T from1)
Definition ct_utils.h:45

References cond_zero_mem(), expand_mask(), and select().

Referenced by cond_zero_mem().

◆ conditional_copy_mem()

template<typename T>
void Botan::CT::conditional_copy_mem ( T value,
T * to,
const T * from0,
const T * from1,
size_t elems )
inline

Definition at line 85 of file ct_utils.h.

90 {
91 const T mask = CT::expand_mask(value);
92
93 for(size_t i = 0; i != elems; ++i)
94 {
95 to[i] = CT::select(mask, from0[i], from1[i]);
96 }
97 }

References conditional_copy_mem(), expand_mask(), and select().

Referenced by conditional_copy_mem().

◆ expand_mask()

template<typename T>
T Botan::CT::expand_mask ( T x)
inline

Definition at line 33 of file ct_utils.h.

34 {
35 T r = x;
36 // First fold r down to a single bit
37 for(size_t i = 1; i != sizeof(T)*8; i *= 2)
38 r |= r >> i;
39 r &= 1;
40 r = ~(r - 1);
41 return r;
42 }

References expand_mask().

Referenced by Botan::bigint_cnd_abs(), Botan::bigint_cnd_add(), Botan::bigint_cnd_sub(), Botan::bigint_cnd_swap(), cond_zero_mem(), conditional_copy_mem(), expand_mask(), expand_top_bit(), is_less(), is_lte(), is_zero(), and val_or_zero().

◆ expand_top_bit()

template<typename T>
T Botan::CT::expand_top_bit ( T a)
inline

Definition at line 114 of file ct_utils.h.

115 {
116 return expand_mask<T>(a >> (sizeof(T)*8-1));
117 }

References expand_mask(), and expand_top_bit().

Referenced by expand_top_bit(), max(), and min().

◆ is_equal()

template<typename T>
T Botan::CT::is_equal ( T x,
T y )
inline

Definition at line 63 of file ct_utils.h.

64 {
65 return is_zero(x ^ y);
66 }
T is_zero(T x)
Definition ct_utils.h:57

References is_equal(), and is_zero().

Referenced by Botan::BigInt::const_time_lookup(), and is_equal().

◆ is_less()

template<typename T>
T Botan::CT::is_less ( T x,
T y )
inline

Definition at line 69 of file ct_utils.h.

70 {
71 /*
72 This expands to a constant time sequence with GCC 5.2.0 on x86-64
73 but something more complicated may be needed for portable const time.
74 */
75 return expand_mask<T>(x < y);
76 }

References expand_mask(), and is_less().

Referenced by is_less().

◆ is_lte()

template<typename T>
T Botan::CT::is_lte ( T x,
T y )
inline

Definition at line 79 of file ct_utils.h.

80 {
81 return expand_mask<T>(x <= y);
82 }

References expand_mask(), and is_lte().

Referenced by is_lte().

◆ is_zero()

template<typename T>
T Botan::CT::is_zero ( T x)
inline

Definition at line 57 of file ct_utils.h.

58 {
59 return ~expand_mask(x);
60 }

References expand_mask(), and is_zero().

Referenced by is_equal(), and is_zero().

◆ max()

template<typename T>
T Botan::CT::max ( T a,
T b )
inline

Definition at line 120 of file ct_utils.h.

121 {
122 const T a_larger = b - a; // negative if a is larger
123 return select(expand_top_bit(a), a, b);
124 }
T expand_top_bit(T a)
Definition ct_utils.h:114

References expand_top_bit(), max(), and select().

Referenced by max().

◆ min()

template<typename T>
T Botan::CT::min ( T a,
T b )
inline

Definition at line 127 of file ct_utils.h.

128 {
129 const T a_larger = b - a; // negative if a is larger
130 return select(expand_top_bit(b), b, a);
131 }

References expand_top_bit(), min(), and select().

Referenced by min().

◆ select()

template<typename T>
T Botan::CT::select ( T mask,
T from0,
T from1 )
inline

◆ val_or_zero()

template<typename PredT, typename ValT>
ValT Botan::CT::val_or_zero ( PredT pred_val,
ValT val )
inline

Definition at line 51 of file ct_utils.h.

52 {
53 return select(CT::expand_mask<ValT>(pred_val), val, static_cast<ValT>(0));
54 }

References expand_mask(), select(), and val_or_zero().

Referenced by val_or_zero().