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 
NAMESPACE_BEGIN(CryptoPP)12 NAMESPACE_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
21 inline 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
SetWords(word * r,word a,size_t n)35 inline 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
CopyWords(word * r,const word * a,size_t n)48 inline 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 		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
XorWords(word * r,const word * a,const word * b,size_t n)66 inline 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
XorWords(word * r,const word * a,size_t n)79 inline 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
AndWords(word * r,const word * a,const word * b,size_t n)93 inline 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
AndWords(word * r,const word * a,size_t n)106 inline 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
OrWords(word * r,const word * a,const word * b,size_t n)120 inline 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
OrWords(word * r,const word * a,size_t n)133 inline 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
ShiftWordsLeftByBits(word * r,size_t n,unsigned int shiftBits)149 inline 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
ShiftWordsRightByBits(word * r,size_t n,unsigned int shiftBits)172 inline 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
ShiftWordsLeftByWords(word * r,size_t n,size_t shiftWords)194 inline 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
ShiftWordsRightByWords(word * r,size_t n,size_t shiftWords)212 inline 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 
223 NAMESPACE_END
224 
225 #endif
226