- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# words.h

    00001 #ifndef CRYPTOPP_WORDS_H
    00002 #define CRYPTOPP_WORDS_H
    00003
    00004 #include "misc.h"
    00005
    00006 NAMESPACE_BEGIN(CryptoPP)
    00007
    00008 inline size_t CountWords(const word *X, size_t N)
    00009 {
    00010         while (N && X[N-1]==0)
    00011                 N--;
    00012         return N;
    00013 }
    00014
    00015 inline void SetWords(word *r, word a, size_t n)
    00016 {
    00017         for (size_t i=0; i<n; i++)
    00018                 r[i] = a;
    00019 }
    00020
    00021 inline void CopyWords(word *r, const word *a, size_t n)
    00022 {
    00023         memcpy(r, a, n*WORD_SIZE);
    00024 }
    00025
    00026 inline void XorWords(word *r, const word *a, const word *b, size_t n)
    00027 {
    00028         for (size_t i=0; i<n; i++)
    00029                 r[i] = a[i] ^ b[i];
    00030 }
    00031
    00032 inline void XorWords(word *r, const word *a, size_t n)
    00033 {
    00034         for (size_t i=0; i<n; i++)
    00035                 r[i] ^= a[i];
    00036 }
    00037
    00038 inline void AndWords(word *r, const word *a, const word *b, size_t n)
    00039 {
    00040         for (size_t i=0; i<n; i++)
    00041                 r[i] = a[i] & b[i];
    00042 }
    00043
    00044 inline void AndWords(word *r, const word *a, size_t n)
    00045 {
    00046         for (size_t i=0; i<n; i++)
    00047                 r[i] &= a[i];
    00048 }
    00049
    00050 inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
    00051 {
    00052         assert (shiftBits<WORD_BITS);
    00053         word u, carry=0;
    00054         if (shiftBits)
    00055                 for (size_t i=0; i<n; i++)
    00056                 {
    00057                         u = r[i];
    00058                         r[i] = (u << shiftBits) | carry;
    00059                         carry = u >> (WORD_BITS-shiftBits);
    00060                 }
    00061         return carry;
    00062 }
    00063
    00064 inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
    00065 {
    00066         assert (shiftBits<WORD_BITS);
    00067         word u, carry=0;
    00068         if (shiftBits)
    00069                 for (size_t i=n; i>0; i--)
    00070                 {
    00071                         u = r[i-1];
    00072                         r[i-1] = (u >> shiftBits) | carry;
    00073                         carry = u << (WORD_BITS-shiftBits);
    00074                 }
    00075         return carry;
    00076 }
    00077
    00078 inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
    00079 {
    00080         shiftWords = STDMIN(shiftWords, n);
    00081         if (shiftWords)
    00082         {
    00083                 for (size_t i=n-1; i>=shiftWords; i--)
    00084                         r[i] = r[i-shiftWords];
    00085                 SetWords(r, 0, shiftWords);
    00086         }
    00087 }
    00088
    00089 inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
    00090 {
    00091         shiftWords = STDMIN(shiftWords, n);
    00092         if (shiftWords)
    00093         {
    00094                 for (size_t i=0; i+shiftWords<n; i++)
    00095                         r[i] = r[i+shiftWords];
    00096                 SetWords(r+n-shiftWords, 0, shiftWords);
    00097         }
    00098 }
    00099
    00100 NAMESPACE_END
    00101
    00102 #endif