| 1 |
{KeccakF[1600] state permutation for 32 bit compilers with MMX/ASM support} |
| 2 |
{Compiled from Pascal source to MMX with ad-hoc compiler by Eric Grange. } |
| 3 |
{Slightly changed by WE to make it compatible with plain D6+ and FPC. } |
| 4 |
|
| 5 |
{This code is used if the symbol USE_MMCODE is defined in unit sha3, FPC } |
| 6 |
{and Delphi 6+. For the variables, structure etc see also kperm_64.inc. } |
| 7 |
|
| 8 |
{$ifdef HAS_UINT64} |
| 9 |
type u64bit = uint64; |
| 10 |
{$else} |
| 11 |
type u64bit = int64; |
| 12 |
{$endif} |
| 13 |
|
| 14 |
{$ifdef FPC} |
| 15 |
{$ASMMODE INTEL} |
| 16 |
{$endif} |
| 17 |
|
| 18 |
type |
| 19 |
pu64bit = ^u64bit; |
| 20 |
|
| 21 |
const |
| 22 |
cRoundConstants: array[0..23] of u64bit = ( |
| 23 |
u64bit($0000000000000001), u64bit($0000000000008082), |
| 24 |
u64bit($800000000000808A), u64bit($8000000080008000), |
| 25 |
u64bit($000000000000808B), u64bit($0000000080000001), |
| 26 |
u64bit($8000000080008081), u64bit($8000000000008009), |
| 27 |
u64bit($000000000000008A), u64bit($0000000000000088), |
| 28 |
u64bit($0000000080008009), u64bit($000000008000000A), |
| 29 |
u64bit($000000008000808B), u64bit($800000000000008B), |
| 30 |
u64bit($8000000000008089), u64bit($8000000000008003), |
| 31 |
u64bit($8000000000008002), u64bit($8000000000000080), |
| 32 |
u64bit($000000000000800A), u64bit($800000008000000A), |
| 33 |
u64bit($8000000080008081), u64bit($8000000000008080), |
| 34 |
u64bit($0000000080000001), u64bit($8000000080008008)); |
| 35 |
|
| 36 |
|
| 37 |
{---------------------------------------------------------------------------} |
| 38 |
procedure KeccakPermutationKernel(B, A, C: pointer); |
| 39 |
begin |
| 40 |
asm |
| 41 |
mov eax, [B] |
| 42 |
mov edx, [A] |
| 43 |
mov ecx, [C] |
| 44 |
add edx, 128 |
| 45 |
add eax, 128 |
| 46 |
// Theta |
| 47 |
movq mm2, [edx-112] |
| 48 |
movq mm0, [edx-128] |
| 49 |
pxor mm0, [edx-88] |
| 50 |
pxor mm0, [edx-48] |
| 51 |
pxor mm0, [edx-8] |
| 52 |
pxor mm0, [edx+32] |
| 53 |
movq mm1, [edx-120] |
| 54 |
movq [ecx], mm0 |
| 55 |
pxor mm2, [edx-72] |
| 56 |
pxor mm2, [edx-32] |
| 57 |
pxor mm2, [edx+8] |
| 58 |
pxor mm2, [edx+48] |
| 59 |
movq mm3, [edx-104] |
| 60 |
movq [ecx+16], mm2 |
| 61 |
pxor mm1, [edx-80] |
| 62 |
pxor mm1, [edx-40] |
| 63 |
pxor mm1, [edx] |
| 64 |
pxor mm1, [edx+40] |
| 65 |
movq mm4, [edx-96] |
| 66 |
movq [ecx+8], mm1 |
| 67 |
pxor mm3, [edx-64] |
| 68 |
pxor mm3, [edx-24] |
| 69 |
pxor mm3, [edx+16] |
| 70 |
pxor mm3, [edx+56] |
| 71 |
movq [ecx+24], mm3 |
| 72 |
pxor mm4, [edx-56] |
| 73 |
pxor mm4, [edx-16] |
| 74 |
pxor mm4, [edx+24] |
| 75 |
pxor mm4, [edx+64] |
| 76 |
movq [ecx+32], mm4 |
| 77 |
movq mm6, mm2 |
| 78 |
psllq mm2, 1 |
| 79 |
psrlq mm6, 63 |
| 80 |
por mm2, mm6 |
| 81 |
pxor mm2, mm0 |
| 82 |
movq mm7, mm0 |
| 83 |
psllq mm0, 1 |
| 84 |
psrlq mm7, 63 |
| 85 |
por mm0, mm7 |
| 86 |
pxor mm0, mm3 |
| 87 |
movq mm5, mm3 |
| 88 |
psllq mm3, 1 |
| 89 |
psrlq mm5, 63 |
| 90 |
por mm3, mm5 |
| 91 |
pxor mm3, mm1 |
| 92 |
movq mm6, mm1 |
| 93 |
psllq mm1, 1 |
| 94 |
psrlq mm6, 63 |
| 95 |
por mm1, mm6 |
| 96 |
pxor mm1, mm4 |
| 97 |
movq mm7, mm4 |
| 98 |
psllq mm4, 1 |
| 99 |
psrlq mm7, 63 |
| 100 |
por mm4, mm7 |
| 101 |
pxor mm4, [ecx+16] |
| 102 |
// Rho Pi |
| 103 |
movq mm5, [edx-24] |
| 104 |
movq mm6, [edx-80] |
| 105 |
pxor mm6, mm2 |
| 106 |
movq mm7, mm6 |
| 107 |
psllq mm6, 44 |
| 108 |
psrlq mm7, 20 |
| 109 |
por mm6, mm7 |
| 110 |
movq [eax-120], mm6 |
| 111 |
movq mm6, [edx+24] |
| 112 |
pxor mm5, mm4 |
| 113 |
movq mm7, mm5 |
| 114 |
psllq mm5, 25 |
| 115 |
psrlq mm7, 39 |
| 116 |
por mm5, mm7 |
| 117 |
movq [eax-32], mm5 |
| 118 |
movq mm5, [edx-96] |
| 119 |
pxor mm6, mm0 |
| 120 |
movq mm7, mm6 |
| 121 |
psllq mm6, 8 |
| 122 |
psrlq mm7, 56 |
| 123 |
por mm6, mm7 |
| 124 |
movq [eax-24], mm6 |
| 125 |
movq mm6, [edx-64] |
| 126 |
pxor mm5, mm0 |
| 127 |
movq mm7, mm5 |
| 128 |
psllq mm5, 27 |
| 129 |
psrlq mm7, 37 |
| 130 |
por mm5, mm7 |
| 131 |
movq [eax-8], mm5 |
| 132 |
pxor mm6, mm4 |
| 133 |
movq mm7, mm6 |
| 134 |
psllq mm6, 55 |
| 135 |
psrlq mm7, 9 |
| 136 |
por mm6, mm7 |
| 137 |
movq mm5, [edx] |
| 138 |
movq [eax+40], mm6 |
| 139 |
movq mm6, [edx-128] |
| 140 |
pxor mm6, mm1 |
| 141 |
movq [eax-128], mm6 |
| 142 |
movq mm6, [edx-88] |
| 143 |
pxor mm5, mm2 |
| 144 |
movq mm7, mm5 |
| 145 |
psllq mm5, 45 |
| 146 |
psrlq mm7, 19 |
| 147 |
por mm5, mm7 |
| 148 |
movq [eax-64], mm5 |
| 149 |
movq mm5, [edx-16] |
| 150 |
pxor mm6, mm1 |
| 151 |
movq mm7, mm6 |
| 152 |
psllq mm6, 36 |
| 153 |
psrlq mm7, 28 |
| 154 |
por mm6, mm7 |
| 155 |
movq [eax], mm6 |
| 156 |
movq mm6, [edx+8] |
| 157 |
pxor mm5, mm0 |
| 158 |
movq mm7, mm5 |
| 159 |
psllq mm5, 39 |
| 160 |
psrlq mm7, 25 |
| 161 |
por mm5, mm7 |
| 162 |
movq [eax+48], mm5 |
| 163 |
movq mm5, [edx-104] |
| 164 |
pxor mm6, mm3 |
| 165 |
movq mm7, mm6 |
| 166 |
psllq mm6, 15 |
| 167 |
psrlq mm7, 49 |
| 168 |
por mm6, mm7 |
| 169 |
movq [eax+16], mm6 |
| 170 |
movq mm6, [edx-120] |
| 171 |
pxor mm5, mm4 |
| 172 |
movq mm7, mm5 |
| 173 |
psllq mm5, 28 |
| 174 |
psrlq mm7, 36 |
| 175 |
por mm5, mm7 |
| 176 |
movq [eax-88], mm5 |
| 177 |
movq mm5, [edx-48] |
| 178 |
pxor mm6, mm2 |
| 179 |
movq mm7, mm6 |
| 180 |
psllq mm6, 1 |
| 181 |
psrlq mm7, 63 |
| 182 |
por mm6, mm7 |
| 183 |
movq [eax-48], mm6 |
| 184 |
movq mm6, [edx+56] |
| 185 |
pxor mm5, mm1 |
| 186 |
movq mm7, mm5 |
| 187 |
psllq mm5, 3 |
| 188 |
psrlq mm7, 61 |
| 189 |
por mm5, mm7 |
| 190 |
movq [eax-72], mm5 |
| 191 |
movq mm5, [edx+16] |
| 192 |
pxor mm6, mm4 |
| 193 |
movq mm7, mm6 |
| 194 |
psllq mm6, 56 |
| 195 |
psrlq mm7, 8 |
| 196 |
por mm6, mm7 |
| 197 |
movq [eax+24], mm6 |
| 198 |
movq mm6, [edx+32] |
| 199 |
pxor mm5, mm4 |
| 200 |
movq mm7, mm5 |
| 201 |
psllq mm5, 21 |
| 202 |
psrlq mm7, 43 |
| 203 |
por mm5, mm7 |
| 204 |
movq [eax-104], mm5 |
| 205 |
movq mm5, [edx+40] |
| 206 |
pxor mm6, mm1 |
| 207 |
movq mm7, mm6 |
| 208 |
psllq mm6, 18 |
| 209 |
psrlq mm7, 46 |
| 210 |
por mm6, mm7 |
| 211 |
movq [eax-16], mm6 |
| 212 |
movq mm6, [edx-72] |
| 213 |
pxor mm5, mm2 |
| 214 |
movq mm7, mm5 |
| 215 |
psllq mm5, 2 |
| 216 |
psrlq mm7, 62 |
| 217 |
por mm5, mm7 |
| 218 |
movq [eax+64], mm5 |
| 219 |
movq mm5, [edx-40] |
| 220 |
pxor mm6, mm3 |
| 221 |
movq mm7, mm6 |
| 222 |
psllq mm6, 6 |
| 223 |
psrlq mm7, 58 |
| 224 |
por mm6, mm7 |
| 225 |
movq [eax-40], mm6 |
| 226 |
movq mm6, [edx-56] |
| 227 |
pxor mm5, mm2 |
| 228 |
movq mm7, mm5 |
| 229 |
psllq mm5, 10 |
| 230 |
psrlq mm7, 54 |
| 231 |
por mm5, mm7 |
| 232 |
movq [eax+8], mm5 |
| 233 |
movq mm5, [edx-112] |
| 234 |
pxor mm6, mm0 |
| 235 |
movq mm7, mm6 |
| 236 |
psllq mm6, 20 |
| 237 |
psrlq mm7, 44 |
| 238 |
por mm6, mm7 |
| 239 |
movq [eax-80], mm6 |
| 240 |
movq mm6, [edx+48] |
| 241 |
pxor mm5, mm3 |
| 242 |
movq mm7, mm5 |
| 243 |
psllq mm5, 62 |
| 244 |
psrlq mm7, 2 |
| 245 |
por mm5, mm7 |
| 246 |
movq [eax+32], mm5 |
| 247 |
movq mm5, [edx-8] |
| 248 |
pxor mm6, mm3 |
| 249 |
movq mm7, mm6 |
| 250 |
psllq mm6, 61 |
| 251 |
psrlq mm7, 3 |
| 252 |
por mm6, mm7 |
| 253 |
movq [eax-56], mm6 |
| 254 |
movq mm6, [edx+64] |
| 255 |
pxor mm5, mm1 |
| 256 |
movq mm7, mm5 |
| 257 |
psllq mm5, 41 |
| 258 |
psrlq mm7, 23 |
| 259 |
por mm5, mm7 |
| 260 |
movq [eax+56], mm5 |
| 261 |
movq mm5, [edx-32] |
| 262 |
pxor mm6, mm0 |
| 263 |
movq mm7, mm6 |
| 264 |
psllq mm6, 14 |
| 265 |
psrlq mm7, 50 |
| 266 |
por mm6, mm7 |
| 267 |
movq [eax-96], mm6 |
| 268 |
pxor mm5, mm3 |
| 269 |
movq mm7, mm5 |
| 270 |
psllq mm5, 43 |
| 271 |
psrlq mm7, 21 |
| 272 |
por mm5, mm7 |
| 273 |
movq [eax-112], mm5 |
| 274 |
// Chi |
| 275 |
movq mm4, [eax-128] |
| 276 |
movq mm2, [eax-104] |
| 277 |
movq mm1, mm5 |
| 278 |
movq mm3, mm6 |
| 279 |
pandn mm3, mm4 |
| 280 |
pxor mm3, mm2 |
| 281 |
movq mm0, [eax-120] |
| 282 |
movq [edx-104], mm3 |
| 283 |
pandn mm1, mm2 |
| 284 |
pxor mm1, mm0 |
| 285 |
movq [edx-120], mm1 |
| 286 |
pandn mm2, mm6 |
| 287 |
pxor mm2, mm5 |
| 288 |
movq [edx-112], mm2 |
| 289 |
pandn mm0, mm5 |
| 290 |
pxor mm0, mm4 |
| 291 |
movq mm5, [eax-80] |
| 292 |
movq [edx-128], mm0 |
| 293 |
pandn mm4, [eax-120] |
| 294 |
pxor mm4, mm6 |
| 295 |
movq mm0, [eax-56] |
| 296 |
movq mm6, [eax-72] |
| 297 |
movq mm1, [eax-88] |
| 298 |
movq [edx-96], mm4 |
| 299 |
pandn mm5, mm6 |
| 300 |
pxor mm5, mm1 |
| 301 |
movq mm7, [eax-64] |
| 302 |
movq [edx-88], mm5 |
| 303 |
pandn mm0, mm1 |
| 304 |
pxor mm0, mm7 |
| 305 |
movq [edx-64], mm0 |
| 306 |
pandn mm6, mm7 |
| 307 |
pxor mm6, [eax-80] |
| 308 |
movq mm5, [eax-16] |
| 309 |
movq [edx-80], mm6 |
| 310 |
pandn mm1, [eax-80] |
| 311 |
pxor mm1, [eax-56] |
| 312 |
movq mm6, [eax-48] |
| 313 |
movq mm4, [eax-24] |
| 314 |
movq [edx-56], mm1 |
| 315 |
pandn mm5, mm6 |
| 316 |
pxor mm5, mm4 |
| 317 |
movq mm3, [eax-32] |
| 318 |
movq [edx-24], mm5 |
| 319 |
pandn mm7, [eax-56] |
| 320 |
pxor mm7, [eax-72] |
| 321 |
movq mm2, [eax-40] |
| 322 |
movq [edx-72], mm7 |
| 323 |
pandn mm3, mm4 |
| 324 |
pxor mm3, mm2 |
| 325 |
movq mm0, [eax+8] |
| 326 |
movq [edx-40], mm3 |
| 327 |
pandn mm2, [eax-32] |
| 328 |
pxor mm2, mm6 |
| 329 |
movq mm7, [eax] |
| 330 |
movq mm1, [eax+16] |
| 331 |
movq [edx-48], mm2 |
| 332 |
pandn mm0, mm1 |
| 333 |
pxor mm0, mm7 |
| 334 |
movq [edx], mm0 |
| 335 |
pandn mm6, [eax-40] |
| 336 |
pxor mm6, [eax-16] |
| 337 |
movq mm3, [eax-8] |
| 338 |
movq [edx-16], mm6 |
| 339 |
movq mm6, [eax+56] |
| 340 |
pandn mm7, [eax+8] |
| 341 |
pxor mm7, mm3 |
| 342 |
movq mm5, [eax+48] |
| 343 |
movq [edx-8], mm7 |
| 344 |
movq mm7, [eax+64] |
| 345 |
pandn mm6, mm7 |
| 346 |
pxor mm6, mm5 |
| 347 |
movq [edx+48], mm6 |
| 348 |
pandn mm4, [eax-16] |
| 349 |
pxor mm4, [eax-32] |
| 350 |
movq mm2, [eax+24] |
| 351 |
movq [edx-32], mm4 |
| 352 |
pandn mm3, [eax] |
| 353 |
pxor mm3, mm2 |
| 354 |
movq mm4, [eax+40] |
| 355 |
movq mm0, [eax+32] |
| 356 |
movq [edx+24], mm3 |
| 357 |
pandn mm7, mm0 |
| 358 |
pxor mm7, [eax+56] |
| 359 |
movq [edx+56], mm7 |
| 360 |
pandn mm4, mm5 |
| 361 |
pxor mm4, mm0 |
| 362 |
movq [edx+32], mm4 |
| 363 |
pandn mm1, mm2 |
| 364 |
pxor mm1, [eax+8] |
| 365 |
movq [edx+8], mm1 |
| 366 |
pandn mm0, [eax+40] |
| 367 |
pxor mm0, [eax+64] |
| 368 |
movq [edx+64], mm0 |
| 369 |
pandn mm5, [eax+56] |
| 370 |
pxor mm5, [eax+40] |
| 371 |
movq [edx+40], mm5 |
| 372 |
pandn mm2, [eax-8] |
| 373 |
pxor mm2, [eax+16] |
| 374 |
movq [edx+16], mm2 |
| 375 |
end; |
| 376 |
end; |
| 377 |
|
| 378 |
|
| 379 |
{---------------------------------------------------------------------------} |
| 380 |
procedure EMMS; |
| 381 |
begin |
| 382 |
asm |
| 383 |
emms |
| 384 |
end; |
| 385 |
end; |
| 386 |
|
| 387 |
|
| 388 |
{---------------------------------------------------------------------------} |
| 389 |
procedure KeccakPermutation(var state: TState_L); |
| 390 |
var |
| 391 |
A: u64bit absolute state; |
| 392 |
B: array[0..24] of u64bit; |
| 393 |
C: array[0..4] of u64bit; |
| 394 |
i: integer; |
| 395 |
begin |
| 396 |
for i:=0 to 23 do begin |
| 397 |
KeccakPermutationKernel(@B, @A, @C); |
| 398 |
A := A xor cRoundConstants[i]; |
| 399 |
end; |
| 400 |
EMMS; |
| 401 |
end; |
| 402 |
|
| 403 |
|
| 404 |
{---------------------------------------------------------------------------} |
| 405 |
procedure ExtractFromState(outp: pointer; const state: TState_L; laneCount: integer); |
| 406 |
var |
| 407 |
pI, pS: pu64bit; |
| 408 |
i: integer; |
| 409 |
begin |
| 410 |
pI := outp; |
| 411 |
pS := @state[0]; |
| 412 |
for i:=laneCount-1 downto 0 do begin |
| 413 |
pI^ := pS^; |
| 414 |
inc(pI); |
| 415 |
inc(pS); |
| 416 |
end; |
| 417 |
end; |
| 418 |
|
| 419 |
|
| 420 |
{---------------------------------------------------------------------------} |
| 421 |
procedure XorIntoState(var state: TState_L; inp: PLongint; laneCount: integer); |
| 422 |
{-Include input message data bits into the sponge state} |
| 423 |
var |
| 424 |
pI, pS: pu64bit; |
| 425 |
i: integer; |
| 426 |
begin |
| 427 |
pI := pu64bit(inp); |
| 428 |
pS := @state[0]; |
| 429 |
for i:=laneCount-1 downto 0 do begin |
| 430 |
pS^ := pS^ xor pI^; |
| 431 |
inc(pI); |
| 432 |
inc(pS); |
| 433 |
end; |
| 434 |
end; |