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