rsaz-avx512.pl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. # Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  2. # Copyright (c) 2020, Intel Corporation. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. #
  9. #
  10. # Originally written by Ilya Albrekht, Sergey Kirillov and Andrey Matyukov
  11. # Intel Corporation
  12. #
  13. # December 2020
  14. #
  15. # Initial release.
  16. #
  17. # Implementation utilizes 256-bit (ymm) registers to avoid frequency scaling issues.
  18. #
  19. # IceLake-Client @ 1.3GHz
  20. # |---------+----------------------+--------------+-------------|
  21. # | | OpenSSL 3.0.0-alpha9 | this | Unit |
  22. # |---------+----------------------+--------------+-------------|
  23. # | rsa2048 | 2 127 659 | 1 015 625 | cycles/sign |
  24. # | | 611 | 1280 / +109% | sign/s |
  25. # |---------+----------------------+--------------+-------------|
  26. #
  27. # $output is the last argument if it looks like a file (it has an extension)
  28. # $flavour is the first argument if it doesn't look like a file
  29. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  30. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  31. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  32. $avx512ifma=0;
  33. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  34. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  35. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  36. die "can't locate x86_64-xlate.pl";
  37. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  38. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  39. $avx512ifma = ($1>=2.26);
  40. }
  41. if (!$avx512 && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  42. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)(?:\.([0-9]+))?/) {
  43. $avx512ifma = ($1==2.11 && $2>=8) + ($1>=2.12);
  44. }
  45. if (!$avx512 && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+\.[0-9]+)/) {
  46. $avx512ifma = ($2>=7.0);
  47. }
  48. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
  49. or die "can't call $xlate: $!";
  50. *STDOUT=*OUT;
  51. if ($avx512ifma>0) {{{
  52. @_6_args_universal_ABI = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
  53. $code.=<<___;
  54. .extern OPENSSL_ia32cap_P
  55. .globl ossl_rsaz_avx512ifma_eligible
  56. .type ossl_rsaz_avx512ifma_eligible,\@abi-omnipotent
  57. .align 32
  58. ossl_rsaz_avx512ifma_eligible:
  59. mov OPENSSL_ia32cap_P+8(%rip), %ecx
  60. xor %eax,%eax
  61. and \$`1<<31|1<<21|1<<17|1<<16`, %ecx # avx512vl + avx512ifma + avx512dq + avx512f
  62. cmp \$`1<<31|1<<21|1<<17|1<<16`, %ecx
  63. cmove %ecx,%eax
  64. ret
  65. .size ossl_rsaz_avx512ifma_eligible, .-ossl_rsaz_avx512ifma_eligible
  66. ___
  67. ###############################################################################
  68. # Almost Montgomery Multiplication (AMM) for 20-digit number in radix 2^52.
  69. #
  70. # AMM is defined as presented in the paper
  71. # "Efficient Software Implementations of Modular Exponentiation" by Shay Gueron.
  72. #
  73. # The input and output are presented in 2^52 radix domain, i.e.
  74. # |res|, |a|, |b|, |m| are arrays of 20 64-bit qwords with 12 high bits zeroed.
  75. # |k0| is a Montgomery coefficient, which is here k0 = -1/m mod 2^64
  76. # (note, the implementation counts only 52 bits from it).
  77. #
  78. # NB: the AMM implementation does not perform "conditional" subtraction step as
  79. # specified in the original algorithm as according to the paper "Enhanced Montgomery
  80. # Multiplication" by Shay Gueron (see Lemma 1), the result will be always < 2*2^1024
  81. # and can be used as a direct input to the next AMM iteration.
  82. # This post-condition is true, provided the correct parameter |s| is choosen, i.e.
  83. # s >= n + 2 * k, which matches our case: 1040 > 1024 + 2 * 1.
  84. #
  85. # void ossl_rsaz_amm52x20_x1_256(BN_ULONG *res,
  86. # const BN_ULONG *a,
  87. # const BN_ULONG *b,
  88. # const BN_ULONG *m,
  89. # BN_ULONG k0);
  90. ###############################################################################
  91. {
  92. # input parameters ("%rdi","%rsi","%rdx","%rcx","%r8")
  93. my ($res,$a,$b,$m,$k0) = @_6_args_universal_ABI;
  94. my $mask52 = "%rax";
  95. my $acc0_0 = "%r9";
  96. my $acc0_0_low = "%r9d";
  97. my $acc0_1 = "%r15";
  98. my $acc0_1_low = "%r15d";
  99. my $b_ptr = "%r11";
  100. my $iter = "%ebx";
  101. my $zero = "%ymm0";
  102. my ($R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0) = ("%ymm1", map("%ymm$_",(16..19)));
  103. my ($R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1) = ("%ymm2", map("%ymm$_",(20..23)));
  104. my $Bi = "%ymm3";
  105. my $Yi = "%ymm4";
  106. # Registers mapping for normalization.
  107. # We can reuse Bi, Yi registers here.
  108. my $TMP = $Bi;
  109. my $mask52x4 = $Yi;
  110. my ($T0,$T0h,$T1,$T1h,$T2) = map("%ymm$_", (24..28));
  111. sub amm52x20_x1() {
  112. # _data_offset - offset in the |a| or |m| arrays pointing to the beginning
  113. # of data for corresponding AMM operation;
  114. # _b_offset - offset in the |b| array pointing to the next qword digit;
  115. my ($_data_offset,$_b_offset,$_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2,$_k0) = @_;
  116. my $_R0_xmm = $_R0;
  117. $_R0_xmm =~ s/%y/%x/;
  118. $code.=<<___;
  119. movq $_b_offset($b_ptr), %r13 # b[i]
  120. vpbroadcastq %r13, $Bi # broadcast b[i]
  121. movq $_data_offset($a), %rdx
  122. mulx %r13, %r13, %r12 # a[0]*b[i] = (t0,t2)
  123. addq %r13, $_acc # acc += t0
  124. movq %r12, %r10
  125. adcq \$0, %r10 # t2 += CF
  126. movq $_k0, %r13
  127. imulq $_acc, %r13 # acc * k0
  128. andq $mask52, %r13 # yi = (acc * k0) & mask52
  129. vpbroadcastq %r13, $Yi # broadcast y[i]
  130. movq $_data_offset($m), %rdx
  131. mulx %r13, %r13, %r12 # yi * m[0] = (t0,t1)
  132. addq %r13, $_acc # acc += t0
  133. adcq %r12, %r10 # t2 += (t1 + CF)
  134. shrq \$52, $_acc
  135. salq \$12, %r10
  136. or %r10, $_acc # acc = ((acc >> 52) | (t2 << 12))
  137. vpmadd52luq `$_data_offset+64*0`($a), $Bi, $_R0
  138. vpmadd52luq `$_data_offset+64*0+32`($a), $Bi, $_R0h
  139. vpmadd52luq `$_data_offset+64*1`($a), $Bi, $_R1
  140. vpmadd52luq `$_data_offset+64*1+32`($a), $Bi, $_R1h
  141. vpmadd52luq `$_data_offset+64*2`($a), $Bi, $_R2
  142. vpmadd52luq `$_data_offset+64*0`($m), $Yi, $_R0
  143. vpmadd52luq `$_data_offset+64*0+32`($m), $Yi, $_R0h
  144. vpmadd52luq `$_data_offset+64*1`($m), $Yi, $_R1
  145. vpmadd52luq `$_data_offset+64*1+32`($m), $Yi, $_R1h
  146. vpmadd52luq `$_data_offset+64*2`($m), $Yi, $_R2
  147. # Shift accumulators right by 1 qword, zero extending the highest one
  148. valignq \$1, $_R0, $_R0h, $_R0
  149. valignq \$1, $_R0h, $_R1, $_R0h
  150. valignq \$1, $_R1, $_R1h, $_R1
  151. valignq \$1, $_R1h, $_R2, $_R1h
  152. valignq \$1, $_R2, $zero, $_R2
  153. vmovq $_R0_xmm, %r13
  154. addq %r13, $_acc # acc += R0[0]
  155. vpmadd52huq `$_data_offset+64*0`($a), $Bi, $_R0
  156. vpmadd52huq `$_data_offset+64*0+32`($a), $Bi, $_R0h
  157. vpmadd52huq `$_data_offset+64*1`($a), $Bi, $_R1
  158. vpmadd52huq `$_data_offset+64*1+32`($a), $Bi, $_R1h
  159. vpmadd52huq `$_data_offset+64*2`($a), $Bi, $_R2
  160. vpmadd52huq `$_data_offset+64*0`($m), $Yi, $_R0
  161. vpmadd52huq `$_data_offset+64*0+32`($m), $Yi, $_R0h
  162. vpmadd52huq `$_data_offset+64*1`($m), $Yi, $_R1
  163. vpmadd52huq `$_data_offset+64*1+32`($m), $Yi, $_R1h
  164. vpmadd52huq `$_data_offset+64*2`($m), $Yi, $_R2
  165. ___
  166. }
  167. # Normalization routine: handles carry bits in R0..R2 QWs and
  168. # gets R0..R2 back to normalized 2^52 representation.
  169. #
  170. # Uses %r8-14,%e[bcd]x
  171. sub amm52x20_x1_norm {
  172. my ($_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2) = @_;
  173. $code.=<<___;
  174. # Put accumulator to low qword in R0
  175. vpbroadcastq $_acc, $TMP
  176. vpblendd \$3, $TMP, $_R0, $_R0
  177. # Extract "carries" (12 high bits) from each QW of R0..R2
  178. # Save them to LSB of QWs in T0..T2
  179. vpsrlq \$52, $_R0, $T0
  180. vpsrlq \$52, $_R0h, $T0h
  181. vpsrlq \$52, $_R1, $T1
  182. vpsrlq \$52, $_R1h, $T1h
  183. vpsrlq \$52, $_R2, $T2
  184. # "Shift left" T0..T2 by 1 QW
  185. valignq \$3, $T1h, $T2, $T2
  186. valignq \$3, $T1, $T1h, $T1h
  187. valignq \$3, $T0h, $T1, $T1
  188. valignq \$3, $T0, $T0h, $T0h
  189. valignq \$3, $zero, $T0, $T0
  190. # Drop "carries" from R0..R2 QWs
  191. vpandq $mask52x4, $_R0, $_R0
  192. vpandq $mask52x4, $_R0h, $_R0h
  193. vpandq $mask52x4, $_R1, $_R1
  194. vpandq $mask52x4, $_R1h, $_R1h
  195. vpandq $mask52x4, $_R2, $_R2
  196. # Sum R0..R2 with corresponding adjusted carries
  197. vpaddq $T0, $_R0, $_R0
  198. vpaddq $T0h, $_R0h, $_R0h
  199. vpaddq $T1, $_R1, $_R1
  200. vpaddq $T1h, $_R1h, $_R1h
  201. vpaddq $T2, $_R2, $_R2
  202. # Now handle carry bits from this addition
  203. # Get mask of QWs which 52-bit parts overflow...
  204. vpcmpuq \$1, $_R0, $mask52x4, %k1 # OP=lt
  205. vpcmpuq \$1, $_R0h, $mask52x4, %k2
  206. vpcmpuq \$1, $_R1, $mask52x4, %k3
  207. vpcmpuq \$1, $_R1h, $mask52x4, %k4
  208. vpcmpuq \$1, $_R2, $mask52x4, %k5
  209. kmovb %k1, %r14d # k1
  210. kmovb %k2, %r13d # k1h
  211. kmovb %k3, %r12d # k2
  212. kmovb %k4, %r11d # k2h
  213. kmovb %k5, %r10d # k3
  214. # ...or saturated
  215. vpcmpuq \$0, $_R0, $mask52x4, %k1 # OP=eq
  216. vpcmpuq \$0, $_R0h, $mask52x4, %k2
  217. vpcmpuq \$0, $_R1, $mask52x4, %k3
  218. vpcmpuq \$0, $_R1h, $mask52x4, %k4
  219. vpcmpuq \$0, $_R2, $mask52x4, %k5
  220. kmovb %k1, %r9d # k4
  221. kmovb %k2, %r8d # k4h
  222. kmovb %k3, %ebx # k5
  223. kmovb %k4, %ecx # k5h
  224. kmovb %k5, %edx # k6
  225. # Get mask of QWs where carries shall be propagated to.
  226. # Merge 4-bit masks to 8-bit values to use add with carry.
  227. shl \$4, %r13b
  228. or %r13b, %r14b
  229. shl \$4, %r11b
  230. or %r11b, %r12b
  231. add %r14b, %r14b
  232. adc %r12b, %r12b
  233. adc %r10b, %r10b
  234. shl \$4, %r8b
  235. or %r8b,%r9b
  236. shl \$4, %cl
  237. or %cl, %bl
  238. add %r9b, %r14b
  239. adc %bl, %r12b
  240. adc %dl, %r10b
  241. xor %r9b, %r14b
  242. xor %bl, %r12b
  243. xor %dl, %r10b
  244. kmovb %r14d, %k1
  245. shr \$4, %r14b
  246. kmovb %r14d, %k2
  247. kmovb %r12d, %k3
  248. shr \$4, %r12b
  249. kmovb %r12d, %k4
  250. kmovb %r10d, %k5
  251. # Add carries according to the obtained mask
  252. vpsubq $mask52x4, $_R0, ${_R0}{%k1}
  253. vpsubq $mask52x4, $_R0h, ${_R0h}{%k2}
  254. vpsubq $mask52x4, $_R1, ${_R1}{%k3}
  255. vpsubq $mask52x4, $_R1h, ${_R1h}{%k4}
  256. vpsubq $mask52x4, $_R2, ${_R2}{%k5}
  257. vpandq $mask52x4, $_R0, $_R0
  258. vpandq $mask52x4, $_R0h, $_R0h
  259. vpandq $mask52x4, $_R1, $_R1
  260. vpandq $mask52x4, $_R1h, $_R1h
  261. vpandq $mask52x4, $_R2, $_R2
  262. ___
  263. }
  264. $code.=<<___;
  265. .text
  266. .globl ossl_rsaz_amm52x20_x1_256
  267. .type ossl_rsaz_amm52x20_x1_256,\@function,5
  268. .align 32
  269. ossl_rsaz_amm52x20_x1_256:
  270. .cfi_startproc
  271. endbranch
  272. push %rbx
  273. .cfi_push %rbx
  274. push %rbp
  275. .cfi_push %rbp
  276. push %r12
  277. .cfi_push %r12
  278. push %r13
  279. .cfi_push %r13
  280. push %r14
  281. .cfi_push %r14
  282. push %r15
  283. .cfi_push %r15
  284. .Lrsaz_amm52x20_x1_256_body:
  285. # Zeroing accumulators
  286. vpxord $zero, $zero, $zero
  287. vmovdqa64 $zero, $R0_0
  288. vmovdqa64 $zero, $R0_0h
  289. vmovdqa64 $zero, $R1_0
  290. vmovdqa64 $zero, $R1_0h
  291. vmovdqa64 $zero, $R2_0
  292. xorl $acc0_0_low, $acc0_0_low
  293. movq $b, $b_ptr # backup address of b
  294. movq \$0xfffffffffffff, $mask52 # 52-bit mask
  295. # Loop over 20 digits unrolled by 4
  296. mov \$5, $iter
  297. .align 32
  298. .Lloop5:
  299. ___
  300. foreach my $idx (0..3) {
  301. &amm52x20_x1(0,8*$idx,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$k0);
  302. }
  303. $code.=<<___;
  304. lea `4*8`($b_ptr), $b_ptr
  305. dec $iter
  306. jne .Lloop5
  307. vmovdqa64 .Lmask52x4(%rip), $mask52x4
  308. ___
  309. &amm52x20_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0);
  310. $code.=<<___;
  311. vmovdqu64 $R0_0, ($res)
  312. vmovdqu64 $R0_0h, 32($res)
  313. vmovdqu64 $R1_0, 64($res)
  314. vmovdqu64 $R1_0h, 96($res)
  315. vmovdqu64 $R2_0, 128($res)
  316. vzeroupper
  317. mov 0(%rsp),%r15
  318. .cfi_restore %r15
  319. mov 8(%rsp),%r14
  320. .cfi_restore %r14
  321. mov 16(%rsp),%r13
  322. .cfi_restore %r13
  323. mov 24(%rsp),%r12
  324. .cfi_restore %r12
  325. mov 32(%rsp),%rbp
  326. .cfi_restore %rbp
  327. mov 40(%rsp),%rbx
  328. .cfi_restore %rbx
  329. lea 48(%rsp),%rsp
  330. .cfi_adjust_cfa_offset -48
  331. .Lrsaz_amm52x20_x1_256_epilogue:
  332. ret
  333. .cfi_endproc
  334. .size ossl_rsaz_amm52x20_x1_256, .-ossl_rsaz_amm52x20_x1_256
  335. ___
  336. $code.=<<___;
  337. .data
  338. .align 32
  339. .Lmask52x4:
  340. .quad 0xfffffffffffff
  341. .quad 0xfffffffffffff
  342. .quad 0xfffffffffffff
  343. .quad 0xfffffffffffff
  344. ___
  345. ###############################################################################
  346. # Dual Almost Montgomery Multiplication for 20-digit number in radix 2^52
  347. #
  348. # See description of ossl_rsaz_amm52x20_x1_256() above for details about Almost
  349. # Montgomery Multiplication algorithm and function input parameters description.
  350. #
  351. # This function does two AMMs for two independent inputs, hence dual.
  352. #
  353. # void ossl_rsaz_amm52x20_x2_256(BN_ULONG out[2][20],
  354. # const BN_ULONG a[2][20],
  355. # const BN_ULONG b[2][20],
  356. # const BN_ULONG m[2][20],
  357. # const BN_ULONG k0[2]);
  358. ###############################################################################
  359. $code.=<<___;
  360. .text
  361. .globl ossl_rsaz_amm52x20_x2_256
  362. .type ossl_rsaz_amm52x20_x2_256,\@function,5
  363. .align 32
  364. ossl_rsaz_amm52x20_x2_256:
  365. .cfi_startproc
  366. endbranch
  367. push %rbx
  368. .cfi_push %rbx
  369. push %rbp
  370. .cfi_push %rbp
  371. push %r12
  372. .cfi_push %r12
  373. push %r13
  374. .cfi_push %r13
  375. push %r14
  376. .cfi_push %r14
  377. push %r15
  378. .cfi_push %r15
  379. .Lrsaz_amm52x20_x2_256_body:
  380. # Zeroing accumulators
  381. vpxord $zero, $zero, $zero
  382. vmovdqa64 $zero, $R0_0
  383. vmovdqa64 $zero, $R0_0h
  384. vmovdqa64 $zero, $R1_0
  385. vmovdqa64 $zero, $R1_0h
  386. vmovdqa64 $zero, $R2_0
  387. vmovdqa64 $zero, $R0_1
  388. vmovdqa64 $zero, $R0_1h
  389. vmovdqa64 $zero, $R1_1
  390. vmovdqa64 $zero, $R1_1h
  391. vmovdqa64 $zero, $R2_1
  392. xorl $acc0_0_low, $acc0_0_low
  393. xorl $acc0_1_low, $acc0_1_low
  394. movq $b, $b_ptr # backup address of b
  395. movq \$0xfffffffffffff, $mask52 # 52-bit mask
  396. mov \$20, $iter
  397. .align 32
  398. .Lloop20:
  399. ___
  400. &amm52x20_x1( 0, 0,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,"($k0)");
  401. # 20*8 = offset of the next dimension in two-dimension array
  402. &amm52x20_x1(20*8,20*8,$acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,"8($k0)");
  403. $code.=<<___;
  404. lea 8($b_ptr), $b_ptr
  405. dec $iter
  406. jne .Lloop20
  407. vmovdqa64 .Lmask52x4(%rip), $mask52x4
  408. ___
  409. &amm52x20_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0);
  410. &amm52x20_x1_norm($acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1);
  411. $code.=<<___;
  412. vmovdqu64 $R0_0, ($res)
  413. vmovdqu64 $R0_0h, 32($res)
  414. vmovdqu64 $R1_0, 64($res)
  415. vmovdqu64 $R1_0h, 96($res)
  416. vmovdqu64 $R2_0, 128($res)
  417. vmovdqu64 $R0_1, 160($res)
  418. vmovdqu64 $R0_1h, 192($res)
  419. vmovdqu64 $R1_1, 224($res)
  420. vmovdqu64 $R1_1h, 256($res)
  421. vmovdqu64 $R2_1, 288($res)
  422. vzeroupper
  423. mov 0(%rsp),%r15
  424. .cfi_restore %r15
  425. mov 8(%rsp),%r14
  426. .cfi_restore %r14
  427. mov 16(%rsp),%r13
  428. .cfi_restore %r13
  429. mov 24(%rsp),%r12
  430. .cfi_restore %r12
  431. mov 32(%rsp),%rbp
  432. .cfi_restore %rbp
  433. mov 40(%rsp),%rbx
  434. .cfi_restore %rbx
  435. lea 48(%rsp),%rsp
  436. .cfi_adjust_cfa_offset -48
  437. .Lrsaz_amm52x20_x2_256_epilogue:
  438. ret
  439. .cfi_endproc
  440. .size ossl_rsaz_amm52x20_x2_256, .-ossl_rsaz_amm52x20_x2_256
  441. ___
  442. }
  443. ###############################################################################
  444. # Constant time extraction from the precomputed table of powers base^i, where
  445. # i = 0..2^EXP_WIN_SIZE-1
  446. #
  447. # The input |red_table| contains precomputations for two independent base values,
  448. # so the |tbl_idx| indicates for which base shall we extract the value.
  449. # |red_table_idx| is a power index.
  450. #
  451. # Extracted value (output) is 20 digit number in 2^52 radix.
  452. #
  453. # void ossl_extract_multiplier_2x20_win5(BN_ULONG *red_Y,
  454. # const BN_ULONG red_table[1 << EXP_WIN_SIZE][2][20],
  455. # int red_table_idx,
  456. # int tbl_idx); # 0 or 1
  457. #
  458. # EXP_WIN_SIZE = 5
  459. ###############################################################################
  460. {
  461. # input parameters
  462. my ($out,$red_tbl,$red_tbl_idx,$tbl_idx) = @_6_args_universal_ABI;
  463. my ($t0,$t1,$t2,$t3,$t4) = map("%ymm$_", (0..4));
  464. my $t4xmm = $t4;
  465. $t4xmm =~ s/%y/%x/;
  466. my ($tmp0,$tmp1,$tmp2,$tmp3,$tmp4) = map("%ymm$_", (16..20));
  467. my ($cur_idx,$idx,$ones) = map("%ymm$_", (21..23));
  468. $code.=<<___;
  469. .text
  470. .align 32
  471. .globl ossl_extract_multiplier_2x20_win5
  472. .type ossl_extract_multiplier_2x20_win5,\@function,4
  473. ossl_extract_multiplier_2x20_win5:
  474. .cfi_startproc
  475. endbranch
  476. leaq ($tbl_idx,$tbl_idx,4), %rax
  477. salq \$5, %rax
  478. addq %rax, $red_tbl
  479. vmovdqa64 .Lones(%rip), $ones # broadcast ones
  480. vpbroadcastq $red_tbl_idx, $idx
  481. leaq `(1<<5)*2*20*8`($red_tbl), %rax # holds end of the tbl
  482. vpxor $t4xmm, $t4xmm, $t4xmm
  483. vmovdqa64 $t4, $t3 # zeroing t0..4, cur_idx
  484. vmovdqa64 $t4, $t2
  485. vmovdqa64 $t4, $t1
  486. vmovdqa64 $t4, $t0
  487. vmovdqa64 $t4, $cur_idx
  488. .align 32
  489. .Lloop:
  490. vpcmpq \$0, $cur_idx, $idx, %k1 # mask of (idx == cur_idx)
  491. addq \$320, $red_tbl # 320 = 2 * 20 digits * 8 bytes
  492. vpaddq $ones, $cur_idx, $cur_idx # increment cur_idx
  493. vmovdqu64 -320($red_tbl), $tmp0 # load data from red_tbl
  494. vmovdqu64 -288($red_tbl), $tmp1
  495. vmovdqu64 -256($red_tbl), $tmp2
  496. vmovdqu64 -224($red_tbl), $tmp3
  497. vmovdqu64 -192($red_tbl), $tmp4
  498. vpblendmq $tmp0, $t0, ${t0}{%k1} # extract data when mask is not zero
  499. vpblendmq $tmp1, $t1, ${t1}{%k1}
  500. vpblendmq $tmp2, $t2, ${t2}{%k1}
  501. vpblendmq $tmp3, $t3, ${t3}{%k1}
  502. vpblendmq $tmp4, $t4, ${t4}{%k1}
  503. cmpq $red_tbl, %rax
  504. jne .Lloop
  505. vmovdqu64 $t0, ($out) # store t0..4
  506. vmovdqu64 $t1, 32($out)
  507. vmovdqu64 $t2, 64($out)
  508. vmovdqu64 $t3, 96($out)
  509. vmovdqu64 $t4, 128($out)
  510. ret
  511. .cfi_endproc
  512. .size ossl_extract_multiplier_2x20_win5, .-ossl_extract_multiplier_2x20_win5
  513. ___
  514. $code.=<<___;
  515. .data
  516. .align 32
  517. .Lones:
  518. .quad 1,1,1,1
  519. ___
  520. }
  521. if ($win64) {
  522. $rec="%rcx";
  523. $frame="%rdx";
  524. $context="%r8";
  525. $disp="%r9";
  526. $code.=<<___
  527. .extern __imp_RtlVirtualUnwind
  528. .type rsaz_def_handler,\@abi-omnipotent
  529. .align 16
  530. rsaz_def_handler:
  531. push %rsi
  532. push %rdi
  533. push %rbx
  534. push %rbp
  535. push %r12
  536. push %r13
  537. push %r14
  538. push %r15
  539. pushfq
  540. sub \$64,%rsp
  541. mov 120($context),%rax # pull context->Rax
  542. mov 248($context),%rbx # pull context->Rip
  543. mov 8($disp),%rsi # disp->ImageBase
  544. mov 56($disp),%r11 # disp->HandlerData
  545. mov 0(%r11),%r10d # HandlerData[0]
  546. lea (%rsi,%r10),%r10 # prologue label
  547. cmp %r10,%rbx # context->Rip<.Lprologue
  548. jb .Lcommon_seh_tail
  549. mov 152($context),%rax # pull context->Rsp
  550. mov 4(%r11),%r10d # HandlerData[1]
  551. lea (%rsi,%r10),%r10 # epilogue label
  552. cmp %r10,%rbx # context->Rip>=.Lepilogue
  553. jae .Lcommon_seh_tail
  554. lea 48(%rax),%rax
  555. mov -8(%rax),%rbx
  556. mov -16(%rax),%rbp
  557. mov -24(%rax),%r12
  558. mov -32(%rax),%r13
  559. mov -40(%rax),%r14
  560. mov -48(%rax),%r15
  561. mov %rbx,144($context) # restore context->Rbx
  562. mov %rbp,160($context) # restore context->Rbp
  563. mov %r12,216($context) # restore context->R12
  564. mov %r13,224($context) # restore context->R13
  565. mov %r14,232($context) # restore context->R14
  566. mov %r15,240($context) # restore context->R14
  567. .Lcommon_seh_tail:
  568. mov 8(%rax),%rdi
  569. mov 16(%rax),%rsi
  570. mov %rax,152($context) # restore context->Rsp
  571. mov %rsi,168($context) # restore context->Rsi
  572. mov %rdi,176($context) # restore context->Rdi
  573. mov 40($disp),%rdi # disp->ContextRecord
  574. mov $context,%rsi # context
  575. mov \$154,%ecx # sizeof(CONTEXT)
  576. .long 0xa548f3fc # cld; rep movsq
  577. mov $disp,%rsi
  578. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  579. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  580. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  581. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  582. mov 40(%rsi),%r10 # disp->ContextRecord
  583. lea 56(%rsi),%r11 # &disp->HandlerData
  584. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  585. mov %r10,32(%rsp) # arg5
  586. mov %r11,40(%rsp) # arg6
  587. mov %r12,48(%rsp) # arg7
  588. mov %rcx,56(%rsp) # arg8, (NULL)
  589. call *__imp_RtlVirtualUnwind(%rip)
  590. mov \$1,%eax # ExceptionContinueSearch
  591. add \$64,%rsp
  592. popfq
  593. pop %r15
  594. pop %r14
  595. pop %r13
  596. pop %r12
  597. pop %rbp
  598. pop %rbx
  599. pop %rdi
  600. pop %rsi
  601. ret
  602. .size rsaz_def_handler,.-rsaz_def_handler
  603. .section .pdata
  604. .align 4
  605. .rva .LSEH_begin_ossl_rsaz_amm52x20_x1_256
  606. .rva .LSEH_end_ossl_rsaz_amm52x20_x1_256
  607. .rva .LSEH_info_ossl_rsaz_amm52x20_x1_256
  608. .rva .LSEH_begin_ossl_rsaz_amm52x20_x2_256
  609. .rva .LSEH_end_ossl_rsaz_amm52x20_x2_256
  610. .rva .LSEH_info_ossl_rsaz_amm52x20_x2_256
  611. .rva .LSEH_begin_ossl_extract_multiplier_2x20_win5
  612. .rva .LSEH_end_ossl_extract_multiplier_2x20_win5
  613. .rva .LSEH_info_ossl_extract_multiplier_2x20_win5
  614. .section .xdata
  615. .align 8
  616. .LSEH_info_ossl_rsaz_amm52x20_x1_256:
  617. .byte 9,0,0,0
  618. .rva rsaz_def_handler
  619. .rva .Lrsaz_amm52x20_x1_256_body,.Lrsaz_amm52x20_x1_256_epilogue
  620. .LSEH_info_ossl_rsaz_amm52x20_x2_256:
  621. .byte 9,0,0,0
  622. .rva rsaz_def_handler
  623. .rva .Lrsaz_amm52x20_x2_256_body,.Lrsaz_amm52x20_x2_256_epilogue
  624. .LSEH_info_ossl_extract_multiplier_2x20_win5:
  625. .byte 9,0,0,0
  626. .rva rsaz_def_handler
  627. .rva .LSEH_begin_ossl_extract_multiplier_2x20_win5,.LSEH_begin_ossl_extract_multiplier_2x20_win5
  628. ___
  629. }
  630. }}} else {{{ # fallback for old assembler
  631. $code.=<<___;
  632. .text
  633. .globl ossl_rsaz_avx512ifma_eligible
  634. .type ossl_rsaz_avx512ifma_eligible,\@abi-omnipotent
  635. ossl_rsaz_avx512ifma_eligible:
  636. xor %eax,%eax
  637. ret
  638. .size ossl_rsaz_avx512ifma_eligible, .-ossl_rsaz_avx512ifma_eligible
  639. .globl ossl_rsaz_amm52x20_x1_256
  640. .globl ossl_rsaz_amm52x20_x2_256
  641. .globl ossl_extract_multiplier_2x20_win5
  642. .type ossl_rsaz_amm52x20_x1_256,\@abi-omnipotent
  643. ossl_rsaz_amm52x20_x1_256:
  644. ossl_rsaz_amm52x20_x2_256:
  645. ossl_extract_multiplier_2x20_win5:
  646. .byte 0x0f,0x0b # ud2
  647. ret
  648. .size ossl_rsaz_amm52x20_x1_256, .-ossl_rsaz_amm52x20_x1_256
  649. ___
  650. }}}
  651. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  652. print $code;
  653. close STDOUT or die "error closing STDOUT: $!";