Botan 3.9.0
Crypto and TLS for C&
Botan::CT::Choice Class Referencefinal

#include <ct_utils.h>

Public Member Functions

constexpr bool as_bool () const
constexpr Choice (Choice &&other)=default
constexpr Choice (const Choice &other)=default
constexpr Choice operator! () const
constexpr Choice operator!= (const Choice &other) const
constexpr Choice operator&& (const Choice &other) const
constexpr Choiceoperator= (Choice &&other) noexcept=default
constexpr Choiceoperator= (const Choice &other) noexcept=default
constexpr Choice operator== (const Choice &other) const
constexpr Choice operator|| (const Choice &other) const
constexpr uint32_t value () const
 Return the masked value.
constexpr ~Choice ()=default

Static Public Member Functions

template<typename T>
requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
static constexpr Choice from_int (T v)
static constexpr Choice from_mask (uint32_t v)
static constexpr Choice no ()
static constexpr Choice yes ()

Detailed Description

A Choice is used for constant-time conditionals.

Internally it always is either |0| (all 0 bits) or |1| (all 1 bits) and measures are taken to block compilers from reasoning about the expected value of a Choice.

Definition at line 307 of file ct_utils.h.

Constructor & Destructor Documentation

◆ Choice() [1/2]

Botan::CT::Choice::Choice ( const Choice & other)
constexprdefault

◆ Choice() [2/2]

Botan::CT::Choice::Choice ( Choice && other)
constexprdefault

References Choice().

◆ ~Choice()

Botan::CT::Choice::~Choice ( )
constexprdefault

Member Function Documentation

◆ as_bool()

bool Botan::CT::Choice::as_bool ( ) const
inlineconstexpr

Unsafe conversion to bool

This conversion itself is (probably) constant time, but once the choice is reduced to a simple bool, it's entirely possible for the compiler to perform range analysis on the values, since there are just the two. As a consequence even if the caller is not using this in an obviously branchy way (if(choice.as_bool()) ...) a smart compiler may introduce branches depending on the value.

Definition at line 357 of file ct_utils.h.

357{ return m_value != 0; }

Referenced by Botan::to_affine_batch().

◆ from_int()

template<typename T>
requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
constexpr Choice Botan::CT::Choice::from_int ( T v)
inlinestaticconstexpr

If v == 0 return an unset (false) Choice, otherwise a set Choice

Definition at line 314 of file ct_utils.h.

314 {
315 // Mask of T that is either |0| or |1|
316 const T v_is_0 = ct_is_zero<T>(value_barrier<T>(v));
317
318 // We want the mask to be set if v != 0 so we must check that
319 // v_is_0 is itself zero.
320 //
321 // Also sizeof(T) may not equal sizeof(uint32_t) so we must
322 // use ct_is_zero<uint32_t>. It's ok to either truncate or
323 // zero extend v_is_0 to 32 bits since we know it is |0| or |1|
324 // so even just the low bit is sufficient.
325 return Choice(ct_is_zero<uint32_t>(static_cast<uint32_t>(v_is_0)));
326 }
constexpr Choice(const Choice &other)=default
constexpr T value_barrier(T x)
Definition ct_utils.h:277
BOTAN_FORCE_INLINE constexpr T ct_is_zero(T x)
Definition bit_ops.h:35

References Choice(), Botan::ct_is_zero(), and Botan::CT::value_barrier().

Referenced by Botan::CT::Mask< T >::as_choice(), Botan::bitvector_base< secure_allocator >::has_odd_hamming_weight(), and Botan::IntMod< MontgomeryRep< ScalarParams > >::is_even().

◆ from_mask()

constexpr Choice Botan::CT::Choice::from_mask ( uint32_t v)
inlinestaticconstexpr

Create a Choice directly from a mask value - this assumes v is either |0| or |1|

Definition at line 331 of file ct_utils.h.

331{ return Choice(v); }

References Choice().

Referenced by Botan::CT::Mask< T >::as_choice().

◆ no()

constexpr Choice Botan::CT::Choice::no ( )
inlinestaticconstexpr

Definition at line 335 of file ct_utils.h.

335{ return Choice(0); }

References Choice().

Referenced by Botan::to_affine_batch().

◆ operator!()

Choice Botan::CT::Choice::operator! ( ) const
inlineconstexpr

Definition at line 337 of file ct_utils.h.

337{ return Choice(~value()); }
constexpr uint32_t value() const
Return the masked value.
Definition ct_utils.h:360

References Choice(), and value().

◆ operator!=()

Choice Botan::CT::Choice::operator!= ( const Choice & other) const
inlineconstexpr

Definition at line 343 of file ct_utils.h.

343{ return Choice(value() ^ other.value()); }

References Choice(), and value().

◆ operator&&()

Choice Botan::CT::Choice::operator&& ( const Choice & other) const
inlineconstexpr

Definition at line 339 of file ct_utils.h.

339{ return Choice(value() & other.value()); }

References Choice(), and value().

◆ operator=() [1/2]

Choice & Botan::CT::Choice::operator= ( Choice && other)
constexprdefaultnoexcept

References Choice().

◆ operator=() [2/2]

Choice & Botan::CT::Choice::operator= ( const Choice & other)
constexprdefaultnoexcept

References Choice().

◆ operator==()

Choice Botan::CT::Choice::operator== ( const Choice & other) const
inlineconstexpr

Definition at line 345 of file ct_utils.h.

345{ return !(*this != other); }

References Choice().

◆ operator||()

Choice Botan::CT::Choice::operator|| ( const Choice & other) const
inlineconstexpr

Definition at line 341 of file ct_utils.h.

341{ return Choice(value() | other.value()); }

References Choice(), and value().

◆ value()

uint32_t Botan::CT::Choice::value ( ) const
inlineconstexpr

Return the masked value.

Definition at line 360 of file ct_utils.h.

360{ return value_barrier(m_value); }

References Botan::CT::value_barrier().

Referenced by Botan::CT::Mask< T >::from_choice(), operator!(), operator!=(), operator&&(), and operator||().

◆ yes()

constexpr Choice Botan::CT::Choice::yes ( )
inlinestaticconstexpr

Definition at line 333 of file ct_utils.h.

333{ return Choice(static_cast<uint32_t>(-1)); }

References Choice().

Referenced by Botan::CT::strip_leading_zeros().


The documentation for this class was generated from the following file: