rsaz-2k-avx512.pl 23 KB

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