Crypto++

- 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     if (r != a)
    00024         memcpy(r, a, n*WORD_SIZE);
    00025 }
    00026
    00027 inline void XorWords(word *r, const word *a, const word *b, size_t n)
    00028 {
    00029     for (size_t i=0; i<n; i++)
    00030         r[i] = a[i] ^ b[i];
    00031 }
    00032
    00033 inline void XorWords(word *r, const word *a, size_t n)
    00034 {
    00035     for (size_t i=0; i<n; i++)
    00036         r[i] ^= a[i];
    00037 }
    00038
    00039 inline void AndWords(word *r, const word *a, const word *b, size_t n)
    00040 {
    00041     for (size_t i=0; i<n; i++)
    00042         r[i] = a[i] & b[i];
    00043 }
    00044
    00045 inline void AndWords(word *r, const word *a, size_t n)
    00046 {
    00047     for (size_t i=0; i<n; i++)
    00048         r[i] &= a[i];
    00049 }
    00050
    00051 inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
    00052 {
    00053     assert (shiftBits<WORD_BITS);
    00054     word u, carry=0;
    00055     if (shiftBits)
    00056         for (size_t 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, size_t n, unsigned int shiftBits)
    00066 {
    00067     assert (shiftBits<WORD_BITS);
    00068     word u, carry=0;
    00069     if (shiftBits)
    00070         for (size_t i=n; i>0; i--)
    00071         {
    00072             u = r[i-1];
    00073             r[i-1] = (u >> shiftBits) | carry;
    00074             carry = u << (WORD_BITS-shiftBits);
    00075         }
    00076     return carry;
    00077 }
    00078
    00079 inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
    00080 {
    00081     shiftWords = STDMIN(shiftWords, n);
    00082     if (shiftWords)
    00083     {
    00084         for (size_t 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, size_t n, size_t shiftWords)
    00091 {
    00092     shiftWords = STDMIN(shiftWords, n);
    00093     if (shiftWords)
    00094     {
    00095         for (size_t 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