2
0

modexp512-x86_64.pl 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (c) 2010-2011 Intel Corp.
  4. # Author: Vinodh.Gopal@intel.com
  5. # Jim Guilford
  6. # Erdinc.Ozturk@intel.com
  7. # Maxim.Perminov@intel.com
  8. #
  9. # More information about algorithm used can be found at:
  10. # http://www.cse.buffalo.edu/srds2009/escs2009_submission_Gopal.pdf
  11. #
  12. # ====================================================================
  13. # Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  14. #
  15. # Redistribution and use in source and binary forms, with or without
  16. # modification, are permitted provided that the following conditions
  17. # are met:
  18. #
  19. # 1. Redistributions of source code must retain the above copyright
  20. # notice, this list of conditions and the following disclaimer.
  21. #
  22. # 2. Redistributions in binary form must reproduce the above copyright
  23. # notice, this list of conditions and the following disclaimer in
  24. # the documentation and/or other materials provided with the
  25. # distribution.
  26. #
  27. # 3. All advertising materials mentioning features or use of this
  28. # software must display the following acknowledgment:
  29. # "This product includes software developed by the OpenSSL Project
  30. # for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  31. #
  32. # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  33. # endorse or promote products derived from this software without
  34. # prior written permission. For written permission, please contact
  35. # licensing@OpenSSL.org.
  36. #
  37. # 5. Products derived from this software may not be called "OpenSSL"
  38. # nor may "OpenSSL" appear in their names without prior written
  39. # permission of the OpenSSL Project.
  40. #
  41. # 6. Redistributions of any form whatsoever must retain the following
  42. # acknowledgment:
  43. # "This product includes software developed by the OpenSSL Project
  44. # for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  45. #
  46. # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  47. # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  49. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  50. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  51. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  52. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  55. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  57. # OF THE POSSIBILITY OF SUCH DAMAGE.
  58. # ====================================================================
  59. $flavour = shift;
  60. $output = shift;
  61. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  62. my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  63. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  64. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  65. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  66. die "can't locate x86_64-xlate.pl";
  67. open STDOUT,"| $^X $xlate $flavour $output";
  68. use strict;
  69. my $code=".text\n\n";
  70. my $m=0;
  71. #
  72. # Define x512 macros
  73. #
  74. #MULSTEP_512_ADD MACRO x7, x6, x5, x4, x3, x2, x1, x0, dst, src1, src2, add_src, tmp1, tmp2
  75. #
  76. # uses rax, rdx, and args
  77. sub MULSTEP_512_ADD
  78. {
  79. my ($x, $DST, $SRC2, $ASRC, $OP, $TMP)=@_;
  80. my @X=@$x; # make a copy
  81. $code.=<<___;
  82. mov (+8*0)($SRC2), %rax
  83. mul $OP # rdx:rax = %OP * [0]
  84. mov ($ASRC), $X[0]
  85. add %rax, $X[0]
  86. adc \$0, %rdx
  87. mov $X[0], $DST
  88. ___
  89. for(my $i=1;$i<8;$i++) {
  90. $code.=<<___;
  91. mov %rdx, $TMP
  92. mov (+8*$i)($SRC2), %rax
  93. mul $OP # rdx:rax = %OP * [$i]
  94. mov (+8*$i)($ASRC), $X[$i]
  95. add %rax, $X[$i]
  96. adc \$0, %rdx
  97. add $TMP, $X[$i]
  98. adc \$0, %rdx
  99. ___
  100. }
  101. $code.=<<___;
  102. mov %rdx, $X[0]
  103. ___
  104. }
  105. #MULSTEP_512 MACRO x7, x6, x5, x4, x3, x2, x1, x0, dst, src2, src1_val, tmp
  106. #
  107. # uses rax, rdx, and args
  108. sub MULSTEP_512
  109. {
  110. my ($x, $DST, $SRC2, $OP, $TMP)=@_;
  111. my @X=@$x; # make a copy
  112. $code.=<<___;
  113. mov (+8*0)($SRC2), %rax
  114. mul $OP # rdx:rax = %OP * [0]
  115. add %rax, $X[0]
  116. adc \$0, %rdx
  117. mov $X[0], $DST
  118. ___
  119. for(my $i=1;$i<8;$i++) {
  120. $code.=<<___;
  121. mov %rdx, $TMP
  122. mov (+8*$i)($SRC2), %rax
  123. mul $OP # rdx:rax = %OP * [$i]
  124. add %rax, $X[$i]
  125. adc \$0, %rdx
  126. add $TMP, $X[$i]
  127. adc \$0, %rdx
  128. ___
  129. }
  130. $code.=<<___;
  131. mov %rdx, $X[0]
  132. ___
  133. }
  134. #
  135. # Swizzle Macros
  136. #
  137. # macro to copy data from flat space to swizzled table
  138. #MACRO swizzle pDst, pSrc, tmp1, tmp2
  139. # pDst and pSrc are modified
  140. sub swizzle
  141. {
  142. my ($pDst, $pSrc, $cnt, $d0)=@_;
  143. $code.=<<___;
  144. mov \$8, $cnt
  145. loop_$m:
  146. mov ($pSrc), $d0
  147. mov $d0#w, ($pDst)
  148. shr \$16, $d0
  149. mov $d0#w, (+64*1)($pDst)
  150. shr \$16, $d0
  151. mov $d0#w, (+64*2)($pDst)
  152. shr \$16, $d0
  153. mov $d0#w, (+64*3)($pDst)
  154. lea 8($pSrc), $pSrc
  155. lea 64*4($pDst), $pDst
  156. dec $cnt
  157. jnz loop_$m
  158. ___
  159. $m++;
  160. }
  161. # macro to copy data from swizzled table to flat space
  162. #MACRO unswizzle pDst, pSrc, tmp*3
  163. sub unswizzle
  164. {
  165. my ($pDst, $pSrc, $cnt, $d0, $d1)=@_;
  166. $code.=<<___;
  167. mov \$4, $cnt
  168. loop_$m:
  169. movzxw (+64*3+256*0)($pSrc), $d0
  170. movzxw (+64*3+256*1)($pSrc), $d1
  171. shl \$16, $d0
  172. shl \$16, $d1
  173. mov (+64*2+256*0)($pSrc), $d0#w
  174. mov (+64*2+256*1)($pSrc), $d1#w
  175. shl \$16, $d0
  176. shl \$16, $d1
  177. mov (+64*1+256*0)($pSrc), $d0#w
  178. mov (+64*1+256*1)($pSrc), $d1#w
  179. shl \$16, $d0
  180. shl \$16, $d1
  181. mov (+64*0+256*0)($pSrc), $d0#w
  182. mov (+64*0+256*1)($pSrc), $d1#w
  183. mov $d0, (+8*0)($pDst)
  184. mov $d1, (+8*1)($pDst)
  185. lea 256*2($pSrc), $pSrc
  186. lea 8*2($pDst), $pDst
  187. sub \$1, $cnt
  188. jnz loop_$m
  189. ___
  190. $m++;
  191. }
  192. #
  193. # Data Structures
  194. #
  195. # Reduce Data
  196. #
  197. #
  198. # Offset Value
  199. # 0C0 Carries
  200. # 0B8 X2[10]
  201. # 0B0 X2[9]
  202. # 0A8 X2[8]
  203. # 0A0 X2[7]
  204. # 098 X2[6]
  205. # 090 X2[5]
  206. # 088 X2[4]
  207. # 080 X2[3]
  208. # 078 X2[2]
  209. # 070 X2[1]
  210. # 068 X2[0]
  211. # 060 X1[12] P[10]
  212. # 058 X1[11] P[9] Z[8]
  213. # 050 X1[10] P[8] Z[7]
  214. # 048 X1[9] P[7] Z[6]
  215. # 040 X1[8] P[6] Z[5]
  216. # 038 X1[7] P[5] Z[4]
  217. # 030 X1[6] P[4] Z[3]
  218. # 028 X1[5] P[3] Z[2]
  219. # 020 X1[4] P[2] Z[1]
  220. # 018 X1[3] P[1] Z[0]
  221. # 010 X1[2] P[0] Y[2]
  222. # 008 X1[1] Q[1] Y[1]
  223. # 000 X1[0] Q[0] Y[0]
  224. my $X1_offset = 0; # 13 qwords
  225. my $X2_offset = $X1_offset + 13*8; # 11 qwords
  226. my $Carries_offset = $X2_offset + 11*8; # 1 qword
  227. my $Q_offset = 0; # 2 qwords
  228. my $P_offset = $Q_offset + 2*8; # 11 qwords
  229. my $Y_offset = 0; # 3 qwords
  230. my $Z_offset = $Y_offset + 3*8; # 9 qwords
  231. my $Red_Data_Size = $Carries_offset + 1*8; # (25 qwords)
  232. #
  233. # Stack Frame
  234. #
  235. #
  236. # offset value
  237. # ... <old stack contents>
  238. # ...
  239. # 280 Garray
  240. # 278 tmp16[15]
  241. # ... ...
  242. # 200 tmp16[0]
  243. # 1F8 tmp[7]
  244. # ... ...
  245. # 1C0 tmp[0]
  246. # 1B8 GT[7]
  247. # ... ...
  248. # 180 GT[0]
  249. # 178 Reduce Data
  250. # ... ...
  251. # 0B8 Reduce Data
  252. # 0B0 reserved
  253. # 0A8 reserved
  254. # 0A0 reserved
  255. # 098 reserved
  256. # 090 reserved
  257. # 088 reduce result addr
  258. # 080 exp[8]
  259. # ...
  260. # 048 exp[1]
  261. # 040 exp[0]
  262. # 038 reserved
  263. # 030 loop_idx
  264. # 028 pg
  265. # 020 i
  266. # 018 pData ; arg 4
  267. # 010 pG ; arg 2
  268. # 008 pResult ; arg 1
  269. # 000 rsp ; stack pointer before subtract
  270. my $rsp_offset = 0;
  271. my $pResult_offset = 8*1 + $rsp_offset;
  272. my $pG_offset = 8*1 + $pResult_offset;
  273. my $pData_offset = 8*1 + $pG_offset;
  274. my $i_offset = 8*1 + $pData_offset;
  275. my $pg_offset = 8*1 + $i_offset;
  276. my $loop_idx_offset = 8*1 + $pg_offset;
  277. my $reserved1_offset = 8*1 + $loop_idx_offset;
  278. my $exp_offset = 8*1 + $reserved1_offset;
  279. my $red_result_addr_offset= 8*9 + $exp_offset;
  280. my $reserved2_offset = 8*1 + $red_result_addr_offset;
  281. my $Reduce_Data_offset = 8*5 + $reserved2_offset;
  282. my $GT_offset = $Red_Data_Size + $Reduce_Data_offset;
  283. my $tmp_offset = 8*8 + $GT_offset;
  284. my $tmp16_offset = 8*8 + $tmp_offset;
  285. my $garray_offset = 8*16 + $tmp16_offset;
  286. my $mem_size = 8*8*32 + $garray_offset;
  287. #
  288. # Offsets within Reduce Data
  289. #
  290. #
  291. # struct MODF_2FOLD_MONT_512_C1_DATA {
  292. # UINT64 t[8][8];
  293. # UINT64 m[8];
  294. # UINT64 m1[8]; /* 2^768 % m */
  295. # UINT64 m2[8]; /* 2^640 % m */
  296. # UINT64 k1[2]; /* (- 1/m) % 2^128 */
  297. # };
  298. my $T = 0;
  299. my $M = 512; # = 8 * 8 * 8
  300. my $M1 = 576; # = 8 * 8 * 9 /* += 8 * 8 */
  301. my $M2 = 640; # = 8 * 8 * 10 /* += 8 * 8 */
  302. my $K1 = 704; # = 8 * 8 * 11 /* += 8 * 8 */
  303. #
  304. # FUNCTIONS
  305. #
  306. {{{
  307. #
  308. # MULADD_128x512 : Function to multiply 128-bits (2 qwords) by 512-bits (8 qwords)
  309. # and add 512-bits (8 qwords)
  310. # to get 640 bits (10 qwords)
  311. # Input: 128-bit mul source: [rdi+8*1], rbp
  312. # 512-bit mul source: [rsi+8*n]
  313. # 512-bit add source: r15, r14, ..., r9, r8
  314. # Output: r9, r8, r15, r14, r13, r12, r11, r10, [rcx+8*1], [rcx+8*0]
  315. # Clobbers all regs except: rcx, rsi, rdi
  316. $code.=<<___;
  317. .type MULADD_128x512,\@abi-omnipotent
  318. .align 16
  319. MULADD_128x512:
  320. ___
  321. &MULSTEP_512([map("%r$_",(8..15))], "(+8*0)(%rcx)", "%rsi", "%rbp", "%rbx");
  322. $code.=<<___;
  323. mov (+8*1)(%rdi), %rbp
  324. ___
  325. &MULSTEP_512([map("%r$_",(9..15,8))], "(+8*1)(%rcx)", "%rsi", "%rbp", "%rbx");
  326. $code.=<<___;
  327. ret
  328. .size MULADD_128x512,.-MULADD_128x512
  329. ___
  330. }}}
  331. {{{
  332. #MULADD_256x512 MACRO pDst, pA, pB, OP, TMP, X7, X6, X5, X4, X3, X2, X1, X0
  333. #
  334. # Inputs: pDst: Destination (768 bits, 12 qwords)
  335. # pA: Multiplicand (1024 bits, 16 qwords)
  336. # pB: Multiplicand (512 bits, 8 qwords)
  337. # Dst = Ah * B + Al
  338. # where Ah is (in qwords) A[15:12] (256 bits) and Al is A[7:0] (512 bits)
  339. # Results in X3 X2 X1 X0 X7 X6 X5 X4 Dst[3:0]
  340. # Uses registers: arguments, RAX, RDX
  341. sub MULADD_256x512
  342. {
  343. my ($pDst, $pA, $pB, $OP, $TMP, $X)=@_;
  344. $code.=<<___;
  345. mov (+8*12)($pA), $OP
  346. ___
  347. &MULSTEP_512_ADD($X, "(+8*0)($pDst)", $pB, $pA, $OP, $TMP);
  348. push(@$X,shift(@$X));
  349. $code.=<<___;
  350. mov (+8*13)($pA), $OP
  351. ___
  352. &MULSTEP_512($X, "(+8*1)($pDst)", $pB, $OP, $TMP);
  353. push(@$X,shift(@$X));
  354. $code.=<<___;
  355. mov (+8*14)($pA), $OP
  356. ___
  357. &MULSTEP_512($X, "(+8*2)($pDst)", $pB, $OP, $TMP);
  358. push(@$X,shift(@$X));
  359. $code.=<<___;
  360. mov (+8*15)($pA), $OP
  361. ___
  362. &MULSTEP_512($X, "(+8*3)($pDst)", $pB, $OP, $TMP);
  363. push(@$X,shift(@$X));
  364. }
  365. #
  366. # mont_reduce(UINT64 *x, /* 1024 bits, 16 qwords */
  367. # UINT64 *m, /* 512 bits, 8 qwords */
  368. # MODF_2FOLD_MONT_512_C1_DATA *data,
  369. # UINT64 *r) /* 512 bits, 8 qwords */
  370. # Input: x (number to be reduced): tmp16 (Implicit)
  371. # m (modulus): [pM] (Implicit)
  372. # data (reduce data): [pData] (Implicit)
  373. # Output: r (result): Address in [red_res_addr]
  374. # result also in: r9, r8, r15, r14, r13, r12, r11, r10
  375. my @X=map("%r$_",(8..15));
  376. $code.=<<___;
  377. .type mont_reduce,\@abi-omnipotent
  378. .align 16
  379. mont_reduce:
  380. ___
  381. my $STACK_DEPTH = 8;
  382. #
  383. # X1 = Xh * M1 + Xl
  384. $code.=<<___;
  385. lea (+$Reduce_Data_offset+$X1_offset+$STACK_DEPTH)(%rsp), %rdi # pX1 (Dst) 769 bits, 13 qwords
  386. mov (+$pData_offset+$STACK_DEPTH)(%rsp), %rsi # pM1 (Bsrc) 512 bits, 8 qwords
  387. add \$$M1, %rsi
  388. lea (+$tmp16_offset+$STACK_DEPTH)(%rsp), %rcx # X (Asrc) 1024 bits, 16 qwords
  389. ___
  390. &MULADD_256x512("%rdi", "%rcx", "%rsi", "%rbp", "%rbx", \@X); # rotates @X 4 times
  391. # results in r11, r10, r9, r8, r15, r14, r13, r12, X1[3:0]
  392. $code.=<<___;
  393. xor %rax, %rax
  394. # X1 += xl
  395. add (+8*8)(%rcx), $X[4]
  396. adc (+8*9)(%rcx), $X[5]
  397. adc (+8*10)(%rcx), $X[6]
  398. adc (+8*11)(%rcx), $X[7]
  399. adc \$0, %rax
  400. # X1 is now rax, r11-r8, r15-r12, tmp16[3:0]
  401. #
  402. # check for carry ;; carry stored in rax
  403. mov $X[4], (+8*8)(%rdi) # rdi points to X1
  404. mov $X[5], (+8*9)(%rdi)
  405. mov $X[6], %rbp
  406. mov $X[7], (+8*11)(%rdi)
  407. mov %rax, (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp)
  408. mov (+8*0)(%rdi), $X[4]
  409. mov (+8*1)(%rdi), $X[5]
  410. mov (+8*2)(%rdi), $X[6]
  411. mov (+8*3)(%rdi), $X[7]
  412. # X1 is now stored in: X1[11], rbp, X1[9:8], r15-r8
  413. # rdi -> X1
  414. # rsi -> M1
  415. #
  416. # X2 = Xh * M2 + Xl
  417. # do first part (X2 = Xh * M2)
  418. add \$8*10, %rdi # rdi -> pXh ; 128 bits, 2 qwords
  419. # Xh is actually { [rdi+8*1], rbp }
  420. add \$($M2-$M1), %rsi # rsi -> M2
  421. lea (+$Reduce_Data_offset+$X2_offset+$STACK_DEPTH)(%rsp), %rcx # rcx -> pX2 ; 641 bits, 11 qwords
  422. ___
  423. unshift(@X,pop(@X)); unshift(@X,pop(@X));
  424. $code.=<<___;
  425. call MULADD_128x512 # args in rcx, rdi / rbp, rsi, r15-r8
  426. # result in r9, r8, r15, r14, r13, r12, r11, r10, X2[1:0]
  427. mov (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp), %rax
  428. # X2 += Xl
  429. add (+8*8-8*10)(%rdi), $X[6] # (-8*10) is to adjust rdi -> Xh to Xl
  430. adc (+8*9-8*10)(%rdi), $X[7]
  431. mov $X[6], (+8*8)(%rcx)
  432. mov $X[7], (+8*9)(%rcx)
  433. adc %rax, %rax
  434. mov %rax, (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp)
  435. lea (+$Reduce_Data_offset+$Q_offset+$STACK_DEPTH)(%rsp), %rdi # rdi -> pQ ; 128 bits, 2 qwords
  436. add \$($K1-$M2), %rsi # rsi -> pK1 ; 128 bits, 2 qwords
  437. # MUL_128x128t128 rdi, rcx, rsi ; Q = X2 * K1 (bottom half)
  438. # B1:B0 = rsi[1:0] = K1[1:0]
  439. # A1:A0 = rcx[1:0] = X2[1:0]
  440. # Result = rdi[1],rbp = Q[1],rbp
  441. mov (%rsi), %r8 # B0
  442. mov (+8*1)(%rsi), %rbx # B1
  443. mov (%rcx), %rax # A0
  444. mul %r8 # B0
  445. mov %rax, %rbp
  446. mov %rdx, %r9
  447. mov (+8*1)(%rcx), %rax # A1
  448. mul %r8 # B0
  449. add %rax, %r9
  450. mov (%rcx), %rax # A0
  451. mul %rbx # B1
  452. add %rax, %r9
  453. mov %r9, (+8*1)(%rdi)
  454. # end MUL_128x128t128
  455. sub \$($K1-$M), %rsi
  456. mov (%rcx), $X[6]
  457. mov (+8*1)(%rcx), $X[7] # r9:r8 = X2[1:0]
  458. call MULADD_128x512 # args in rcx, rdi / rbp, rsi, r15-r8
  459. # result in r9, r8, r15, r14, r13, r12, r11, r10, X2[1:0]
  460. # load first half of m to rdx, rdi, rbx, rax
  461. # moved this here for efficiency
  462. mov (+8*0)(%rsi), %rax
  463. mov (+8*1)(%rsi), %rbx
  464. mov (+8*2)(%rsi), %rdi
  465. mov (+8*3)(%rsi), %rdx
  466. # continue with reduction
  467. mov (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp), %rbp
  468. add (+8*8)(%rcx), $X[6]
  469. adc (+8*9)(%rcx), $X[7]
  470. #accumulate the final carry to rbp
  471. adc %rbp, %rbp
  472. # Add in overflow corrections: R = (X2>>128) += T[overflow]
  473. # R = {r9, r8, r15, r14, ..., r10}
  474. shl \$3, %rbp
  475. mov (+$pData_offset+$STACK_DEPTH)(%rsp), %rcx # rsi -> Data (and points to T)
  476. add %rcx, %rbp # pT ; 512 bits, 8 qwords, spread out
  477. # rsi will be used to generate a mask after the addition
  478. xor %rsi, %rsi
  479. add (+8*8*0)(%rbp), $X[0]
  480. adc (+8*8*1)(%rbp), $X[1]
  481. adc (+8*8*2)(%rbp), $X[2]
  482. adc (+8*8*3)(%rbp), $X[3]
  483. adc (+8*8*4)(%rbp), $X[4]
  484. adc (+8*8*5)(%rbp), $X[5]
  485. adc (+8*8*6)(%rbp), $X[6]
  486. adc (+8*8*7)(%rbp), $X[7]
  487. # if there is a carry: rsi = 0xFFFFFFFFFFFFFFFF
  488. # if carry is clear: rsi = 0x0000000000000000
  489. sbb \$0, %rsi
  490. # if carry is clear, subtract 0. Otherwise, subtract 256 bits of m
  491. and %rsi, %rax
  492. and %rsi, %rbx
  493. and %rsi, %rdi
  494. and %rsi, %rdx
  495. mov \$1, %rbp
  496. sub %rax, $X[0]
  497. sbb %rbx, $X[1]
  498. sbb %rdi, $X[2]
  499. sbb %rdx, $X[3]
  500. # if there is a borrow: rbp = 0
  501. # if there is no borrow: rbp = 1
  502. # this is used to save the borrows in between the first half and the 2nd half of the subtraction of m
  503. sbb \$0, %rbp
  504. #load second half of m to rdx, rdi, rbx, rax
  505. add \$$M, %rcx
  506. mov (+8*4)(%rcx), %rax
  507. mov (+8*5)(%rcx), %rbx
  508. mov (+8*6)(%rcx), %rdi
  509. mov (+8*7)(%rcx), %rdx
  510. # use the rsi mask as before
  511. # if carry is clear, subtract 0. Otherwise, subtract 256 bits of m
  512. and %rsi, %rax
  513. and %rsi, %rbx
  514. and %rsi, %rdi
  515. and %rsi, %rdx
  516. # if rbp = 0, there was a borrow before, it is moved to the carry flag
  517. # if rbp = 1, there was not a borrow before, carry flag is cleared
  518. sub \$1, %rbp
  519. sbb %rax, $X[4]
  520. sbb %rbx, $X[5]
  521. sbb %rdi, $X[6]
  522. sbb %rdx, $X[7]
  523. # write R back to memory
  524. mov (+$red_result_addr_offset+$STACK_DEPTH)(%rsp), %rsi
  525. mov $X[0], (+8*0)(%rsi)
  526. mov $X[1], (+8*1)(%rsi)
  527. mov $X[2], (+8*2)(%rsi)
  528. mov $X[3], (+8*3)(%rsi)
  529. mov $X[4], (+8*4)(%rsi)
  530. mov $X[5], (+8*5)(%rsi)
  531. mov $X[6], (+8*6)(%rsi)
  532. mov $X[7], (+8*7)(%rsi)
  533. ret
  534. .size mont_reduce,.-mont_reduce
  535. ___
  536. }}}
  537. {{{
  538. #MUL_512x512 MACRO pDst, pA, pB, x7, x6, x5, x4, x3, x2, x1, x0, tmp*2
  539. #
  540. # Inputs: pDst: Destination (1024 bits, 16 qwords)
  541. # pA: Multiplicand (512 bits, 8 qwords)
  542. # pB: Multiplicand (512 bits, 8 qwords)
  543. # Uses registers rax, rdx, args
  544. # B operand in [pB] and also in x7...x0
  545. sub MUL_512x512
  546. {
  547. my ($pDst, $pA, $pB, $x, $OP, $TMP, $pDst_o)=@_;
  548. my ($pDst, $pDst_o) = ($pDst =~ m/([^+]*)\+?(.*)?/);
  549. my @X=@$x; # make a copy
  550. $code.=<<___;
  551. mov (+8*0)($pA), $OP
  552. mov $X[0], %rax
  553. mul $OP # rdx:rax = %OP * [0]
  554. mov %rax, (+$pDst_o+8*0)($pDst)
  555. mov %rdx, $X[0]
  556. ___
  557. for(my $i=1;$i<8;$i++) {
  558. $code.=<<___;
  559. mov $X[$i], %rax
  560. mul $OP # rdx:rax = %OP * [$i]
  561. add %rax, $X[$i-1]
  562. adc \$0, %rdx
  563. mov %rdx, $X[$i]
  564. ___
  565. }
  566. for(my $i=1;$i<8;$i++) {
  567. $code.=<<___;
  568. mov (+8*$i)($pA), $OP
  569. ___
  570. &MULSTEP_512(\@X, "(+$pDst_o+8*$i)($pDst)", $pB, $OP, $TMP);
  571. push(@X,shift(@X));
  572. }
  573. $code.=<<___;
  574. mov $X[0], (+$pDst_o+8*8)($pDst)
  575. mov $X[1], (+$pDst_o+8*9)($pDst)
  576. mov $X[2], (+$pDst_o+8*10)($pDst)
  577. mov $X[3], (+$pDst_o+8*11)($pDst)
  578. mov $X[4], (+$pDst_o+8*12)($pDst)
  579. mov $X[5], (+$pDst_o+8*13)($pDst)
  580. mov $X[6], (+$pDst_o+8*14)($pDst)
  581. mov $X[7], (+$pDst_o+8*15)($pDst)
  582. ___
  583. }
  584. #
  585. # mont_mul_a3b : subroutine to compute (Src1 * Src2) % M (all 512-bits)
  586. # Input: src1: Address of source 1: rdi
  587. # src2: Address of source 2: rsi
  588. # Output: dst: Address of destination: [red_res_addr]
  589. # src2 and result also in: r9, r8, r15, r14, r13, r12, r11, r10
  590. # Temp: Clobbers [tmp16], all registers
  591. $code.=<<___;
  592. .type mont_mul_a3b,\@abi-omnipotent
  593. .align 16
  594. mont_mul_a3b:
  595. #
  596. # multiply tmp = src1 * src2
  597. # For multiply: dst = rcx, src1 = rdi, src2 = rsi
  598. # stack depth is extra 8 from call
  599. ___
  600. &MUL_512x512("%rsp+$tmp16_offset+8", "%rdi", "%rsi", [map("%r$_",(10..15,8..9))], "%rbp", "%rbx");
  601. $code.=<<___;
  602. #
  603. # Dst = tmp % m
  604. # Call reduce(tmp, m, data, dst)
  605. # tail recursion optimization: jmp to mont_reduce and return from there
  606. jmp mont_reduce
  607. # call mont_reduce
  608. # ret
  609. .size mont_mul_a3b,.-mont_mul_a3b
  610. ___
  611. }}}
  612. {{{
  613. #SQR_512 MACRO pDest, pA, x7, x6, x5, x4, x3, x2, x1, x0, tmp*4
  614. #
  615. # Input in memory [pA] and also in x7...x0
  616. # Uses all argument registers plus rax and rdx
  617. #
  618. # This version computes all of the off-diagonal terms into memory,
  619. # and then it adds in the diagonal terms
  620. sub SQR_512
  621. {
  622. my ($pDst, $pA, $x, $A, $tmp, $x7, $x6, $pDst_o)=@_;
  623. my ($pDst, $pDst_o) = ($pDst =~ m/([^+]*)\+?(.*)?/);
  624. my @X=@$x; # make a copy
  625. $code.=<<___;
  626. # ------------------
  627. # first pass 01...07
  628. # ------------------
  629. mov $X[0], $A
  630. mov $X[1],%rax
  631. mul $A
  632. mov %rax, (+$pDst_o+8*1)($pDst)
  633. ___
  634. for(my $i=2;$i<8;$i++) {
  635. $code.=<<___;
  636. mov %rdx, $X[$i-2]
  637. mov $X[$i],%rax
  638. mul $A
  639. add %rax, $X[$i-2]
  640. adc \$0, %rdx
  641. ___
  642. }
  643. $code.=<<___;
  644. mov %rdx, $x7
  645. mov $X[0], (+$pDst_o+8*2)($pDst)
  646. # ------------------
  647. # second pass 12...17
  648. # ------------------
  649. mov (+8*1)($pA), $A
  650. mov (+8*2)($pA),%rax
  651. mul $A
  652. add %rax, $X[1]
  653. adc \$0, %rdx
  654. mov $X[1], (+$pDst_o+8*3)($pDst)
  655. mov %rdx, $X[0]
  656. mov (+8*3)($pA),%rax
  657. mul $A
  658. add %rax, $X[2]
  659. adc \$0, %rdx
  660. add $X[0], $X[2]
  661. adc \$0, %rdx
  662. mov $X[2], (+$pDst_o+8*4)($pDst)
  663. mov %rdx, $X[0]
  664. mov (+8*4)($pA),%rax
  665. mul $A
  666. add %rax, $X[3]
  667. adc \$0, %rdx
  668. add $X[0], $X[3]
  669. adc \$0, %rdx
  670. mov %rdx, $X[0]
  671. mov (+8*5)($pA),%rax
  672. mul $A
  673. add %rax, $X[4]
  674. adc \$0, %rdx
  675. add $X[0], $X[4]
  676. adc \$0, %rdx
  677. mov %rdx, $X[0]
  678. mov $X[6],%rax
  679. mul $A
  680. add %rax, $X[5]
  681. adc \$0, %rdx
  682. add $X[0], $X[5]
  683. adc \$0, %rdx
  684. mov %rdx, $X[0]
  685. mov $X[7],%rax
  686. mul $A
  687. add %rax, $x7
  688. adc \$0, %rdx
  689. add $X[0], $x7
  690. adc \$0, %rdx
  691. mov %rdx, $X[1]
  692. # ------------------
  693. # third pass 23...27
  694. # ------------------
  695. mov (+8*2)($pA), $A
  696. mov (+8*3)($pA),%rax
  697. mul $A
  698. add %rax, $X[3]
  699. adc \$0, %rdx
  700. mov $X[3], (+$pDst_o+8*5)($pDst)
  701. mov %rdx, $X[0]
  702. mov (+8*4)($pA),%rax
  703. mul $A
  704. add %rax, $X[4]
  705. adc \$0, %rdx
  706. add $X[0], $X[4]
  707. adc \$0, %rdx
  708. mov $X[4], (+$pDst_o+8*6)($pDst)
  709. mov %rdx, $X[0]
  710. mov (+8*5)($pA),%rax
  711. mul $A
  712. add %rax, $X[5]
  713. adc \$0, %rdx
  714. add $X[0], $X[5]
  715. adc \$0, %rdx
  716. mov %rdx, $X[0]
  717. mov $X[6],%rax
  718. mul $A
  719. add %rax, $x7
  720. adc \$0, %rdx
  721. add $X[0], $x7
  722. adc \$0, %rdx
  723. mov %rdx, $X[0]
  724. mov $X[7],%rax
  725. mul $A
  726. add %rax, $X[1]
  727. adc \$0, %rdx
  728. add $X[0], $X[1]
  729. adc \$0, %rdx
  730. mov %rdx, $X[2]
  731. # ------------------
  732. # fourth pass 34...37
  733. # ------------------
  734. mov (+8*3)($pA), $A
  735. mov (+8*4)($pA),%rax
  736. mul $A
  737. add %rax, $X[5]
  738. adc \$0, %rdx
  739. mov $X[5], (+$pDst_o+8*7)($pDst)
  740. mov %rdx, $X[0]
  741. mov (+8*5)($pA),%rax
  742. mul $A
  743. add %rax, $x7
  744. adc \$0, %rdx
  745. add $X[0], $x7
  746. adc \$0, %rdx
  747. mov $x7, (+$pDst_o+8*8)($pDst)
  748. mov %rdx, $X[0]
  749. mov $X[6],%rax
  750. mul $A
  751. add %rax, $X[1]
  752. adc \$0, %rdx
  753. add $X[0], $X[1]
  754. adc \$0, %rdx
  755. mov %rdx, $X[0]
  756. mov $X[7],%rax
  757. mul $A
  758. add %rax, $X[2]
  759. adc \$0, %rdx
  760. add $X[0], $X[2]
  761. adc \$0, %rdx
  762. mov %rdx, $X[5]
  763. # ------------------
  764. # fifth pass 45...47
  765. # ------------------
  766. mov (+8*4)($pA), $A
  767. mov (+8*5)($pA),%rax
  768. mul $A
  769. add %rax, $X[1]
  770. adc \$0, %rdx
  771. mov $X[1], (+$pDst_o+8*9)($pDst)
  772. mov %rdx, $X[0]
  773. mov $X[6],%rax
  774. mul $A
  775. add %rax, $X[2]
  776. adc \$0, %rdx
  777. add $X[0], $X[2]
  778. adc \$0, %rdx
  779. mov $X[2], (+$pDst_o+8*10)($pDst)
  780. mov %rdx, $X[0]
  781. mov $X[7],%rax
  782. mul $A
  783. add %rax, $X[5]
  784. adc \$0, %rdx
  785. add $X[0], $X[5]
  786. adc \$0, %rdx
  787. mov %rdx, $X[1]
  788. # ------------------
  789. # sixth pass 56...57
  790. # ------------------
  791. mov (+8*5)($pA), $A
  792. mov $X[6],%rax
  793. mul $A
  794. add %rax, $X[5]
  795. adc \$0, %rdx
  796. mov $X[5], (+$pDst_o+8*11)($pDst)
  797. mov %rdx, $X[0]
  798. mov $X[7],%rax
  799. mul $A
  800. add %rax, $X[1]
  801. adc \$0, %rdx
  802. add $X[0], $X[1]
  803. adc \$0, %rdx
  804. mov $X[1], (+$pDst_o+8*12)($pDst)
  805. mov %rdx, $X[2]
  806. # ------------------
  807. # seventh pass 67
  808. # ------------------
  809. mov $X[6], $A
  810. mov $X[7],%rax
  811. mul $A
  812. add %rax, $X[2]
  813. adc \$0, %rdx
  814. mov $X[2], (+$pDst_o+8*13)($pDst)
  815. mov %rdx, (+$pDst_o+8*14)($pDst)
  816. # start finalize (add in squares, and double off-terms)
  817. mov (+$pDst_o+8*1)($pDst), $X[0]
  818. mov (+$pDst_o+8*2)($pDst), $X[1]
  819. mov (+$pDst_o+8*3)($pDst), $X[2]
  820. mov (+$pDst_o+8*4)($pDst), $X[3]
  821. mov (+$pDst_o+8*5)($pDst), $X[4]
  822. mov (+$pDst_o+8*6)($pDst), $X[5]
  823. mov (+8*3)($pA), %rax
  824. mul %rax
  825. mov %rax, $x6
  826. mov %rdx, $X[6]
  827. add $X[0], $X[0]
  828. adc $X[1], $X[1]
  829. adc $X[2], $X[2]
  830. adc $X[3], $X[3]
  831. adc $X[4], $X[4]
  832. adc $X[5], $X[5]
  833. adc \$0, $X[6]
  834. mov (+8*0)($pA), %rax
  835. mul %rax
  836. mov %rax, (+$pDst_o+8*0)($pDst)
  837. mov %rdx, $A
  838. mov (+8*1)($pA), %rax
  839. mul %rax
  840. add $A, $X[0]
  841. adc %rax, $X[1]
  842. adc \$0, %rdx
  843. mov %rdx, $A
  844. mov $X[0], (+$pDst_o+8*1)($pDst)
  845. mov $X[1], (+$pDst_o+8*2)($pDst)
  846. mov (+8*2)($pA), %rax
  847. mul %rax
  848. add $A, $X[2]
  849. adc %rax, $X[3]
  850. adc \$0, %rdx
  851. mov %rdx, $A
  852. mov $X[2], (+$pDst_o+8*3)($pDst)
  853. mov $X[3], (+$pDst_o+8*4)($pDst)
  854. xor $tmp, $tmp
  855. add $A, $X[4]
  856. adc $x6, $X[5]
  857. adc \$0, $tmp
  858. mov $X[4], (+$pDst_o+8*5)($pDst)
  859. mov $X[5], (+$pDst_o+8*6)($pDst)
  860. # %%tmp has 0/1 in column 7
  861. # %%A6 has a full value in column 7
  862. mov (+$pDst_o+8*7)($pDst), $X[0]
  863. mov (+$pDst_o+8*8)($pDst), $X[1]
  864. mov (+$pDst_o+8*9)($pDst), $X[2]
  865. mov (+$pDst_o+8*10)($pDst), $X[3]
  866. mov (+$pDst_o+8*11)($pDst), $X[4]
  867. mov (+$pDst_o+8*12)($pDst), $X[5]
  868. mov (+$pDst_o+8*13)($pDst), $x6
  869. mov (+$pDst_o+8*14)($pDst), $x7
  870. mov $X[7], %rax
  871. mul %rax
  872. mov %rax, $X[7]
  873. mov %rdx, $A
  874. add $X[0], $X[0]
  875. adc $X[1], $X[1]
  876. adc $X[2], $X[2]
  877. adc $X[3], $X[3]
  878. adc $X[4], $X[4]
  879. adc $X[5], $X[5]
  880. adc $x6, $x6
  881. adc $x7, $x7
  882. adc \$0, $A
  883. add $tmp, $X[0]
  884. mov (+8*4)($pA), %rax
  885. mul %rax
  886. add $X[6], $X[0]
  887. adc %rax, $X[1]
  888. adc \$0, %rdx
  889. mov %rdx, $tmp
  890. mov $X[0], (+$pDst_o+8*7)($pDst)
  891. mov $X[1], (+$pDst_o+8*8)($pDst)
  892. mov (+8*5)($pA), %rax
  893. mul %rax
  894. add $tmp, $X[2]
  895. adc %rax, $X[3]
  896. adc \$0, %rdx
  897. mov %rdx, $tmp
  898. mov $X[2], (+$pDst_o+8*9)($pDst)
  899. mov $X[3], (+$pDst_o+8*10)($pDst)
  900. mov (+8*6)($pA), %rax
  901. mul %rax
  902. add $tmp, $X[4]
  903. adc %rax, $X[5]
  904. adc \$0, %rdx
  905. mov $X[4], (+$pDst_o+8*11)($pDst)
  906. mov $X[5], (+$pDst_o+8*12)($pDst)
  907. add %rdx, $x6
  908. adc $X[7], $x7
  909. adc \$0, $A
  910. mov $x6, (+$pDst_o+8*13)($pDst)
  911. mov $x7, (+$pDst_o+8*14)($pDst)
  912. mov $A, (+$pDst_o+8*15)($pDst)
  913. ___
  914. }
  915. #
  916. # sqr_reduce: subroutine to compute Result = reduce(Result * Result)
  917. #
  918. # input and result also in: r9, r8, r15, r14, r13, r12, r11, r10
  919. #
  920. $code.=<<___;
  921. .type sqr_reduce,\@abi-omnipotent
  922. .align 16
  923. sqr_reduce:
  924. mov (+$pResult_offset+8)(%rsp), %rcx
  925. ___
  926. &SQR_512("%rsp+$tmp16_offset+8", "%rcx", [map("%r$_",(10..15,8..9))], "%rbx", "%rbp", "%rsi", "%rdi");
  927. $code.=<<___;
  928. # tail recursion optimization: jmp to mont_reduce and return from there
  929. jmp mont_reduce
  930. # call mont_reduce
  931. # ret
  932. .size sqr_reduce,.-sqr_reduce
  933. ___
  934. }}}
  935. #
  936. # MAIN FUNCTION
  937. #
  938. #mod_exp_512(UINT64 *result, /* 512 bits, 8 qwords */
  939. # UINT64 *g, /* 512 bits, 8 qwords */
  940. # UINT64 *exp, /* 512 bits, 8 qwords */
  941. # struct mod_ctx_512 *data)
  942. # window size = 5
  943. # table size = 2^5 = 32
  944. #table_entries equ 32
  945. #table_size equ table_entries * 8
  946. $code.=<<___;
  947. .globl mod_exp_512
  948. .type mod_exp_512,\@function,4
  949. mod_exp_512:
  950. push %rbp
  951. push %rbx
  952. push %r12
  953. push %r13
  954. push %r14
  955. push %r15
  956. # adjust stack down and then align it with cache boundary
  957. mov %rsp, %r8
  958. sub \$($mem_size), %rsp
  959. and \$-64, %rsp
  960. # store previous stack pointer and arguments
  961. mov %r8, (+$rsp_offset)(%rsp)
  962. mov %rdi, (+$pResult_offset)(%rsp)
  963. mov %rsi, (+$pG_offset)(%rsp)
  964. mov %rcx, (+$pData_offset)(%rsp)
  965. .Lbody:
  966. # transform g into montgomery space
  967. # GT = reduce(g * C2) = reduce(g * (2^256))
  968. # reduce expects to have the input in [tmp16]
  969. pxor %xmm4, %xmm4
  970. movdqu (+16*0)(%rsi), %xmm0
  971. movdqu (+16*1)(%rsi), %xmm1
  972. movdqu (+16*2)(%rsi), %xmm2
  973. movdqu (+16*3)(%rsi), %xmm3
  974. movdqa %xmm4, (+$tmp16_offset+16*0)(%rsp)
  975. movdqa %xmm4, (+$tmp16_offset+16*1)(%rsp)
  976. movdqa %xmm4, (+$tmp16_offset+16*6)(%rsp)
  977. movdqa %xmm4, (+$tmp16_offset+16*7)(%rsp)
  978. movdqa %xmm0, (+$tmp16_offset+16*2)(%rsp)
  979. movdqa %xmm1, (+$tmp16_offset+16*3)(%rsp)
  980. movdqa %xmm2, (+$tmp16_offset+16*4)(%rsp)
  981. movdqa %xmm3, (+$tmp16_offset+16*5)(%rsp)
  982. # load pExp before rdx gets blown away
  983. movdqu (+16*0)(%rdx), %xmm0
  984. movdqu (+16*1)(%rdx), %xmm1
  985. movdqu (+16*2)(%rdx), %xmm2
  986. movdqu (+16*3)(%rdx), %xmm3
  987. lea (+$GT_offset)(%rsp), %rbx
  988. mov %rbx, (+$red_result_addr_offset)(%rsp)
  989. call mont_reduce
  990. # Initialize tmp = C
  991. lea (+$tmp_offset)(%rsp), %rcx
  992. xor %rax, %rax
  993. mov %rax, (+8*0)(%rcx)
  994. mov %rax, (+8*1)(%rcx)
  995. mov %rax, (+8*3)(%rcx)
  996. mov %rax, (+8*4)(%rcx)
  997. mov %rax, (+8*5)(%rcx)
  998. mov %rax, (+8*6)(%rcx)
  999. mov %rax, (+8*7)(%rcx)
  1000. mov %rax, (+$exp_offset+8*8)(%rsp)
  1001. movq \$1, (+8*2)(%rcx)
  1002. lea (+$garray_offset)(%rsp), %rbp
  1003. mov %rcx, %rsi # pTmp
  1004. mov %rbp, %rdi # Garray[][0]
  1005. ___
  1006. &swizzle("%rdi", "%rcx", "%rax", "%rbx");
  1007. # for (rax = 31; rax != 0; rax--) {
  1008. # tmp = reduce(tmp * G)
  1009. # swizzle(pg, tmp);
  1010. # pg += 2; }
  1011. $code.=<<___;
  1012. mov \$31, %rax
  1013. mov %rax, (+$i_offset)(%rsp)
  1014. mov %rbp, (+$pg_offset)(%rsp)
  1015. # rsi -> pTmp
  1016. mov %rsi, (+$red_result_addr_offset)(%rsp)
  1017. mov (+8*0)(%rsi), %r10
  1018. mov (+8*1)(%rsi), %r11
  1019. mov (+8*2)(%rsi), %r12
  1020. mov (+8*3)(%rsi), %r13
  1021. mov (+8*4)(%rsi), %r14
  1022. mov (+8*5)(%rsi), %r15
  1023. mov (+8*6)(%rsi), %r8
  1024. mov (+8*7)(%rsi), %r9
  1025. init_loop:
  1026. lea (+$GT_offset)(%rsp), %rdi
  1027. call mont_mul_a3b
  1028. lea (+$tmp_offset)(%rsp), %rsi
  1029. mov (+$pg_offset)(%rsp), %rbp
  1030. add \$2, %rbp
  1031. mov %rbp, (+$pg_offset)(%rsp)
  1032. mov %rsi, %rcx # rcx = rsi = addr of tmp
  1033. ___
  1034. &swizzle("%rbp", "%rcx", "%rax", "%rbx");
  1035. $code.=<<___;
  1036. mov (+$i_offset)(%rsp), %rax
  1037. sub \$1, %rax
  1038. mov %rax, (+$i_offset)(%rsp)
  1039. jne init_loop
  1040. #
  1041. # Copy exponent onto stack
  1042. movdqa %xmm0, (+$exp_offset+16*0)(%rsp)
  1043. movdqa %xmm1, (+$exp_offset+16*1)(%rsp)
  1044. movdqa %xmm2, (+$exp_offset+16*2)(%rsp)
  1045. movdqa %xmm3, (+$exp_offset+16*3)(%rsp)
  1046. #
  1047. # Do exponentiation
  1048. # Initialize result to G[exp{511:507}]
  1049. mov (+$exp_offset+62)(%rsp), %eax
  1050. mov %rax, %rdx
  1051. shr \$11, %rax
  1052. and \$0x07FF, %edx
  1053. mov %edx, (+$exp_offset+62)(%rsp)
  1054. lea (+$garray_offset)(%rsp,%rax,2), %rsi
  1055. mov (+$pResult_offset)(%rsp), %rdx
  1056. ___
  1057. &unswizzle("%rdx", "%rsi", "%rbp", "%rbx", "%rax");
  1058. #
  1059. # Loop variables
  1060. # rcx = [loop_idx] = index: 510-5 to 0 by 5
  1061. $code.=<<___;
  1062. movq \$505, (+$loop_idx_offset)(%rsp)
  1063. mov (+$pResult_offset)(%rsp), %rcx
  1064. mov %rcx, (+$red_result_addr_offset)(%rsp)
  1065. mov (+8*0)(%rcx), %r10
  1066. mov (+8*1)(%rcx), %r11
  1067. mov (+8*2)(%rcx), %r12
  1068. mov (+8*3)(%rcx), %r13
  1069. mov (+8*4)(%rcx), %r14
  1070. mov (+8*5)(%rcx), %r15
  1071. mov (+8*6)(%rcx), %r8
  1072. mov (+8*7)(%rcx), %r9
  1073. jmp sqr_2
  1074. main_loop_a3b:
  1075. call sqr_reduce
  1076. call sqr_reduce
  1077. call sqr_reduce
  1078. sqr_2:
  1079. call sqr_reduce
  1080. call sqr_reduce
  1081. #
  1082. # Do multiply, first look up proper value in Garray
  1083. mov (+$loop_idx_offset)(%rsp), %rcx # bit index
  1084. mov %rcx, %rax
  1085. shr \$4, %rax # rax is word pointer
  1086. mov (+$exp_offset)(%rsp,%rax,2), %edx
  1087. and \$15, %rcx
  1088. shrq %cl, %rdx
  1089. and \$0x1F, %rdx
  1090. lea (+$garray_offset)(%rsp,%rdx,2), %rsi
  1091. lea (+$tmp_offset)(%rsp), %rdx
  1092. mov %rdx, %rdi
  1093. ___
  1094. &unswizzle("%rdx", "%rsi", "%rbp", "%rbx", "%rax");
  1095. # rdi = tmp = pG
  1096. #
  1097. # Call mod_mul_a1(pDst, pSrc1, pSrc2, pM, pData)
  1098. # result result pG M Data
  1099. $code.=<<___;
  1100. mov (+$pResult_offset)(%rsp), %rsi
  1101. call mont_mul_a3b
  1102. #
  1103. # finish loop
  1104. mov (+$loop_idx_offset)(%rsp), %rcx
  1105. sub \$5, %rcx
  1106. mov %rcx, (+$loop_idx_offset)(%rsp)
  1107. jge main_loop_a3b
  1108. #
  1109. end_main_loop_a3b:
  1110. # transform result out of Montgomery space
  1111. # result = reduce(result)
  1112. mov (+$pResult_offset)(%rsp), %rdx
  1113. pxor %xmm4, %xmm4
  1114. movdqu (+16*0)(%rdx), %xmm0
  1115. movdqu (+16*1)(%rdx), %xmm1
  1116. movdqu (+16*2)(%rdx), %xmm2
  1117. movdqu (+16*3)(%rdx), %xmm3
  1118. movdqa %xmm4, (+$tmp16_offset+16*4)(%rsp)
  1119. movdqa %xmm4, (+$tmp16_offset+16*5)(%rsp)
  1120. movdqa %xmm4, (+$tmp16_offset+16*6)(%rsp)
  1121. movdqa %xmm4, (+$tmp16_offset+16*7)(%rsp)
  1122. movdqa %xmm0, (+$tmp16_offset+16*0)(%rsp)
  1123. movdqa %xmm1, (+$tmp16_offset+16*1)(%rsp)
  1124. movdqa %xmm2, (+$tmp16_offset+16*2)(%rsp)
  1125. movdqa %xmm3, (+$tmp16_offset+16*3)(%rsp)
  1126. call mont_reduce
  1127. # If result > m, subract m
  1128. # load result into r15:r8
  1129. mov (+$pResult_offset)(%rsp), %rax
  1130. mov (+8*0)(%rax), %r8
  1131. mov (+8*1)(%rax), %r9
  1132. mov (+8*2)(%rax), %r10
  1133. mov (+8*3)(%rax), %r11
  1134. mov (+8*4)(%rax), %r12
  1135. mov (+8*5)(%rax), %r13
  1136. mov (+8*6)(%rax), %r14
  1137. mov (+8*7)(%rax), %r15
  1138. # subtract m
  1139. mov (+$pData_offset)(%rsp), %rbx
  1140. add \$$M, %rbx
  1141. sub (+8*0)(%rbx), %r8
  1142. sbb (+8*1)(%rbx), %r9
  1143. sbb (+8*2)(%rbx), %r10
  1144. sbb (+8*3)(%rbx), %r11
  1145. sbb (+8*4)(%rbx), %r12
  1146. sbb (+8*5)(%rbx), %r13
  1147. sbb (+8*6)(%rbx), %r14
  1148. sbb (+8*7)(%rbx), %r15
  1149. # if Carry is clear, replace result with difference
  1150. mov (+8*0)(%rax), %rsi
  1151. mov (+8*1)(%rax), %rdi
  1152. mov (+8*2)(%rax), %rcx
  1153. mov (+8*3)(%rax), %rdx
  1154. cmovnc %r8, %rsi
  1155. cmovnc %r9, %rdi
  1156. cmovnc %r10, %rcx
  1157. cmovnc %r11, %rdx
  1158. mov %rsi, (+8*0)(%rax)
  1159. mov %rdi, (+8*1)(%rax)
  1160. mov %rcx, (+8*2)(%rax)
  1161. mov %rdx, (+8*3)(%rax)
  1162. mov (+8*4)(%rax), %rsi
  1163. mov (+8*5)(%rax), %rdi
  1164. mov (+8*6)(%rax), %rcx
  1165. mov (+8*7)(%rax), %rdx
  1166. cmovnc %r12, %rsi
  1167. cmovnc %r13, %rdi
  1168. cmovnc %r14, %rcx
  1169. cmovnc %r15, %rdx
  1170. mov %rsi, (+8*4)(%rax)
  1171. mov %rdi, (+8*5)(%rax)
  1172. mov %rcx, (+8*6)(%rax)
  1173. mov %rdx, (+8*7)(%rax)
  1174. mov (+$rsp_offset)(%rsp), %rsi
  1175. mov 0(%rsi),%r15
  1176. mov 8(%rsi),%r14
  1177. mov 16(%rsi),%r13
  1178. mov 24(%rsi),%r12
  1179. mov 32(%rsi),%rbx
  1180. mov 40(%rsi),%rbp
  1181. lea 48(%rsi),%rsp
  1182. .Lepilogue:
  1183. ret
  1184. .size mod_exp_512, . - mod_exp_512
  1185. ___
  1186. if ($win64) {
  1187. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1188. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1189. my $rec="%rcx";
  1190. my $frame="%rdx";
  1191. my $context="%r8";
  1192. my $disp="%r9";
  1193. $code.=<<___;
  1194. .extern __imp_RtlVirtualUnwind
  1195. .type mod_exp_512_se_handler,\@abi-omnipotent
  1196. .align 16
  1197. mod_exp_512_se_handler:
  1198. push %rsi
  1199. push %rdi
  1200. push %rbx
  1201. push %rbp
  1202. push %r12
  1203. push %r13
  1204. push %r14
  1205. push %r15
  1206. pushfq
  1207. sub \$64,%rsp
  1208. mov 120($context),%rax # pull context->Rax
  1209. mov 248($context),%rbx # pull context->Rip
  1210. lea .Lbody(%rip),%r10
  1211. cmp %r10,%rbx # context->Rip<prologue label
  1212. jb .Lin_prologue
  1213. mov 152($context),%rax # pull context->Rsp
  1214. lea .Lepilogue(%rip),%r10
  1215. cmp %r10,%rbx # context->Rip>=epilogue label
  1216. jae .Lin_prologue
  1217. mov $rsp_offset(%rax),%rax # pull saved Rsp
  1218. mov 32(%rax),%rbx
  1219. mov 40(%rax),%rbp
  1220. mov 24(%rax),%r12
  1221. mov 16(%rax),%r13
  1222. mov 8(%rax),%r14
  1223. mov 0(%rax),%r15
  1224. lea 48(%rax),%rax
  1225. mov %rbx,144($context) # restore context->Rbx
  1226. mov %rbp,160($context) # restore context->Rbp
  1227. mov %r12,216($context) # restore context->R12
  1228. mov %r13,224($context) # restore context->R13
  1229. mov %r14,232($context) # restore context->R14
  1230. mov %r15,240($context) # restore context->R15
  1231. .Lin_prologue:
  1232. mov 8(%rax),%rdi
  1233. mov 16(%rax),%rsi
  1234. mov %rax,152($context) # restore context->Rsp
  1235. mov %rsi,168($context) # restore context->Rsi
  1236. mov %rdi,176($context) # restore context->Rdi
  1237. mov 40($disp),%rdi # disp->ContextRecord
  1238. mov $context,%rsi # context
  1239. mov \$154,%ecx # sizeof(CONTEXT)
  1240. .long 0xa548f3fc # cld; rep movsq
  1241. mov $disp,%rsi
  1242. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1243. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1244. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1245. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1246. mov 40(%rsi),%r10 # disp->ContextRecord
  1247. lea 56(%rsi),%r11 # &disp->HandlerData
  1248. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1249. mov %r10,32(%rsp) # arg5
  1250. mov %r11,40(%rsp) # arg6
  1251. mov %r12,48(%rsp) # arg7
  1252. mov %rcx,56(%rsp) # arg8, (NULL)
  1253. call *__imp_RtlVirtualUnwind(%rip)
  1254. mov \$1,%eax # ExceptionContinueSearch
  1255. add \$64,%rsp
  1256. popfq
  1257. pop %r15
  1258. pop %r14
  1259. pop %r13
  1260. pop %r12
  1261. pop %rbp
  1262. pop %rbx
  1263. pop %rdi
  1264. pop %rsi
  1265. ret
  1266. .size mod_exp_512_se_handler,.-mod_exp_512_se_handler
  1267. .section .pdata
  1268. .align 4
  1269. .rva .LSEH_begin_mod_exp_512
  1270. .rva .LSEH_end_mod_exp_512
  1271. .rva .LSEH_info_mod_exp_512
  1272. .section .xdata
  1273. .align 8
  1274. .LSEH_info_mod_exp_512:
  1275. .byte 9,0,0,0
  1276. .rva mod_exp_512_se_handler
  1277. ___
  1278. }
  1279. sub reg_part {
  1280. my ($reg,$conv)=@_;
  1281. if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
  1282. elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
  1283. elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
  1284. elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
  1285. return $reg;
  1286. }
  1287. $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
  1288. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  1289. $code =~ s/(\(\+[^)]+\))/eval $1/gem;
  1290. print $code;
  1291. close STDOUT;