]> Git Repo - haraka-avx512.git/blob - haraka.c
finnal commit hopefully
[haraka-avx512.git] / haraka.c
1 /*\r
2 The MIT License (MIT)\r
3 \r
4 Copyright (c) 2016 kste\r
5 \r
6 Permission is hereby granted, free of charge, to any person obtaining a copy\r
7 of this software and associated documentation files (the "Software"), to deal\r
8 in the Software without restriction, including without limitation the rights\r
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
10 copies of the Software, and to permit persons to whom the Software is\r
11 furnished to do so, subject to the following conditions:\r
12 \r
13 The above copyright notice and this permission notice shall be included in all\r
14 copies or substantial portions of the Software.\r
15 \r
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
22 SOFTWARE.\r
23 \r
24 Optimized Implementations for Haraka256 and Haraka512\r
25 */\r
26 \r
27 #include <stdio.h>\r
28 #include "haraka.h"\r
29 #include <stdint.h>\r
30 u512 rc256[20];\r
31 u512 rc[10];\r
32 u512 rc0[10] = {0};\r
33 u512 MIX_4;\r
34 u512 MIX_2;\r
35 void load_constants() {\r
36   //MIX_4 = _mm512_set_epi32(3,11,7,15,8,0,12,4,9,1,13,5,2,10,6,14); stupid thing goes the wrong way\r
37         MIX_4 = _mm512_set_epi32(14,6,10,2,5,13,1,9,4,12,0,8,15,7,11,3);\r
38         MIX_2 = _mm512_set_epi32(7+8,3+8,6+8,2+8,5+8,1+8,4+8,0+8,7,3,6,2,5,1,4,0);\r
39 \r
40   rc[0] = _mm512_set_epi32(0x0ed6eae6,0x2e7b4f08,0xbbf3bcaf,0xfd5b4f79,0x3402de2d,0x53f28498,0xcf029d60,0x9f029114,0x8b66b4e1,0x88f3a06b,0x640f6ba4,0x2f08f717,0x0684704c,0xe620c00a,0xb2c5fef0,0x75817b9d);\r
41   rc[1] = _mm512_set_epi32(0x2924d9b0,0xafcacc07,0x675ffde2,0x1fc70b3b,0x67c28f43,0x5e2e7cd0,0xe2412761,0xda4fef1b,0x7eeacdee,0x6e9032b7,0x8d5335ed,0x2b8a057b,0xcbcfb0cb,0x4872448b,0x79eecd1c,0xbe397044);\r
42   rc[2] = _mm512_set_epi32(0xfa0478a6,0xde6f5572,0x4aaa9ec8,0x5c9d2d8a,0xb2cc0bb9,0x941723bf,0x69028b2e,0x8df69800,0x1c30bf84,0xd4b7cd64,0x5b2a404f,0xad037e33,0xab4d63f1,0xe6867fe9,0xecdb8fca,0xb9d465ee);\r
43   rc[3] = _mm512_set_epi32(0x21025ed8,0x9d199c4f,0x78a2c7e3,0x27e593ec,0xaf044988,0x4b050084,0x5f9600c9,0x9ca8eca6,0x1ea10344,0xf449a236,0x32d611ae,0xbb6a12ee,0xdfb49f2b,0x6b772a12,0x0efa4f2e,0x29129fd4);\r
44   rc[4] = _mm512_set_epi32(0x9223973c,0x226b68bb,0x2caf92e8,0x36d1943a,0x5aca45c2,0x21300443,0x81c29153,0xf6fc9ac6,0x6260700d,0x6186b017,0x37f2efd9,0x10307d6b,0xbf3aaaf8,0xa759c9b7,0xb9282ecd,0x82d40173);\r
45   rc[5] = _mm512_set_epi32(0x734bd3dc,0xe2e4d19c,0x2db91a4e,0xc72bf77d,0xbb606268,0xffeba09c,0x83e48de3,0xcb2212b1,0xdb863ce5,0xaef0c677,0x933dfddd,0x24e1128d,0xd3bf9238,0x225886eb,0x6cbab958,0xe51071b4);\r
46   rc[6] = _mm512_set_epi32(0xcda75a17,0xd6de7d77,0x6d1be5b9,0xb88617f9,0x6df3614b,0x3c755977,0x8e5e2302,0x7eca472c,0xdba775a8,0xe707eff6,0x03b231dd,0x16eb6899,0x43bb47c3,0x61301b43,0x4b1415c4,0x2cb3924e);\r
47   rc[7] = _mm512_set_epi32(0xf0b1a5a1,0x96e90cab,0x80bbbabc,0x63a4a350,0x2cee0c75,0x00da619c,0xe4ed0353,0x600ed0d9,0xcb1e6950,0xf957332b,0xa2531159,0x3bf327c1,0xec6b43f0,0x6ba8e9aa,0x9d6c069d,0xa946ee5d);\r
48   rc[8] = _mm512_set_epi32(0x26f65241,0xcbe55438,0x43ce5918,0xffbaafde,0x34bb8a5b,0x5f427fd7,0xaeb6b779,0x360a16f6,0x17bb8f38,0xd554a40b,0x8814f3a8,0x2e75b442,0xae3db102,0x5e962988,0xab0dde30,0x938dca39);\r
49   rc[9] = _mm512_set_epi32(0x756acc03,0x02288288,0x4ad6bdfd,0xe9c59da1,0xa0c1613c,0xba7ed22b,0xc173bc0f,0x48a659cf,0xae51a51a,0x1bdff7be,0x40c06e28,0x22901235,0x4ce99a54,0xb9f3026a,0xa2ca9cf7,0x839ec978);\r
50 \r
51 \r
52   rc256[0] = _mm512_set_epi32(0x8b66b4e1,0x88f3a06b,0x640f6ba4,0x2f08f717,0x0684704c,0xe620c00a,0xb2c5fef0,0x75817b9d,0x8b66b4e1,0x88f3a06b,0x640f6ba4,0x2f08f717,0x0684704c,0xe620c00a,0xb2c5fef0,0x75817b9d);\r
53   rc256[1] = _mm512_set_epi32(0x7eeacdee,0x6e9032b7,0x8d5335ed,0x2b8a057b,0xcbcfb0cb,0x4872448b,0x79eecd1c,0xbe397044,0x7eeacdee,0x6e9032b7,0x8d5335ed,0x2b8a057b,0xcbcfb0cb,0x4872448b,0x79eecd1c,0xbe397044);\r
54   rc256[2] = _mm512_set_epi32(0x1c30bf84,0xd4b7cd64,0x5b2a404f,0xad037e33,0xab4d63f1,0xe6867fe9,0xecdb8fca,0xb9d465ee,0x1c30bf84,0xd4b7cd64,0x5b2a404f,0xad037e33,0xab4d63f1,0xe6867fe9,0xecdb8fca,0xb9d465ee);\r
55   rc256[3] = _mm512_set_epi32(0x1ea10344,0xf449a236,0x32d611ae,0xbb6a12ee,0xdfb49f2b,0x6b772a12,0x0efa4f2e,0x29129fd4,0x1ea10344,0xf449a236,0x32d611ae,0xbb6a12ee,0xdfb49f2b,0x6b772a12,0x0efa4f2e,0x29129fd4);\r
56   rc256[4] = _mm512_set_epi32(0x6260700d,0x6186b017,0x37f2efd9,0x10307d6b,0xbf3aaaf8,0xa759c9b7,0xb9282ecd,0x82d40173,0x6260700d,0x6186b017,0x37f2efd9,0x10307d6b,0xbf3aaaf8,0xa759c9b7,0xb9282ecd,0x82d40173);\r
57   rc256[5] = _mm512_set_epi32(0xdb863ce5,0xaef0c677,0x933dfddd,0x24e1128d,0xd3bf9238,0x225886eb,0x6cbab958,0xe51071b4,0xdb863ce5,0xaef0c677,0x933dfddd,0x24e1128d,0xd3bf9238,0x225886eb,0x6cbab958,0xe51071b4);\r
58   rc256[6] = _mm512_set_epi32(0xdba775a8,0xe707eff6,0x03b231dd,0x16eb6899,0x43bb47c3,0x61301b43,0x4b1415c4,0x2cb3924e,0xdba775a8,0xe707eff6,0x03b231dd,0x16eb6899,0x43bb47c3,0x61301b43,0x4b1415c4,0x2cb3924e);\r
59   rc256[7] = _mm512_set_epi32(0xcb1e6950,0xf957332b,0xa2531159,0x3bf327c1,0xec6b43f0,0x6ba8e9aa,0x9d6c069d,0xa946ee5d,0xcb1e6950,0xf957332b,0xa2531159,0x3bf327c1,0xec6b43f0,0x6ba8e9aa,0x9d6c069d,0xa946ee5d);\r
60   rc256[8] = _mm512_set_epi32(0x17bb8f38,0xd554a40b,0x8814f3a8,0x2e75b442,0xae3db102,0x5e962988,0xab0dde30,0x938dca39,0x17bb8f38,0xd554a40b,0x8814f3a8,0x2e75b442,0xae3db102,0x5e962988,0xab0dde30,0x938dca39);\r
61   rc256[9] = _mm512_set_epi32(0xae51a51a,0x1bdff7be,0x40c06e28,0x22901235,0x4ce99a54,0xb9f3026a,0xa2ca9cf7,0x839ec978,0xae51a51a,0x1bdff7be,0x40c06e28,0x22901235,0x4ce99a54,0xb9f3026a,0xa2ca9cf7,0x839ec978);\r
62   rc256[10] = _mm512_set_epi32(0x0ed6eae6,0x2e7b4f08,0xbbf3bcaf,0xfd5b4f79,0x3402de2d,0x53f28498,0xcf029d60,0x9f029114,0x0ed6eae6,0x2e7b4f08,0xbbf3bcaf,0xfd5b4f79,0x3402de2d,0x53f28498,0xcf029d60,0x9f029114); // definatly a better way to do this than this\r
63   rc256[11] = _mm512_set_epi32(0x2924d9b0,0xafcacc07,0x675ffde2,0x1fc70b3b,0x67c28f43,0x5e2e7cd0,0xe2412761,0xda4fef1b,0x2924d9b0,0xafcacc07,0x675ffde2,0x1fc70b3b,0x67c28f43,0x5e2e7cd0,0xe2412761,0xda4fef1b);\r
64   rc256[12] = _mm512_set_epi32(0xfa0478a6,0xde6f5572,0x4aaa9ec8,0x5c9d2d8a,0xb2cc0bb9,0x941723bf,0x69028b2e,0x8df69800,0xfa0478a6,0xde6f5572,0x4aaa9ec8,0x5c9d2d8a,0xb2cc0bb9,0x941723bf,0x69028b2e,0x8df69800);\r
65   rc256[13] = _mm512_set_epi32(0x21025ed8,0x9d199c4f,0x78a2c7e3,0x27e593ec,0xaf044988,0x4b050084,0x5f9600c9,0x9ca8eca6,0x21025ed8,0x9d199c4f,0x78a2c7e3,0x27e593ec,0xaf044988,0x4b050084,0x5f9600c9,0x9ca8eca6);\r
66   rc256[14] = _mm512_set_epi32(0x9223973c,0x226b68bb,0x2caf92e8,0x36d1943a,0x5aca45c2,0x21300443,0x81c29153,0xf6fc9ac6,0x9223973c,0x226b68bb,0x2caf92e8,0x36d1943a,0x5aca45c2,0x21300443,0x81c29153,0xf6fc9ac6);\r
67   rc256[15] = _mm512_set_epi32(0x734bd3dc,0xe2e4d19c,0x2db91a4e,0xc72bf77d,0xbb606268,0xffeba09c,0x83e48de3,0xcb2212b1,0x734bd3dc,0xe2e4d19c,0x2db91a4e,0xc72bf77d,0xbb606268,0xffeba09c,0x83e48de3,0xcb2212b1);\r
68   rc256[16] = _mm512_set_epi32(0xcda75a17,0xd6de7d77,0x6d1be5b9,0xb88617f9,0x6df3614b,0x3c755977,0x8e5e2302,0x7eca472c,0xcda75a17,0xd6de7d77,0x6d1be5b9,0xb88617f9,0x6df3614b,0x3c755977,0x8e5e2302,0x7eca472c);\r
69   rc256[17] = _mm512_set_epi32(0xf0b1a5a1,0x96e90cab,0x80bbbabc,0x63a4a350,0x2cee0c75,0x00da619c,0xe4ed0353,0x600ed0d9,0xf0b1a5a1,0x96e90cab,0x80bbbabc,0x63a4a350,0x2cee0c75,0x00da619c,0xe4ed0353,0x600ed0d9);\r
70   rc256[18] = _mm512_set_epi32(0x26f65241,0xcbe55438,0x43ce5918,0xffbaafde,0x34bb8a5b,0x5f427fd7,0xaeb6b779,0x360a16f6,0x26f65241,0xcbe55438,0x43ce5918,0xffbaafde,0x34bb8a5b,0x5f427fd7,0xaeb6b779,0x360a16f6);\r
71   rc256[19] = _mm512_set_epi32(0x756acc03,0x02288288,0x4ad6bdfd,0xe9c59da1,0xa0c1613c,0xba7ed22b,0xc173bc0f,0x48a659cf,0x756acc03,0x02288288,0x4ad6bdfd,0xe9c59da1,0xa0c1613c,0xba7ed22b,0xc173bc0f,0x48a659cf);\r
72 \r
73 \r
74 }//duck its endien ness // good programers make programs to make their programs i fixed it\r
75 \r
76 \r
77 int test_implementations() {\r
78         alignas(64)  unsigned char in[64*8];\r
79         alignas(64)  unsigned char out256[32*8];\r
80         alignas(64)  unsigned char out512[32*8];\r
81         alignas(64)  unsigned char testvector256[32] = {0x80, 0x27, 0xcc, 0xb8, 0x79, 0x49, 0x77, 0x4b,\r
82                                      0x78, 0xd0, 0x54, 0x5f, 0xb7, 0x2b, 0xf7, 0x0c,\r
83                                      0x69, 0x5c, 0x2a, 0x09, 0x23, 0xcb, 0xd4, 0x7b,\r
84                                      0xba, 0x11, 0x59, 0xef, 0xbf, 0x2b, 0x2c, 0x1c};\r
85 \r
86         alignas(64)  unsigned char testvector512[32] = {0xbe, 0x7f, 0x72, 0x3b, 0x4e, 0x80, 0xa9, 0x98,\r
87                                     0x13, 0xb2, 0x92, 0x28, 0x7f, 0x30, 0x6f, 0x62,\r
88                                     0x5a, 0x6d, 0x57, 0x33, 0x1c, 0xae, 0x5f, 0x34,\r
89                                     0xdd, 0x92, 0x77, 0xb0, 0x94, 0x5b, 0xe2, 0xaa};\r
90 \r
91 \r
92 \r
93   int i;\r
94 \r
95   // Input for testvector\r
96   for(i = 0; i < 512; i++) {\r
97     in[i] = i % 64;\r
98   }\r
99 \r
100   load_constants();\r
101   haraka256_4x(out512, in);\r
102 \r
103   // Verify output\r
104   for(i = 0; i < 32; i++) {\r
105     if (out512[i % 32] != testvector256[i]) {\r
106       printf("Error: testvector incorrect.\n");\r
107       return -1;\r
108     }\r
109   }\r
110   return 0;\r
111 }\r
112 \r
113 void haraka256(unsigned char *out, const unsigned char *in) {\r
114           u512 s,i;\r
115           s = LOAD (in);\r
116           i = s;\r
117 \r
118           AES2(s, 0);\r
119           MIX2(s);\r
120 \r
121           AES2(s, 1);\r
122           MIX2(s);\r
123 \r
124           AES2(s, 2);\r
125           MIX2(s);\r
126 \r
127           AES2(s, 3);\r
128           MIX2(s);\r
129 \r
130           AES2(s, 4);\r
131           MIX2(s);\r
132 \r
133           s = _mm512_xor_si512(s, i);\r
134 \r
135           STORE(out, s);\r
136 }\r
137 \r
138 \r
139 \r
140 void haraka256_4x(unsigned char *out, const unsigned char *in) {\r
141           u512 s[2] ,i[2];\r
142           s[0] = LOAD (in);\r
143           s[1] = LOAD (in+512);\r
144 \r
145           i[0] = s[0];\r
146           i[1] = s[1];\r
147 \r
148 \r
149           AES2_4x(s, 0);\r
150 \r
151           MIX2(s[0]);\r
152           MIX2(s[1]);\r
153 \r
154           AES2_4x(s, 1);\r
155 \r
156           MIX2(s[0]);\r
157           MIX2(s[1]);\r
158 \r
159           AES2_4x(s, 2);\r
160 \r
161           MIX2(s[0]);\r
162           MIX2(s[1]);\r
163 \r
164           AES2_4x(s, 3);\r
165 \r
166           MIX2(s[0]);\r
167           MIX2(s[1]);\r
168 \r
169           AES2_4x(s, 4);\r
170 \r
171           MIX2(s[0]);\r
172           MIX2(s[1]);\r
173 \r
174           s[0] = _mm512_xor_si512(s[0], i[0]);\r
175           s[1] = _mm512_xor_si512(s[1], i[1]);\r
176 \r
177           STORE(out, s[0]);\r
178           STORE(out+512, s[1]);\r
179 \r
180 }\r
181 \r
182 void haraka256_8x(unsigned char *out, const unsigned char *in) {\r
183   haraka256_4x(out, in);\r
184   haraka256_4x(out + 128, in + 128);\r
185 }\r
186 \r
187 void haraka512(unsigned char *out, const unsigned char *in) {\r
188   u512 s,i;\r
189   s = LOAD (in);\r
190   i = s;\r
191 \r
192   AES4(s, 0);\r
193   MIX4(s);\r
194 \r
195   AES4(s, 2);\r
196   MIX4(s);\r
197 \r
198   AES4(s, 4);\r
199   MIX4(s);\r
200 \r
201   AES4(s, 6);\r
202   MIX4(s);\r
203 \r
204   AES4(s, 8);\r
205   MIX4(s);\r
206 \r
207   s = _mm512_xor_si512(s, i);\r
208 \r
209   TRUNCSTORE(out, s);\r
210 }\r
211 \r
212 void haraka512_zero(unsigned char *out, const unsigned char *in) {\r
213   u512 s,i;\r
214   s = LOAD (in);\r
215   i = s;\r
216   AES4_zero(s, 0);\r
217   MIX4(s);\r
218 \r
219   AES4_zero(s, 2);\r
220   MIX4(s);\r
221 \r
222   AES4_zero(s, 4);\r
223   MIX4(s);\r
224 \r
225   AES4_zero(s, 6);\r
226   MIX4(s);\r
227 \r
228   AES4_zero(s, 8);\r
229   MIX4(s);\r
230 \r
231   s = _mm512_xor_si512(s, i);\r
232 \r
233   TRUNCSTORE(out, s);\r
234 }\r
235 \r
236 void haraka512_4x(unsigned char *out, const unsigned char *in) {\r
237         u512 s[4],i[4];\r
238 \r
239         s[0] = LOAD(in);\r
240         s[1] = LOAD(in + 64);\r
241         s[2] = LOAD(in + 128);\r
242         s[3] = LOAD(in + 192);\r
243         i[0] = s[0];\r
244         i[1] = s[1];\r
245         i[2] = s[2];\r
246         i[3] = s[3];\r
247 \r
248 \r
249 \r
250   AES4_4x(s, 0);\r
251   MIX4(s[0]);\r
252   MIX4(s[1]);\r
253   MIX4(s[2]);\r
254   MIX4(s[3]);\r
255 \r
256   AES4_4x(s, 2);\r
257   MIX4(s[0]);\r
258   MIX4(s[1]);\r
259   MIX4(s[2]);\r
260   MIX4(s[3]);\r
261 \r
262   AES4_4x(s, 4);\r
263   MIX4(s[0]);\r
264   MIX4(s[1]);\r
265   MIX4(s[2]);\r
266   MIX4(s[3]);\r
267 \r
268   AES4_4x(s, 6);\r
269   MIX4(s[0]);\r
270   MIX4(s[1]);\r
271   MIX4(s[2]);\r
272   MIX4(s[3]);\r
273 \r
274   AES4_4x(s, 8);\r
275   MIX4(s[0]);\r
276   MIX4(s[1]);\r
277   MIX4(s[2]);\r
278   MIX4(s[3]);\r
279 \r
280 \r
281   s[0] = _mm512_xor_si512(s[0], i[0]);\r
282   s[1] = _mm512_xor_si512(s[1], i[1]);\r
283   s[2] = _mm512_xor_si512(s[2], i[2]);\r
284   s[3] = _mm512_xor_si512(s[3], i[3]);\r
285 \r
286 \r
287   TRUNCSTORE(out, s[0]);\r
288   TRUNCSTORE(out + 32, s[1]);\r
289   TRUNCSTORE(out + 64, s[2]);\r
290   TRUNCSTORE(out + 96, s[3]);\r
291 }\r
292 \r
293 void haraka512_8x(unsigned char *out, const unsigned char *in) {\r
294   haraka512_4x(out, in);\r
295   haraka512_4x(out + 128, in + 256);\r
296 }\r
This page took 0.042267 seconds and 4 git commands to generate.