2 # Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
3 # Copyright (c) 2012, Intel Corporation. All Rights Reserved.
5 # Licensed under the OpenSSL license (the "License"). You may not use
6 # this file except in compliance with the License. You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
10 # Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
11 # (1) Intel Corporation, Israel Development Center, Haifa, Israel
12 # (2) University of Haifa, Israel
15 # [1] S. Gueron, "Efficient Software Implementations of Modular
16 # Exponentiation", http://eprint.iacr.org/2011/239
17 # [2] S. Gueron, V. Krasnov. "Speeding up Big-Numbers Squaring".
18 # IEEE Proceedings of 9th International Conference on Information
19 # Technology: New Generations (ITNG 2012), 821-823 (2012).
20 # [3] S. Gueron, Efficient Software Implementations of Modular Exponentiation
21 # Journal of Cryptographic Engineering 2:31-43 (2012).
22 # [4] S. Gueron, V. Krasnov: "[PATCH] Efficient and side channel analysis
23 # resistant 512-bit and 1024-bit modular exponentiation for optimizing
24 # RSA1024 and RSA2048 on x86_64 platforms",
25 # http://rt.openssl.org/Ticket/Display.html?id=2582&user=guest&pass=guest
27 # While original submission covers 512- and 1024-bit exponentiation,
28 # this module is limited to 512-bit version only (and as such
29 # accelerates RSA1024 sign). This is because improvement for longer
30 # keys is not high enough to justify the effort, highest measured
31 # was ~5% on Westmere. [This is relative to OpenSSL 1.0.2, upcoming
32 # for the moment of this writing!] Nor does this module implement
33 # "monolithic" complete exponentiation jumbo-subroutine, but adheres
34 # to more modular mixture of C and assembly. And it's optimized even
35 # for processors other than Intel Core family (see table below for
36 # improvement coefficients).
39 # RSA1024 sign/sec this/original |this/rsax(*) this/fips(*)
40 # ----------------+---------------------------
41 # Opteron +13% |+5% +20%
42 # Bulldozer -0% |-1% +10%
44 # Westmere +5% |+14% +17%
45 # Sandy Bridge +2% |+12% +29%
46 # Ivy Bridge +1% |+11% +35%
47 # Haswell(**) -0% |+12% +39%
49 # VIA Nano +70% |+9% +25%
51 # (*) rsax engine and fips numbers are presented for reference
53 # (**) MULX was attempted, but found to give only marginal improvement;
57 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
59 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
61 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
62 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
63 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
64 die "can't locate x86_64-xlate.pl";
66 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
69 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
70 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
74 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
75 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
79 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
80 `ml64 2>&1` =~ /Version ([0-9]+)\./) {
84 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
85 my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
89 ($out, $inp, $mod) = ("%rdi", "%rsi", "%rbp"); # common internal API
91 my ($out,$inp,$mod,$n0,$times) = ("%rdi","%rsi","%rdx","%rcx","%r8d");
96 .extern OPENSSL_ia32cap_P
99 .type rsaz_512_sqr,\@function,5
101 rsaz_512_sqr: # 25-29% faster than rsaz_512_mul
117 .cfi_adjust_cfa_offset 128+24
119 movq $mod, %rbp # common argument
124 $code.=<<___ if ($addx);
126 andl OPENSSL_ia32cap_P+8(%rip),%r11d
127 cmpl \$0x80100,%r11d # check for MULX and ADO/CX
135 movl $times,128+8(%rsp)
179 addq %r8, %r8 #shlq \$1, %r8
181 adcq %r9, %r9 #shld \$1, %r8, %r9
242 lea (%rcx,%r10,2), %r10 #shld \$1, %rcx, %r10
244 adcq %r11, %r11 #shld \$1, %r10, %r11
282 lea (%rbx,%r12,2), %r12 #shld \$1, %rbx, %r12
300 leaq (%r10,%r13,2), %r13 #shld \$1, %r12, %r13
330 leaq (%rcx,%r14,2), %r14 #shld \$1, %rcx, %r14
348 leaq (%r12,%r15,2),%r15 #shld \$1, %r14, %r15
373 leaq (%rbx,%r8,2), %r8 #shld \$1, %rbx, %r8
388 leaq (%r12,%r9,2), %r9 #shld \$1, %r8, %r9
412 leaq (%rcx,%r10,2), %r10 #shld \$1, %rcx, %r10
420 leaq (%r15,%r11,2), %r11 #shld \$1, %r10, %r11
441 adcq %r12, %r12 #shld \$1, %rbx, %r12
442 adcq %r13, %r13 #shld \$1, %r12, %r13
443 adcq %r14, %r14 #shld \$1, %r13, %r14
473 call __rsaz_512_reduce
485 call __rsaz_512_subtract
489 movl 128+8(%rsp), $times
501 movl $times,128+8(%rsp)
502 movq $out, %xmm0 # off-load
503 movq %rbp, %xmm1 # off-load
507 mulx 16($inp), %rcx, %r10
508 xor %rbp, %rbp # cf=0, of=0
510 mulx 24($inp), %rax, %r11
513 mulx 32($inp), %rcx, %r12
516 mulx 40($inp), %rax, %r13
519 .byte 0xc4,0x62,0xf3,0xf6,0xb6,0x30,0x00,0x00,0x00 # mulx 48($inp), %rcx, %r14
523 .byte 0xc4,0x62,0xfb,0xf6,0xbe,0x38,0x00,0x00,0x00 # mulx 56($inp), %rax, %r15
525 adcx %rbp, %r15 # %rbp is 0
532 mulx %rdx, %rax, %rdx
541 mulx 16($inp), %rax, %rbx
545 .byte 0xc4,0x62,0xc3,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 24($inp), $out, %r8
549 mulx 32($inp), %rax, %rbx
553 mulx 40($inp), $out, %r8
557 .byte 0xc4,0xe2,0xfb,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 48($inp), %rax, %rbx
561 .byte 0xc4,0x62,0xc3,0xf6,0x86,0x38,0x00,0x00,0x00 # mulx 56($inp), $out, %r8
571 mulx %rdx, %rax, %rcx
578 .byte 0x4c,0x89,0x94,0x24,0x18,0x00,0x00,0x00 # mov %r10, 24(%rsp)
581 .byte 0xc4,0x62,0xc3,0xf6,0x8e,0x18,0x00,0x00,0x00 # mulx 24($inp), $out, %r9
585 mulx 32($inp), %rax, %rcx
589 mulx 40($inp), $out, %r9
593 .byte 0xc4,0xe2,0xfb,0xf6,0x8e,0x30,0x00,0x00,0x00 # mulx 48($inp), %rax, %rcx
597 .byte 0xc4,0x62,0xc3,0xf6,0x8e,0x38,0x00,0x00,0x00 # mulx 56($inp), $out, %r9
607 mulx %rdx, %rax, %rdx
614 .byte 0x4c,0x89,0xa4,0x24,0x28,0x00,0x00,0x00 # mov %r12, 40(%rsp)
617 .byte 0xc4,0xe2,0xfb,0xf6,0x9e,0x20,0x00,0x00,0x00 # mulx 32($inp), %rax, %rbx
621 mulx 40($inp), $out, %r10
625 mulx 48($inp), %rax, %rbx
629 mulx 56($inp), $out, %r10
640 mulx %rdx, %rax, %rdx
650 .byte 0xc4,0x62,0xc3,0xf6,0x9e,0x28,0x00,0x00,0x00 # mulx 40($inp), $out, %r11
654 mulx 48($inp), %rax, %rcx
658 mulx 56($inp), $out, %r11
668 mulx %rdx, %rax, %rdx
678 .byte 0xc4,0xe2,0xfb,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 48($inp), %rax, %rbx
682 .byte 0xc4,0x62,0xc3,0xf6,0xa6,0x38,0x00,0x00,0x00 # mulx 56($inp), $out, %r12
692 mulx %rdx, %rax, %rdx
702 .byte 0xc4,0x62,0xfb,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 56($inp), %rax, %r13
712 mulx %rdx, %rax, %rdx
718 .byte 0x4c,0x89,0x9c,0x24,0x60,0x00,0x00,0x00 # mov %r11, 96(%rsp)
719 .byte 0x4c,0x89,0xa4,0x24,0x68,0x00,0x00,0x00 # mov %r12, 104(%rsp)
722 mulx %rdx, %rax, %rdx
734 movq 128(%rsp), %rdx # pull $n0
744 call __rsaz_512_reducex
756 call __rsaz_512_subtract
760 movl 128+8(%rsp), $times
771 leaq 128+24+48(%rsp), %rax
786 .cfi_def_cfa_register %rsp
790 .size rsaz_512_sqr,.-rsaz_512_sqr
794 my ($out,$ap,$bp,$mod,$n0) = ("%rdi","%rsi","%rdx","%rcx","%r8");
797 .type rsaz_512_mul,\@function,5
815 .cfi_adjust_cfa_offset 128+24
817 movq $out, %xmm0 # off-load arguments
821 $code.=<<___ if ($addx);
823 andl OPENSSL_ia32cap_P+8(%rip),%r11d
824 cmpl \$0x80100,%r11d # check for MULX and ADO/CX
828 movq ($bp), %rbx # pass b[0]
829 movq $bp, %rbp # pass argument
844 call __rsaz_512_reduce
846 $code.=<<___ if ($addx);
851 movq $bp, %rbp # pass argument
852 movq ($bp), %rdx # pass b[0]
858 movq 128(%rsp), %rdx # pull $n0
868 call __rsaz_512_reducex
882 call __rsaz_512_subtract
884 leaq 128+24+48(%rsp), %rax
899 .cfi_def_cfa_register %rsp
903 .size rsaz_512_mul,.-rsaz_512_mul
907 my ($out,$ap,$bp,$mod,$n0,$pwr) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9d");
909 .globl rsaz_512_mul_gather4
910 .type rsaz_512_mul_gather4,\@function,6
912 rsaz_512_mul_gather4:
927 subq \$`128+24+($win64?0xb0:0)`, %rsp
928 .cfi_adjust_cfa_offset `128+24+($win64?0xb0:0)`
930 $code.=<<___ if ($win64);
931 movaps %xmm6,0xa0(%rsp)
932 movaps %xmm7,0xb0(%rsp)
933 movaps %xmm8,0xc0(%rsp)
934 movaps %xmm9,0xd0(%rsp)
935 movaps %xmm10,0xe0(%rsp)
936 movaps %xmm11,0xf0(%rsp)
937 movaps %xmm12,0x100(%rsp)
938 movaps %xmm13,0x110(%rsp)
939 movaps %xmm14,0x120(%rsp)
940 movaps %xmm15,0x130(%rsp)
945 movdqa .Linc+16(%rip),%xmm1 # 00000002000000020000000200000002
946 movdqa .Linc(%rip),%xmm0 # 00000001000000010000000000000000
948 pshufd \$0,%xmm8,%xmm8 # broadcast $power
952 ########################################################################
953 # calculate mask by comparing 0..15 to $power
955 for($i=0;$i<4;$i++) {
957 paddd %xmm`$i`,%xmm`$i+1`
958 pcmpeqd %xmm8,%xmm`$i`
959 movdqa %xmm7,%xmm`$i+3`
964 paddd %xmm`$i`,%xmm`$i+1`
965 pcmpeqd %xmm8,%xmm`$i`
971 movdqa 16*0($bp),%xmm8
972 movdqa 16*1($bp),%xmm9
973 movdqa 16*2($bp),%xmm10
974 movdqa 16*3($bp),%xmm11
976 movdqa 16*4($bp),%xmm12
978 movdqa 16*5($bp),%xmm13
980 movdqa 16*6($bp),%xmm14
982 movdqa 16*7($bp),%xmm15
996 pshufd \$0x4e,%xmm8,%xmm9
999 $code.=<<___ if ($addx);
1000 movl \$0x80100,%r11d
1001 andl OPENSSL_ia32cap_P+8(%rip),%r11d
1002 cmpl \$0x80100,%r11d # check for MULX and ADO/CX
1008 movq $n0, 128(%rsp) # off-load arguments
1009 movq $out, 128+8(%rsp)
1010 movq $mod, 128+16(%rsp)
1014 mulq %rbx # 0 iteration
1063 jmp .Loop_mul_gather
1067 movdqa 16*0(%rbp),%xmm8
1068 movdqa 16*1(%rbp),%xmm9
1069 movdqa 16*2(%rbp),%xmm10
1070 movdqa 16*3(%rbp),%xmm11
1072 movdqa 16*4(%rbp),%xmm12
1074 movdqa 16*5(%rbp),%xmm13
1076 movdqa 16*6(%rbp),%xmm14
1078 movdqa 16*7(%rbp),%xmm15
1079 leaq 128(%rbp), %rbp
1092 pshufd \$0x4e,%xmm8,%xmm9
1162 jnz .Loop_mul_gather
1173 movq 128+8(%rsp), $out
1174 movq 128+16(%rsp), %rbp
1185 call __rsaz_512_reduce
1187 $code.=<<___ if ($addx);
1188 jmp .Lmul_gather_tail
1194 mov $n0, 128(%rsp) # off-load arguments
1195 mov $out, 128+8(%rsp)
1196 mov $mod, 128+16(%rsp)
1198 mulx ($ap), %rbx, %r8 # 0 iteration
1200 xor %edi, %edi # cf=0, of=0
1202 mulx 8($ap), %rax, %r9
1204 mulx 16($ap), %rbx, %r10
1207 mulx 24($ap), %rax, %r11
1210 mulx 32($ap), %rbx, %r12
1213 mulx 40($ap), %rax, %r13
1216 mulx 48($ap), %rbx, %r14
1219 mulx 56($ap), %rax, %r15
1224 adcx %rdi, %r15 # %rdi is 0
1227 jmp .Loop_mulx_gather
1231 movdqa 16*0(%rbp),%xmm8
1232 movdqa 16*1(%rbp),%xmm9
1233 movdqa 16*2(%rbp),%xmm10
1234 movdqa 16*3(%rbp),%xmm11
1236 movdqa 16*4(%rbp),%xmm12
1238 movdqa 16*5(%rbp),%xmm13
1240 movdqa 16*6(%rbp),%xmm14
1242 movdqa 16*7(%rbp),%xmm15
1243 leaq 128(%rbp), %rbp
1256 pshufd \$0x4e,%xmm8,%xmm9
1260 .byte 0xc4,0x62,0xfb,0xf6,0x86,0x00,0x00,0x00,0x00 # mulx ($ap), %rax, %r8
1264 mulx 8($ap), %rax, %r9
1268 mulx 16($ap), %rax, %r10
1272 .byte 0xc4,0x62,0xfb,0xf6,0x9e,0x18,0x00,0x00,0x00 # mulx 24($ap), %rax, %r11
1276 mulx 32($ap), %rax, %r12
1280 mulx 40($ap), %rax, %r13
1284 .byte 0xc4,0x62,0xfb,0xf6,0xb6,0x30,0x00,0x00,0x00 # mulx 48($ap), %rax, %r14
1289 mulx 56($ap), %rax, %r15
1290 mov %rbx, 64(%rsp,%rcx,8)
1294 adcx %rdi, %r15 # cf=0
1297 jnz .Loop_mulx_gather
1301 mov %r10, 64+16(%rsp)
1302 mov %r11, 64+24(%rsp)
1303 mov %r12, 64+32(%rsp)
1304 mov %r13, 64+40(%rsp)
1305 mov %r14, 64+48(%rsp)
1306 mov %r15, 64+56(%rsp)
1308 mov 128(%rsp), %rdx # pull arguments
1309 mov 128+8(%rsp), $out
1310 mov 128+16(%rsp), %rbp
1321 call __rsaz_512_reducex
1331 adcq 104(%rsp), %r13
1332 adcq 112(%rsp), %r14
1333 adcq 120(%rsp), %r15
1336 call __rsaz_512_subtract
1338 leaq 128+24+48(%rsp), %rax
1340 $code.=<<___ if ($win64);
1341 movaps 0xa0-0xc8(%rax),%xmm6
1342 movaps 0xb0-0xc8(%rax),%xmm7
1343 movaps 0xc0-0xc8(%rax),%xmm8
1344 movaps 0xd0-0xc8(%rax),%xmm9
1345 movaps 0xe0-0xc8(%rax),%xmm10
1346 movaps 0xf0-0xc8(%rax),%xmm11
1347 movaps 0x100-0xc8(%rax),%xmm12
1348 movaps 0x110-0xc8(%rax),%xmm13
1349 movaps 0x120-0xc8(%rax),%xmm14
1350 movaps 0x130-0xc8(%rax),%xmm15
1355 movq -48(%rax), %r15
1357 movq -40(%rax), %r14
1359 movq -32(%rax), %r13
1361 movq -24(%rax), %r12
1363 movq -16(%rax), %rbp
1368 .cfi_def_cfa_register %rsp
1369 .Lmul_gather4_epilogue:
1372 .size rsaz_512_mul_gather4,.-rsaz_512_mul_gather4
1376 my ($out,$ap,$mod,$n0,$tbl,$pwr) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9d");
1378 .globl rsaz_512_mul_scatter4
1379 .type rsaz_512_mul_scatter4,\@function,6
1381 rsaz_512_mul_scatter4:
1398 .cfi_adjust_cfa_offset 128+24
1399 .Lmul_scatter4_body:
1400 leaq ($tbl,$pwr,8), $tbl
1401 movq $out, %xmm0 # off-load arguments
1408 $code.=<<___ if ($addx);
1409 movl \$0x80100,%r11d
1410 andl OPENSSL_ia32cap_P+8(%rip),%r11d
1411 cmpl \$0x80100,%r11d # check for MULX and ADO/CX
1415 movq ($out),%rbx # pass b[0]
1430 call __rsaz_512_reduce
1432 $code.=<<___ if ($addx);
1433 jmp .Lmul_scatter_tail
1437 movq ($out), %rdx # pass b[0]
1438 call __rsaz_512_mulx
1443 movq 128(%rsp), %rdx # pull $n0
1453 call __rsaz_512_reducex
1463 adcq 104(%rsp), %r13
1464 adcq 112(%rsp), %r14
1465 adcq 120(%rsp), %r15
1469 call __rsaz_512_subtract
1471 movq %r8, 128*0($inp) # scatter
1472 movq %r9, 128*1($inp)
1473 movq %r10, 128*2($inp)
1474 movq %r11, 128*3($inp)
1475 movq %r12, 128*4($inp)
1476 movq %r13, 128*5($inp)
1477 movq %r14, 128*6($inp)
1478 movq %r15, 128*7($inp)
1480 leaq 128+24+48(%rsp), %rax
1482 movq -48(%rax), %r15
1484 movq -40(%rax), %r14
1486 movq -32(%rax), %r13
1488 movq -24(%rax), %r12
1490 movq -16(%rax), %rbp
1495 .cfi_def_cfa_register %rsp
1496 .Lmul_scatter4_epilogue:
1499 .size rsaz_512_mul_scatter4,.-rsaz_512_mul_scatter4
1503 my ($out,$inp,$mod,$n0) = ("%rdi","%rsi","%rdx","%rcx");
1505 .globl rsaz_512_mul_by_one
1506 .type rsaz_512_mul_by_one,\@function,4
1508 rsaz_512_mul_by_one:
1524 .cfi_adjust_cfa_offset 128+24
1527 $code.=<<___ if ($addx);
1528 movl OPENSSL_ia32cap_P+8(%rip),%eax
1531 movq $mod, %rbp # reassign argument
1544 movdqa %xmm0, (%rsp)
1545 movdqa %xmm0, 16(%rsp)
1546 movdqa %xmm0, 32(%rsp)
1547 movdqa %xmm0, 48(%rsp)
1548 movdqa %xmm0, 64(%rsp)
1549 movdqa %xmm0, 80(%rsp)
1550 movdqa %xmm0, 96(%rsp)
1552 $code.=<<___ if ($addx);
1554 cmpl \$0x80100,%eax # check for MULX and ADO/CX
1558 call __rsaz_512_reduce
1560 $code.=<<___ if ($addx);
1564 movq 128(%rsp), %rdx # pull $n0
1565 call __rsaz_512_reducex
1578 leaq 128+24+48(%rsp), %rax
1580 movq -48(%rax), %r15
1582 movq -40(%rax), %r14
1584 movq -32(%rax), %r13
1586 movq -24(%rax), %r12
1588 movq -16(%rax), %rbp
1593 .cfi_def_cfa_register %rsp
1594 .Lmul_by_one_epilogue:
1597 .size rsaz_512_mul_by_one,.-rsaz_512_mul_by_one
1600 { # __rsaz_512_reduce
1602 # input: %r8-%r15, %rbp - mod, 128(%rsp) - n0
1604 # clobbers: everything except %rbp and %rdi
1606 .type __rsaz_512_reduce,\@abi-omnipotent
1610 imulq 128+8(%rsp), %rbx
1613 jmp .Lreduction_loop
1644 movq 128+8(%rsp), %rsi
1685 jne .Lreduction_loop
1688 .size __rsaz_512_reduce,.-__rsaz_512_reduce
1692 # __rsaz_512_reducex
1694 # input: %r8-%r15, %rbp - mod, 128(%rsp) - n0
1696 # clobbers: everything except %rbp and %rdi
1698 .type __rsaz_512_reducex,\@abi-omnipotent
1701 #movq 128+8(%rsp), %rdx # pull $n0
1703 xorq %rsi, %rsi # cf=0,of=0
1705 jmp .Lreduction_loopx
1710 mulx 0(%rbp), %rax, %r8
1714 mulx 8(%rbp), %rax, %r9
1718 mulx 16(%rbp), %rbx, %r10
1722 mulx 24(%rbp), %rbx, %r11
1726 .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 32(%rbp), %rbx, %r12
1732 mulx 128+8(%rsp), %rbx, %rdx
1735 mulx 40(%rbp), %rax, %r13
1739 .byte 0xc4,0x62,0xfb,0xf6,0xb5,0x30,0x00,0x00,0x00 # mulx 48(%rbp), %rax, %r14
1743 mulx 56(%rbp), %rax, %r15
1746 adox %rsi, %r15 # %rsi is 0
1747 adcx %rsi, %r15 # cf=0
1750 jne .Lreduction_loopx
1753 .size __rsaz_512_reducex,.-__rsaz_512_reducex
1756 { # __rsaz_512_subtract
1757 # input: %r8-%r15, %rdi - $out, %rbp - $mod, %rcx - mask
1759 # clobbers: everything but %rdi, %rsi and %rbp
1761 .type __rsaz_512_subtract,\@abi-omnipotent
1763 __rsaz_512_subtract:
1817 .size __rsaz_512_subtract,.-__rsaz_512_subtract
1822 # input: %rsi - ap, %rbp - bp
1824 # clobbers: everything
1825 my ($ap,$bp) = ("%rsi","%rbp");
1827 .type __rsaz_512_mul,\@abi-omnipotent
1968 .size __rsaz_512_mul,.-__rsaz_512_mul
1974 # input: %rsi - ap, %rbp - bp
1976 # clobbers: everything
1977 my ($ap,$bp,$zero) = ("%rsi","%rbp","%rdi");
1979 .type __rsaz_512_mulx,\@abi-omnipotent
1982 mulx ($ap), %rbx, %r8 # initial %rdx preloaded by caller
1985 mulx 8($ap), %rax, %r9
1988 mulx 16($ap), %rbx, %r10
1991 mulx 24($ap), %rax, %r11
1994 mulx 32($ap), %rbx, %r12
1997 mulx 40($ap), %rax, %r13
2000 mulx 48($ap), %rbx, %r14
2003 mulx 56($ap), %rax, %r15
2009 xor $zero, $zero # cf=0,of=0
2015 mulx ($ap), %rax, %r8
2019 mulx 8($ap), %rax, %r9
2023 mulx 16($ap), %rax, %r10
2027 mulx 24($ap), %rax, %r11
2031 .byte 0x3e,0xc4,0x62,0xfb,0xf6,0xa6,0x20,0x00,0x00,0x00 # mulx 32($ap), %rax, %r12
2035 mulx 40($ap), %rax, %r13
2039 mulx 48($ap), %rax, %r14
2043 mulx 56($ap), %rax, %r15
2044 movq 64($bp,%rcx,8), %rdx
2045 movq %rbx, 8+64-8(%rsp,%rcx,8)
2048 adcx $zero, %r15 # cf=0
2054 mulx ($ap), %rax, %r8
2058 .byte 0xc4,0x62,0xfb,0xf6,0x8e,0x08,0x00,0x00,0x00 # mulx 8($ap), %rax, %r9
2062 .byte 0xc4,0x62,0xfb,0xf6,0x96,0x10,0x00,0x00,0x00 # mulx 16($ap), %rax, %r10
2066 mulx 24($ap), %rax, %r11
2070 mulx 32($ap), %rax, %r12
2074 mulx 40($ap), %rax, %r13
2078 .byte 0xc4,0x62,0xfb,0xf6,0xb6,0x30,0x00,0x00,0x00 # mulx 48($ap), %rax, %r14
2082 .byte 0xc4,0x62,0xfb,0xf6,0xbe,0x38,0x00,0x00,0x00 # mulx 56($ap), %rax, %r15
2087 mov %rbx, 8+64-8(%rsp)
2089 mov %r9, 8+64+8(%rsp)
2090 mov %r10, 8+64+16(%rsp)
2091 mov %r11, 8+64+24(%rsp)
2092 mov %r12, 8+64+32(%rsp)
2093 mov %r13, 8+64+40(%rsp)
2094 mov %r14, 8+64+48(%rsp)
2095 mov %r15, 8+64+56(%rsp)
2098 .size __rsaz_512_mulx,.-__rsaz_512_mulx
2102 my ($out,$inp,$power)= $win64 ? ("%rcx","%rdx","%r8d") : ("%rdi","%rsi","%edx");
2104 .globl rsaz_512_scatter4
2105 .type rsaz_512_scatter4,\@abi-omnipotent
2108 leaq ($out,$power,8), $out
2116 leaq 128($out), $out
2120 .size rsaz_512_scatter4,.-rsaz_512_scatter4
2122 .globl rsaz_512_gather4
2123 .type rsaz_512_gather4,\@abi-omnipotent
2127 $code.=<<___ if ($win64);
2128 .LSEH_begin_rsaz_512_gather4:
2129 .byte 0x48,0x81,0xec,0xa8,0x00,0x00,0x00 # sub $0xa8,%rsp
2130 .byte 0x0f,0x29,0x34,0x24 # movaps %xmm6,(%rsp)
2131 .byte 0x0f,0x29,0x7c,0x24,0x10 # movaps %xmm7,0x10(%rsp)
2132 .byte 0x44,0x0f,0x29,0x44,0x24,0x20 # movaps %xmm8,0x20(%rsp)
2133 .byte 0x44,0x0f,0x29,0x4c,0x24,0x30 # movaps %xmm9,0x30(%rsp)
2134 .byte 0x44,0x0f,0x29,0x54,0x24,0x40 # movaps %xmm10,0x40(%rsp)
2135 .byte 0x44,0x0f,0x29,0x5c,0x24,0x50 # movaps %xmm11,0x50(%rsp)
2136 .byte 0x44,0x0f,0x29,0x64,0x24,0x60 # movaps %xmm12,0x60(%rsp)
2137 .byte 0x44,0x0f,0x29,0x6c,0x24,0x70 # movaps %xmm13,0x70(%rsp)
2138 .byte 0x44,0x0f,0x29,0xb4,0x24,0x80,0,0,0 # movaps %xmm14,0x80(%rsp)
2139 .byte 0x44,0x0f,0x29,0xbc,0x24,0x90,0,0,0 # movaps %xmm15,0x90(%rsp)
2143 movdqa .Linc+16(%rip),%xmm1 # 00000002000000020000000200000002
2144 movdqa .Linc(%rip),%xmm0 # 00000001000000010000000000000000
2146 pshufd \$0,%xmm8,%xmm8 # broadcast $power
2150 ########################################################################
2151 # calculate mask by comparing 0..15 to $power
2153 for($i=0;$i<4;$i++) {
2155 paddd %xmm`$i`,%xmm`$i+1`
2156 pcmpeqd %xmm8,%xmm`$i`
2157 movdqa %xmm7,%xmm`$i+3`
2162 paddd %xmm`$i`,%xmm`$i+1`
2163 pcmpeqd %xmm8,%xmm`$i`
2172 movdqa 16*0($inp),%xmm8
2173 movdqa 16*1($inp),%xmm9
2174 movdqa 16*2($inp),%xmm10
2175 movdqa 16*3($inp),%xmm11
2177 movdqa 16*4($inp),%xmm12
2179 movdqa 16*5($inp),%xmm13
2181 movdqa 16*6($inp),%xmm14
2183 movdqa 16*7($inp),%xmm15
2184 leaq 128($inp), $inp
2197 pshufd \$0x4e,%xmm8,%xmm9
2204 $code.=<<___ if ($win64);
2205 movaps 0x00(%rsp),%xmm6
2206 movaps 0x10(%rsp),%xmm7
2207 movaps 0x20(%rsp),%xmm8
2208 movaps 0x30(%rsp),%xmm9
2209 movaps 0x40(%rsp),%xmm10
2210 movaps 0x50(%rsp),%xmm11
2211 movaps 0x60(%rsp),%xmm12
2212 movaps 0x70(%rsp),%xmm13
2213 movaps 0x80(%rsp),%xmm14
2214 movaps 0x90(%rsp),%xmm15
2219 .LSEH_end_rsaz_512_gather4:
2220 .size rsaz_512_gather4,.-rsaz_512_gather4
2229 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2230 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
2238 .extern __imp_RtlVirtualUnwind
2239 .type se_handler,\@abi-omnipotent
2253 mov 120($context),%rax # pull context->Rax
2254 mov 248($context),%rbx # pull context->Rip
2256 mov 8($disp),%rsi # disp->ImageBase
2257 mov 56($disp),%r11 # disp->HandlerData
2259 mov 0(%r11),%r10d # HandlerData[0]
2260 lea (%rsi,%r10),%r10 # end of prologue label
2261 cmp %r10,%rbx # context->Rip<end of prologue label
2262 jb .Lcommon_seh_tail
2264 mov 152($context),%rax # pull context->Rsp
2266 mov 4(%r11),%r10d # HandlerData[1]
2267 lea (%rsi,%r10),%r10 # epilogue label
2268 cmp %r10,%rbx # context->Rip>=epilogue label
2269 jae .Lcommon_seh_tail
2271 lea 128+24+48(%rax),%rax
2273 lea .Lmul_gather4_epilogue(%rip),%rbx
2275 jne .Lse_not_in_mul_gather4
2279 lea -48-0xa8(%rax),%rsi
2280 lea 512($context),%rdi
2282 .long 0xa548f3fc # cld; rep movsq
2284 .Lse_not_in_mul_gather4:
2291 mov %rbx,144($context) # restore context->Rbx
2292 mov %rbp,160($context) # restore context->Rbp
2293 mov %r12,216($context) # restore context->R12
2294 mov %r13,224($context) # restore context->R13
2295 mov %r14,232($context) # restore context->R14
2296 mov %r15,240($context) # restore context->R15
2301 mov %rax,152($context) # restore context->Rsp
2302 mov %rsi,168($context) # restore context->Rsi
2303 mov %rdi,176($context) # restore context->Rdi
2305 mov 40($disp),%rdi # disp->ContextRecord
2306 mov $context,%rsi # context
2307 mov \$154,%ecx # sizeof(CONTEXT)
2308 .long 0xa548f3fc # cld; rep movsq
2311 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
2312 mov 8(%rsi),%rdx # arg2, disp->ImageBase
2313 mov 0(%rsi),%r8 # arg3, disp->ControlPc
2314 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
2315 mov 40(%rsi),%r10 # disp->ContextRecord
2316 lea 56(%rsi),%r11 # &disp->HandlerData
2317 lea 24(%rsi),%r12 # &disp->EstablisherFrame
2318 mov %r10,32(%rsp) # arg5
2319 mov %r11,40(%rsp) # arg6
2320 mov %r12,48(%rsp) # arg7
2321 mov %rcx,56(%rsp) # arg8, (NULL)
2322 call *__imp_RtlVirtualUnwind(%rip)
2324 mov \$1,%eax # ExceptionContinueSearch
2336 .size se_handler,.-se_handler
2340 .rva .LSEH_begin_rsaz_512_sqr
2341 .rva .LSEH_end_rsaz_512_sqr
2342 .rva .LSEH_info_rsaz_512_sqr
2344 .rva .LSEH_begin_rsaz_512_mul
2345 .rva .LSEH_end_rsaz_512_mul
2346 .rva .LSEH_info_rsaz_512_mul
2348 .rva .LSEH_begin_rsaz_512_mul_gather4
2349 .rva .LSEH_end_rsaz_512_mul_gather4
2350 .rva .LSEH_info_rsaz_512_mul_gather4
2352 .rva .LSEH_begin_rsaz_512_mul_scatter4
2353 .rva .LSEH_end_rsaz_512_mul_scatter4
2354 .rva .LSEH_info_rsaz_512_mul_scatter4
2356 .rva .LSEH_begin_rsaz_512_mul_by_one
2357 .rva .LSEH_end_rsaz_512_mul_by_one
2358 .rva .LSEH_info_rsaz_512_mul_by_one
2360 .rva .LSEH_begin_rsaz_512_gather4
2361 .rva .LSEH_end_rsaz_512_gather4
2362 .rva .LSEH_info_rsaz_512_gather4
2366 .LSEH_info_rsaz_512_sqr:
2369 .rva .Lsqr_body,.Lsqr_epilogue # HandlerData[]
2370 .LSEH_info_rsaz_512_mul:
2373 .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
2374 .LSEH_info_rsaz_512_mul_gather4:
2377 .rva .Lmul_gather4_body,.Lmul_gather4_epilogue # HandlerData[]
2378 .LSEH_info_rsaz_512_mul_scatter4:
2381 .rva .Lmul_scatter4_body,.Lmul_scatter4_epilogue # HandlerData[]
2382 .LSEH_info_rsaz_512_mul_by_one:
2385 .rva .Lmul_by_one_body,.Lmul_by_one_epilogue # HandlerData[]
2386 .LSEH_info_rsaz_512_gather4:
2387 .byte 0x01,0x46,0x16,0x00
2388 .byte 0x46,0xf8,0x09,0x00 # vmovaps 0x90(rsp),xmm15
2389 .byte 0x3d,0xe8,0x08,0x00 # vmovaps 0x80(rsp),xmm14
2390 .byte 0x34,0xd8,0x07,0x00 # vmovaps 0x70(rsp),xmm13
2391 .byte 0x2e,0xc8,0x06,0x00 # vmovaps 0x60(rsp),xmm12
2392 .byte 0x28,0xb8,0x05,0x00 # vmovaps 0x50(rsp),xmm11
2393 .byte 0x22,0xa8,0x04,0x00 # vmovaps 0x40(rsp),xmm10
2394 .byte 0x1c,0x98,0x03,0x00 # vmovaps 0x30(rsp),xmm9
2395 .byte 0x16,0x88,0x02,0x00 # vmovaps 0x20(rsp),xmm8
2396 .byte 0x10,0x78,0x01,0x00 # vmovaps 0x10(rsp),xmm7
2397 .byte 0x0b,0x68,0x00,0x00 # vmovaps 0x00(rsp),xmm6
2398 .byte 0x07,0x01,0x15,0x00 # sub rsp,0xa8
2402 $code =~ s/\`([^\`]*)\`/eval $1/gem;