Botan  1.10.16
Functions
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 ( cond,
T *  array,
size_t  elems 
)
inline

Definition at line 100 of file ct_utils.h.

References expand_mask(), and select().

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

◆ conditional_copy_mem()

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

Definition at line 85 of file ct_utils.h.

References expand_mask(), and select().

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  }
T expand_mask(T x)
Definition: ct_utils.h:33
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45

◆ expand_mask()

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

Definition at line 33 of file ct_utils.h.

Referenced by Botan::bigint_cnd_abs(), Botan::bigint_cnd_add(), Botan::bigint_cnd_sub(), Botan::bigint_cnd_swap(), cond_zero_mem(), conditional_copy_mem(), and is_zero().

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  }

◆ expand_top_bit()

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

Definition at line 114 of file ct_utils.h.

Referenced by max(), and min().

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

◆ is_equal()

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

Definition at line 63 of file ct_utils.h.

References is_zero().

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

◆ is_less()

template<typename T >
T Botan::CT::is_less ( x,
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  }

◆ is_lte()

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

Definition at line 79 of file ct_utils.h.

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

◆ is_zero()

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

Definition at line 57 of file ct_utils.h.

References expand_mask().

Referenced by is_equal(), Botan::BigInt::is_nonzero(), and Botan::PointGFp::negate().

58  {
59  return ~expand_mask(x);
60  }
T expand_mask(T x)
Definition: ct_utils.h:33

◆ max()

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

Definition at line 120 of file ct_utils.h.

References expand_top_bit(), and select().

Referenced by Botan::OpenPGP_S2K::derive_key(), Botan::dl_work_factor(), Botan::multi_exponentiate(), Botan::operator+(), Botan::BigInt::operator+=(), Botan::operator-(), Botan::BigInt::operator-=(), Botan::operator^(), and Botan::Device_EntropySource::poll().

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
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45

◆ min()

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

◆ select()

template<typename T >
T Botan::CT::select ( mask,
from0,
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.

References select().

52  {
53  return select(CT::expand_mask<ValT>(pred_val), val, static_cast<ValT>(0));
54  }
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45