Skip to content

Commit 4df2421

Browse files
committed
modified functions that return array values to static constexpr arrays
1 parent de43c3d commit 4df2421

File tree

1 file changed

+45
-80
lines changed

1 file changed

+45
-80
lines changed

hashing/ripemd_160.cpp

Lines changed: 45 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class RIPEMD160 {
111111
* @return returns the function value
112112
*/
113113
uint32_t f(int j, uint32_t B, uint32_t C, uint32_t D) {
114-
switch(j) {
114+
switch (j) {
115115
case 0:
116116
return B ^ C ^ D;
117117
case 1:
@@ -131,16 +131,17 @@ class RIPEMD160 {
131131
* @return appropriate K value
132132
*/
133133
uint32_t K(int j) {
134-
switch(j) {
135-
case 0: return static_cast<uint32_t>(0x00000000);
136-
case 1:
137-
return static_cast<uint32_t>(0x5A827999);
138-
case 2:
139-
return static_cast<uint32_t>(0x6ED9EBA1);
140-
case 3:
141-
return static_cast<uint32_t>(0x8F1BBCDC);
142-
case 4:
143-
return static_cast<uint32_t>(0xA953FD4E);
134+
switch (j) {
135+
case 0:
136+
return static_cast<uint32_t>(0x00000000);
137+
case 1:
138+
return static_cast<uint32_t>(0x5A827999);
139+
case 2:
140+
return static_cast<uint32_t>(0x6ED9EBA1);
141+
case 3:
142+
return static_cast<uint32_t>(0x8F1BBCDC);
143+
case 4:
144+
return static_cast<uint32_t>(0xA953FD4E);
144145
}
145146
}
146147

@@ -150,7 +151,7 @@ class RIPEMD160 {
150151
* @return appropriate K' value
151152
*/
152153
uint32_t K_dash(int j) {
153-
switch(j){
154+
switch (j) {
154155
case 0:
155156
return 0x50A28BE6;
156157
case 1:
@@ -159,74 +160,38 @@ class RIPEMD160 {
159160
return 0x6D703EF3;
160161
case 3:
161162
return 0x7A6D76E9;
162-
case 4:
163+
case 4:
163164
return 0x00000000;
164165
}
165166
}
166167

167-
/**
168-
* @brief implements r value for a given j
169-
* @param j round number j
170-
* @return appropriate r value
171-
*/
172-
int r(int j) {
173-
static const int r_value[80] = {
174-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
175-
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
176-
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
177-
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
178-
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13};
179-
180-
return r_value[j];
181-
}
182-
183-
/**
184-
* @brief implements r' value for a given j
185-
* @param j round number j
186-
* @return appropriate r' value
187-
*/
188-
int r_dash(int j) {
189-
static const int r_dash_value[80] = {
190-
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
191-
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
192-
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
193-
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
194-
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11};
195-
196-
return r_dash_value[j];
197-
}
198-
199-
/**
200-
* @brief implements s value for a given j
201-
* @param j round number j
202-
* @return appropriate s value
203-
*/
204-
int s(int j) {
205-
static const int s_value[80] = {
206-
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
207-
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
208-
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
209-
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
210-
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6};
211-
212-
return s_value[j];
213-
}
214-
215-
/**
216-
* @brief implements s' value for a given j
217-
* @param j round number j
218-
* @return appropriate s' value
219-
*/
220-
int s_dash(int j) {
221-
static const int s_dash_value[80] = {
222-
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
223-
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
224-
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
225-
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
226-
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11};
227-
228-
return s_dash_value[j];
229-
}
168+
static constexpr int r[80] = {
169+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
170+
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
171+
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
172+
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
173+
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13};
174+
175+
static constexpr int r_dash[80] = {
176+
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
177+
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
178+
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
179+
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
180+
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11};
181+
182+
static constexpr int s[80] = {
183+
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
184+
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
185+
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
186+
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
187+
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6};
188+
189+
static constexpr int s_dash[80] = {
190+
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
191+
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
192+
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
193+
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
194+
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11};
230195

231196
/**
232197
* @brief cyclic left shift of uint32_t
@@ -257,8 +222,8 @@ class RIPEMD160 {
257222
const std::vector<uint32_t> &current_block_data) {
258223
for (int j = 0; j < 80; j++) {
259224
*T = cyclic_left_shift(*A + f(j >> 4, *B, *C, *D) +
260-
current_block_data[r(j)] + K(j >> 4),
261-
s(j)) +
225+
current_block_data[r[j]] + K(j >> 4),
226+
s[j]) +
262227
*E;
263228

264229
*A = *E;
@@ -289,9 +254,9 @@ class RIPEMD160 {
289254
const std::vector<uint32_t> &current_block_data) {
290255
for (int j = 0; j < 80; j++) {
291256
*T = cyclic_left_shift(*A + f(4 - (j >> 4), *B, *C, *D) +
292-
current_block_data[r_dash(j)] +
257+
current_block_data[r_dash[j]] +
293258
K_dash(j >> 4),
294-
s_dash(j)) +
259+
s_dash[j]) +
295260
*E;
296261

297262
*A = *E;

0 commit comments

Comments
 (0)