ext-cryptopp/words.h

104 lines
1.9 KiB
C
Raw Normal View History

2002-10-04 17:31:41 +00:00
#ifndef CRYPTOPP_WORDS_H
#define CRYPTOPP_WORDS_H
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
2005-07-12 04:23:32 +00:00
inline size_t CountWords(const word *X, size_t N)
2002-10-04 17:31:41 +00:00
{
while (N && X[N-1]==0)
N--;
return N;
}
2005-07-12 04:23:32 +00:00
inline void SetWords(word *r, word a, size_t n)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
for (size_t i=0; i<n; i++)
2002-10-04 17:31:41 +00:00
r[i] = a;
}
2005-07-12 04:23:32 +00:00
inline void CopyWords(word *r, const word *a, size_t n)
2002-10-04 17:31:41 +00:00
{
if (r != a)
memcpy(r, a, n*WORD_SIZE);
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
inline void XorWords(word *r, const word *a, const word *b, size_t n)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
for (size_t i=0; i<n; i++)
2002-10-04 17:31:41 +00:00
r[i] = a[i] ^ b[i];
}
2005-07-12 04:23:32 +00:00
inline void XorWords(word *r, const word *a, size_t n)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
for (size_t i=0; i<n; i++)
2002-10-04 17:31:41 +00:00
r[i] ^= a[i];
}
2005-07-12 04:23:32 +00:00
inline void AndWords(word *r, const word *a, const word *b, size_t n)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
for (size_t i=0; i<n; i++)
2002-10-04 17:31:41 +00:00
r[i] = a[i] & b[i];
}
2005-07-12 04:23:32 +00:00
inline void AndWords(word *r, const word *a, size_t n)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
for (size_t i=0; i<n; i++)
2002-10-04 17:31:41 +00:00
r[i] &= a[i];
}
2005-07-12 04:23:32 +00:00
inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
2002-10-04 17:31:41 +00:00
{
assert (shiftBits<WORD_BITS);
word u, carry=0;
if (shiftBits)
2005-07-12 04:23:32 +00:00
for (size_t i=0; i<n; i++)
2002-10-04 17:31:41 +00:00
{
u = r[i];
r[i] = (u << shiftBits) | carry;
carry = u >> (WORD_BITS-shiftBits);
}
return carry;
}
2005-07-12 04:23:32 +00:00
inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
2002-10-04 17:31:41 +00:00
{
assert (shiftBits<WORD_BITS);
word u, carry=0;
if (shiftBits)
2005-07-12 04:23:32 +00:00
for (size_t i=n; i>0; i--)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
u = r[i-1];
r[i-1] = (u >> shiftBits) | carry;
2002-10-04 17:31:41 +00:00
carry = u << (WORD_BITS-shiftBits);
}
return carry;
}
2005-07-12 04:23:32 +00:00
inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
2002-10-04 17:31:41 +00:00
{
shiftWords = STDMIN(shiftWords, n);
if (shiftWords)
{
2005-07-12 04:23:32 +00:00
for (size_t i=n-1; i>=shiftWords; i--)
2002-10-04 17:31:41 +00:00
r[i] = r[i-shiftWords];
SetWords(r, 0, shiftWords);
}
}
2005-07-12 04:23:32 +00:00
inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
2002-10-04 17:31:41 +00:00
{
shiftWords = STDMIN(shiftWords, n);
if (shiftWords)
{
2005-07-12 04:23:32 +00:00
for (size_t i=0; i+shiftWords<n; i++)
2002-10-04 17:31:41 +00:00
r[i] = r[i+shiftWords];
SetWords(r+n-shiftWords, 0, shiftWords);
}
}
NAMESPACE_END
#endif