Fabcoin Core  0.16.2
P2P Digital Currency
serpentp.h
Go to the documentation of this file.
1 // private header for Serpent and Sosemanuk
2 
4 
5 // linear transformation
6 #define LT(i,a,b,c,d,e) {\
7  a = rotlFixed(a, 13); \
8  c = rotlFixed(c, 3); \
9  d = rotlFixed(d ^ c ^ (a << 3), 7); \
10  b = rotlFixed(b ^ a ^ c, 1); \
11  a = rotlFixed(a ^ b ^ d, 5); \
12  c = rotlFixed(c ^ d ^ (b << 7), 22);}
13 
14 // inverse linear transformation
15 #define ILT(i,a,b,c,d,e) {\
16  c = rotrFixed(c, 22); \
17  a = rotrFixed(a, 5); \
18  c ^= d ^ (b << 7); \
19  a ^= b ^ d; \
20  b = rotrFixed(b, 1); \
21  d = rotrFixed(d, 7) ^ c ^ (a << 3); \
22  b ^= a ^ c; \
23  c = rotrFixed(c, 3); \
24  a = rotrFixed(a, 13);}
25 
26 // order of output from S-box functions
27 #define beforeS0(f) f(0,a,b,c,d,e)
28 #define afterS0(f) f(1,b,e,c,a,d)
29 #define afterS1(f) f(2,c,b,a,e,d)
30 #define afterS2(f) f(3,a,e,b,d,c)
31 #define afterS3(f) f(4,e,b,d,c,a)
32 #define afterS4(f) f(5,b,a,e,c,d)
33 #define afterS5(f) f(6,a,c,b,e,d)
34 #define afterS6(f) f(7,a,c,d,b,e)
35 #define afterS7(f) f(8,d,e,b,a,c)
36 
37 // order of output from inverse S-box functions
38 #define beforeI7(f) f(8,a,b,c,d,e)
39 #define afterI7(f) f(7,d,a,b,e,c)
40 #define afterI6(f) f(6,a,b,c,e,d)
41 #define afterI5(f) f(5,b,d,e,c,a)
42 #define afterI4(f) f(4,b,c,e,a,d)
43 #define afterI3(f) f(3,a,b,e,c,d)
44 #define afterI2(f) f(2,b,d,e,c,a)
45 #define afterI1(f) f(1,a,b,c,e,d)
46 #define afterI0(f) f(0,a,d,b,e,c)
47 
48 // The instruction sequences for the S-box functions
49 // come from Dag Arne Osvik's paper "Speeding up Serpent".
50 
51 #define S0(i, r0, r1, r2, r3, r4) \
52  { \
53  r3 ^= r0; \
54  r4 = r1; \
55  r1 &= r3; \
56  r4 ^= r2; \
57  r1 ^= r0; \
58  r0 |= r3; \
59  r0 ^= r4; \
60  r4 ^= r3; \
61  r3 ^= r2; \
62  r2 |= r1; \
63  r2 ^= r4; \
64  r4 = ~r4; \
65  r4 |= r1; \
66  r1 ^= r3; \
67  r1 ^= r4; \
68  r3 |= r0; \
69  r1 ^= r3; \
70  r4 ^= r3; \
71  }
72 
73 #define I0(i, r0, r1, r2, r3, r4) \
74  { \
75  r2 = ~r2; \
76  r4 = r1; \
77  r1 |= r0; \
78  r4 = ~r4; \
79  r1 ^= r2; \
80  r2 |= r4; \
81  r1 ^= r3; \
82  r0 ^= r4; \
83  r2 ^= r0; \
84  r0 &= r3; \
85  r4 ^= r0; \
86  r0 |= r1; \
87  r0 ^= r2; \
88  r3 ^= r4; \
89  r2 ^= r1; \
90  r3 ^= r0; \
91  r3 ^= r1; \
92  r2 &= r3; \
93  r4 ^= r2; \
94  }
95 
96 #define S1(i, r0, r1, r2, r3, r4) \
97  { \
98  r0 = ~r0; \
99  r2 = ~r2; \
100  r4 = r0; \
101  r0 &= r1; \
102  r2 ^= r0; \
103  r0 |= r3; \
104  r3 ^= r2; \
105  r1 ^= r0; \
106  r0 ^= r4; \
107  r4 |= r1; \
108  r1 ^= r3; \
109  r2 |= r0; \
110  r2 &= r4; \
111  r0 ^= r1; \
112  r1 &= r2; \
113  r1 ^= r0; \
114  r0 &= r2; \
115  r0 ^= r4; \
116  }
117 
118 #define I1(i, r0, r1, r2, r3, r4) \
119  { \
120  r4 = r1; \
121  r1 ^= r3; \
122  r3 &= r1; \
123  r4 ^= r2; \
124  r3 ^= r0; \
125  r0 |= r1; \
126  r2 ^= r3; \
127  r0 ^= r4; \
128  r0 |= r2; \
129  r1 ^= r3; \
130  r0 ^= r1; \
131  r1 |= r3; \
132  r1 ^= r0; \
133  r4 = ~r4; \
134  r4 ^= r1; \
135  r1 |= r0; \
136  r1 ^= r0; \
137  r1 |= r4; \
138  r3 ^= r1; \
139  }
140 
141 #define S2(i, r0, r1, r2, r3, r4) \
142  { \
143  r4 = r0; \
144  r0 &= r2; \
145  r0 ^= r3; \
146  r2 ^= r1; \
147  r2 ^= r0; \
148  r3 |= r4; \
149  r3 ^= r1; \
150  r4 ^= r2; \
151  r1 = r3; \
152  r3 |= r4; \
153  r3 ^= r0; \
154  r0 &= r1; \
155  r4 ^= r0; \
156  r1 ^= r3; \
157  r1 ^= r4; \
158  r4 = ~r4; \
159  }
160 
161 #define I2(i, r0, r1, r2, r3, r4) \
162  { \
163  r2 ^= r3; \
164  r3 ^= r0; \
165  r4 = r3; \
166  r3 &= r2; \
167  r3 ^= r1; \
168  r1 |= r2; \
169  r1 ^= r4; \
170  r4 &= r3; \
171  r2 ^= r3; \
172  r4 &= r0; \
173  r4 ^= r2; \
174  r2 &= r1; \
175  r2 |= r0; \
176  r3 = ~r3; \
177  r2 ^= r3; \
178  r0 ^= r3; \
179  r0 &= r1; \
180  r3 ^= r4; \
181  r3 ^= r0; \
182  }
183 
184 #define S3(i, r0, r1, r2, r3, r4) \
185  { \
186  r4 = r0; \
187  r0 |= r3; \
188  r3 ^= r1; \
189  r1 &= r4; \
190  r4 ^= r2; \
191  r2 ^= r3; \
192  r3 &= r0; \
193  r4 |= r1; \
194  r3 ^= r4; \
195  r0 ^= r1; \
196  r4 &= r0; \
197  r1 ^= r3; \
198  r4 ^= r2; \
199  r1 |= r0; \
200  r1 ^= r2; \
201  r0 ^= r3; \
202  r2 = r1; \
203  r1 |= r3; \
204  r1 ^= r0; \
205  }
206 
207 #define I3(i, r0, r1, r2, r3, r4) \
208  { \
209  r4 = r2; \
210  r2 ^= r1; \
211  r1 &= r2; \
212  r1 ^= r0; \
213  r0 &= r4; \
214  r4 ^= r3; \
215  r3 |= r1; \
216  r3 ^= r2; \
217  r0 ^= r4; \
218  r2 ^= r0; \
219  r0 |= r3; \
220  r0 ^= r1; \
221  r4 ^= r2; \
222  r2 &= r3; \
223  r1 |= r3; \
224  r1 ^= r2; \
225  r4 ^= r0; \
226  r2 ^= r4; \
227  }
228 
229 #define S4(i, r0, r1, r2, r3, r4) \
230  { \
231  r1 ^= r3; \
232  r3 = ~r3; \
233  r2 ^= r3; \
234  r3 ^= r0; \
235  r4 = r1; \
236  r1 &= r3; \
237  r1 ^= r2; \
238  r4 ^= r3; \
239  r0 ^= r4; \
240  r2 &= r4; \
241  r2 ^= r0; \
242  r0 &= r1; \
243  r3 ^= r0; \
244  r4 |= r1; \
245  r4 ^= r0; \
246  r0 |= r3; \
247  r0 ^= r2; \
248  r2 &= r3; \
249  r0 = ~r0; \
250  r4 ^= r2; \
251  }
252 
253 #define I4(i, r0, r1, r2, r3, r4) \
254  { \
255  r4 = r2; \
256  r2 &= r3; \
257  r2 ^= r1; \
258  r1 |= r3; \
259  r1 &= r0; \
260  r4 ^= r2; \
261  r4 ^= r1; \
262  r1 &= r2; \
263  r0 = ~r0; \
264  r3 ^= r4; \
265  r1 ^= r3; \
266  r3 &= r0; \
267  r3 ^= r2; \
268  r0 ^= r1; \
269  r2 &= r0; \
270  r3 ^= r0; \
271  r2 ^= r4; \
272  r2 |= r3; \
273  r3 ^= r0; \
274  r2 ^= r1; \
275  }
276 
277 #define S5(i, r0, r1, r2, r3, r4) \
278  { \
279  r0 ^= r1; \
280  r1 ^= r3; \
281  r3 = ~r3; \
282  r4 = r1; \
283  r1 &= r0; \
284  r2 ^= r3; \
285  r1 ^= r2; \
286  r2 |= r4; \
287  r4 ^= r3; \
288  r3 &= r1; \
289  r3 ^= r0; \
290  r4 ^= r1; \
291  r4 ^= r2; \
292  r2 ^= r0; \
293  r0 &= r3; \
294  r2 = ~r2; \
295  r0 ^= r4; \
296  r4 |= r3; \
297  r2 ^= r4; \
298  }
299 
300 #define I5(i, r0, r1, r2, r3, r4) \
301  { \
302  r1 = ~r1; \
303  r4 = r3; \
304  r2 ^= r1; \
305  r3 |= r0; \
306  r3 ^= r2; \
307  r2 |= r1; \
308  r2 &= r0; \
309  r4 ^= r3; \
310  r2 ^= r4; \
311  r4 |= r0; \
312  r4 ^= r1; \
313  r1 &= r2; \
314  r1 ^= r3; \
315  r4 ^= r2; \
316  r3 &= r4; \
317  r4 ^= r1; \
318  r3 ^= r0; \
319  r3 ^= r4; \
320  r4 = ~r4; \
321  }
322 
323 #define S6(i, r0, r1, r2, r3, r4) \
324  { \
325  r2 = ~r2; \
326  r4 = r3; \
327  r3 &= r0; \
328  r0 ^= r4; \
329  r3 ^= r2; \
330  r2 |= r4; \
331  r1 ^= r3; \
332  r2 ^= r0; \
333  r0 |= r1; \
334  r2 ^= r1; \
335  r4 ^= r0; \
336  r0 |= r3; \
337  r0 ^= r2; \
338  r4 ^= r3; \
339  r4 ^= r0; \
340  r3 = ~r3; \
341  r2 &= r4; \
342  r2 ^= r3; \
343  }
344 
345 #define I6(i, r0, r1, r2, r3, r4) \
346  { \
347  r0 ^= r2; \
348  r4 = r2; \
349  r2 &= r0; \
350  r4 ^= r3; \
351  r2 = ~r2; \
352  r3 ^= r1; \
353  r2 ^= r3; \
354  r4 |= r0; \
355  r0 ^= r2; \
356  r3 ^= r4; \
357  r4 ^= r1; \
358  r1 &= r3; \
359  r1 ^= r0; \
360  r0 ^= r3; \
361  r0 |= r2; \
362  r3 ^= r1; \
363  r4 ^= r0; \
364  }
365 
366 #define S7(i, r0, r1, r2, r3, r4) \
367  { \
368  r4 = r2; \
369  r2 &= r1; \
370  r2 ^= r3; \
371  r3 &= r1; \
372  r4 ^= r2; \
373  r2 ^= r1; \
374  r1 ^= r0; \
375  r0 |= r4; \
376  r0 ^= r2; \
377  r3 ^= r1; \
378  r2 ^= r3; \
379  r3 &= r0; \
380  r3 ^= r4; \
381  r4 ^= r2; \
382  r2 &= r0; \
383  r4 = ~r4; \
384  r2 ^= r4; \
385  r4 &= r0; \
386  r1 ^= r3; \
387  r4 ^= r1; \
388  }
389 
390 #define I7(i, r0, r1, r2, r3, r4) \
391  { \
392  r4 = r2; \
393  r2 ^= r0; \
394  r0 &= r3; \
395  r2 = ~r2; \
396  r4 |= r3; \
397  r3 ^= r1; \
398  r1 |= r0; \
399  r0 ^= r2; \
400  r2 &= r4; \
401  r1 ^= r2; \
402  r2 ^= r0; \
403  r0 |= r2; \
404  r3 &= r4; \
405  r0 ^= r3; \
406  r4 ^= r1; \
407  r3 ^= r4; \
408  r4 |= r0; \
409  r3 ^= r2; \
410  r4 ^= r2; \
411  }
412 
413 // key xor
414 #define KX(r, a, b, c, d, e) {\
415  a ^= k[4 * r + 0]; \
416  b ^= k[4 * r + 1]; \
417  c ^= k[4 * r + 2]; \
418  d ^= k[4 * r + 3];}
419 
420 #define LK(r, a, b, c, d, e) {\
421  a = k[(8-r)*4 + 0]; \
422  b = k[(8-r)*4 + 1]; \
423  c = k[(8-r)*4 + 2]; \
424  d = k[(8-r)*4 + 3];}
425 
426 #define SK(r, a, b, c, d, e) {\
427  k[(8-r)*4 + 4] = a; \
428  k[(8-r)*4 + 5] = b; \
429  k[(8-r)*4 + 6] = c; \
430  k[(8-r)*4 + 7] = d;}
431 
432 void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen);
433 
uint8_t byte
Definition: Common.h:57
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen)
Definition: serpent.cpp:13
#define NAMESPACE_END
Definition: config.h:201
unsigned int word32
Definition: config.h:231