// words.h - originally written and placed in the public domain by Wei Dai /// \file words.h /// \brief Support functions for word operations #ifndef CRYPTOPP_WORDS_H #define CRYPTOPP_WORDS_H #include "config.h" #include "misc.h" NAMESPACE_BEGIN(CryptoPP) /// \brief Count the number of words /// \param x word array /// \param n size of the word array, in elements /// \return number of words used in the array. /// \details CountWords counts the number of words in a word array. /// Leading 0-words are not included in the count. /// \since Crypto++ 1.0 inline size_t CountWords(const word *x, size_t n) { while (n && x[n-1]==0) n--; return n; } /// \brief Set the value of words /// \param r word array /// \param a value /// \param n size of the word array, in elements /// \details SetWords sets all elements in the word array to the /// specified value. /// \since Crypto++ 1.0 inline void SetWords(word *r, word a, size_t n) { for (size_t i=0; i> (WORD_BITS-shiftBits); } return carry; } /// \brief Right shift word array /// \param r word array /// \param n size of the word array, in elements /// \param shiftBits number of bits to shift /// \return word shifted out /// \details ShiftWordsRightByBits shifts the word array shight by /// shiftBits. ShiftWordsRightByBits shifts bits out on the right. /// \note shiftBits must be less than WORD_BITS. /// \since Crypto++ 1.0 inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits) { CRYPTOPP_ASSERT (shiftBits0; i--) { u = r[i-1]; r[i-1] = (u >> shiftBits) | carry; carry = u << (WORD_BITS-shiftBits); } return carry; } /// \brief Left shift word array /// \param r word array /// \param n size of the word array, in elements /// \param shiftWords number of words to shift /// \details ShiftWordsLeftByWords shifts the word array left by /// shiftWords. ShiftWordsLeftByWords shifts bits out on the left; /// it does not extend the array. /// \since Crypto++ 1.0 inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords) { shiftWords = STDMIN(shiftWords, n); if (shiftWords) { for (size_t i=n-1; i>=shiftWords; i--) r[i] = r[i-shiftWords]; SetWords(r, 0, shiftWords); } } /// \brief Right shift word array /// \param r word array /// \param n size of the word array, in elements /// \param shiftWords number of words to shift /// \details ShiftWordsRightByWords shifts the word array right by /// shiftWords. ShiftWordsRightByWords shifts bits out on the right. /// \since Crypto++ 1.0 inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords) { shiftWords = STDMIN(shiftWords, n); if (shiftWords) { for (size_t i=0; i+shiftWords