ecp_nistz256-x86.pl 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. #
  15. # ECP_NISTZ256 module for x86/SSE2.
  16. #
  17. # October 2014.
  18. #
  19. # Original ECP_NISTZ256 submission targeting x86_64 is detailed in
  20. # http://eprint.iacr.org/2013/816. In the process of adaptation
  21. # original .c module was made 32-bit savvy in order to make this
  22. # implementation possible.
  23. #
  24. # with/without -DECP_NISTZ256_ASM
  25. # Pentium +66-163%
  26. # PIII +72-172%
  27. # P4 +65-132%
  28. # Core2 +90-215%
  29. # Sandy Bridge +105-265% (contemporary i[57]-* are all close to this)
  30. # Atom +65-155%
  31. # Opteron +54-110%
  32. # Bulldozer +99-240%
  33. # VIA Nano +93-290%
  34. #
  35. # Ranges denote minimum and maximum improvement coefficients depending
  36. # on benchmark. Lower coefficients are for ECDSA sign, server-side
  37. # operation. Keep in mind that +200% means 3x improvement.
  38. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  39. push(@INC,"${dir}","${dir}../../perlasm");
  40. require "x86asm.pl";
  41. $output=pop;
  42. open STDOUT,">$output";
  43. &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  44. $sse2=0;
  45. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  46. &external_label("OPENSSL_ia32cap_P") if ($sse2);
  47. ########################################################################
  48. # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
  49. #
  50. open TABLE,"<ecp_nistz256_table.c" or
  51. open TABLE,"<${dir}../ecp_nistz256_table.c" or
  52. die "failed to open ecp_nistz256_table.c:",$!;
  53. use integer;
  54. foreach(<TABLE>) {
  55. s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
  56. }
  57. close TABLE;
  58. # See ecp_nistz256_table.c for explanation for why it's 64*16*37.
  59. # 64*16*37-1 is because $#arr returns last valid index or @arr, not
  60. # amount of elements.
  61. die "insane number of elements" if ($#arr != 64*16*37-1);
  62. &public_label("ecp_nistz256_precomputed");
  63. &align(4096);
  64. &set_label("ecp_nistz256_precomputed");
  65. ########################################################################
  66. # this conversion smashes P256_POINT_AFFINE by individual bytes with
  67. # 64 byte interval, similar to
  68. # 1111222233334444
  69. # 1234123412341234
  70. for(1..37) {
  71. @tbl = splice(@arr,0,64*16);
  72. for($i=0;$i<64;$i++) {
  73. undef @line;
  74. for($j=0;$j<64;$j++) {
  75. push @line,(@tbl[$j*16+$i/4]>>(($i%4)*8))&0xff;
  76. }
  77. &data_byte(join(',',map { sprintf "0x%02x",$_} @line));
  78. }
  79. }
  80. ########################################################################
  81. # Keep in mind that constants are stored least to most significant word
  82. &static_label("RR");
  83. &set_label("RR",64);
  84. &data_word(3,0,-1,-5,-2,-1,-3,4); # 2^512 mod P-256
  85. &static_label("ONE_mont");
  86. &set_label("ONE_mont");
  87. &data_word(1,0,0,-1,-1,-1,-2,0);
  88. &static_label("ONE");
  89. &set_label("ONE");
  90. &data_word(1,0,0,0,0,0,0,0);
  91. &asciz("ECP_NISZ256 for x86/SSE2, CRYPTOGAMS by <appro\@openssl.org>");
  92. &align(64);
  93. ########################################################################
  94. # void ecp_nistz256_mul_by_2(BN_ULONG edi[8],const BN_ULONG esi[8]);
  95. &function_begin("ecp_nistz256_mul_by_2");
  96. &mov ("esi",&wparam(1));
  97. &mov ("edi",&wparam(0));
  98. &mov ("ebp","esi");
  99. ########################################################################
  100. # common pattern for internal functions is that %edi is result pointer,
  101. # %esi and %ebp are input ones, %ebp being optional. %edi is preserved.
  102. &call ("_ecp_nistz256_add");
  103. &function_end("ecp_nistz256_mul_by_2");
  104. ########################################################################
  105. # void ecp_nistz256_mul_by_3(BN_ULONG edi[8],const BN_ULONG esi[8]);
  106. &function_begin("ecp_nistz256_mul_by_3");
  107. &mov ("esi",&wparam(1));
  108. # multiplication by 3 is performed
  109. # as 2*n+n, but we can't use output
  110. # to store 2*n, because if output
  111. # pointer equals to input, then
  112. # we'll get 2*n+2*n.
  113. &stack_push(8); # therefore we need to allocate
  114. # 256-bit intermediate buffer.
  115. &mov ("edi","esp");
  116. &mov ("ebp","esi");
  117. &call ("_ecp_nistz256_add");
  118. &lea ("esi",&DWP(0,"edi"));
  119. &mov ("ebp",&wparam(1));
  120. &mov ("edi",&wparam(0));
  121. &call ("_ecp_nistz256_add");
  122. &stack_pop(8);
  123. &function_end("ecp_nistz256_mul_by_3");
  124. ########################################################################
  125. # void ecp_nistz256_div_by_2(BN_ULONG edi[8],const BN_ULONG esi[8]);
  126. &function_begin("ecp_nistz256_div_by_2");
  127. &mov ("esi",&wparam(1));
  128. &mov ("edi",&wparam(0));
  129. &call ("_ecp_nistz256_div_by_2");
  130. &function_end("ecp_nistz256_div_by_2");
  131. &function_begin_B("_ecp_nistz256_div_by_2");
  132. # tmp = a is odd ? a+mod : a
  133. #
  134. # note that because mod has special form, i.e. consists of
  135. # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  136. # assigning least significant bit of input to one register,
  137. # %ebp, and its negative to another, %edx.
  138. &mov ("ebp",&DWP(0,"esi"));
  139. &xor ("edx","edx");
  140. &mov ("ebx",&DWP(4,"esi"));
  141. &mov ("eax","ebp");
  142. &and ("ebp",1);
  143. &mov ("ecx",&DWP(8,"esi"));
  144. &sub ("edx","ebp");
  145. &add ("eax","edx");
  146. &adc ("ebx","edx");
  147. &mov (&DWP(0,"edi"),"eax");
  148. &adc ("ecx","edx");
  149. &mov (&DWP(4,"edi"),"ebx");
  150. &mov (&DWP(8,"edi"),"ecx");
  151. &mov ("eax",&DWP(12,"esi"));
  152. &mov ("ebx",&DWP(16,"esi"));
  153. &adc ("eax",0);
  154. &mov ("ecx",&DWP(20,"esi"));
  155. &adc ("ebx",0);
  156. &mov (&DWP(12,"edi"),"eax");
  157. &adc ("ecx",0);
  158. &mov (&DWP(16,"edi"),"ebx");
  159. &mov (&DWP(20,"edi"),"ecx");
  160. &mov ("eax",&DWP(24,"esi"));
  161. &mov ("ebx",&DWP(28,"esi"));
  162. &adc ("eax","ebp");
  163. &adc ("ebx","edx");
  164. &mov (&DWP(24,"edi"),"eax");
  165. &sbb ("esi","esi"); # broadcast carry bit
  166. &mov (&DWP(28,"edi"),"ebx");
  167. # ret = tmp >> 1
  168. &mov ("eax",&DWP(0,"edi"));
  169. &mov ("ebx",&DWP(4,"edi"));
  170. &mov ("ecx",&DWP(8,"edi"));
  171. &mov ("edx",&DWP(12,"edi"));
  172. &shr ("eax",1);
  173. &mov ("ebp","ebx");
  174. &shl ("ebx",31);
  175. &or ("eax","ebx");
  176. &shr ("ebp",1);
  177. &mov ("ebx","ecx");
  178. &shl ("ecx",31);
  179. &mov (&DWP(0,"edi"),"eax");
  180. &or ("ebp","ecx");
  181. &mov ("eax",&DWP(16,"edi"));
  182. &shr ("ebx",1);
  183. &mov ("ecx","edx");
  184. &shl ("edx",31);
  185. &mov (&DWP(4,"edi"),"ebp");
  186. &or ("ebx","edx");
  187. &mov ("ebp",&DWP(20,"edi"));
  188. &shr ("ecx",1);
  189. &mov ("edx","eax");
  190. &shl ("eax",31);
  191. &mov (&DWP(8,"edi"),"ebx");
  192. &or ("ecx","eax");
  193. &mov ("ebx",&DWP(24,"edi"));
  194. &shr ("edx",1);
  195. &mov ("eax","ebp");
  196. &shl ("ebp",31);
  197. &mov (&DWP(12,"edi"),"ecx");
  198. &or ("edx","ebp");
  199. &mov ("ecx",&DWP(28,"edi"));
  200. &shr ("eax",1);
  201. &mov ("ebp","ebx");
  202. &shl ("ebx",31);
  203. &mov (&DWP(16,"edi"),"edx");
  204. &or ("eax","ebx");
  205. &shr ("ebp",1);
  206. &mov ("ebx","ecx");
  207. &shl ("ecx",31);
  208. &mov (&DWP(20,"edi"),"eax");
  209. &or ("ebp","ecx");
  210. &shr ("ebx",1);
  211. &shl ("esi",31);
  212. &mov (&DWP(24,"edi"),"ebp");
  213. &or ("ebx","esi"); # handle top-most carry bit
  214. &mov (&DWP(28,"edi"),"ebx");
  215. &ret ();
  216. &function_end_B("_ecp_nistz256_div_by_2");
  217. ########################################################################
  218. # void ecp_nistz256_add(BN_ULONG edi[8],const BN_ULONG esi[8],
  219. # const BN_ULONG ebp[8]);
  220. &function_begin("ecp_nistz256_add");
  221. &mov ("esi",&wparam(1));
  222. &mov ("ebp",&wparam(2));
  223. &mov ("edi",&wparam(0));
  224. &call ("_ecp_nistz256_add");
  225. &function_end("ecp_nistz256_add");
  226. &function_begin_B("_ecp_nistz256_add");
  227. &mov ("eax",&DWP(0,"esi"));
  228. &mov ("ebx",&DWP(4,"esi"));
  229. &mov ("ecx",&DWP(8,"esi"));
  230. &add ("eax",&DWP(0,"ebp"));
  231. &mov ("edx",&DWP(12,"esi"));
  232. &adc ("ebx",&DWP(4,"ebp"));
  233. &mov (&DWP(0,"edi"),"eax");
  234. &adc ("ecx",&DWP(8,"ebp"));
  235. &mov (&DWP(4,"edi"),"ebx");
  236. &adc ("edx",&DWP(12,"ebp"));
  237. &mov (&DWP(8,"edi"),"ecx");
  238. &mov (&DWP(12,"edi"),"edx");
  239. &mov ("eax",&DWP(16,"esi"));
  240. &mov ("ebx",&DWP(20,"esi"));
  241. &mov ("ecx",&DWP(24,"esi"));
  242. &adc ("eax",&DWP(16,"ebp"));
  243. &mov ("edx",&DWP(28,"esi"));
  244. &adc ("ebx",&DWP(20,"ebp"));
  245. &mov (&DWP(16,"edi"),"eax");
  246. &adc ("ecx",&DWP(24,"ebp"));
  247. &mov (&DWP(20,"edi"),"ebx");
  248. &mov ("esi",0);
  249. &adc ("edx",&DWP(28,"ebp"));
  250. &mov (&DWP(24,"edi"),"ecx");
  251. &adc ("esi",0);
  252. &mov (&DWP(28,"edi"),"edx");
  253. # if a+b >= modulus, subtract modulus.
  254. #
  255. # But since comparison implies subtraction, we subtract modulus
  256. # to see if it borrows, and then subtract it for real if
  257. # subtraction didn't borrow.
  258. &mov ("eax",&DWP(0,"edi"));
  259. &mov ("ebx",&DWP(4,"edi"));
  260. &mov ("ecx",&DWP(8,"edi"));
  261. &sub ("eax",-1);
  262. &mov ("edx",&DWP(12,"edi"));
  263. &sbb ("ebx",-1);
  264. &mov ("eax",&DWP(16,"edi"));
  265. &sbb ("ecx",-1);
  266. &mov ("ebx",&DWP(20,"edi"));
  267. &sbb ("edx",0);
  268. &mov ("ecx",&DWP(24,"edi"));
  269. &sbb ("eax",0);
  270. &mov ("edx",&DWP(28,"edi"));
  271. &sbb ("ebx",0);
  272. &sbb ("ecx",1);
  273. &sbb ("edx",-1);
  274. &sbb ("esi",0);
  275. # Note that because mod has special form, i.e. consists of
  276. # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  277. # by using borrow.
  278. &not ("esi");
  279. &mov ("eax",&DWP(0,"edi"));
  280. &mov ("ebp","esi");
  281. &mov ("ebx",&DWP(4,"edi"));
  282. &shr ("ebp",31);
  283. &mov ("ecx",&DWP(8,"edi"));
  284. &sub ("eax","esi");
  285. &mov ("edx",&DWP(12,"edi"));
  286. &sbb ("ebx","esi");
  287. &mov (&DWP(0,"edi"),"eax");
  288. &sbb ("ecx","esi");
  289. &mov (&DWP(4,"edi"),"ebx");
  290. &sbb ("edx",0);
  291. &mov (&DWP(8,"edi"),"ecx");
  292. &mov (&DWP(12,"edi"),"edx");
  293. &mov ("eax",&DWP(16,"edi"));
  294. &mov ("ebx",&DWP(20,"edi"));
  295. &mov ("ecx",&DWP(24,"edi"));
  296. &sbb ("eax",0);
  297. &mov ("edx",&DWP(28,"edi"));
  298. &sbb ("ebx",0);
  299. &mov (&DWP(16,"edi"),"eax");
  300. &sbb ("ecx","ebp");
  301. &mov (&DWP(20,"edi"),"ebx");
  302. &sbb ("edx","esi");
  303. &mov (&DWP(24,"edi"),"ecx");
  304. &mov (&DWP(28,"edi"),"edx");
  305. &ret ();
  306. &function_end_B("_ecp_nistz256_add");
  307. ########################################################################
  308. # void ecp_nistz256_sub(BN_ULONG edi[8],const BN_ULONG esi[8],
  309. # const BN_ULONG ebp[8]);
  310. &function_begin("ecp_nistz256_sub");
  311. &mov ("esi",&wparam(1));
  312. &mov ("ebp",&wparam(2));
  313. &mov ("edi",&wparam(0));
  314. &call ("_ecp_nistz256_sub");
  315. &function_end("ecp_nistz256_sub");
  316. &function_begin_B("_ecp_nistz256_sub");
  317. &mov ("eax",&DWP(0,"esi"));
  318. &mov ("ebx",&DWP(4,"esi"));
  319. &mov ("ecx",&DWP(8,"esi"));
  320. &sub ("eax",&DWP(0,"ebp"));
  321. &mov ("edx",&DWP(12,"esi"));
  322. &sbb ("ebx",&DWP(4,"ebp"));
  323. &mov (&DWP(0,"edi"),"eax");
  324. &sbb ("ecx",&DWP(8,"ebp"));
  325. &mov (&DWP(4,"edi"),"ebx");
  326. &sbb ("edx",&DWP(12,"ebp"));
  327. &mov (&DWP(8,"edi"),"ecx");
  328. &mov (&DWP(12,"edi"),"edx");
  329. &mov ("eax",&DWP(16,"esi"));
  330. &mov ("ebx",&DWP(20,"esi"));
  331. &mov ("ecx",&DWP(24,"esi"));
  332. &sbb ("eax",&DWP(16,"ebp"));
  333. &mov ("edx",&DWP(28,"esi"));
  334. &sbb ("ebx",&DWP(20,"ebp"));
  335. &sbb ("ecx",&DWP(24,"ebp"));
  336. &mov (&DWP(16,"edi"),"eax");
  337. &sbb ("edx",&DWP(28,"ebp"));
  338. &mov (&DWP(20,"edi"),"ebx");
  339. &sbb ("esi","esi"); # broadcast borrow bit
  340. &mov (&DWP(24,"edi"),"ecx");
  341. &mov (&DWP(28,"edi"),"edx");
  342. # if a-b borrows, add modulus.
  343. #
  344. # Note that because mod has special form, i.e. consists of
  345. # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  346. # assigning borrow bit to one register, %ebp, and its negative
  347. # to another, %esi. But we started by calculating %esi...
  348. &mov ("eax",&DWP(0,"edi"));
  349. &mov ("ebp","esi");
  350. &mov ("ebx",&DWP(4,"edi"));
  351. &shr ("ebp",31);
  352. &mov ("ecx",&DWP(8,"edi"));
  353. &add ("eax","esi");
  354. &mov ("edx",&DWP(12,"edi"));
  355. &adc ("ebx","esi");
  356. &mov (&DWP(0,"edi"),"eax");
  357. &adc ("ecx","esi");
  358. &mov (&DWP(4,"edi"),"ebx");
  359. &adc ("edx",0);
  360. &mov (&DWP(8,"edi"),"ecx");
  361. &mov (&DWP(12,"edi"),"edx");
  362. &mov ("eax",&DWP(16,"edi"));
  363. &mov ("ebx",&DWP(20,"edi"));
  364. &mov ("ecx",&DWP(24,"edi"));
  365. &adc ("eax",0);
  366. &mov ("edx",&DWP(28,"edi"));
  367. &adc ("ebx",0);
  368. &mov (&DWP(16,"edi"),"eax");
  369. &adc ("ecx","ebp");
  370. &mov (&DWP(20,"edi"),"ebx");
  371. &adc ("edx","esi");
  372. &mov (&DWP(24,"edi"),"ecx");
  373. &mov (&DWP(28,"edi"),"edx");
  374. &ret ();
  375. &function_end_B("_ecp_nistz256_sub");
  376. ########################################################################
  377. # void ecp_nistz256_neg(BN_ULONG edi[8],const BN_ULONG esi[8]);
  378. &function_begin("ecp_nistz256_neg");
  379. &mov ("ebp",&wparam(1));
  380. &mov ("edi",&wparam(0));
  381. &xor ("eax","eax");
  382. &stack_push(8);
  383. &mov (&DWP(0,"esp"),"eax");
  384. &mov ("esi","esp");
  385. &mov (&DWP(4,"esp"),"eax");
  386. &mov (&DWP(8,"esp"),"eax");
  387. &mov (&DWP(12,"esp"),"eax");
  388. &mov (&DWP(16,"esp"),"eax");
  389. &mov (&DWP(20,"esp"),"eax");
  390. &mov (&DWP(24,"esp"),"eax");
  391. &mov (&DWP(28,"esp"),"eax");
  392. &call ("_ecp_nistz256_sub");
  393. &stack_pop(8);
  394. &function_end("ecp_nistz256_neg");
  395. &function_begin_B("_picup_eax");
  396. &mov ("eax",&DWP(0,"esp"));
  397. &ret ();
  398. &function_end_B("_picup_eax");
  399. ########################################################################
  400. # void ecp_nistz256_to_mont(BN_ULONG edi[8],const BN_ULONG esi[8]);
  401. &function_begin("ecp_nistz256_to_mont");
  402. &mov ("esi",&wparam(1));
  403. &call ("_picup_eax");
  404. &set_label("pic");
  405. &lea ("ebp",&DWP(&label("RR")."-".&label("pic"),"eax"));
  406. if ($sse2) {
  407. &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
  408. &mov ("eax",&DWP(0,"eax")); }
  409. &mov ("edi",&wparam(0));
  410. &call ("_ecp_nistz256_mul_mont");
  411. &function_end("ecp_nistz256_to_mont");
  412. ########################################################################
  413. # void ecp_nistz256_from_mont(BN_ULONG edi[8],const BN_ULONG esi[8]);
  414. &function_begin("ecp_nistz256_from_mont");
  415. &mov ("esi",&wparam(1));
  416. &call ("_picup_eax");
  417. &set_label("pic");
  418. &lea ("ebp",&DWP(&label("ONE")."-".&label("pic"),"eax"));
  419. if ($sse2) {
  420. &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
  421. &mov ("eax",&DWP(0,"eax")); }
  422. &mov ("edi",&wparam(0));
  423. &call ("_ecp_nistz256_mul_mont");
  424. &function_end("ecp_nistz256_from_mont");
  425. ########################################################################
  426. # void ecp_nistz256_mul_mont(BN_ULONG edi[8],const BN_ULONG esi[8],
  427. # const BN_ULONG ebp[8]);
  428. &function_begin("ecp_nistz256_mul_mont");
  429. &mov ("esi",&wparam(1));
  430. &mov ("ebp",&wparam(2));
  431. if ($sse2) {
  432. &call ("_picup_eax");
  433. &set_label("pic");
  434. &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
  435. &mov ("eax",&DWP(0,"eax")); }
  436. &mov ("edi",&wparam(0));
  437. &call ("_ecp_nistz256_mul_mont");
  438. &function_end("ecp_nistz256_mul_mont");
  439. ########################################################################
  440. # void ecp_nistz256_sqr_mont(BN_ULONG edi[8],const BN_ULONG esi[8]);
  441. &function_begin("ecp_nistz256_sqr_mont");
  442. &mov ("esi",&wparam(1));
  443. if ($sse2) {
  444. &call ("_picup_eax");
  445. &set_label("pic");
  446. &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
  447. &mov ("eax",&DWP(0,"eax")); }
  448. &mov ("edi",&wparam(0));
  449. &mov ("ebp","esi");
  450. &call ("_ecp_nistz256_mul_mont");
  451. &function_end("ecp_nistz256_sqr_mont");
  452. &function_begin_B("_ecp_nistz256_mul_mont");
  453. if ($sse2) {
  454. &and ("eax",1<<24|1<<26);
  455. &cmp ("eax",1<<24|1<<26); # see if XMM+SSE2 is on
  456. &jne (&label("mul_mont_ialu"));
  457. ########################################
  458. # SSE2 code path featuring 32x16-bit
  459. # multiplications is ~2x faster than
  460. # IALU counterpart (except on Atom)...
  461. ########################################
  462. # stack layout:
  463. # +------------------------------------+< %esp
  464. # | 7 16-byte temporary XMM words, |
  465. # | "sliding" toward lower address |
  466. # . .
  467. # +------------------------------------+
  468. # | unused XMM word |
  469. # +------------------------------------+< +128,%ebx
  470. # | 8 16-byte XMM words holding copies |
  471. # | of a[i]<<64|a[i] |
  472. # . .
  473. # . .
  474. # +------------------------------------+< +256
  475. &mov ("edx","esp");
  476. &sub ("esp",0x100);
  477. &movd ("xmm7",&DWP(0,"ebp")); # b[0] -> 0000.00xy
  478. &lea ("ebp",&DWP(4,"ebp"));
  479. &pcmpeqd("xmm6","xmm6");
  480. &psrlq ("xmm6",48); # compose 0xffff<<64|0xffff
  481. &pshuflw("xmm7","xmm7",0b11011100); # 0000.00xy -> 0000.0x0y
  482. &and ("esp",-64);
  483. &pshufd ("xmm7","xmm7",0b11011100); # 0000.0x0y -> 000x.000y
  484. &lea ("ebx",&DWP(0x80,"esp"));
  485. &movd ("xmm0",&DWP(4*0,"esi")); # a[0] -> 0000.00xy
  486. &pshufd ("xmm0","xmm0",0b11001100); # 0000.00xy -> 00xy.00xy
  487. &movd ("xmm1",&DWP(4*1,"esi")); # a[1] -> ...
  488. &movdqa (&QWP(0x00,"ebx"),"xmm0"); # offload converted a[0]
  489. &pmuludq("xmm0","xmm7"); # a[0]*b[0]
  490. &movd ("xmm2",&DWP(4*2,"esi"));
  491. &pshufd ("xmm1","xmm1",0b11001100);
  492. &movdqa (&QWP(0x10,"ebx"),"xmm1");
  493. &pmuludq("xmm1","xmm7"); # a[1]*b[0]
  494. &movq ("xmm4","xmm0"); # clear upper 64 bits
  495. &pslldq("xmm4",6);
  496. &paddq ("xmm4","xmm0");
  497. &movdqa("xmm5","xmm4");
  498. &psrldq("xmm4",10); # upper 32 bits of a[0]*b[0]
  499. &pand ("xmm5","xmm6"); # lower 32 bits of a[0]*b[0]
  500. # Upper half of a[0]*b[i] is carried into next multiplication
  501. # iteration, while lower one "participates" in actual reduction.
  502. # Normally latter is done by accumulating result of multiplication
  503. # of modulus by "magic" digit, but thanks to special form of modulus
  504. # and "magic" digit it can be performed only with additions and
  505. # subtractions (see note in IALU section below). Note that we are
  506. # not bothered with carry bits, they are accumulated in "flatten"
  507. # phase after all multiplications and reductions.
  508. &movd ("xmm3",&DWP(4*3,"esi"));
  509. &pshufd ("xmm2","xmm2",0b11001100);
  510. &movdqa (&QWP(0x20,"ebx"),"xmm2");
  511. &pmuludq("xmm2","xmm7"); # a[2]*b[0]
  512. &paddq ("xmm1","xmm4"); # a[1]*b[0]+hw(a[0]*b[0]), carry
  513. &movdqa (&QWP(0x00,"esp"),"xmm1"); # t[0]
  514. &movd ("xmm0",&DWP(4*4,"esi"));
  515. &pshufd ("xmm3","xmm3",0b11001100);
  516. &movdqa (&QWP(0x30,"ebx"),"xmm3");
  517. &pmuludq("xmm3","xmm7"); # a[3]*b[0]
  518. &movdqa (&QWP(0x10,"esp"),"xmm2");
  519. &movd ("xmm1",&DWP(4*5,"esi"));
  520. &pshufd ("xmm0","xmm0",0b11001100);
  521. &movdqa (&QWP(0x40,"ebx"),"xmm0");
  522. &pmuludq("xmm0","xmm7"); # a[4]*b[0]
  523. &paddq ("xmm3","xmm5"); # a[3]*b[0]+lw(a[0]*b[0]), reduction step
  524. &movdqa (&QWP(0x20,"esp"),"xmm3");
  525. &movd ("xmm2",&DWP(4*6,"esi"));
  526. &pshufd ("xmm1","xmm1",0b11001100);
  527. &movdqa (&QWP(0x50,"ebx"),"xmm1");
  528. &pmuludq("xmm1","xmm7"); # a[5]*b[0]
  529. &movdqa (&QWP(0x30,"esp"),"xmm0");
  530. &pshufd("xmm4","xmm5",0b10110001); # xmm4 = xmm5<<32, reduction step
  531. &movd ("xmm3",&DWP(4*7,"esi"));
  532. &pshufd ("xmm2","xmm2",0b11001100);
  533. &movdqa (&QWP(0x60,"ebx"),"xmm2");
  534. &pmuludq("xmm2","xmm7"); # a[6]*b[0]
  535. &movdqa (&QWP(0x40,"esp"),"xmm1");
  536. &psubq ("xmm4","xmm5"); # xmm4 = xmm5*0xffffffff, reduction step
  537. &movd ("xmm0",&DWP(0,"ebp")); # b[1] -> 0000.00xy
  538. &pshufd ("xmm3","xmm3",0b11001100);
  539. &movdqa (&QWP(0x70,"ebx"),"xmm3");
  540. &pmuludq("xmm3","xmm7"); # a[7]*b[0]
  541. &pshuflw("xmm7","xmm0",0b11011100); # 0000.00xy -> 0000.0x0y
  542. &movdqa ("xmm0",&QWP(0x00,"ebx")); # pre-load converted a[0]
  543. &pshufd ("xmm7","xmm7",0b11011100); # 0000.0x0y -> 000x.000y
  544. &mov ("ecx",6);
  545. &lea ("ebp",&DWP(4,"ebp"));
  546. &jmp (&label("madd_sse2"));
  547. &set_label("madd_sse2",16);
  548. &paddq ("xmm2","xmm5"); # a[6]*b[i-1]+lw(a[0]*b[i-1]), reduction step [modulo-scheduled]
  549. &paddq ("xmm3","xmm4"); # a[7]*b[i-1]+lw(a[0]*b[i-1])*0xffffffff, reduction step [modulo-scheduled]
  550. &movdqa ("xmm1",&QWP(0x10,"ebx"));
  551. &pmuludq("xmm0","xmm7"); # a[0]*b[i]
  552. &movdqa(&QWP(0x50,"esp"),"xmm2");
  553. &movdqa ("xmm2",&QWP(0x20,"ebx"));
  554. &pmuludq("xmm1","xmm7"); # a[1]*b[i]
  555. &movdqa(&QWP(0x60,"esp"),"xmm3");
  556. &paddq ("xmm0",&QWP(0x00,"esp"));
  557. &movdqa ("xmm3",&QWP(0x30,"ebx"));
  558. &pmuludq("xmm2","xmm7"); # a[2]*b[i]
  559. &movq ("xmm4","xmm0"); # clear upper 64 bits
  560. &pslldq("xmm4",6);
  561. &paddq ("xmm1",&QWP(0x10,"esp"));
  562. &paddq ("xmm4","xmm0");
  563. &movdqa("xmm5","xmm4");
  564. &psrldq("xmm4",10); # upper 33 bits of a[0]*b[i]+t[0]
  565. &movdqa ("xmm0",&QWP(0x40,"ebx"));
  566. &pmuludq("xmm3","xmm7"); # a[3]*b[i]
  567. &paddq ("xmm1","xmm4"); # a[1]*b[i]+hw(a[0]*b[i]), carry
  568. &paddq ("xmm2",&QWP(0x20,"esp"));
  569. &movdqa (&QWP(0x00,"esp"),"xmm1");
  570. &movdqa ("xmm1",&QWP(0x50,"ebx"));
  571. &pmuludq("xmm0","xmm7"); # a[4]*b[i]
  572. &paddq ("xmm3",&QWP(0x30,"esp"));
  573. &movdqa (&QWP(0x10,"esp"),"xmm2");
  574. &pand ("xmm5","xmm6"); # lower 32 bits of a[0]*b[i]
  575. &movdqa ("xmm2",&QWP(0x60,"ebx"));
  576. &pmuludq("xmm1","xmm7"); # a[5]*b[i]
  577. &paddq ("xmm3","xmm5"); # a[3]*b[i]+lw(a[0]*b[i]), reduction step
  578. &paddq ("xmm0",&QWP(0x40,"esp"));
  579. &movdqa (&QWP(0x20,"esp"),"xmm3");
  580. &pshufd("xmm4","xmm5",0b10110001); # xmm4 = xmm5<<32, reduction step
  581. &movdqa ("xmm3","xmm7");
  582. &pmuludq("xmm2","xmm7"); # a[6]*b[i]
  583. &movd ("xmm7",&DWP(0,"ebp")); # b[i++] -> 0000.00xy
  584. &lea ("ebp",&DWP(4,"ebp"));
  585. &paddq ("xmm1",&QWP(0x50,"esp"));
  586. &psubq ("xmm4","xmm5"); # xmm4 = xmm5*0xffffffff, reduction step
  587. &movdqa (&QWP(0x30,"esp"),"xmm0");
  588. &pshuflw("xmm7","xmm7",0b11011100); # 0000.00xy -> 0000.0x0y
  589. &pmuludq("xmm3",&QWP(0x70,"ebx")); # a[7]*b[i]
  590. &pshufd("xmm7","xmm7",0b11011100); # 0000.0x0y -> 000x.000y
  591. &movdqa("xmm0",&QWP(0x00,"ebx")); # pre-load converted a[0]
  592. &movdqa (&QWP(0x40,"esp"),"xmm1");
  593. &paddq ("xmm2",&QWP(0x60,"esp"));
  594. &dec ("ecx");
  595. &jnz (&label("madd_sse2"));
  596. &paddq ("xmm2","xmm5"); # a[6]*b[6]+lw(a[0]*b[6]), reduction step [modulo-scheduled]
  597. &paddq ("xmm3","xmm4"); # a[7]*b[6]+lw(a[0]*b[6])*0xffffffff, reduction step [modulo-scheduled]
  598. &movdqa ("xmm1",&QWP(0x10,"ebx"));
  599. &pmuludq("xmm0","xmm7"); # a[0]*b[7]
  600. &movdqa(&QWP(0x50,"esp"),"xmm2");
  601. &movdqa ("xmm2",&QWP(0x20,"ebx"));
  602. &pmuludq("xmm1","xmm7"); # a[1]*b[7]
  603. &movdqa(&QWP(0x60,"esp"),"xmm3");
  604. &paddq ("xmm0",&QWP(0x00,"esp"));
  605. &movdqa ("xmm3",&QWP(0x30,"ebx"));
  606. &pmuludq("xmm2","xmm7"); # a[2]*b[7]
  607. &movq ("xmm4","xmm0"); # clear upper 64 bits
  608. &pslldq("xmm4",6);
  609. &paddq ("xmm1",&QWP(0x10,"esp"));
  610. &paddq ("xmm4","xmm0");
  611. &movdqa("xmm5","xmm4");
  612. &psrldq("xmm4",10); # upper 33 bits of a[0]*b[i]+t[0]
  613. &movdqa ("xmm0",&QWP(0x40,"ebx"));
  614. &pmuludq("xmm3","xmm7"); # a[3]*b[7]
  615. &paddq ("xmm1","xmm4"); # a[1]*b[7]+hw(a[0]*b[7]), carry
  616. &paddq ("xmm2",&QWP(0x20,"esp"));
  617. &movdqa (&QWP(0x00,"esp"),"xmm1");
  618. &movdqa ("xmm1",&QWP(0x50,"ebx"));
  619. &pmuludq("xmm0","xmm7"); # a[4]*b[7]
  620. &paddq ("xmm3",&QWP(0x30,"esp"));
  621. &movdqa (&QWP(0x10,"esp"),"xmm2");
  622. &pand ("xmm5","xmm6"); # lower 32 bits of a[0]*b[i]
  623. &movdqa ("xmm2",&QWP(0x60,"ebx"));
  624. &pmuludq("xmm1","xmm7"); # a[5]*b[7]
  625. &paddq ("xmm3","xmm5"); # reduction step
  626. &paddq ("xmm0",&QWP(0x40,"esp"));
  627. &movdqa (&QWP(0x20,"esp"),"xmm3");
  628. &pshufd("xmm4","xmm5",0b10110001); # xmm4 = xmm5<<32, reduction step
  629. &movdqa ("xmm3",&QWP(0x70,"ebx"));
  630. &pmuludq("xmm2","xmm7"); # a[6]*b[7]
  631. &paddq ("xmm1",&QWP(0x50,"esp"));
  632. &psubq ("xmm4","xmm5"); # xmm4 = xmm5*0xffffffff, reduction step
  633. &movdqa (&QWP(0x30,"esp"),"xmm0");
  634. &pmuludq("xmm3","xmm7"); # a[7]*b[7]
  635. &pcmpeqd("xmm7","xmm7");
  636. &movdqa ("xmm0",&QWP(0x00,"esp"));
  637. &pslldq ("xmm7",8);
  638. &movdqa (&QWP(0x40,"esp"),"xmm1");
  639. &paddq ("xmm2",&QWP(0x60,"esp"));
  640. &paddq ("xmm2","xmm5"); # a[6]*b[7]+lw(a[0]*b[7]), reduction step
  641. &paddq ("xmm3","xmm4"); # a[6]*b[7]+lw(a[0]*b[7])*0xffffffff, reduction step
  642. &movdqa(&QWP(0x50,"esp"),"xmm2");
  643. &movdqa(&QWP(0x60,"esp"),"xmm3");
  644. &movdqa ("xmm1",&QWP(0x10,"esp"));
  645. &movdqa ("xmm2",&QWP(0x20,"esp"));
  646. &movdqa ("xmm3",&QWP(0x30,"esp"));
  647. &movq ("xmm4","xmm0"); # "flatten"
  648. &pand ("xmm0","xmm7");
  649. &xor ("ebp","ebp");
  650. &pslldq ("xmm4",6);
  651. &movq ("xmm5","xmm1");
  652. &paddq ("xmm0","xmm4");
  653. &pand ("xmm1","xmm7");
  654. &psrldq ("xmm0",6);
  655. &movd ("eax","xmm0");
  656. &psrldq ("xmm0",4);
  657. &paddq ("xmm5","xmm0");
  658. &movdqa ("xmm0",&QWP(0x40,"esp"));
  659. &sub ("eax",-1); # start subtracting modulus,
  660. # this is used to determine
  661. # if result is larger/smaller
  662. # than modulus (see below)
  663. &pslldq ("xmm5",6);
  664. &movq ("xmm4","xmm2");
  665. &paddq ("xmm1","xmm5");
  666. &pand ("xmm2","xmm7");
  667. &psrldq ("xmm1",6);
  668. &mov (&DWP(4*0,"edi"),"eax");
  669. &movd ("eax","xmm1");
  670. &psrldq ("xmm1",4);
  671. &paddq ("xmm4","xmm1");
  672. &movdqa ("xmm1",&QWP(0x50,"esp"));
  673. &sbb ("eax",-1);
  674. &pslldq ("xmm4",6);
  675. &movq ("xmm5","xmm3");
  676. &paddq ("xmm2","xmm4");
  677. &pand ("xmm3","xmm7");
  678. &psrldq ("xmm2",6);
  679. &mov (&DWP(4*1,"edi"),"eax");
  680. &movd ("eax","xmm2");
  681. &psrldq ("xmm2",4);
  682. &paddq ("xmm5","xmm2");
  683. &movdqa ("xmm2",&QWP(0x60,"esp"));
  684. &sbb ("eax",-1);
  685. &pslldq ("xmm5",6);
  686. &movq ("xmm4","xmm0");
  687. &paddq ("xmm3","xmm5");
  688. &pand ("xmm0","xmm7");
  689. &psrldq ("xmm3",6);
  690. &mov (&DWP(4*2,"edi"),"eax");
  691. &movd ("eax","xmm3");
  692. &psrldq ("xmm3",4);
  693. &paddq ("xmm4","xmm3");
  694. &sbb ("eax",0);
  695. &pslldq ("xmm4",6);
  696. &movq ("xmm5","xmm1");
  697. &paddq ("xmm0","xmm4");
  698. &pand ("xmm1","xmm7");
  699. &psrldq ("xmm0",6);
  700. &mov (&DWP(4*3,"edi"),"eax");
  701. &movd ("eax","xmm0");
  702. &psrldq ("xmm0",4);
  703. &paddq ("xmm5","xmm0");
  704. &sbb ("eax",0);
  705. &pslldq ("xmm5",6);
  706. &movq ("xmm4","xmm2");
  707. &paddq ("xmm1","xmm5");
  708. &pand ("xmm2","xmm7");
  709. &psrldq ("xmm1",6);
  710. &movd ("ebx","xmm1");
  711. &psrldq ("xmm1",4);
  712. &mov ("esp","edx");
  713. &paddq ("xmm4","xmm1");
  714. &pslldq ("xmm4",6);
  715. &paddq ("xmm2","xmm4");
  716. &psrldq ("xmm2",6);
  717. &movd ("ecx","xmm2");
  718. &psrldq ("xmm2",4);
  719. &sbb ("ebx",0);
  720. &movd ("edx","xmm2");
  721. &pextrw ("esi","xmm2",2); # top-most overflow bit
  722. &sbb ("ecx",1);
  723. &sbb ("edx",-1);
  724. &sbb ("esi",0); # borrow from subtraction
  725. # Final step is "if result > mod, subtract mod", and at this point
  726. # we have result - mod written to output buffer, as well as borrow
  727. # bit from this subtraction, and if borrow bit is set, we add
  728. # modulus back.
  729. #
  730. # Note that because mod has special form, i.e. consists of
  731. # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  732. # assigning borrow bit to one register, %ebp, and its negative
  733. # to another, %esi. But we started by calculating %esi...
  734. &sub ("ebp","esi");
  735. &add (&DWP(4*0,"edi"),"esi"); # add modulus or zero
  736. &adc (&DWP(4*1,"edi"),"esi");
  737. &adc (&DWP(4*2,"edi"),"esi");
  738. &adc (&DWP(4*3,"edi"),0);
  739. &adc ("eax",0);
  740. &adc ("ebx",0);
  741. &mov (&DWP(4*4,"edi"),"eax");
  742. &adc ("ecx","ebp");
  743. &mov (&DWP(4*5,"edi"),"ebx");
  744. &adc ("edx","esi");
  745. &mov (&DWP(4*6,"edi"),"ecx");
  746. &mov (&DWP(4*7,"edi"),"edx");
  747. &ret ();
  748. &set_label("mul_mont_ialu",16); }
  749. ########################################
  750. # IALU code path suitable for all CPUs.
  751. ########################################
  752. # stack layout:
  753. # +------------------------------------+< %esp
  754. # | 8 32-bit temporary words, accessed |
  755. # | as circular buffer |
  756. # . .
  757. # . .
  758. # +------------------------------------+< +32
  759. # | offloaded destination pointer |
  760. # +------------------------------------+
  761. # | unused |
  762. # +------------------------------------+< +40
  763. &sub ("esp",10*4);
  764. &mov ("eax",&DWP(0*4,"esi")); # a[0]
  765. &mov ("ebx",&DWP(0*4,"ebp")); # b[0]
  766. &mov (&DWP(8*4,"esp"),"edi"); # off-load dst ptr
  767. &mul ("ebx"); # a[0]*b[0]
  768. &mov (&DWP(0*4,"esp"),"eax"); # t[0]
  769. &mov ("eax",&DWP(1*4,"esi"));
  770. &mov ("ecx","edx")
  771. &mul ("ebx"); # a[1]*b[0]
  772. &add ("ecx","eax");
  773. &mov ("eax",&DWP(2*4,"esi"));
  774. &adc ("edx",0);
  775. &mov (&DWP(1*4,"esp"),"ecx"); # t[1]
  776. &mov ("ecx","edx");
  777. &mul ("ebx"); # a[2]*b[0]
  778. &add ("ecx","eax");
  779. &mov ("eax",&DWP(3*4,"esi"));
  780. &adc ("edx",0);
  781. &mov (&DWP(2*4,"esp"),"ecx"); # t[2]
  782. &mov ("ecx","edx");
  783. &mul ("ebx"); # a[3]*b[0]
  784. &add ("ecx","eax");
  785. &mov ("eax",&DWP(4*4,"esi"));
  786. &adc ("edx",0);
  787. &mov (&DWP(3*4,"esp"),"ecx"); # t[3]
  788. &mov ("ecx","edx");
  789. &mul ("ebx"); # a[4]*b[0]
  790. &add ("ecx","eax");
  791. &mov ("eax",&DWP(5*4,"esi"));
  792. &adc ("edx",0);
  793. &mov (&DWP(4*4,"esp"),"ecx"); # t[4]
  794. &mov ("ecx","edx");
  795. &mul ("ebx"); # a[5]*b[0]
  796. &add ("ecx","eax");
  797. &mov ("eax",&DWP(6*4,"esi"));
  798. &adc ("edx",0);
  799. &mov (&DWP(5*4,"esp"),"ecx"); # t[5]
  800. &mov ("ecx","edx");
  801. &mul ("ebx"); # a[6]*b[0]
  802. &add ("ecx","eax");
  803. &mov ("eax",&DWP(7*4,"esi"));
  804. &adc ("edx",0);
  805. &mov (&DWP(6*4,"esp"),"ecx"); # t[6]
  806. &mov ("ecx","edx");
  807. &xor ("edi","edi"); # initial top-most carry
  808. &mul ("ebx"); # a[7]*b[0]
  809. &add ("ecx","eax"); # t[7]
  810. &mov ("eax",&DWP(0*4,"esp")); # t[0]
  811. &adc ("edx",0); # t[8]
  812. for ($i=0;$i<7;$i++) {
  813. my $j=$i+1;
  814. # Reduction iteration is normally performed by accumulating
  815. # result of multiplication of modulus by "magic" digit [and
  816. # omitting least significant word, which is guaranteed to
  817. # be 0], but thanks to special form of modulus and "magic"
  818. # digit being equal to least significant word, it can be
  819. # performed with additions and subtractions alone. Indeed:
  820. #
  821. # ffff.0001.0000.0000.0000.ffff.ffff.ffff
  822. # * abcd
  823. # + xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
  824. #
  825. # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
  826. # rewrite above as:
  827. #
  828. # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
  829. # + abcd.0000.abcd.0000.0000.abcd.0000.0000.0000
  830. # - abcd.0000.0000.0000.0000.0000.0000.abcd
  831. #
  832. # or marking redundant operations:
  833. #
  834. # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.----
  835. # + abcd.0000.abcd.0000.0000.abcd.----.----.----
  836. # - abcd.----.----.----.----.----.----.----
  837. &add (&DWP((($i+3)%8)*4,"esp"),"eax"); # t[3]+=t[0]
  838. &adc (&DWP((($i+4)%8)*4,"esp"),0); # t[4]+=0
  839. &adc (&DWP((($i+5)%8)*4,"esp"),0); # t[5]+=0
  840. &adc (&DWP((($i+6)%8)*4,"esp"),"eax"); # t[6]+=t[0]
  841. &adc ("ecx",0); # t[7]+=0
  842. &adc ("edx","eax"); # t[8]+=t[0]
  843. &adc ("edi",0); # top-most carry
  844. &mov ("ebx",&DWP($j*4,"ebp")); # b[i]
  845. &sub ("ecx","eax"); # t[7]-=t[0]
  846. &mov ("eax",&DWP(0*4,"esi")); # a[0]
  847. &sbb ("edx",0); # t[8]-=0
  848. &mov (&DWP((($i+7)%8)*4,"esp"),"ecx");
  849. &sbb ("edi",0); # top-most carry,
  850. # keep in mind that
  851. # netto result is
  852. # *addition* of value
  853. # with (abcd<<32)-abcd
  854. # on top, so that
  855. # underflow is
  856. # impossible, because
  857. # (abcd<<32)-abcd
  858. # doesn't underflow
  859. &mov (&DWP((($i+8)%8)*4,"esp"),"edx");
  860. &mul ("ebx"); # a[0]*b[i]
  861. &add ("eax",&DWP((($j+0)%8)*4,"esp"));
  862. &adc ("edx",0);
  863. &mov (&DWP((($j+0)%8)*4,"esp"),"eax");
  864. &mov ("eax",&DWP(1*4,"esi"));
  865. &mov ("ecx","edx")
  866. &mul ("ebx"); # a[1]*b[i]
  867. &add ("ecx",&DWP((($j+1)%8)*4,"esp"));
  868. &adc ("edx",0);
  869. &add ("ecx","eax");
  870. &adc ("edx",0);
  871. &mov ("eax",&DWP(2*4,"esi"));
  872. &mov (&DWP((($j+1)%8)*4,"esp"),"ecx");
  873. &mov ("ecx","edx");
  874. &mul ("ebx"); # a[2]*b[i]
  875. &add ("ecx",&DWP((($j+2)%8)*4,"esp"));
  876. &adc ("edx",0);
  877. &add ("ecx","eax");
  878. &adc ("edx",0);
  879. &mov ("eax",&DWP(3*4,"esi"));
  880. &mov (&DWP((($j+2)%8)*4,"esp"),"ecx");
  881. &mov ("ecx","edx");
  882. &mul ("ebx"); # a[3]*b[i]
  883. &add ("ecx",&DWP((($j+3)%8)*4,"esp"));
  884. &adc ("edx",0);
  885. &add ("ecx","eax");
  886. &adc ("edx",0);
  887. &mov ("eax",&DWP(4*4,"esi"));
  888. &mov (&DWP((($j+3)%8)*4,"esp"),"ecx");
  889. &mov ("ecx","edx");
  890. &mul ("ebx"); # a[4]*b[i]
  891. &add ("ecx",&DWP((($j+4)%8)*4,"esp"));
  892. &adc ("edx",0);
  893. &add ("ecx","eax");
  894. &adc ("edx",0);
  895. &mov ("eax",&DWP(5*4,"esi"));
  896. &mov (&DWP((($j+4)%8)*4,"esp"),"ecx");
  897. &mov ("ecx","edx");
  898. &mul ("ebx"); # a[5]*b[i]
  899. &add ("ecx",&DWP((($j+5)%8)*4,"esp"));
  900. &adc ("edx",0);
  901. &add ("ecx","eax");
  902. &adc ("edx",0);
  903. &mov ("eax",&DWP(6*4,"esi"));
  904. &mov (&DWP((($j+5)%8)*4,"esp"),"ecx");
  905. &mov ("ecx","edx");
  906. &mul ("ebx"); # a[6]*b[i]
  907. &add ("ecx",&DWP((($j+6)%8)*4,"esp"));
  908. &adc ("edx",0);
  909. &add ("ecx","eax");
  910. &adc ("edx",0);
  911. &mov ("eax",&DWP(7*4,"esi"));
  912. &mov (&DWP((($j+6)%8)*4,"esp"),"ecx");
  913. &mov ("ecx","edx");
  914. &mul ("ebx"); # a[7]*b[i]
  915. &add ("ecx",&DWP((($j+7)%8)*4,"esp"));
  916. &adc ("edx",0);
  917. &add ("ecx","eax"); # t[7]
  918. &mov ("eax",&DWP((($j+0)%8)*4,"esp")); # t[0]
  919. &adc ("edx","edi"); # t[8]
  920. &mov ("edi",0);
  921. &adc ("edi",0); # top-most carry
  922. }
  923. &mov ("ebp",&DWP(8*4,"esp")); # restore dst ptr
  924. &xor ("esi","esi");
  925. my $j=$i+1;
  926. # last multiplication-less reduction
  927. &add (&DWP((($i+3)%8)*4,"esp"),"eax"); # t[3]+=t[0]
  928. &adc (&DWP((($i+4)%8)*4,"esp"),0); # t[4]+=0
  929. &adc (&DWP((($i+5)%8)*4,"esp"),0); # t[5]+=0
  930. &adc (&DWP((($i+6)%8)*4,"esp"),"eax"); # t[6]+=t[0]
  931. &adc ("ecx",0); # t[7]+=0
  932. &adc ("edx","eax"); # t[8]+=t[0]
  933. &adc ("edi",0); # top-most carry
  934. &mov ("ebx",&DWP((($j+1)%8)*4,"esp"));
  935. &sub ("ecx","eax"); # t[7]-=t[0]
  936. &mov ("eax",&DWP((($j+0)%8)*4,"esp"));
  937. &sbb ("edx",0); # t[8]-=0
  938. &mov (&DWP((($i+7)%8)*4,"esp"),"ecx");
  939. &sbb ("edi",0); # top-most carry
  940. &mov (&DWP((($i+8)%8)*4,"esp"),"edx");
  941. # Final step is "if result > mod, subtract mod", but we do it
  942. # "other way around", namely write result - mod to output buffer
  943. # and if subtraction borrowed, add modulus back.
  944. &mov ("ecx",&DWP((($j+2)%8)*4,"esp"));
  945. &sub ("eax",-1);
  946. &mov ("edx",&DWP((($j+3)%8)*4,"esp"));
  947. &sbb ("ebx",-1);
  948. &mov (&DWP(0*4,"ebp"),"eax");
  949. &sbb ("ecx",-1);
  950. &mov (&DWP(1*4,"ebp"),"ebx");
  951. &sbb ("edx",0);
  952. &mov (&DWP(2*4,"ebp"),"ecx");
  953. &mov (&DWP(3*4,"ebp"),"edx");
  954. &mov ("eax",&DWP((($j+4)%8)*4,"esp"));
  955. &mov ("ebx",&DWP((($j+5)%8)*4,"esp"));
  956. &mov ("ecx",&DWP((($j+6)%8)*4,"esp"));
  957. &sbb ("eax",0);
  958. &mov ("edx",&DWP((($j+7)%8)*4,"esp"));
  959. &sbb ("ebx",0);
  960. &sbb ("ecx",1);
  961. &sbb ("edx",-1);
  962. &sbb ("edi",0);
  963. # Note that because mod has special form, i.e. consists of
  964. # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
  965. # assigning borrow bit to one register, %ebp, and its negative
  966. # to another, %esi. But we started by calculating %esi...
  967. &sub ("esi","edi");
  968. &add (&DWP(0*4,"ebp"),"edi"); # add modulus or zero
  969. &adc (&DWP(1*4,"ebp"),"edi");
  970. &adc (&DWP(2*4,"ebp"),"edi");
  971. &adc (&DWP(3*4,"ebp"),0);
  972. &adc ("eax",0);
  973. &adc ("ebx",0);
  974. &mov (&DWP(4*4,"ebp"),"eax");
  975. &adc ("ecx","esi");
  976. &mov (&DWP(5*4,"ebp"),"ebx");
  977. &adc ("edx","edi");
  978. &mov (&DWP(6*4,"ebp"),"ecx");
  979. &mov ("edi","ebp"); # fulfill contract
  980. &mov (&DWP(7*4,"ebp"),"edx");
  981. &add ("esp",10*4);
  982. &ret ();
  983. &function_end_B("_ecp_nistz256_mul_mont");
  984. ########################################################################
  985. # void ecp_nistz256_scatter_w5(void *edi,const P256_POINT *esi,
  986. # int ebp);
  987. &function_begin("ecp_nistz256_scatter_w5");
  988. &mov ("edi",&wparam(0));
  989. &mov ("esi",&wparam(1));
  990. &mov ("ebp",&wparam(2));
  991. &lea ("edi",&DWP(128-4,"edi","ebp",4));
  992. &mov ("ebp",96/16);
  993. &set_label("scatter_w5_loop");
  994. &mov ("eax",&DWP(0,"esi"));
  995. &mov ("ebx",&DWP(4,"esi"));
  996. &mov ("ecx",&DWP(8,"esi"));
  997. &mov ("edx",&DWP(12,"esi"));
  998. &lea ("esi",&DWP(16,"esi"));
  999. &mov (&DWP(64*0-128,"edi"),"eax");
  1000. &mov (&DWP(64*1-128,"edi"),"ebx");
  1001. &mov (&DWP(64*2-128,"edi"),"ecx");
  1002. &mov (&DWP(64*3-128,"edi"),"edx");
  1003. &lea ("edi",&DWP(64*4,"edi"));
  1004. &dec ("ebp");
  1005. &jnz (&label("scatter_w5_loop"));
  1006. &function_end("ecp_nistz256_scatter_w5");
  1007. ########################################################################
  1008. # void ecp_nistz256_gather_w5(P256_POINT *edi,const void *esi,
  1009. # int ebp);
  1010. &function_begin("ecp_nistz256_gather_w5");
  1011. &mov ("esi",&wparam(1));
  1012. &mov ("ebp",&wparam(2));
  1013. &lea ("esi",&DWP(0,"esi","ebp",4));
  1014. &neg ("ebp");
  1015. &sar ("ebp",31);
  1016. &mov ("edi",&wparam(0));
  1017. &lea ("esi",&DWP(0,"esi","ebp",4));
  1018. for($i=0;$i<24;$i+=4) {
  1019. &mov ("eax",&DWP(64*($i+0),"esi"));
  1020. &mov ("ebx",&DWP(64*($i+1),"esi"));
  1021. &mov ("ecx",&DWP(64*($i+2),"esi"));
  1022. &mov ("edx",&DWP(64*($i+3),"esi"));
  1023. &and ("eax","ebp");
  1024. &and ("ebx","ebp");
  1025. &and ("ecx","ebp");
  1026. &and ("edx","ebp");
  1027. &mov (&DWP(4*($i+0),"edi"),"eax");
  1028. &mov (&DWP(4*($i+1),"edi"),"ebx");
  1029. &mov (&DWP(4*($i+2),"edi"),"ecx");
  1030. &mov (&DWP(4*($i+3),"edi"),"edx");
  1031. }
  1032. &function_end("ecp_nistz256_gather_w5");
  1033. ########################################################################
  1034. # void ecp_nistz256_scatter_w7(void *edi,const P256_POINT_AFFINE *esi,
  1035. # int ebp);
  1036. &function_begin("ecp_nistz256_scatter_w7");
  1037. &mov ("edi",&wparam(0));
  1038. &mov ("esi",&wparam(1));
  1039. &mov ("ebp",&wparam(2));
  1040. &lea ("edi",&DWP(0,"edi","ebp"));
  1041. &mov ("ebp",64/4);
  1042. &set_label("scatter_w7_loop");
  1043. &mov ("eax",&DWP(0,"esi"));
  1044. &lea ("esi",&DWP(4,"esi"));
  1045. &mov (&BP(64*0,"edi"),"al");
  1046. &mov (&BP(64*1,"edi"),"ah");
  1047. &shr ("eax",16);
  1048. &mov (&BP(64*2,"edi"),"al");
  1049. &mov (&BP(64*3,"edi"),"ah");
  1050. &lea ("edi",&DWP(64*4,"edi"));
  1051. &dec ("ebp");
  1052. &jnz (&label("scatter_w7_loop"));
  1053. &function_end("ecp_nistz256_scatter_w7");
  1054. ########################################################################
  1055. # void ecp_nistz256_gather_w7(P256_POINT_AFFINE *edi,const void *esi,
  1056. # int ebp);
  1057. &function_begin("ecp_nistz256_gather_w7");
  1058. &mov ("esi",&wparam(1));
  1059. &mov ("ebp",&wparam(2));
  1060. &add ("esi","ebp");
  1061. &neg ("ebp"),
  1062. &sar ("ebp",31);
  1063. &mov ("edi",&wparam(0));
  1064. &lea ("esi",&DWP(0,"esi","ebp"));
  1065. for($i=0;$i<64;$i+=4) {
  1066. &movz ("eax",&BP(64*($i+0),"esi"));
  1067. &movz ("ebx",&BP(64*($i+1),"esi"));
  1068. &movz ("ecx",&BP(64*($i+2),"esi"));
  1069. &and ("eax","ebp");
  1070. &movz ("edx",&BP(64*($i+3),"esi"));
  1071. &and ("ebx","ebp");
  1072. &mov (&BP($i+0,"edi"),"al");
  1073. &and ("ecx","ebp");
  1074. &mov (&BP($i+1,"edi"),"bl");
  1075. &and ("edx","ebp");
  1076. &mov (&BP($i+2,"edi"),"cl");
  1077. &mov (&BP($i+3,"edi"),"dl");
  1078. }
  1079. &function_end("ecp_nistz256_gather_w7");
  1080. ########################################################################
  1081. # following subroutines are "literal" implementation of those found in
  1082. # ecp_nistz256.c
  1083. #
  1084. ########################################################################
  1085. # void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
  1086. #
  1087. &static_label("point_double_shortcut");
  1088. &function_begin("ecp_nistz256_point_double");
  1089. { my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
  1090. &mov ("esi",&wparam(1));
  1091. # above map() describes stack layout with 5 temporary
  1092. # 256-bit vectors on top, then we take extra word for
  1093. # OPENSSL_ia32cap_P copy.
  1094. &stack_push(8*5+1);
  1095. if ($sse2) {
  1096. &call ("_picup_eax");
  1097. &set_label("pic");
  1098. &picmeup("edx","OPENSSL_ia32cap_P","eax",&label("pic"));
  1099. &mov ("ebp",&DWP(0,"edx")); }
  1100. &set_label("point_double_shortcut");
  1101. &mov ("eax",&DWP(0,"esi")); # copy in_x
  1102. &mov ("ebx",&DWP(4,"esi"));
  1103. &mov ("ecx",&DWP(8,"esi"));
  1104. &mov ("edx",&DWP(12,"esi"));
  1105. &mov (&DWP($in_x+0,"esp"),"eax");
  1106. &mov (&DWP($in_x+4,"esp"),"ebx");
  1107. &mov (&DWP($in_x+8,"esp"),"ecx");
  1108. &mov (&DWP($in_x+12,"esp"),"edx");
  1109. &mov ("eax",&DWP(16,"esi"));
  1110. &mov ("ebx",&DWP(20,"esi"));
  1111. &mov ("ecx",&DWP(24,"esi"));
  1112. &mov ("edx",&DWP(28,"esi"));
  1113. &mov (&DWP($in_x+16,"esp"),"eax");
  1114. &mov (&DWP($in_x+20,"esp"),"ebx");
  1115. &mov (&DWP($in_x+24,"esp"),"ecx");
  1116. &mov (&DWP($in_x+28,"esp"),"edx");
  1117. &mov (&DWP(32*5,"esp"),"ebp"); # OPENSSL_ia32cap_P copy
  1118. &lea ("ebp",&DWP(32,"esi"));
  1119. &lea ("esi",&DWP(32,"esi"));
  1120. &lea ("edi",&DWP($S,"esp"));
  1121. &call ("_ecp_nistz256_add"); # p256_mul_by_2(S, in_y);
  1122. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1123. &mov ("esi",64);
  1124. &add ("esi",&wparam(1));
  1125. &lea ("edi",&DWP($Zsqr,"esp"));
  1126. &mov ("ebp","esi");
  1127. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Zsqr, in_z);
  1128. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1129. &lea ("esi",&DWP($S,"esp"));
  1130. &lea ("ebp",&DWP($S,"esp"));
  1131. &lea ("edi",&DWP($S,"esp"));
  1132. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(S, S);
  1133. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1134. &mov ("ebp",&wparam(1));
  1135. &lea ("esi",&DWP(32,"ebp"));
  1136. &lea ("ebp",&DWP(64,"ebp"));
  1137. &lea ("edi",&DWP($tmp0,"esp"));
  1138. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(tmp0, in_z, in_y);
  1139. &lea ("esi",&DWP($in_x,"esp"));
  1140. &lea ("ebp",&DWP($Zsqr,"esp"));
  1141. &lea ("edi",&DWP($M,"esp"));
  1142. &call ("_ecp_nistz256_add"); # p256_add(M, in_x, Zsqr);
  1143. &mov ("edi",64);
  1144. &lea ("esi",&DWP($tmp0,"esp"));
  1145. &lea ("ebp",&DWP($tmp0,"esp"));
  1146. &add ("edi",&wparam(0));
  1147. &call ("_ecp_nistz256_add"); # p256_mul_by_2(res_z, tmp0);
  1148. &lea ("esi",&DWP($in_x,"esp"));
  1149. &lea ("ebp",&DWP($Zsqr,"esp"));
  1150. &lea ("edi",&DWP($Zsqr,"esp"));
  1151. &call ("_ecp_nistz256_sub"); # p256_sub(Zsqr, in_x, Zsqr);
  1152. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1153. &lea ("esi",&DWP($S,"esp"));
  1154. &lea ("ebp",&DWP($S,"esp"));
  1155. &lea ("edi",&DWP($tmp0,"esp"));
  1156. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(tmp0, S);
  1157. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1158. &lea ("esi",&DWP($M,"esp"));
  1159. &lea ("ebp",&DWP($Zsqr,"esp"));
  1160. &lea ("edi",&DWP($M,"esp"));
  1161. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(M, M, Zsqr);
  1162. &mov ("edi",32);
  1163. &lea ("esi",&DWP($tmp0,"esp"));
  1164. &add ("edi",&wparam(0));
  1165. &call ("_ecp_nistz256_div_by_2"); # p256_div_by_2(res_y, tmp0);
  1166. &lea ("esi",&DWP($M,"esp"));
  1167. &lea ("ebp",&DWP($M,"esp"));
  1168. &lea ("edi",&DWP($tmp0,"esp"));
  1169. &call ("_ecp_nistz256_add"); # 1/2 p256_mul_by_3(M, M);
  1170. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1171. &lea ("esi",&DWP($in_x,"esp"));
  1172. &lea ("ebp",&DWP($S,"esp"));
  1173. &lea ("edi",&DWP($S,"esp"));
  1174. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S, S, in_x);
  1175. &lea ("esi",&DWP($tmp0,"esp"));
  1176. &lea ("ebp",&DWP($M,"esp"));
  1177. &lea ("edi",&DWP($M,"esp"));
  1178. &call ("_ecp_nistz256_add"); # 2/2 p256_mul_by_3(M, M);
  1179. &lea ("esi",&DWP($S,"esp"));
  1180. &lea ("ebp",&DWP($S,"esp"));
  1181. &lea ("edi",&DWP($tmp0,"esp"));
  1182. &call ("_ecp_nistz256_add"); # p256_mul_by_2(tmp0, S);
  1183. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1184. &lea ("esi",&DWP($M,"esp"));
  1185. &lea ("ebp",&DWP($M,"esp"));
  1186. &mov ("edi",&wparam(0));
  1187. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(res_x, M);
  1188. &mov ("esi","edi"); # %edi is still res_x here
  1189. &lea ("ebp",&DWP($tmp0,"esp"));
  1190. &call ("_ecp_nistz256_sub"); # p256_sub(res_x, res_x, tmp0);
  1191. &lea ("esi",&DWP($S,"esp"));
  1192. &mov ("ebp","edi"); # %edi is still res_x
  1193. &lea ("edi",&DWP($S,"esp"));
  1194. &call ("_ecp_nistz256_sub"); # p256_sub(S, S, res_x);
  1195. &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
  1196. &mov ("esi","edi"); # %edi is still &S
  1197. &lea ("ebp",&DWP($M,"esp"));
  1198. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S, S, M);
  1199. &mov ("ebp",32);
  1200. &lea ("esi",&DWP($S,"esp"));
  1201. &add ("ebp",&wparam(0));
  1202. &mov ("edi","ebp");
  1203. &call ("_ecp_nistz256_sub"); # p256_sub(res_y, S, res_y);
  1204. &stack_pop(8*5+1);
  1205. } &function_end("ecp_nistz256_point_double");
  1206. ########################################################################
  1207. # void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
  1208. # const P256_POINT *in2);
  1209. &function_begin("ecp_nistz256_point_add");
  1210. { my ($res_x,$res_y,$res_z,
  1211. $in1_x,$in1_y,$in1_z,
  1212. $in2_x,$in2_y,$in2_z,
  1213. $H,$Hsqr,$R,$Rsqr,$Hcub,
  1214. $U1,$U2,$S1,$S2)=map(32*$_,(0..17));
  1215. my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
  1216. &mov ("esi",&wparam(2));
  1217. # above map() describes stack layout with 18 temporary
  1218. # 256-bit vectors on top, then we take extra words for
  1219. # ~in1infty, ~in2infty, result of check for zero and
  1220. # OPENSSL_ia32cap_P copy. [one unused word for padding]
  1221. &stack_push(8*18+5);
  1222. if ($sse2) {
  1223. &call ("_picup_eax");
  1224. &set_label("pic");
  1225. &picmeup("edx","OPENSSL_ia32cap_P","eax",&label("pic"));
  1226. &mov ("ebp",&DWP(0,"edx")); }
  1227. &lea ("edi",&DWP($in2_x,"esp"));
  1228. for($i=0;$i<96;$i+=16) {
  1229. &mov ("eax",&DWP($i+0,"esi")); # copy in2
  1230. &mov ("ebx",&DWP($i+4,"esi"));
  1231. &mov ("ecx",&DWP($i+8,"esi"));
  1232. &mov ("edx",&DWP($i+12,"esi"));
  1233. &mov (&DWP($i+0,"edi"),"eax");
  1234. &mov (&DWP(32*18+12,"esp"),"ebp") if ($i==0);
  1235. &mov ("ebp","eax") if ($i==64);
  1236. &or ("ebp","eax") if ($i>64);
  1237. &mov (&DWP($i+4,"edi"),"ebx");
  1238. &or ("ebp","ebx") if ($i>=64);
  1239. &mov (&DWP($i+8,"edi"),"ecx");
  1240. &or ("ebp","ecx") if ($i>=64);
  1241. &mov (&DWP($i+12,"edi"),"edx");
  1242. &or ("ebp","edx") if ($i>=64);
  1243. }
  1244. &xor ("eax","eax");
  1245. &mov ("esi",&wparam(1));
  1246. &sub ("eax","ebp");
  1247. &or ("ebp","eax");
  1248. &sar ("ebp",31);
  1249. &mov (&DWP(32*18+4,"esp"),"ebp"); # ~in2infty
  1250. &lea ("edi",&DWP($in1_x,"esp"));
  1251. for($i=0;$i<96;$i+=16) {
  1252. &mov ("eax",&DWP($i+0,"esi")); # copy in1
  1253. &mov ("ebx",&DWP($i+4,"esi"));
  1254. &mov ("ecx",&DWP($i+8,"esi"));
  1255. &mov ("edx",&DWP($i+12,"esi"));
  1256. &mov (&DWP($i+0,"edi"),"eax");
  1257. &mov ("ebp","eax") if ($i==64);
  1258. &or ("ebp","eax") if ($i>64);
  1259. &mov (&DWP($i+4,"edi"),"ebx");
  1260. &or ("ebp","ebx") if ($i>=64);
  1261. &mov (&DWP($i+8,"edi"),"ecx");
  1262. &or ("ebp","ecx") if ($i>=64);
  1263. &mov (&DWP($i+12,"edi"),"edx");
  1264. &or ("ebp","edx") if ($i>=64);
  1265. }
  1266. &xor ("eax","eax");
  1267. &sub ("eax","ebp");
  1268. &or ("ebp","eax");
  1269. &sar ("ebp",31);
  1270. &mov (&DWP(32*18+0,"esp"),"ebp"); # ~in1infty
  1271. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1272. &lea ("esi",&DWP($in2_z,"esp"));
  1273. &lea ("ebp",&DWP($in2_z,"esp"));
  1274. &lea ("edi",&DWP($Z2sqr,"esp"));
  1275. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Z2sqr, in2_z);
  1276. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1277. &lea ("esi",&DWP($in1_z,"esp"));
  1278. &lea ("ebp",&DWP($in1_z,"esp"));
  1279. &lea ("edi",&DWP($Z1sqr,"esp"));
  1280. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Z1sqr, in1_z);
  1281. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1282. &lea ("esi",&DWP($Z2sqr,"esp"));
  1283. &lea ("ebp",&DWP($in2_z,"esp"));
  1284. &lea ("edi",&DWP($S1,"esp"));
  1285. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S1, Z2sqr, in2_z);
  1286. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1287. &lea ("esi",&DWP($Z1sqr,"esp"));
  1288. &lea ("ebp",&DWP($in1_z,"esp"));
  1289. &lea ("edi",&DWP($S2,"esp"));
  1290. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, Z1sqr, in1_z);
  1291. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1292. &lea ("esi",&DWP($in1_y,"esp"));
  1293. &lea ("ebp",&DWP($S1,"esp"));
  1294. &lea ("edi",&DWP($S1,"esp"));
  1295. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S1, S1, in1_y);
  1296. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1297. &lea ("esi",&DWP($in2_y,"esp"));
  1298. &lea ("ebp",&DWP($S2,"esp"));
  1299. &lea ("edi",&DWP($S2,"esp"));
  1300. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, S2, in2_y);
  1301. &lea ("esi",&DWP($S2,"esp"));
  1302. &lea ("ebp",&DWP($S1,"esp"));
  1303. &lea ("edi",&DWP($R,"esp"));
  1304. &call ("_ecp_nistz256_sub"); # p256_sub(R, S2, S1);
  1305. &or ("ebx","eax"); # see if result is zero
  1306. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1307. &or ("ebx","ecx");
  1308. &or ("ebx","edx");
  1309. &or ("ebx",&DWP(0,"edi"));
  1310. &or ("ebx",&DWP(4,"edi"));
  1311. &lea ("esi",&DWP($in1_x,"esp"));
  1312. &or ("ebx",&DWP(8,"edi"));
  1313. &lea ("ebp",&DWP($Z2sqr,"esp"));
  1314. &or ("ebx",&DWP(12,"edi"));
  1315. &lea ("edi",&DWP($U1,"esp"));
  1316. &mov (&DWP(32*18+8,"esp"),"ebx");
  1317. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U1, in1_x, Z2sqr);
  1318. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1319. &lea ("esi",&DWP($in2_x,"esp"));
  1320. &lea ("ebp",&DWP($Z1sqr,"esp"));
  1321. &lea ("edi",&DWP($U2,"esp"));
  1322. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, in2_x, Z1sqr);
  1323. &lea ("esi",&DWP($U2,"esp"));
  1324. &lea ("ebp",&DWP($U1,"esp"));
  1325. &lea ("edi",&DWP($H,"esp"));
  1326. &call ("_ecp_nistz256_sub"); # p256_sub(H, U2, U1);
  1327. &or ("eax","ebx"); # see if result is zero
  1328. &or ("eax","ecx");
  1329. &or ("eax","edx");
  1330. &or ("eax",&DWP(0,"edi"));
  1331. &or ("eax",&DWP(4,"edi"));
  1332. &or ("eax",&DWP(8,"edi"));
  1333. &or ("eax",&DWP(12,"edi")); # ~is_equal(U1,U2)
  1334. &mov ("ebx",&DWP(32*18+0,"esp")); # ~in1infty
  1335. &not ("ebx"); # -1/0 -> 0/-1
  1336. &or ("eax","ebx");
  1337. &mov ("ebx",&DWP(32*18+4,"esp")); # ~in2infty
  1338. &not ("ebx"); # -1/0 -> 0/-1
  1339. &or ("eax","ebx");
  1340. &or ("eax",&DWP(32*18+8,"esp")); # ~is_equal(S1,S2)
  1341. # if (~is_equal(U1,U2) | in1infty | in2infty | ~is_equal(S1,S2))
  1342. &data_byte(0x3e); # predict taken
  1343. &jnz (&label("add_proceed"));
  1344. &set_label("add_double",16);
  1345. &mov ("esi",&wparam(1));
  1346. &mov ("ebp",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1347. &add ("esp",4*((8*18+5)-(8*5+1))); # difference in frame sizes
  1348. &jmp (&label("point_double_shortcut"));
  1349. &set_label("add_proceed",16);
  1350. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1351. &lea ("esi",&DWP($R,"esp"));
  1352. &lea ("ebp",&DWP($R,"esp"));
  1353. &lea ("edi",&DWP($Rsqr,"esp"));
  1354. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Rsqr, R);
  1355. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1356. &lea ("esi",&DWP($H,"esp"));
  1357. &lea ("ebp",&DWP($in1_z,"esp"));
  1358. &lea ("edi",&DWP($res_z,"esp"));
  1359. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_z, H, in1_z);
  1360. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1361. &lea ("esi",&DWP($H,"esp"));
  1362. &lea ("ebp",&DWP($H,"esp"));
  1363. &lea ("edi",&DWP($Hsqr,"esp"));
  1364. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Hsqr, H);
  1365. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1366. &lea ("esi",&DWP($in2_z,"esp"));
  1367. &lea ("ebp",&DWP($res_z,"esp"));
  1368. &lea ("edi",&DWP($res_z,"esp"));
  1369. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_z, res_z, in2_z);
  1370. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1371. &lea ("esi",&DWP($Hsqr,"esp"));
  1372. &lea ("ebp",&DWP($U1,"esp"));
  1373. &lea ("edi",&DWP($U2,"esp"));
  1374. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, U1, Hsqr);
  1375. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1376. &lea ("esi",&DWP($H,"esp"));
  1377. &lea ("ebp",&DWP($Hsqr,"esp"));
  1378. &lea ("edi",&DWP($Hcub,"esp"));
  1379. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(Hcub, Hsqr, H);
  1380. &lea ("esi",&DWP($U2,"esp"));
  1381. &lea ("ebp",&DWP($U2,"esp"));
  1382. &lea ("edi",&DWP($Hsqr,"esp"));
  1383. &call ("_ecp_nistz256_add"); # p256_mul_by_2(Hsqr, U2);
  1384. &lea ("esi",&DWP($Rsqr,"esp"));
  1385. &lea ("ebp",&DWP($Hsqr,"esp"));
  1386. &lea ("edi",&DWP($res_x,"esp"));
  1387. &call ("_ecp_nistz256_sub"); # p256_sub(res_x, Rsqr, Hsqr);
  1388. &lea ("esi",&DWP($res_x,"esp"));
  1389. &lea ("ebp",&DWP($Hcub,"esp"));
  1390. &lea ("edi",&DWP($res_x,"esp"));
  1391. &call ("_ecp_nistz256_sub"); # p256_sub(res_x, res_x, Hcub);
  1392. &lea ("esi",&DWP($U2,"esp"));
  1393. &lea ("ebp",&DWP($res_x,"esp"));
  1394. &lea ("edi",&DWP($res_y,"esp"));
  1395. &call ("_ecp_nistz256_sub"); # p256_sub(res_y, U2, res_x);
  1396. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1397. &lea ("esi",&DWP($Hcub,"esp"));
  1398. &lea ("ebp",&DWP($S1,"esp"));
  1399. &lea ("edi",&DWP($S2,"esp"));
  1400. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, S1, Hcub);
  1401. &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
  1402. &lea ("esi",&DWP($R,"esp"));
  1403. &lea ("ebp",&DWP($res_y,"esp"));
  1404. &lea ("edi",&DWP($res_y,"esp"));
  1405. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_y, R, res_y);
  1406. &lea ("esi",&DWP($res_y,"esp"));
  1407. &lea ("ebp",&DWP($S2,"esp"));
  1408. &lea ("edi",&DWP($res_y,"esp"));
  1409. &call ("_ecp_nistz256_sub"); # p256_sub(res_y, res_y, S2);
  1410. &mov ("ebp",&DWP(32*18+0,"esp")); # ~in1infty
  1411. &mov ("esi",&DWP(32*18+4,"esp")); # ~in2infty
  1412. &mov ("edi",&wparam(0));
  1413. &mov ("edx","ebp");
  1414. &not ("ebp");
  1415. &and ("edx","esi"); # ~in1infty & ~in2infty
  1416. &and ("ebp","esi"); # in1infty & ~in2infty
  1417. &not ("esi"); # in2infty
  1418. ########################################
  1419. # conditional moves
  1420. for($i=64;$i<96;$i+=4) {
  1421. &mov ("eax","edx"); # ~in1infty & ~in2infty
  1422. &and ("eax",&DWP($res_x+$i,"esp"));
  1423. &mov ("ebx","ebp"); # in1infty & ~in2infty
  1424. &and ("ebx",&DWP($in2_x+$i,"esp"));
  1425. &mov ("ecx","esi"); # in2infty
  1426. &and ("ecx",&DWP($in1_x+$i,"esp"));
  1427. &or ("eax","ebx");
  1428. &or ("eax","ecx");
  1429. &mov (&DWP($i,"edi"),"eax");
  1430. }
  1431. for($i=0;$i<64;$i+=4) {
  1432. &mov ("eax","edx"); # ~in1infty & ~in2infty
  1433. &and ("eax",&DWP($res_x+$i,"esp"));
  1434. &mov ("ebx","ebp"); # in1infty & ~in2infty
  1435. &and ("ebx",&DWP($in2_x+$i,"esp"));
  1436. &mov ("ecx","esi"); # in2infty
  1437. &and ("ecx",&DWP($in1_x+$i,"esp"));
  1438. &or ("eax","ebx");
  1439. &or ("eax","ecx");
  1440. &mov (&DWP($i,"edi"),"eax");
  1441. }
  1442. &set_label("add_done");
  1443. &stack_pop(8*18+5);
  1444. } &function_end("ecp_nistz256_point_add");
  1445. ########################################################################
  1446. # void ecp_nistz256_point_add_affine(P256_POINT *out,
  1447. # const P256_POINT *in1,
  1448. # const P256_POINT_AFFINE *in2);
  1449. &function_begin("ecp_nistz256_point_add_affine");
  1450. {
  1451. my ($res_x,$res_y,$res_z,
  1452. $in1_x,$in1_y,$in1_z,
  1453. $in2_x,$in2_y,
  1454. $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..14));
  1455. my $Z1sqr = $S2;
  1456. my @ONE_mont=(1,0,0,-1,-1,-1,-2,0);
  1457. &mov ("esi",&wparam(1));
  1458. # above map() describes stack layout with 15 temporary
  1459. # 256-bit vectors on top, then we take extra words for
  1460. # ~in1infty, ~in2infty, and OPENSSL_ia32cap_P copy.
  1461. &stack_push(8*15+3);
  1462. if ($sse2) {
  1463. &call ("_picup_eax");
  1464. &set_label("pic");
  1465. &picmeup("edx","OPENSSL_ia32cap_P","eax",&label("pic"));
  1466. &mov ("ebp",&DWP(0,"edx")); }
  1467. &lea ("edi",&DWP($in1_x,"esp"));
  1468. for($i=0;$i<96;$i+=16) {
  1469. &mov ("eax",&DWP($i+0,"esi")); # copy in1
  1470. &mov ("ebx",&DWP($i+4,"esi"));
  1471. &mov ("ecx",&DWP($i+8,"esi"));
  1472. &mov ("edx",&DWP($i+12,"esi"));
  1473. &mov (&DWP($i+0,"edi"),"eax");
  1474. &mov (&DWP(32*15+8,"esp"),"ebp") if ($i==0);
  1475. &mov ("ebp","eax") if ($i==64);
  1476. &or ("ebp","eax") if ($i>64);
  1477. &mov (&DWP($i+4,"edi"),"ebx");
  1478. &or ("ebp","ebx") if ($i>=64);
  1479. &mov (&DWP($i+8,"edi"),"ecx");
  1480. &or ("ebp","ecx") if ($i>=64);
  1481. &mov (&DWP($i+12,"edi"),"edx");
  1482. &or ("ebp","edx") if ($i>=64);
  1483. }
  1484. &xor ("eax","eax");
  1485. &mov ("esi",&wparam(2));
  1486. &sub ("eax","ebp");
  1487. &or ("ebp","eax");
  1488. &sar ("ebp",31);
  1489. &mov (&DWP(32*15+0,"esp"),"ebp"); # ~in1infty
  1490. &lea ("edi",&DWP($in2_x,"esp"));
  1491. for($i=0;$i<64;$i+=16) {
  1492. &mov ("eax",&DWP($i+0,"esi")); # copy in2
  1493. &mov ("ebx",&DWP($i+4,"esi"));
  1494. &mov ("ecx",&DWP($i+8,"esi"));
  1495. &mov ("edx",&DWP($i+12,"esi"));
  1496. &mov (&DWP($i+0,"edi"),"eax");
  1497. &mov ("ebp","eax") if ($i==0);
  1498. &or ("ebp","eax") if ($i!=0);
  1499. &mov (&DWP($i+4,"edi"),"ebx");
  1500. &or ("ebp","ebx");
  1501. &mov (&DWP($i+8,"edi"),"ecx");
  1502. &or ("ebp","ecx");
  1503. &mov (&DWP($i+12,"edi"),"edx");
  1504. &or ("ebp","edx");
  1505. }
  1506. &xor ("ebx","ebx");
  1507. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1508. &sub ("ebx","ebp");
  1509. &lea ("esi",&DWP($in1_z,"esp"));
  1510. &or ("ebx","ebp");
  1511. &lea ("ebp",&DWP($in1_z,"esp"));
  1512. &sar ("ebx",31);
  1513. &lea ("edi",&DWP($Z1sqr,"esp"));
  1514. &mov (&DWP(32*15+4,"esp"),"ebx"); # ~in2infty
  1515. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Z1sqr, in1_z);
  1516. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1517. &lea ("esi",&DWP($in2_x,"esp"));
  1518. &mov ("ebp","edi"); # %esi is stull &Z1sqr
  1519. &lea ("edi",&DWP($U2,"esp"));
  1520. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, Z1sqr, in2_x);
  1521. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1522. &lea ("esi",&DWP($in1_z,"esp"));
  1523. &lea ("ebp",&DWP($Z1sqr,"esp"));
  1524. &lea ("edi",&DWP($S2,"esp"));
  1525. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, Z1sqr, in1_z);
  1526. &lea ("esi",&DWP($U2,"esp"));
  1527. &lea ("ebp",&DWP($in1_x,"esp"));
  1528. &lea ("edi",&DWP($H,"esp"));
  1529. &call ("_ecp_nistz256_sub"); # p256_sub(H, U2, in1_x);
  1530. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1531. &lea ("esi",&DWP($in2_y,"esp"));
  1532. &lea ("ebp",&DWP($S2,"esp"));
  1533. &lea ("edi",&DWP($S2,"esp"));
  1534. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, S2, in2_y);
  1535. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1536. &lea ("esi",&DWP($in1_z,"esp"));
  1537. &lea ("ebp",&DWP($H,"esp"));
  1538. &lea ("edi",&DWP($res_z,"esp"));
  1539. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_z, H, in1_z);
  1540. &lea ("esi",&DWP($S2,"esp"));
  1541. &lea ("ebp",&DWP($in1_y,"esp"));
  1542. &lea ("edi",&DWP($R,"esp"));
  1543. &call ("_ecp_nistz256_sub"); # p256_sub(R, S2, in1_y);
  1544. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1545. &lea ("esi",&DWP($H,"esp"));
  1546. &lea ("ebp",&DWP($H,"esp"));
  1547. &lea ("edi",&DWP($Hsqr,"esp"));
  1548. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Hsqr, H);
  1549. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1550. &lea ("esi",&DWP($R,"esp"));
  1551. &lea ("ebp",&DWP($R,"esp"));
  1552. &lea ("edi",&DWP($Rsqr,"esp"));
  1553. &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Rsqr, R);
  1554. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1555. &lea ("esi",&DWP($in1_x,"esp"));
  1556. &lea ("ebp",&DWP($Hsqr,"esp"));
  1557. &lea ("edi",&DWP($U2,"esp"));
  1558. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, in1_x, Hsqr);
  1559. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1560. &lea ("esi",&DWP($H,"esp"));
  1561. &lea ("ebp",&DWP($Hsqr,"esp"));
  1562. &lea ("edi",&DWP($Hcub,"esp"));
  1563. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(Hcub, Hsqr, H);
  1564. &lea ("esi",&DWP($U2,"esp"));
  1565. &lea ("ebp",&DWP($U2,"esp"));
  1566. &lea ("edi",&DWP($Hsqr,"esp"));
  1567. &call ("_ecp_nistz256_add"); # p256_mul_by_2(Hsqr, U2);
  1568. &lea ("esi",&DWP($Rsqr,"esp"));
  1569. &lea ("ebp",&DWP($Hsqr,"esp"));
  1570. &lea ("edi",&DWP($res_x,"esp"));
  1571. &call ("_ecp_nistz256_sub"); # p256_sub(res_x, Rsqr, Hsqr);
  1572. &lea ("esi",&DWP($res_x,"esp"));
  1573. &lea ("ebp",&DWP($Hcub,"esp"));
  1574. &lea ("edi",&DWP($res_x,"esp"));
  1575. &call ("_ecp_nistz256_sub"); # p256_sub(res_x, res_x, Hcub);
  1576. &lea ("esi",&DWP($U2,"esp"));
  1577. &lea ("ebp",&DWP($res_x,"esp"));
  1578. &lea ("edi",&DWP($res_y,"esp"));
  1579. &call ("_ecp_nistz256_sub"); # p256_sub(res_y, U2, res_x);
  1580. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1581. &lea ("esi",&DWP($Hcub,"esp"));
  1582. &lea ("ebp",&DWP($in1_y,"esp"));
  1583. &lea ("edi",&DWP($S2,"esp"));
  1584. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, Hcub, in1_y);
  1585. &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
  1586. &lea ("esi",&DWP($R,"esp"));
  1587. &lea ("ebp",&DWP($res_y,"esp"));
  1588. &lea ("edi",&DWP($res_y,"esp"));
  1589. &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_y, res_y, R);
  1590. &lea ("esi",&DWP($res_y,"esp"));
  1591. &lea ("ebp",&DWP($S2,"esp"));
  1592. &lea ("edi",&DWP($res_y,"esp"));
  1593. &call ("_ecp_nistz256_sub"); # p256_sub(res_y, res_y, S2);
  1594. &mov ("ebp",&DWP(32*15+0,"esp")); # ~in1infty
  1595. &mov ("esi",&DWP(32*15+4,"esp")); # ~in2infty
  1596. &mov ("edi",&wparam(0));
  1597. &mov ("edx","ebp");
  1598. &not ("ebp");
  1599. &and ("edx","esi"); # ~in1infty & ~in2infty
  1600. &and ("ebp","esi"); # in1infty & ~in2infty
  1601. &not ("esi"); # in2infty
  1602. ########################################
  1603. # conditional moves
  1604. for($i=64;$i<96;$i+=4) {
  1605. my $one=@ONE_mont[($i-64)/4];
  1606. &mov ("eax","edx");
  1607. &and ("eax",&DWP($res_x+$i,"esp"));
  1608. &mov ("ebx","ebp") if ($one && $one!=-1);
  1609. &and ("ebx",$one) if ($one && $one!=-1);
  1610. &mov ("ecx","esi");
  1611. &and ("ecx",&DWP($in1_x+$i,"esp"));
  1612. &or ("eax",$one==-1?"ebp":"ebx") if ($one);
  1613. &or ("eax","ecx");
  1614. &mov (&DWP($i,"edi"),"eax");
  1615. }
  1616. for($i=0;$i<64;$i+=4) {
  1617. &mov ("eax","edx"); # ~in1infty & ~in2infty
  1618. &and ("eax",&DWP($res_x+$i,"esp"));
  1619. &mov ("ebx","ebp"); # in1infty & ~in2infty
  1620. &and ("ebx",&DWP($in2_x+$i,"esp"));
  1621. &mov ("ecx","esi"); # in2infty
  1622. &and ("ecx",&DWP($in1_x+$i,"esp"));
  1623. &or ("eax","ebx");
  1624. &or ("eax","ecx");
  1625. &mov (&DWP($i,"edi"),"eax");
  1626. }
  1627. &stack_pop(8*15+3);
  1628. } &function_end("ecp_nistz256_point_add_affine");
  1629. &asm_finish();
  1630. close STDOUT or die "error closing STDOUT: $!";