Botan 3.14.1
Crypto and TLS for C&
Botan::WnafScalar< C, W > Class Template Referencefinal

#include <pcurves_impl.h>

Public Member Functions

int8_t digit (size_t i) const
size_t length () const
 Return the number of digits, ie one more than the index of the top nonzero digit.
 WnafScalar (const typename C::Scalar &scalar)

Static Public Attributes

static constexpr size_t MaxDigits = C::Scalar::BITS + 1

Detailed Description

template<typename C, size_t W>
class Botan::WnafScalar< C, W >

Width W non-adjacent form of a public scalar

Each digit is either zero or an odd integer with absolute value less than 2^(W-1). The recoding is variable time and the digits are stored in plain memory, so this is only usable when the scalar is public.

Definition at line 1527 of file pcurves_impl.h.

Constructor & Destructor Documentation

◆ WnafScalar()

template<typename C, size_t W>
Botan::WnafScalar< C, W >::WnafScalar ( const typename C::Scalar & scalar)
inlineexplicit

Definition at line 1534 of file pcurves_impl.h.

1534 : m_digits{}, m_len(0) {
1535 using Word = typename C::W;
1536 constexpr size_t WordBits = WordInfo<Word>::bits;
1537 constexpr Word WindowMask = (static_cast<Word>(1) << W) - 1;
1538 constexpr Word HalfWindow = static_cast<Word>(1) << (W - 1);
1539
1540 // One extra word so that subtracting a negative digit cannot overflow
1542 const auto sw = scalar.to_words();
1543 std::copy(sw.begin(), sw.end(), k.begin());
1544
1545 // Conditionals ok: this function is variable time
1546 size_t i = 0;
1547 for(;;) {
1548 bool k_is_zero = true;
1549 for(const auto w : k) {
1550 k_is_zero = k_is_zero && (w == 0);
1551 }
1552 if(k_is_zero) {
1553 break;
1554 }
1555
1556 size_t shift = 1;
1557
1558 if((k[0] & 1) == 1) {
1560
1561 const Word d = k[0] & WindowMask;
1562
1563 if(d >= HalfWindow) {
1564 // Negative digit (d - 2^W); subtracting it adds 2^W - d to k
1565 m_digits[i] = static_cast<int8_t>(static_cast<int>(d) - (1 << W));
1566 Word carry = (static_cast<Word>(1) << W) - d;
1567 for(size_t j = 0; j != k.size() && carry > 0; ++j) {
1568 const Word t = k[j] + carry;
1569 carry = (t < k[j]) ? 1 : 0;
1570 k[j] = t;
1571 }
1572 } else {
1573 m_digits[i] = static_cast<int8_t>(d);
1574 Word borrow = d;
1575 for(size_t j = 0; j != k.size() && borrow > 0; ++j) {
1576 const Word t = k[j] - borrow;
1577 borrow = (k[j] < borrow) ? 1 : 0;
1578 k[j] = t;
1579 }
1580 }
1581
1582 m_len = i + 1;
1583
1584 // k is now a multiple of 2^W, so the next W-1 digits are zero
1585 shift = W;
1586 }
1587
1588 for(size_t j = 0; j != k.size(); ++j) {
1589 k[j] >>= shift;
1590 if(j + 1 != k.size()) {
1591 k[j] |= k[j + 1] << (WordBits - shift);
1592 }
1593 }
1594
1595 i += shift;
1596 }
1597 }
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
static constexpr size_t MaxDigits

References BOTAN_DEBUG_ASSERT, and MaxDigits.

Member Function Documentation

◆ digit()

template<typename C, size_t W>
int8_t Botan::WnafScalar< C, W >::digit ( size_t i) const
inline

Definition at line 1602 of file pcurves_impl.h.

1602{ return m_digits[i]; }

◆ length()

template<typename C, size_t W>
size_t Botan::WnafScalar< C, W >::length ( ) const
inline

Return the number of digits, ie one more than the index of the top nonzero digit.

Definition at line 1600 of file pcurves_impl.h.

1600{ return m_len; }

Member Data Documentation

◆ MaxDigits

template<typename C, size_t W>
size_t Botan::WnafScalar< C, W >::MaxDigits = C::Scalar::BITS + 1
staticconstexpr

Definition at line 1532 of file pcurves_impl.h.

Referenced by WnafScalar().


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