2
0

rsaz-4k-avx512.pl 29 KB

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