Crypto++ 8.9
Free C++ class library of cryptographic schemes
words.h
Go to the documentation of this file.
1// words.h - originally written and placed in the public domain by Wei Dai
2
3/// \file words.h
4/// \brief Support functions for word operations
5
6#ifndef CRYPTOPP_WORDS_H
7#define CRYPTOPP_WORDS_H
8
9#include "config.h"
10#include "misc.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14/// \brief Count the number of words
15/// \param x word array
16/// \param n size of the word array, in elements
17/// \return number of words used in the array.
18/// \details CountWords counts the number of words in a word array.
19/// Leading 0-words are not included in the count.
20/// \since Crypto++ 1.0
21inline size_t CountWords(const word *x, size_t n)
22{
23 while (n && x[n-1]==0)
24 n--;
25 return n;
26}
27
28/// \brief Set the value of words
29/// \param r word array
30/// \param a value
31/// \param n size of the word array, in elements
32/// \details SetWords sets all elements in the word array to the
33/// specified value.
34/// \since Crypto++ 1.0
35inline void SetWords(word *r, word a, size_t n)
36{
37 for (size_t i=0; i<n; i++)
38 r[i] = a;
39}
40
41/// \brief Copy word array
42/// \param r destination word array
43/// \param a source word array
44/// \param n size of the word array, in elements
45/// \details CopyWords copies the source word array to the destination
46/// word array.
47/// \since Crypto++ 1.0
48inline void CopyWords(word *r, const word *a, size_t n)
49{
50 if (r != a)
51#if CRYPTOPP_MSC_VERSION
52 memcpy_s(r, n*WORD_SIZE, a, n*WORD_SIZE);
53#else
54 std::memcpy(r, a, n*WORD_SIZE);
55#endif
56}
57
58/// \brief XOR word arrays
59/// \param r destination word array
60/// \param a first source word array
61/// \param b second source word array
62/// \param n size of the word array, in elements
63/// \details XorWords XORs the two source word arrays and copies the
64/// result to the destination word array.
65/// \since Crypto++ 1.0
66inline void XorWords(word *r, const word *a, const word *b, size_t n)
67{
68 for (size_t i=0; i<n; i++)
69 r[i] = a[i] ^ b[i];
70}
71
72/// \brief XOR word arrays
73/// \param r destination word array
74/// \param a source word array
75/// \param n size of the word array, in elements
76/// \details XorWords XORs the source word array with the
77/// destination word array.
78/// \since Crypto++ 1.0
79inline void XorWords(word *r, const word *a, size_t n)
80{
81 for (size_t i=0; i<n; i++)
82 r[i] ^= a[i];
83}
84
85/// \brief AND word arrays
86/// \param r destination word array
87/// \param a first source word array
88/// \param b second source word array
89/// \param n size of the word array, in elements
90/// \details AndWords ANDs the two source word arrays and copies the
91/// result to the destination word array.
92/// \since Crypto++ 1.0
93inline void AndWords(word *r, const word *a, const word *b, size_t n)
94{
95 for (size_t i=0; i<n; i++)
96 r[i] = a[i] & b[i];
97}
98
99/// \brief AND word arrays
100/// \param r destination word array
101/// \param a source word array
102/// \param n size of the word array, in elements
103/// \details AndWords ANDs the source word array with the
104/// destination word array.
105/// \since Crypto++ 1.0
106inline void AndWords(word *r, const word *a, size_t n)
107{
108 for (size_t i=0; i<n; i++)
109 r[i] &= a[i];
110}
111
112/// \brief OR word arrays
113/// \param r destination word array
114/// \param a first source word array
115/// \param b second source word array
116/// \param n size of the word array, in elements
117/// \details OrWords ORs the two source word arrays and copies the
118/// result to the destination word array.
119/// \since Crypto++ 1.0
120inline void OrWords(word *r, const word *a, const word *b, size_t n)
121{
122 for (size_t i=0; i<n; i++)
123 r[i] = a[i] | b[i];
124}
125
126/// \brief OR word arrays
127/// \param r destination word array
128/// \param a source word array
129/// \param n size of the word array, in elements
130/// \details OrWords ORs the source word array with the
131/// destination word array.
132/// \since Crypto++ 1.0
133inline void OrWords(word *r, const word *a, size_t n)
134{
135 for (size_t i=0; i<n; i++)
136 r[i] |= a[i];
137}
138
139/// \brief Left shift word array
140/// \param r word array
141/// \param n size of the word array, in elements
142/// \param shiftBits number of bits to shift
143/// \return word shifted out
144/// \details ShiftWordsLeftByBits shifts the word array left by
145/// shiftBits. ShiftWordsLeftByBits shifts bits out on the left;
146/// it does not extend the array.
147/// \note shiftBits must be less than WORD_BITS.
148/// \since Crypto++ 1.0
149inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
150{
151 CRYPTOPP_ASSERT (shiftBits<WORD_BITS);
152 word u, carry=0;
153 if (shiftBits)
154 for (size_t i=0; i<n; i++)
155 {
156 u = r[i];
157 r[i] = (u << shiftBits) | carry;
158 carry = u >> (WORD_BITS-shiftBits);
159 }
160 return carry;
161}
162
163/// \brief Right shift word array
164/// \param r word array
165/// \param n size of the word array, in elements
166/// \param shiftBits number of bits to shift
167/// \return word shifted out
168/// \details ShiftWordsRightByBits shifts the word array shight by
169/// shiftBits. ShiftWordsRightByBits shifts bits out on the right.
170/// \note shiftBits must be less than WORD_BITS.
171/// \since Crypto++ 1.0
172inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
173{
174 CRYPTOPP_ASSERT (shiftBits<WORD_BITS);
175 word u, carry=0;
176 if (shiftBits)
177 for (size_t i=n; i>0; i--)
178 {
179 u = r[i-1];
180 r[i-1] = (u >> shiftBits) | carry;
181 carry = u << (WORD_BITS-shiftBits);
182 }
183 return carry;
184}
185
186/// \brief Left shift word array
187/// \param r word array
188/// \param n size of the word array, in elements
189/// \param shiftWords number of words to shift
190/// \details ShiftWordsLeftByWords shifts the word array left by
191/// shiftWords. ShiftWordsLeftByWords shifts bits out on the left;
192/// it does not extend the array.
193/// \since Crypto++ 1.0
194inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
195{
196 shiftWords = STDMIN(shiftWords, n);
197 if (shiftWords)
198 {
199 for (size_t i=n-1; i>=shiftWords; i--)
200 r[i] = r[i-shiftWords];
201 SetWords(r, 0, shiftWords);
202 }
203}
204
205/// \brief Right shift word array
206/// \param r word array
207/// \param n size of the word array, in elements
208/// \param shiftWords number of words to shift
209/// \details ShiftWordsRightByWords shifts the word array right by
210/// shiftWords. ShiftWordsRightByWords shifts bits out on the right.
211/// \since Crypto++ 1.0
212inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
213{
214 shiftWords = STDMIN(shiftWords, n);
215 if (shiftWords)
216 {
217 for (size_t i=0; i+shiftWords<n; i++)
218 r[i] = r[i+shiftWords];
219 SetWords(r+n-shiftWords, 0, shiftWords);
220 }
221}
222
223NAMESPACE_END
224
225#endif
Library configuration file.
word64 word
Full word used for multiprecision integer arithmetic.
Definition config_int.h:192
const unsigned int WORD_BITS
Size of a platform word in bits.
Definition config_int.h:260
const unsigned int WORD_SIZE
Size of a platform word in bytes.
Definition config_int.h:255
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:527
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition misc.h:657
Crypto++ library namespace.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68
void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
Right shift word array.
Definition words.h:212
void XorWords(word *r, const word *a, const word *b, size_t n)
XOR word arrays.
Definition words.h:66
void SetWords(word *r, word a, size_t n)
Set the value of words.
Definition words.h:35
void OrWords(word *r, const word *a, const word *b, size_t n)
OR word arrays.
Definition words.h:120
word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
Left shift word array.
Definition words.h:149
void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
Left shift word array.
Definition words.h:194
size_t CountWords(const word *x, size_t n)
Count the number of words.
Definition words.h:21
word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
Right shift word array.
Definition words.h:172
void CopyWords(word *r, const word *a, size_t n)
Copy word array.
Definition words.h:48
void AndWords(word *r, const word *a, const word *b, size_t n)
AND word arrays.
Definition words.h:93