Crypto++  5.6.3
Free C++ class library of cryptographic schemes
words.h
1 #ifndef CRYPTOPP_WORDS_H
2 #define CRYPTOPP_WORDS_H
3 
4 #include "config.h"
5 #include "misc.h"
6 
7 NAMESPACE_BEGIN(CryptoPP)
8 
9 inline size_t CountWords(const word *X, size_t N)
10 {
11  while (N && X[N-1]==0)
12  N--;
13  return N;
14 }
15 
16 inline void SetWords(word *r, word a, size_t n)
17 {
18  for (size_t i=0; i<n; i++)
19  r[i] = a;
20 }
21 
22 inline void CopyWords(word *r, const word *a, size_t n)
23 {
24  if (r != a)
25 #if CRYPTOPP_MSC_VERSION
26  memcpy_s(r, n*WORD_SIZE, a, n*WORD_SIZE);
27 #else
28  memcpy(r, a, n*WORD_SIZE);
29 #endif
30 }
31 
32 inline void XorWords(word *r, const word *a, const word *b, size_t n)
33 {
34  for (size_t i=0; i<n; i++)
35  r[i] = a[i] ^ b[i];
36 }
37 
38 inline void XorWords(word *r, const word *a, size_t n)
39 {
40  for (size_t i=0; i<n; i++)
41  r[i] ^= a[i];
42 }
43 
44 inline void AndWords(word *r, const word *a, const word *b, size_t n)
45 {
46  for (size_t i=0; i<n; i++)
47  r[i] = a[i] & b[i];
48 }
49 
50 inline void AndWords(word *r, const word *a, size_t n)
51 {
52  for (size_t i=0; i<n; i++)
53  r[i] &= a[i];
54 }
55 
56 inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
57 {
58  assert (shiftBits<WORD_BITS);
59  word u, carry=0;
60  if (shiftBits)
61  for (size_t i=0; i<n; i++)
62  {
63  u = r[i];
64  r[i] = (u << shiftBits) | carry;
65  carry = u >> (WORD_BITS-shiftBits);
66  }
67  return carry;
68 }
69 
70 inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
71 {
72  assert (shiftBits<WORD_BITS);
73  word u, carry=0;
74  if (shiftBits)
75  for (size_t i=n; i>0; i--)
76  {
77  u = r[i-1];
78  r[i-1] = (u >> shiftBits) | carry;
79  carry = u << (WORD_BITS-shiftBits);
80  }
81  return carry;
82 }
83 
84 inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
85 {
86  shiftWords = STDMIN(shiftWords, n);
87  if (shiftWords)
88  {
89  for (size_t i=n-1; i>=shiftWords; i--)
90  r[i] = r[i-shiftWords];
91  SetWords(r, 0, shiftWords);
92  }
93 }
94 
95 inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
96 {
97  shiftWords = STDMIN(shiftWords, n);
98  if (shiftWords)
99  {
100  for (size_t i=0; i+shiftWords<n; i++)
101  r[i] = r[i+shiftWords];
102  SetWords(r+n-shiftWords, 0, shiftWords);
103  }
104 }
105 
106 NAMESPACE_END
107 
108 #endif
Utility functions for the Crypto++ library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:301
Library configuration file.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:397
Crypto++ library namespace.