rsaz-3k-avx512.pl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. # Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  2. # Copyright (c) 2021, 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. # Intel Corporation
  12. #
  13. # March 2021
  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-alpha15 | this | Unit |
  22. # |---------+-----------------------+---------------+-------------|
  23. # | rsa3072 | 6 397 637 | 2 866 593 | cycles/sign |
  24. # | | 203.2 | 453.5 / +123% | 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. ###############################################################################
  54. # Almost Montgomery Multiplication (AMM) for 30-digit number in radix 2^52.
  55. #
  56. # AMM is defined as presented in the paper [1].
  57. #
  58. # The input and output are presented in 2^52 radix domain, i.e.
  59. # |res|, |a|, |b|, |m| are arrays of 32 64-bit qwords with 12 high bits zeroed
  60. #
  61. # NOTE: the function uses zero-padded data - 2 high QWs is a padding.
  62. #
  63. # |k0| is a Montgomery coefficient, which is here k0 = -1/m mod 2^64
  64. #
  65. # NB: the AMM implementation does not perform "conditional" subtraction step
  66. # specified in the original algorithm as according to the Lemma 1 from the paper
  67. # [2], the result will be always < 2*m and can be used as a direct input to
  68. # the next AMM iteration. This post-condition is true, provided the correct
  69. # parameter |s| (notion of the Lemma 1 from [2]) is chosen, i.e. s >= n + 2 * k,
  70. # which matches our case: 1560 > 1536 + 2 * 1.
  71. #
  72. # [1] Gueron, S. Efficient software implementations of modular exponentiation.
  73. # DOI: 10.1007/s13389-012-0031-5
  74. # [2] Gueron, S. Enhanced Montgomery Multiplication.
  75. # DOI: 10.1007/3-540-36400-5_5
  76. #
  77. # void ossl_rsaz_amm52x30_x1_ifma256(BN_ULONG *res,
  78. # const BN_ULONG *a,
  79. # const BN_ULONG *b,
  80. # const BN_ULONG *m,
  81. # BN_ULONG k0);
  82. ###############################################################################
  83. {
  84. # input parameters ("%rdi","%rsi","%rdx","%rcx","%r8")
  85. my ($res,$a,$b,$m,$k0) = @_6_args_universal_ABI;
  86. my $mask52 = "%rax";
  87. my $acc0_0 = "%r9";
  88. my $acc0_0_low = "%r9d";
  89. my $acc0_1 = "%r15";
  90. my $acc0_1_low = "%r15d";
  91. my $b_ptr = "%r11";
  92. my $iter = "%ebx";
  93. my $zero = "%ymm0";
  94. my $Bi = "%ymm1";
  95. my $Yi = "%ymm2";
  96. my ($R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h) = map("%ymm$_",(3..10));
  97. my ($R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,$R2_1h,$R3_1,$R3_1h) = map("%ymm$_",(11..18));
  98. # Registers mapping for normalization
  99. my ($T0,$T0h,$T1,$T1h,$T2,$T2h,$T3,$T3h) = ("$zero", "$Bi", "$Yi", map("%ymm$_", (19..23)));
  100. sub amm52x30_x1() {
  101. # _data_offset - offset in the |a| or |m| arrays pointing to the beginning
  102. # of data for corresponding AMM operation;
  103. # _b_offset - offset in the |b| array pointing to the next qword digit;
  104. my ($_data_offset,$_b_offset,$_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2,$_R2h,$_R3,$_R3h,$_k0) = @_;
  105. my $_R0_xmm = $_R0;
  106. $_R0_xmm =~ s/%y/%x/;
  107. $code.=<<___;
  108. movq $_b_offset($b_ptr), %r13 # b[i]
  109. vpbroadcastq %r13, $Bi # broadcast b[i]
  110. movq $_data_offset($a), %rdx
  111. mulx %r13, %r13, %r12 # a[0]*b[i] = (t0,t2)
  112. addq %r13, $_acc # acc += t0
  113. movq %r12, %r10
  114. adcq \$0, %r10 # t2 += CF
  115. movq $_k0, %r13
  116. imulq $_acc, %r13 # acc * k0
  117. andq $mask52, %r13 # yi = (acc * k0) & mask52
  118. vpbroadcastq %r13, $Yi # broadcast y[i]
  119. movq $_data_offset($m), %rdx
  120. mulx %r13, %r13, %r12 # yi * m[0] = (t0,t1)
  121. addq %r13, $_acc # acc += t0
  122. adcq %r12, %r10 # t2 += (t1 + CF)
  123. shrq \$52, $_acc
  124. salq \$12, %r10
  125. or %r10, $_acc # acc = ((acc >> 52) | (t2 << 12))
  126. vpmadd52luq `$_data_offset+64*0`($a), $Bi, $_R0
  127. vpmadd52luq `$_data_offset+64*0+32`($a), $Bi, $_R0h
  128. vpmadd52luq `$_data_offset+64*1`($a), $Bi, $_R1
  129. vpmadd52luq `$_data_offset+64*1+32`($a), $Bi, $_R1h
  130. vpmadd52luq `$_data_offset+64*2`($a), $Bi, $_R2
  131. vpmadd52luq `$_data_offset+64*2+32`($a), $Bi, $_R2h
  132. vpmadd52luq `$_data_offset+64*3`($a), $Bi, $_R3
  133. vpmadd52luq `$_data_offset+64*3+32`($a), $Bi, $_R3h
  134. vpmadd52luq `$_data_offset+64*0`($m), $Yi, $_R0
  135. vpmadd52luq `$_data_offset+64*0+32`($m), $Yi, $_R0h
  136. vpmadd52luq `$_data_offset+64*1`($m), $Yi, $_R1
  137. vpmadd52luq `$_data_offset+64*1+32`($m), $Yi, $_R1h
  138. vpmadd52luq `$_data_offset+64*2`($m), $Yi, $_R2
  139. vpmadd52luq `$_data_offset+64*2+32`($m), $Yi, $_R2h
  140. vpmadd52luq `$_data_offset+64*3`($m), $Yi, $_R3
  141. vpmadd52luq `$_data_offset+64*3+32`($m), $Yi, $_R3h
  142. # Shift accumulators right by 1 qword, zero extending the highest one
  143. valignq \$1, $_R0, $_R0h, $_R0
  144. valignq \$1, $_R0h, $_R1, $_R0h
  145. valignq \$1, $_R1, $_R1h, $_R1
  146. valignq \$1, $_R1h, $_R2, $_R1h
  147. valignq \$1, $_R2, $_R2h, $_R2
  148. valignq \$1, $_R2h, $_R3, $_R2h
  149. valignq \$1, $_R3, $_R3h, $_R3
  150. valignq \$1, $_R3h, $zero, $_R3h
  151. vmovq $_R0_xmm, %r13
  152. addq %r13, $_acc # acc += R0[0]
  153. vpmadd52huq `$_data_offset+64*0`($a), $Bi, $_R0
  154. vpmadd52huq `$_data_offset+64*0+32`($a), $Bi, $_R0h
  155. vpmadd52huq `$_data_offset+64*1`($a), $Bi, $_R1
  156. vpmadd52huq `$_data_offset+64*1+32`($a), $Bi, $_R1h
  157. vpmadd52huq `$_data_offset+64*2`($a), $Bi, $_R2
  158. vpmadd52huq `$_data_offset+64*2+32`($a), $Bi, $_R2h
  159. vpmadd52huq `$_data_offset+64*3`($a), $Bi, $_R3
  160. vpmadd52huq `$_data_offset+64*3+32`($a), $Bi, $_R3h
  161. vpmadd52huq `$_data_offset+64*0`($m), $Yi, $_R0
  162. vpmadd52huq `$_data_offset+64*0+32`($m), $Yi, $_R0h
  163. vpmadd52huq `$_data_offset+64*1`($m), $Yi, $_R1
  164. vpmadd52huq `$_data_offset+64*1+32`($m), $Yi, $_R1h
  165. vpmadd52huq `$_data_offset+64*2`($m), $Yi, $_R2
  166. vpmadd52huq `$_data_offset+64*2+32`($m), $Yi, $_R2h
  167. vpmadd52huq `$_data_offset+64*3`($m), $Yi, $_R3
  168. vpmadd52huq `$_data_offset+64*3+32`($m), $Yi, $_R3h
  169. ___
  170. }
  171. # Normalization routine: handles carry bits and gets bignum qwords to normalized
  172. # 2^52 representation.
  173. #
  174. # Uses %r8-14,%e[abcd]x
  175. sub amm52x30_x1_norm {
  176. my ($_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2,$_R2h,$_R3,$_R3h) = @_;
  177. $code.=<<___;
  178. # Put accumulator to low qword in R0
  179. vpbroadcastq $_acc, $T0
  180. vpblendd \$3, $T0, $_R0, $_R0
  181. # Extract "carries" (12 high bits) from each QW of the bignum
  182. # Save them to LSB of QWs in T0..Tn
  183. vpsrlq \$52, $_R0, $T0
  184. vpsrlq \$52, $_R0h, $T0h
  185. vpsrlq \$52, $_R1, $T1
  186. vpsrlq \$52, $_R1h, $T1h
  187. vpsrlq \$52, $_R2, $T2
  188. vpsrlq \$52, $_R2h, $T2h
  189. vpsrlq \$52, $_R3, $T3
  190. vpsrlq \$52, $_R3h, $T3h
  191. # "Shift left" T0..Tn by 1 QW
  192. valignq \$3, $T3, $T3h, $T3h
  193. valignq \$3, $T2h, $T3, $T3
  194. valignq \$3, $T2, $T2h, $T2h
  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..Rn 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. vpandq .Lmask52x4(%rip), $_R2h, $_R2h
  207. vpandq .Lmask52x4(%rip), $_R3, $_R3
  208. vpandq .Lmask52x4(%rip), $_R3h, $_R3h
  209. # Sum R0..Rn with corresponding adjusted carries
  210. vpaddq $T0, $_R0, $_R0
  211. vpaddq $T0h, $_R0h, $_R0h
  212. vpaddq $T1, $_R1, $_R1
  213. vpaddq $T1h, $_R1h, $_R1h
  214. vpaddq $T2, $_R2, $_R2
  215. vpaddq $T2h, $_R2h, $_R2h
  216. vpaddq $T3, $_R3, $_R3
  217. vpaddq $T3h, $_R3h, $_R3h
  218. # Now handle carry bits from this addition
  219. # Get mask of QWs whose 52-bit parts overflow
  220. vpcmpuq \$6,.Lmask52x4(%rip),${_R0},%k1 # OP=nle (i.e. gt)
  221. vpcmpuq \$6,.Lmask52x4(%rip),${_R0h},%k2
  222. kmovb %k1,%r14d
  223. kmovb %k2,%r13d
  224. shl \$4,%r13b
  225. or %r13b,%r14b
  226. vpcmpuq \$6,.Lmask52x4(%rip),${_R1},%k1
  227. vpcmpuq \$6,.Lmask52x4(%rip),${_R1h},%k2
  228. kmovb %k1,%r13d
  229. kmovb %k2,%r12d
  230. shl \$4,%r12b
  231. or %r12b,%r13b
  232. vpcmpuq \$6,.Lmask52x4(%rip),${_R2},%k1
  233. vpcmpuq \$6,.Lmask52x4(%rip),${_R2h},%k2
  234. kmovb %k1,%r12d
  235. kmovb %k2,%r11d
  236. shl \$4,%r11b
  237. or %r11b,%r12b
  238. vpcmpuq \$6,.Lmask52x4(%rip),${_R3},%k1
  239. vpcmpuq \$6,.Lmask52x4(%rip),${_R3h},%k2
  240. kmovb %k1,%r11d
  241. kmovb %k2,%r10d
  242. shl \$4,%r10b
  243. or %r10b,%r11b
  244. addb %r14b,%r14b
  245. adcb %r13b,%r13b
  246. adcb %r12b,%r12b
  247. adcb %r11b,%r11b
  248. # Get mask of QWs whose 52-bit parts saturated
  249. vpcmpuq \$0,.Lmask52x4(%rip),${_R0},%k1 # OP=eq
  250. vpcmpuq \$0,.Lmask52x4(%rip),${_R0h},%k2
  251. kmovb %k1,%r9d
  252. kmovb %k2,%r8d
  253. shl \$4,%r8b
  254. or %r8b,%r9b
  255. vpcmpuq \$0,.Lmask52x4(%rip),${_R1},%k1
  256. vpcmpuq \$0,.Lmask52x4(%rip),${_R1h},%k2
  257. kmovb %k1,%r8d
  258. kmovb %k2,%edx
  259. shl \$4,%dl
  260. or %dl,%r8b
  261. vpcmpuq \$0,.Lmask52x4(%rip),${_R2},%k1
  262. vpcmpuq \$0,.Lmask52x4(%rip),${_R2h},%k2
  263. kmovb %k1,%edx
  264. kmovb %k2,%ecx
  265. shl \$4,%cl
  266. or %cl,%dl
  267. vpcmpuq \$0,.Lmask52x4(%rip),${_R3},%k1
  268. vpcmpuq \$0,.Lmask52x4(%rip),${_R3h},%k2
  269. kmovb %k1,%ecx
  270. kmovb %k2,%ebx
  271. shl \$4,%bl
  272. or %bl,%cl
  273. addb %r9b,%r14b
  274. adcb %r8b,%r13b
  275. adcb %dl,%r12b
  276. adcb %cl,%r11b
  277. xor %r9b,%r14b
  278. xor %r8b,%r13b
  279. xor %dl,%r12b
  280. xor %cl,%r11b
  281. kmovb %r14d,%k1
  282. shr \$4,%r14b
  283. kmovb %r14d,%k2
  284. kmovb %r13d,%k3
  285. shr \$4,%r13b
  286. kmovb %r13d,%k4
  287. kmovb %r12d,%k5
  288. shr \$4,%r12b
  289. kmovb %r12d,%k6
  290. kmovb %r11d,%k7
  291. vpsubq .Lmask52x4(%rip), $_R0, ${_R0}{%k1}
  292. vpsubq .Lmask52x4(%rip), $_R0h, ${_R0h}{%k2}
  293. vpsubq .Lmask52x4(%rip), $_R1, ${_R1}{%k3}
  294. vpsubq .Lmask52x4(%rip), $_R1h, ${_R1h}{%k4}
  295. vpsubq .Lmask52x4(%rip), $_R2, ${_R2}{%k5}
  296. vpsubq .Lmask52x4(%rip), $_R2h, ${_R2h}{%k6}
  297. vpsubq .Lmask52x4(%rip), $_R3, ${_R3}{%k7}
  298. vpandq .Lmask52x4(%rip), $_R0, $_R0
  299. vpandq .Lmask52x4(%rip), $_R0h, $_R0h
  300. vpandq .Lmask52x4(%rip), $_R1, $_R1
  301. vpandq .Lmask52x4(%rip), $_R1h, $_R1h
  302. vpandq .Lmask52x4(%rip), $_R2, $_R2
  303. vpandq .Lmask52x4(%rip), $_R2h, $_R2h
  304. vpandq .Lmask52x4(%rip), $_R3, $_R3
  305. shr \$4,%r11b
  306. kmovb %r11d,%k1
  307. vpsubq .Lmask52x4(%rip), $_R3h, ${_R3h}{%k1}
  308. vpandq .Lmask52x4(%rip), $_R3h, $_R3h
  309. ___
  310. }
  311. $code.=<<___;
  312. .text
  313. .globl ossl_rsaz_amm52x30_x1_ifma256
  314. .type ossl_rsaz_amm52x30_x1_ifma256,\@function,5
  315. .align 32
  316. ossl_rsaz_amm52x30_x1_ifma256:
  317. .cfi_startproc
  318. endbranch
  319. push %rbx
  320. .cfi_push %rbx
  321. push %rbp
  322. .cfi_push %rbp
  323. push %r12
  324. .cfi_push %r12
  325. push %r13
  326. .cfi_push %r13
  327. push %r14
  328. .cfi_push %r14
  329. push %r15
  330. .cfi_push %r15
  331. ___
  332. $code.=<<___ if ($win64);
  333. lea -168(%rsp),%rsp # 16*10 + (8 bytes to get correct 16-byte SIMD alignment)
  334. vmovdqa64 %xmm6, `0*16`(%rsp) # save non-volatile registers
  335. vmovdqa64 %xmm7, `1*16`(%rsp)
  336. vmovdqa64 %xmm8, `2*16`(%rsp)
  337. vmovdqa64 %xmm9, `3*16`(%rsp)
  338. vmovdqa64 %xmm10,`4*16`(%rsp)
  339. vmovdqa64 %xmm11,`5*16`(%rsp)
  340. vmovdqa64 %xmm12,`6*16`(%rsp)
  341. vmovdqa64 %xmm13,`7*16`(%rsp)
  342. vmovdqa64 %xmm14,`8*16`(%rsp)
  343. vmovdqa64 %xmm15,`9*16`(%rsp)
  344. .Lossl_rsaz_amm52x30_x1_ifma256_body:
  345. ___
  346. $code.=<<___;
  347. # Zeroing accumulators
  348. vpxord $zero, $zero, $zero
  349. vmovdqa64 $zero, $R0_0
  350. vmovdqa64 $zero, $R0_0h
  351. vmovdqa64 $zero, $R1_0
  352. vmovdqa64 $zero, $R1_0h
  353. vmovdqa64 $zero, $R2_0
  354. vmovdqa64 $zero, $R2_0h
  355. vmovdqa64 $zero, $R3_0
  356. vmovdqa64 $zero, $R3_0h
  357. xorl $acc0_0_low, $acc0_0_low
  358. movq $b, $b_ptr # backup address of b
  359. movq \$0xfffffffffffff, $mask52 # 52-bit mask
  360. # Loop over 30 digits unrolled by 4
  361. mov \$7, $iter
  362. .align 32
  363. .Lloop7:
  364. ___
  365. foreach my $idx (0..3) {
  366. &amm52x30_x1(0,8*$idx,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,$k0);
  367. }
  368. $code.=<<___;
  369. lea `4*8`($b_ptr), $b_ptr
  370. dec $iter
  371. jne .Lloop7
  372. ___
  373. &amm52x30_x1(0,8*0,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,$k0);
  374. &amm52x30_x1(0,8*1,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,$k0);
  375. &amm52x30_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h);
  376. $code.=<<___;
  377. vmovdqu64 $R0_0, `0*32`($res)
  378. vmovdqu64 $R0_0h, `1*32`($res)
  379. vmovdqu64 $R1_0, `2*32`($res)
  380. vmovdqu64 $R1_0h, `3*32`($res)
  381. vmovdqu64 $R2_0, `4*32`($res)
  382. vmovdqu64 $R2_0h, `5*32`($res)
  383. vmovdqu64 $R3_0, `6*32`($res)
  384. vmovdqu64 $R3_0h, `7*32`($res)
  385. vzeroupper
  386. lea (%rsp),%rax
  387. .cfi_def_cfa_register %rax
  388. ___
  389. $code.=<<___ if ($win64);
  390. vmovdqa64 `0*16`(%rax),%xmm6
  391. vmovdqa64 `1*16`(%rax),%xmm7
  392. vmovdqa64 `2*16`(%rax),%xmm8
  393. vmovdqa64 `3*16`(%rax),%xmm9
  394. vmovdqa64 `4*16`(%rax),%xmm10
  395. vmovdqa64 `5*16`(%rax),%xmm11
  396. vmovdqa64 `6*16`(%rax),%xmm12
  397. vmovdqa64 `7*16`(%rax),%xmm13
  398. vmovdqa64 `8*16`(%rax),%xmm14
  399. vmovdqa64 `9*16`(%rax),%xmm15
  400. lea 168(%rsp),%rax
  401. ___
  402. $code.=<<___;
  403. mov 0(%rax),%r15
  404. .cfi_restore %r15
  405. mov 8(%rax),%r14
  406. .cfi_restore %r14
  407. mov 16(%rax),%r13
  408. .cfi_restore %r13
  409. mov 24(%rax),%r12
  410. .cfi_restore %r12
  411. mov 32(%rax),%rbp
  412. .cfi_restore %rbp
  413. mov 40(%rax),%rbx
  414. .cfi_restore %rbx
  415. lea 48(%rax),%rsp # restore rsp
  416. .cfi_def_cfa %rsp,8
  417. .Lossl_rsaz_amm52x30_x1_ifma256_epilogue:
  418. ret
  419. .cfi_endproc
  420. .size ossl_rsaz_amm52x30_x1_ifma256, .-ossl_rsaz_amm52x30_x1_ifma256
  421. ___
  422. $code.=<<___;
  423. .data
  424. .align 32
  425. .Lmask52x4:
  426. .quad 0xfffffffffffff
  427. .quad 0xfffffffffffff
  428. .quad 0xfffffffffffff
  429. .quad 0xfffffffffffff
  430. ___
  431. ###############################################################################
  432. # Dual Almost Montgomery Multiplication for 30-digit number in radix 2^52
  433. #
  434. # See description of ossl_rsaz_amm52x30_x1_ifma256() above for details about Almost
  435. # Montgomery Multiplication algorithm and function input parameters description.
  436. #
  437. # This function does two AMMs for two independent inputs, hence dual.
  438. #
  439. # NOTE: the function uses zero-padded data - 2 high QWs is a padding.
  440. #
  441. # void ossl_rsaz_amm52x30_x2_ifma256(BN_ULONG out[2][32],
  442. # const BN_ULONG a[2][32],
  443. # const BN_ULONG b[2][32],
  444. # const BN_ULONG m[2][32],
  445. # const BN_ULONG k0[2]);
  446. ###############################################################################
  447. $code.=<<___;
  448. .text
  449. .globl ossl_rsaz_amm52x30_x2_ifma256
  450. .type ossl_rsaz_amm52x30_x2_ifma256,\@function,5
  451. .align 32
  452. ossl_rsaz_amm52x30_x2_ifma256:
  453. .cfi_startproc
  454. endbranch
  455. push %rbx
  456. .cfi_push %rbx
  457. push %rbp
  458. .cfi_push %rbp
  459. push %r12
  460. .cfi_push %r12
  461. push %r13
  462. .cfi_push %r13
  463. push %r14
  464. .cfi_push %r14
  465. push %r15
  466. .cfi_push %r15
  467. ___
  468. $code.=<<___ if ($win64);
  469. lea -168(%rsp),%rsp
  470. vmovdqa64 %xmm6, `0*16`(%rsp) # save non-volatile registers
  471. vmovdqa64 %xmm7, `1*16`(%rsp)
  472. vmovdqa64 %xmm8, `2*16`(%rsp)
  473. vmovdqa64 %xmm9, `3*16`(%rsp)
  474. vmovdqa64 %xmm10,`4*16`(%rsp)
  475. vmovdqa64 %xmm11,`5*16`(%rsp)
  476. vmovdqa64 %xmm12,`6*16`(%rsp)
  477. vmovdqa64 %xmm13,`7*16`(%rsp)
  478. vmovdqa64 %xmm14,`8*16`(%rsp)
  479. vmovdqa64 %xmm15,`9*16`(%rsp)
  480. .Lossl_rsaz_amm52x30_x2_ifma256_body:
  481. ___
  482. $code.=<<___;
  483. # Zeroing accumulators
  484. vpxord $zero, $zero, $zero
  485. vmovdqa64 $zero, $R0_0
  486. vmovdqa64 $zero, $R0_0h
  487. vmovdqa64 $zero, $R1_0
  488. vmovdqa64 $zero, $R1_0h
  489. vmovdqa64 $zero, $R2_0
  490. vmovdqa64 $zero, $R2_0h
  491. vmovdqa64 $zero, $R3_0
  492. vmovdqa64 $zero, $R3_0h
  493. vmovdqa64 $zero, $R0_1
  494. vmovdqa64 $zero, $R0_1h
  495. vmovdqa64 $zero, $R1_1
  496. vmovdqa64 $zero, $R1_1h
  497. vmovdqa64 $zero, $R2_1
  498. vmovdqa64 $zero, $R2_1h
  499. vmovdqa64 $zero, $R3_1
  500. vmovdqa64 $zero, $R3_1h
  501. xorl $acc0_0_low, $acc0_0_low
  502. xorl $acc0_1_low, $acc0_1_low
  503. movq $b, $b_ptr # backup address of b
  504. movq \$0xfffffffffffff, $mask52 # 52-bit mask
  505. mov \$30, $iter
  506. .align 32
  507. .Lloop30:
  508. ___
  509. &amm52x30_x1( 0, 0,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,"($k0)");
  510. # 32*8 = offset of the next dimension in two-dimension array
  511. &amm52x30_x1(32*8,32*8,$acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,$R2_1h,$R3_1,$R3_1h,"8($k0)");
  512. $code.=<<___;
  513. lea 8($b_ptr), $b_ptr
  514. dec $iter
  515. jne .Lloop30
  516. ___
  517. &amm52x30_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h);
  518. &amm52x30_x1_norm($acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,$R2_1h,$R3_1,$R3_1h);
  519. $code.=<<___;
  520. vmovdqu64 $R0_0, `0*32`($res)
  521. vmovdqu64 $R0_0h, `1*32`($res)
  522. vmovdqu64 $R1_0, `2*32`($res)
  523. vmovdqu64 $R1_0h, `3*32`($res)
  524. vmovdqu64 $R2_0, `4*32`($res)
  525. vmovdqu64 $R2_0h, `5*32`($res)
  526. vmovdqu64 $R3_0, `6*32`($res)
  527. vmovdqu64 $R3_0h, `7*32`($res)
  528. vmovdqu64 $R0_1, `8*32`($res)
  529. vmovdqu64 $R0_1h, `9*32`($res)
  530. vmovdqu64 $R1_1, `10*32`($res)
  531. vmovdqu64 $R1_1h, `11*32`($res)
  532. vmovdqu64 $R2_1, `12*32`($res)
  533. vmovdqu64 $R2_1h, `13*32`($res)
  534. vmovdqu64 $R3_1, `14*32`($res)
  535. vmovdqu64 $R3_1h, `15*32`($res)
  536. vzeroupper
  537. lea (%rsp),%rax
  538. .cfi_def_cfa_register %rax
  539. ___
  540. $code.=<<___ if ($win64);
  541. vmovdqa64 `0*16`(%rax),%xmm6
  542. vmovdqa64 `1*16`(%rax),%xmm7
  543. vmovdqa64 `2*16`(%rax),%xmm8
  544. vmovdqa64 `3*16`(%rax),%xmm9
  545. vmovdqa64 `4*16`(%rax),%xmm10
  546. vmovdqa64 `5*16`(%rax),%xmm11
  547. vmovdqa64 `6*16`(%rax),%xmm12
  548. vmovdqa64 `7*16`(%rax),%xmm13
  549. vmovdqa64 `8*16`(%rax),%xmm14
  550. vmovdqa64 `9*16`(%rax),%xmm15
  551. lea 168(%rsp),%rax
  552. ___
  553. $code.=<<___;
  554. mov 0(%rax),%r15
  555. .cfi_restore %r15
  556. mov 8(%rax),%r14
  557. .cfi_restore %r14
  558. mov 16(%rax),%r13
  559. .cfi_restore %r13
  560. mov 24(%rax),%r12
  561. .cfi_restore %r12
  562. mov 32(%rax),%rbp
  563. .cfi_restore %rbp
  564. mov 40(%rax),%rbx
  565. .cfi_restore %rbx
  566. lea 48(%rax),%rsp
  567. .cfi_def_cfa %rsp,8
  568. .Lossl_rsaz_amm52x30_x2_ifma256_epilogue:
  569. ret
  570. .cfi_endproc
  571. .size ossl_rsaz_amm52x30_x2_ifma256, .-ossl_rsaz_amm52x30_x2_ifma256
  572. ___
  573. }
  574. ###############################################################################
  575. # Constant time extraction from the precomputed table of powers base^i, where
  576. # i = 0..2^EXP_WIN_SIZE-1
  577. #
  578. # The input |red_table| contains precomputations for two independent base values.
  579. # |red_table_idx1| and |red_table_idx2| are corresponding power indexes.
  580. #
  581. # Extracted value (output) is 2 (30 + 2) digits numbers in 2^52 radix.
  582. # (2 high QW is zero padding)
  583. #
  584. # void ossl_extract_multiplier_2x30_win5(BN_ULONG *red_Y,
  585. # const BN_ULONG red_table[1 << EXP_WIN_SIZE][2][32],
  586. # int red_table_idx1, int red_table_idx2);
  587. #
  588. # EXP_WIN_SIZE = 5
  589. ###############################################################################
  590. {
  591. # input parameters
  592. my ($out,$red_tbl,$red_tbl_idx1,$red_tbl_idx2)=$win64 ? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
  593. ("%rdi","%rsi","%rdx","%rcx"); # Unix order
  594. my ($t0,$t1,$t2,$t3,$t4,$t5) = map("%ymm$_", (0..5));
  595. my ($t6,$t7,$t8,$t9,$t10,$t11,$t12,$t13,$t14,$t15) = map("%ymm$_", (16..25));
  596. my ($tmp,$cur_idx,$idx1,$idx2,$ones) = map("%ymm$_", (26..30));
  597. my @t = ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11,$t12,$t13,$t14,$t15);
  598. my $t0xmm = $t0;
  599. $t0xmm =~ s/%y/%x/;
  600. $code.=<<___;
  601. .text
  602. .align 32
  603. .globl ossl_extract_multiplier_2x30_win5
  604. .type ossl_extract_multiplier_2x30_win5,\@abi-omnipotent
  605. ossl_extract_multiplier_2x30_win5:
  606. .cfi_startproc
  607. endbranch
  608. vmovdqa64 .Lones(%rip), $ones # broadcast ones
  609. vpbroadcastq $red_tbl_idx1, $idx1
  610. vpbroadcastq $red_tbl_idx2, $idx2
  611. leaq `(1<<5)*2*32*8`($red_tbl), %rax # holds end of the tbl
  612. # zeroing t0..n, cur_idx
  613. vpxor $t0xmm, $t0xmm, $t0xmm
  614. vmovdqa64 $t0, $cur_idx
  615. ___
  616. foreach (1..15) {
  617. $code.="vmovdqa64 $t0, $t[$_] \n";
  618. }
  619. $code.=<<___;
  620. .align 32
  621. .Lloop:
  622. vpcmpq \$0, $cur_idx, $idx1, %k1 # mask of (idx1 == cur_idx)
  623. vpcmpq \$0, $cur_idx, $idx2, %k2 # mask of (idx2 == cur_idx)
  624. ___
  625. foreach (0..15) {
  626. my $mask = $_<8?"%k1":"%k2";
  627. $code.=<<___;
  628. vmovdqu64 `${_}*32`($red_tbl), $tmp # load data from red_tbl
  629. vpblendmq $tmp, $t[$_], ${t[$_]}{$mask} # extract data when mask is not zero
  630. ___
  631. }
  632. $code.=<<___;
  633. vpaddq $ones, $cur_idx, $cur_idx # increment cur_idx
  634. addq \$`2*32*8`, $red_tbl
  635. cmpq $red_tbl, %rax
  636. jne .Lloop
  637. ___
  638. # store t0..n
  639. foreach (0..15) {
  640. $code.="vmovdqu64 $t[$_], `${_}*32`($out) \n";
  641. }
  642. $code.=<<___;
  643. ret
  644. .cfi_endproc
  645. .size ossl_extract_multiplier_2x30_win5, .-ossl_extract_multiplier_2x30_win5
  646. ___
  647. $code.=<<___;
  648. .data
  649. .align 32
  650. .Lones:
  651. .quad 1,1,1,1
  652. .Lzeros:
  653. .quad 0,0,0,0
  654. ___
  655. }
  656. if ($win64) {
  657. $rec="%rcx";
  658. $frame="%rdx";
  659. $context="%r8";
  660. $disp="%r9";
  661. $code.=<<___;
  662. .extern __imp_RtlVirtualUnwind
  663. .type rsaz_avx_handler,\@abi-omnipotent
  664. .align 16
  665. rsaz_avx_handler:
  666. push %rsi
  667. push %rdi
  668. push %rbx
  669. push %rbp
  670. push %r12
  671. push %r13
  672. push %r14
  673. push %r15
  674. pushfq
  675. sub \$64,%rsp
  676. mov 120($context),%rax # pull context->Rax
  677. mov 248($context),%rbx # pull context->Rip
  678. mov 8($disp),%rsi # disp->ImageBase
  679. mov 56($disp),%r11 # disp->HandlerData
  680. mov 0(%r11),%r10d # HandlerData[0]
  681. lea (%rsi,%r10),%r10 # prologue label
  682. cmp %r10,%rbx # context->Rip<.Lprologue
  683. jb .Lcommon_seh_tail
  684. mov 4(%r11),%r10d # HandlerData[1]
  685. lea (%rsi,%r10),%r10 # epilogue label
  686. cmp %r10,%rbx # context->Rip>=.Lepilogue
  687. jae .Lcommon_seh_tail
  688. mov 152($context),%rax # pull context->Rsp
  689. lea (%rax),%rsi # %xmm save area
  690. lea 512($context),%rdi # & context.Xmm6
  691. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  692. .long 0xa548f3fc # cld; rep movsq
  693. lea `48+168`(%rax),%rax
  694. mov -8(%rax),%rbx
  695. mov -16(%rax),%rbp
  696. mov -24(%rax),%r12
  697. mov -32(%rax),%r13
  698. mov -40(%rax),%r14
  699. mov -48(%rax),%r15
  700. mov %rbx,144($context) # restore context->Rbx
  701. mov %rbp,160($context) # restore context->Rbp
  702. mov %r12,216($context) # restore context->R12
  703. mov %r13,224($context) # restore context->R13
  704. mov %r14,232($context) # restore context->R14
  705. mov %r15,240($context) # restore context->R14
  706. .Lcommon_seh_tail:
  707. mov 8(%rax),%rdi
  708. mov 16(%rax),%rsi
  709. mov %rax,152($context) # restore context->Rsp
  710. mov %rsi,168($context) # restore context->Rsi
  711. mov %rdi,176($context) # restore context->Rdi
  712. mov 40($disp),%rdi # disp->ContextRecord
  713. mov $context,%rsi # context
  714. mov \$154,%ecx # sizeof(CONTEXT)
  715. .long 0xa548f3fc # cld; rep movsq
  716. mov $disp,%rsi
  717. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  718. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  719. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  720. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  721. mov 40(%rsi),%r10 # disp->ContextRecord
  722. lea 56(%rsi),%r11 # &disp->HandlerData
  723. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  724. mov %r10,32(%rsp) # arg5
  725. mov %r11,40(%rsp) # arg6
  726. mov %r12,48(%rsp) # arg7
  727. mov %rcx,56(%rsp) # arg8, (NULL)
  728. call *__imp_RtlVirtualUnwind(%rip)
  729. mov \$1,%eax # ExceptionContinueSearch
  730. add \$64,%rsp
  731. popfq
  732. pop %r15
  733. pop %r14
  734. pop %r13
  735. pop %r12
  736. pop %rbp
  737. pop %rbx
  738. pop %rdi
  739. pop %rsi
  740. ret
  741. .size rsaz_avx_handler,.-rsaz_avx_handler
  742. .section .pdata
  743. .align 4
  744. .rva .LSEH_begin_ossl_rsaz_amm52x30_x1_ifma256
  745. .rva .LSEH_end_ossl_rsaz_amm52x30_x1_ifma256
  746. .rva .LSEH_info_ossl_rsaz_amm52x30_x1_ifma256
  747. .rva .LSEH_begin_ossl_rsaz_amm52x30_x2_ifma256
  748. .rva .LSEH_end_ossl_rsaz_amm52x30_x2_ifma256
  749. .rva .LSEH_info_ossl_rsaz_amm52x30_x2_ifma256
  750. .section .xdata
  751. .align 8
  752. .LSEH_info_ossl_rsaz_amm52x30_x1_ifma256:
  753. .byte 9,0,0,0
  754. .rva rsaz_avx_handler
  755. .rva .Lossl_rsaz_amm52x30_x1_ifma256_body,.Lossl_rsaz_amm52x30_x1_ifma256_epilogue
  756. .LSEH_info_ossl_rsaz_amm52x30_x2_ifma256:
  757. .byte 9,0,0,0
  758. .rva rsaz_avx_handler
  759. .rva .Lossl_rsaz_amm52x30_x2_ifma256_body,.Lossl_rsaz_amm52x30_x2_ifma256_epilogue
  760. ___
  761. }
  762. }}} else {{{ # fallback for old assembler
  763. $code.=<<___;
  764. .text
  765. .globl ossl_rsaz_amm52x30_x1_ifma256
  766. .globl ossl_rsaz_amm52x30_x2_ifma256
  767. .globl ossl_extract_multiplier_2x30_win5
  768. .type ossl_rsaz_amm52x30_x1_ifma256,\@abi-omnipotent
  769. ossl_rsaz_amm52x30_x1_ifma256:
  770. ossl_rsaz_amm52x30_x2_ifma256:
  771. ossl_extract_multiplier_2x30_win5:
  772. .byte 0x0f,0x0b # ud2
  773. ret
  774. .size ossl_rsaz_amm52x30_x1_ifma256, .-ossl_rsaz_amm52x30_x1_ifma256
  775. ___
  776. }}}
  777. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  778. print $code;
  779. close STDOUT or die "error closing STDOUT: $!";