2
0

x86-mont.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # 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. # October 2005
  15. #
  16. # This is a "teaser" code, as it can be improved in several ways...
  17. # First of all non-SSE2 path should be implemented (yes, for now it
  18. # performs Montgomery multiplication/convolution only on SSE2-capable
  19. # CPUs such as P4, others fall down to original code). Then inner loop
  20. # can be unrolled and modulo-scheduled to improve ILP and possibly
  21. # moved to 128-bit XMM register bank (though it would require input
  22. # rearrangement and/or increase bus bandwidth utilization). Dedicated
  23. # squaring procedure should give further performance improvement...
  24. # Yet, for being draft, the code improves rsa512 *sign* benchmark by
  25. # 110%(!), rsa1024 one - by 70% and rsa4096 - by 20%:-)
  26. # December 2006
  27. #
  28. # Modulo-scheduling SSE2 loops results in further 15-20% improvement.
  29. # Integer-only code [being equipped with dedicated squaring procedure]
  30. # gives ~40% on rsa512 sign benchmark...
  31. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  32. push(@INC,"${dir}","${dir}../../perlasm");
  33. require "x86asm.pl";
  34. $output = pop and open STDOUT,">$output";
  35. &asm_init($ARGV[0]);
  36. $sse2=0;
  37. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  38. &external_label("OPENSSL_ia32cap_P") if ($sse2);
  39. &function_begin("bn_mul_mont");
  40. $i="edx";
  41. $j="ecx";
  42. $ap="esi"; $tp="esi"; # overlapping variables!!!
  43. $rp="edi"; $bp="edi"; # overlapping variables!!!
  44. $np="ebp";
  45. $num="ebx";
  46. $_num=&DWP(4*0,"esp"); # stack top layout
  47. $_rp=&DWP(4*1,"esp");
  48. $_ap=&DWP(4*2,"esp");
  49. $_bp=&DWP(4*3,"esp");
  50. $_np=&DWP(4*4,"esp");
  51. $_n0=&DWP(4*5,"esp"); $_n0q=&QWP(4*5,"esp");
  52. $_sp=&DWP(4*6,"esp");
  53. $_bpend=&DWP(4*7,"esp");
  54. $frame=32; # size of above frame rounded up to 16n
  55. &xor ("eax","eax");
  56. &mov ("edi",&wparam(5)); # int num
  57. &cmp ("edi",4);
  58. &jl (&label("just_leave"));
  59. &lea ("esi",&wparam(0)); # put aside pointer to argument block
  60. &lea ("edx",&wparam(1)); # load ap
  61. &add ("edi",2); # extra two words on top of tp
  62. &neg ("edi");
  63. &lea ("ebp",&DWP(-$frame,"esp","edi",4)); # future alloca($frame+4*(num+2))
  64. &neg ("edi");
  65. # minimize cache contention by arranging 2K window between stack
  66. # pointer and ap argument [np is also position sensitive vector,
  67. # but it's assumed to be near ap, as it's allocated at ~same
  68. # time].
  69. &mov ("eax","ebp");
  70. &sub ("eax","edx");
  71. &and ("eax",2047);
  72. &sub ("ebp","eax"); # this aligns sp and ap modulo 2048
  73. &xor ("edx","ebp");
  74. &and ("edx",2048);
  75. &xor ("edx",2048);
  76. &sub ("ebp","edx"); # this splits them apart modulo 4096
  77. &and ("ebp",-64); # align to cache line
  78. # An OS-agnostic version of __chkstk.
  79. #
  80. # Some OSes (Windows) insist on stack being "wired" to
  81. # physical memory in strictly sequential manner, i.e. if stack
  82. # allocation spans two pages, then reference to farmost one can
  83. # be punishable by SEGV. But page walking can do good even on
  84. # other OSes, because it guarantees that villain thread hits
  85. # the guard page before it can make damage to innocent one...
  86. &mov ("eax","esp");
  87. &sub ("eax","ebp");
  88. &and ("eax",-4096);
  89. &mov ("edx","esp"); # saved stack pointer!
  90. &lea ("esp",&DWP(0,"ebp","eax"));
  91. &mov ("eax",&DWP(0,"esp"));
  92. &cmp ("esp","ebp");
  93. &ja (&label("page_walk"));
  94. &jmp (&label("page_walk_done"));
  95. &set_label("page_walk",16);
  96. &lea ("esp",&DWP(-4096,"esp"));
  97. &mov ("eax",&DWP(0,"esp"));
  98. &cmp ("esp","ebp");
  99. &ja (&label("page_walk"));
  100. &set_label("page_walk_done");
  101. ################################# load argument block...
  102. &mov ("eax",&DWP(0*4,"esi"));# BN_ULONG *rp
  103. &mov ("ebx",&DWP(1*4,"esi"));# const BN_ULONG *ap
  104. &mov ("ecx",&DWP(2*4,"esi"));# const BN_ULONG *bp
  105. &mov ("ebp",&DWP(3*4,"esi"));# const BN_ULONG *np
  106. &mov ("esi",&DWP(4*4,"esi"));# const BN_ULONG *n0
  107. #&mov ("edi",&DWP(5*4,"esi"));# int num
  108. &mov ("esi",&DWP(0,"esi")); # pull n0[0]
  109. &mov ($_rp,"eax"); # ... save a copy of argument block
  110. &mov ($_ap,"ebx");
  111. &mov ($_bp,"ecx");
  112. &mov ($_np,"ebp");
  113. &mov ($_n0,"esi");
  114. &lea ($num,&DWP(-3,"edi")); # num=num-1 to assist modulo-scheduling
  115. #&mov ($_num,$num); # redundant as $num is not reused
  116. &mov ($_sp,"edx"); # saved stack pointer!
  117. if($sse2) {
  118. $acc0="mm0"; # mmx register bank layout
  119. $acc1="mm1";
  120. $car0="mm2";
  121. $car1="mm3";
  122. $mul0="mm4";
  123. $mul1="mm5";
  124. $temp="mm6";
  125. $mask="mm7";
  126. &picmeup("eax","OPENSSL_ia32cap_P");
  127. &bt (&DWP(0,"eax"),26);
  128. &jnc (&label("non_sse2"));
  129. &mov ("eax",-1);
  130. &movd ($mask,"eax"); # mask 32 lower bits
  131. &mov ($ap,$_ap); # load input pointers
  132. &mov ($bp,$_bp);
  133. &mov ($np,$_np);
  134. &xor ($i,$i); # i=0
  135. &xor ($j,$j); # j=0
  136. &movd ($mul0,&DWP(0,$bp)); # bp[0]
  137. &movd ($mul1,&DWP(0,$ap)); # ap[0]
  138. &movd ($car1,&DWP(0,$np)); # np[0]
  139. &pmuludq($mul1,$mul0); # ap[0]*bp[0]
  140. &movq ($car0,$mul1);
  141. &movq ($acc0,$mul1); # I wish movd worked for
  142. &pand ($acc0,$mask); # inter-register transfers
  143. &pmuludq($mul1,$_n0q); # *=n0
  144. &pmuludq($car1,$mul1); # "t[0]"*np[0]*n0
  145. &paddq ($car1,$acc0);
  146. &movd ($acc1,&DWP(4,$np)); # np[1]
  147. &movd ($acc0,&DWP(4,$ap)); # ap[1]
  148. &psrlq ($car0,32);
  149. &psrlq ($car1,32);
  150. &inc ($j); # j++
  151. &set_label("1st",16);
  152. &pmuludq($acc0,$mul0); # ap[j]*bp[0]
  153. &pmuludq($acc1,$mul1); # np[j]*m1
  154. &paddq ($car0,$acc0); # +=c0
  155. &paddq ($car1,$acc1); # +=c1
  156. &movq ($acc0,$car0);
  157. &pand ($acc0,$mask);
  158. &movd ($acc1,&DWP(4,$np,$j,4)); # np[j+1]
  159. &paddq ($car1,$acc0); # +=ap[j]*bp[0];
  160. &movd ($acc0,&DWP(4,$ap,$j,4)); # ap[j+1]
  161. &psrlq ($car0,32);
  162. &movd (&DWP($frame-4,"esp",$j,4),$car1); # tp[j-1]=
  163. &psrlq ($car1,32);
  164. &lea ($j,&DWP(1,$j));
  165. &cmp ($j,$num);
  166. &jl (&label("1st"));
  167. &pmuludq($acc0,$mul0); # ap[num-1]*bp[0]
  168. &pmuludq($acc1,$mul1); # np[num-1]*m1
  169. &paddq ($car0,$acc0); # +=c0
  170. &paddq ($car1,$acc1); # +=c1
  171. &movq ($acc0,$car0);
  172. &pand ($acc0,$mask);
  173. &paddq ($car1,$acc0); # +=ap[num-1]*bp[0];
  174. &movd (&DWP($frame-4,"esp",$j,4),$car1); # tp[num-2]=
  175. &psrlq ($car0,32);
  176. &psrlq ($car1,32);
  177. &paddq ($car1,$car0);
  178. &movq (&QWP($frame,"esp",$num,4),$car1); # tp[num].tp[num-1]
  179. &inc ($i); # i++
  180. &set_label("outer");
  181. &xor ($j,$j); # j=0
  182. &movd ($mul0,&DWP(0,$bp,$i,4)); # bp[i]
  183. &movd ($mul1,&DWP(0,$ap)); # ap[0]
  184. &movd ($temp,&DWP($frame,"esp")); # tp[0]
  185. &movd ($car1,&DWP(0,$np)); # np[0]
  186. &pmuludq($mul1,$mul0); # ap[0]*bp[i]
  187. &paddq ($mul1,$temp); # +=tp[0]
  188. &movq ($acc0,$mul1);
  189. &movq ($car0,$mul1);
  190. &pand ($acc0,$mask);
  191. &pmuludq($mul1,$_n0q); # *=n0
  192. &pmuludq($car1,$mul1);
  193. &paddq ($car1,$acc0);
  194. &movd ($temp,&DWP($frame+4,"esp")); # tp[1]
  195. &movd ($acc1,&DWP(4,$np)); # np[1]
  196. &movd ($acc0,&DWP(4,$ap)); # ap[1]
  197. &psrlq ($car0,32);
  198. &psrlq ($car1,32);
  199. &paddq ($car0,$temp); # +=tp[1]
  200. &inc ($j); # j++
  201. &dec ($num);
  202. &set_label("inner");
  203. &pmuludq($acc0,$mul0); # ap[j]*bp[i]
  204. &pmuludq($acc1,$mul1); # np[j]*m1
  205. &paddq ($car0,$acc0); # +=c0
  206. &paddq ($car1,$acc1); # +=c1
  207. &movq ($acc0,$car0);
  208. &movd ($temp,&DWP($frame+4,"esp",$j,4));# tp[j+1]
  209. &pand ($acc0,$mask);
  210. &movd ($acc1,&DWP(4,$np,$j,4)); # np[j+1]
  211. &paddq ($car1,$acc0); # +=ap[j]*bp[i]+tp[j]
  212. &movd ($acc0,&DWP(4,$ap,$j,4)); # ap[j+1]
  213. &psrlq ($car0,32);
  214. &movd (&DWP($frame-4,"esp",$j,4),$car1);# tp[j-1]=
  215. &psrlq ($car1,32);
  216. &paddq ($car0,$temp); # +=tp[j+1]
  217. &dec ($num);
  218. &lea ($j,&DWP(1,$j)); # j++
  219. &jnz (&label("inner"));
  220. &mov ($num,$j);
  221. &pmuludq($acc0,$mul0); # ap[num-1]*bp[i]
  222. &pmuludq($acc1,$mul1); # np[num-1]*m1
  223. &paddq ($car0,$acc0); # +=c0
  224. &paddq ($car1,$acc1); # +=c1
  225. &movq ($acc0,$car0);
  226. &pand ($acc0,$mask);
  227. &paddq ($car1,$acc0); # +=ap[num-1]*bp[i]+tp[num-1]
  228. &movd (&DWP($frame-4,"esp",$j,4),$car1); # tp[num-2]=
  229. &psrlq ($car0,32);
  230. &psrlq ($car1,32);
  231. &movd ($temp,&DWP($frame+4,"esp",$num,4)); # += tp[num]
  232. &paddq ($car1,$car0);
  233. &paddq ($car1,$temp);
  234. &movq (&QWP($frame,"esp",$num,4),$car1); # tp[num].tp[num-1]
  235. &lea ($i,&DWP(1,$i)); # i++
  236. &cmp ($i,$num);
  237. &jle (&label("outer"));
  238. &emms (); # done with mmx bank
  239. &jmp (&label("common_tail"));
  240. &set_label("non_sse2",16);
  241. }
  242. if (0) {
  243. &mov ("esp",$_sp);
  244. &xor ("eax","eax"); # signal "not fast enough [yet]"
  245. &jmp (&label("just_leave"));
  246. # While the below code provides competitive performance for
  247. # all key lengths on modern Intel cores, it's still more
  248. # than 10% slower for 4096-bit key elsewhere:-( "Competitive"
  249. # means compared to the original integer-only assembler.
  250. # 512-bit RSA sign is better by ~40%, but that's about all
  251. # one can say about all CPUs...
  252. } else {
  253. $inp="esi"; # integer path uses these registers differently
  254. $word="edi";
  255. $carry="ebp";
  256. &mov ($inp,$_ap);
  257. &lea ($carry,&DWP(1,$num));
  258. &mov ($word,$_bp);
  259. &xor ($j,$j); # j=0
  260. &mov ("edx",$inp);
  261. &and ($carry,1); # see if num is even
  262. &sub ("edx",$word); # see if ap==bp
  263. &lea ("eax",&DWP(4,$word,$num,4)); # &bp[num]
  264. &or ($carry,"edx");
  265. &mov ($word,&DWP(0,$word)); # bp[0]
  266. &jz (&label("bn_sqr_mont"));
  267. &mov ($_bpend,"eax");
  268. &mov ("eax",&DWP(0,$inp));
  269. &xor ("edx","edx");
  270. &set_label("mull",16);
  271. &mov ($carry,"edx");
  272. &mul ($word); # ap[j]*bp[0]
  273. &add ($carry,"eax");
  274. &lea ($j,&DWP(1,$j));
  275. &adc ("edx",0);
  276. &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j+1]
  277. &cmp ($j,$num);
  278. &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]=
  279. &jl (&label("mull"));
  280. &mov ($carry,"edx");
  281. &mul ($word); # ap[num-1]*bp[0]
  282. &mov ($word,$_n0);
  283. &add ("eax",$carry);
  284. &mov ($inp,$_np);
  285. &adc ("edx",0);
  286. &imul ($word,&DWP($frame,"esp")); # n0*tp[0]
  287. &mov (&DWP($frame,"esp",$num,4),"eax"); # tp[num-1]=
  288. &xor ($j,$j);
  289. &mov (&DWP($frame+4,"esp",$num,4),"edx"); # tp[num]=
  290. &mov (&DWP($frame+8,"esp",$num,4),$j); # tp[num+1]=
  291. &mov ("eax",&DWP(0,$inp)); # np[0]
  292. &mul ($word); # np[0]*m
  293. &add ("eax",&DWP($frame,"esp")); # +=tp[0]
  294. &mov ("eax",&DWP(4,$inp)); # np[1]
  295. &adc ("edx",0);
  296. &inc ($j);
  297. &jmp (&label("2ndmadd"));
  298. &set_label("1stmadd",16);
  299. &mov ($carry,"edx");
  300. &mul ($word); # ap[j]*bp[i]
  301. &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j]
  302. &lea ($j,&DWP(1,$j));
  303. &adc ("edx",0);
  304. &add ($carry,"eax");
  305. &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j+1]
  306. &adc ("edx",0);
  307. &cmp ($j,$num);
  308. &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]=
  309. &jl (&label("1stmadd"));
  310. &mov ($carry,"edx");
  311. &mul ($word); # ap[num-1]*bp[i]
  312. &add ("eax",&DWP($frame,"esp",$num,4)); # +=tp[num-1]
  313. &mov ($word,$_n0);
  314. &adc ("edx",0);
  315. &mov ($inp,$_np);
  316. &add ($carry,"eax");
  317. &adc ("edx",0);
  318. &imul ($word,&DWP($frame,"esp")); # n0*tp[0]
  319. &xor ($j,$j);
  320. &add ("edx",&DWP($frame+4,"esp",$num,4)); # carry+=tp[num]
  321. &mov (&DWP($frame,"esp",$num,4),$carry); # tp[num-1]=
  322. &adc ($j,0);
  323. &mov ("eax",&DWP(0,$inp)); # np[0]
  324. &mov (&DWP($frame+4,"esp",$num,4),"edx"); # tp[num]=
  325. &mov (&DWP($frame+8,"esp",$num,4),$j); # tp[num+1]=
  326. &mul ($word); # np[0]*m
  327. &add ("eax",&DWP($frame,"esp")); # +=tp[0]
  328. &mov ("eax",&DWP(4,$inp)); # np[1]
  329. &adc ("edx",0);
  330. &mov ($j,1);
  331. &set_label("2ndmadd",16);
  332. &mov ($carry,"edx");
  333. &mul ($word); # np[j]*m
  334. &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j]
  335. &lea ($j,&DWP(1,$j));
  336. &adc ("edx",0);
  337. &add ($carry,"eax");
  338. &mov ("eax",&DWP(0,$inp,$j,4)); # np[j+1]
  339. &adc ("edx",0);
  340. &cmp ($j,$num);
  341. &mov (&DWP($frame-8,"esp",$j,4),$carry); # tp[j-1]=
  342. &jl (&label("2ndmadd"));
  343. &mov ($carry,"edx");
  344. &mul ($word); # np[j]*m
  345. &add ($carry,&DWP($frame,"esp",$num,4)); # +=tp[num-1]
  346. &adc ("edx",0);
  347. &add ($carry,"eax");
  348. &adc ("edx",0);
  349. &mov (&DWP($frame-4,"esp",$num,4),$carry); # tp[num-2]=
  350. &xor ("eax","eax");
  351. &mov ($j,$_bp); # &bp[i]
  352. &add ("edx",&DWP($frame+4,"esp",$num,4)); # carry+=tp[num]
  353. &adc ("eax",&DWP($frame+8,"esp",$num,4)); # +=tp[num+1]
  354. &lea ($j,&DWP(4,$j));
  355. &mov (&DWP($frame,"esp",$num,4),"edx"); # tp[num-1]=
  356. &cmp ($j,$_bpend);
  357. &mov (&DWP($frame+4,"esp",$num,4),"eax"); # tp[num]=
  358. &je (&label("common_tail"));
  359. &mov ($word,&DWP(0,$j)); # bp[i+1]
  360. &mov ($inp,$_ap);
  361. &mov ($_bp,$j); # &bp[++i]
  362. &xor ($j,$j);
  363. &xor ("edx","edx");
  364. &mov ("eax",&DWP(0,$inp));
  365. &jmp (&label("1stmadd"));
  366. &set_label("bn_sqr_mont",16);
  367. $sbit=$num;
  368. &mov ($_num,$num);
  369. &mov ($_bp,$j); # i=0
  370. &mov ("eax",$word); # ap[0]
  371. &mul ($word); # ap[0]*ap[0]
  372. &mov (&DWP($frame,"esp"),"eax"); # tp[0]=
  373. &mov ($sbit,"edx");
  374. &shr ("edx",1);
  375. &and ($sbit,1);
  376. &inc ($j);
  377. &set_label("sqr",16);
  378. &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j]
  379. &mov ($carry,"edx");
  380. &mul ($word); # ap[j]*ap[0]
  381. &add ("eax",$carry);
  382. &lea ($j,&DWP(1,$j));
  383. &adc ("edx",0);
  384. &lea ($carry,&DWP(0,$sbit,"eax",2));
  385. &shr ("eax",31);
  386. &cmp ($j,$_num);
  387. &mov ($sbit,"eax");
  388. &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]=
  389. &jl (&label("sqr"));
  390. &mov ("eax",&DWP(0,$inp,$j,4)); # ap[num-1]
  391. &mov ($carry,"edx");
  392. &mul ($word); # ap[num-1]*ap[0]
  393. &add ("eax",$carry);
  394. &mov ($word,$_n0);
  395. &adc ("edx",0);
  396. &mov ($inp,$_np);
  397. &lea ($carry,&DWP(0,$sbit,"eax",2));
  398. &imul ($word,&DWP($frame,"esp")); # n0*tp[0]
  399. &shr ("eax",31);
  400. &mov (&DWP($frame,"esp",$j,4),$carry); # tp[num-1]=
  401. &lea ($carry,&DWP(0,"eax","edx",2));
  402. &mov ("eax",&DWP(0,$inp)); # np[0]
  403. &shr ("edx",31);
  404. &mov (&DWP($frame+4,"esp",$j,4),$carry); # tp[num]=
  405. &mov (&DWP($frame+8,"esp",$j,4),"edx"); # tp[num+1]=
  406. &mul ($word); # np[0]*m
  407. &add ("eax",&DWP($frame,"esp")); # +=tp[0]
  408. &mov ($num,$j);
  409. &adc ("edx",0);
  410. &mov ("eax",&DWP(4,$inp)); # np[1]
  411. &mov ($j,1);
  412. &set_label("3rdmadd",16);
  413. &mov ($carry,"edx");
  414. &mul ($word); # np[j]*m
  415. &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j]
  416. &adc ("edx",0);
  417. &add ($carry,"eax");
  418. &mov ("eax",&DWP(4,$inp,$j,4)); # np[j+1]
  419. &adc ("edx",0);
  420. &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j-1]=
  421. &mov ($carry,"edx");
  422. &mul ($word); # np[j+1]*m
  423. &add ($carry,&DWP($frame+4,"esp",$j,4)); # +=tp[j+1]
  424. &lea ($j,&DWP(2,$j));
  425. &adc ("edx",0);
  426. &add ($carry,"eax");
  427. &mov ("eax",&DWP(0,$inp,$j,4)); # np[j+2]
  428. &adc ("edx",0);
  429. &cmp ($j,$num);
  430. &mov (&DWP($frame-8,"esp",$j,4),$carry); # tp[j]=
  431. &jl (&label("3rdmadd"));
  432. &mov ($carry,"edx");
  433. &mul ($word); # np[j]*m
  434. &add ($carry,&DWP($frame,"esp",$num,4)); # +=tp[num-1]
  435. &adc ("edx",0);
  436. &add ($carry,"eax");
  437. &adc ("edx",0);
  438. &mov (&DWP($frame-4,"esp",$num,4),$carry); # tp[num-2]=
  439. &mov ($j,$_bp); # i
  440. &xor ("eax","eax");
  441. &mov ($inp,$_ap);
  442. &add ("edx",&DWP($frame+4,"esp",$num,4)); # carry+=tp[num]
  443. &adc ("eax",&DWP($frame+8,"esp",$num,4)); # +=tp[num+1]
  444. &mov (&DWP($frame,"esp",$num,4),"edx"); # tp[num-1]=
  445. &cmp ($j,$num);
  446. &mov (&DWP($frame+4,"esp",$num,4),"eax"); # tp[num]=
  447. &je (&label("common_tail"));
  448. &mov ($word,&DWP(4,$inp,$j,4)); # ap[i]
  449. &lea ($j,&DWP(1,$j));
  450. &mov ("eax",$word);
  451. &mov ($_bp,$j); # ++i
  452. &mul ($word); # ap[i]*ap[i]
  453. &add ("eax",&DWP($frame,"esp",$j,4)); # +=tp[i]
  454. &adc ("edx",0);
  455. &mov (&DWP($frame,"esp",$j,4),"eax"); # tp[i]=
  456. &xor ($carry,$carry);
  457. &cmp ($j,$num);
  458. &lea ($j,&DWP(1,$j));
  459. &je (&label("sqrlast"));
  460. &mov ($sbit,"edx"); # zaps $num
  461. &shr ("edx",1);
  462. &and ($sbit,1);
  463. &set_label("sqradd",16);
  464. &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j]
  465. &mov ($carry,"edx");
  466. &mul ($word); # ap[j]*ap[i]
  467. &add ("eax",$carry);
  468. &lea ($carry,&DWP(0,"eax","eax"));
  469. &adc ("edx",0);
  470. &shr ("eax",31);
  471. &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j]
  472. &lea ($j,&DWP(1,$j));
  473. &adc ("eax",0);
  474. &add ($carry,$sbit);
  475. &adc ("eax",0);
  476. &cmp ($j,$_num);
  477. &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]=
  478. &mov ($sbit,"eax");
  479. &jle (&label("sqradd"));
  480. &mov ($carry,"edx");
  481. &add ("edx","edx");
  482. &shr ($carry,31);
  483. &add ("edx",$sbit);
  484. &adc ($carry,0);
  485. &set_label("sqrlast");
  486. &mov ($word,$_n0);
  487. &mov ($inp,$_np);
  488. &imul ($word,&DWP($frame,"esp")); # n0*tp[0]
  489. &add ("edx",&DWP($frame,"esp",$j,4)); # +=tp[num]
  490. &mov ("eax",&DWP(0,$inp)); # np[0]
  491. &adc ($carry,0);
  492. &mov (&DWP($frame,"esp",$j,4),"edx"); # tp[num]=
  493. &mov (&DWP($frame+4,"esp",$j,4),$carry); # tp[num+1]=
  494. &mul ($word); # np[0]*m
  495. &add ("eax",&DWP($frame,"esp")); # +=tp[0]
  496. &lea ($num,&DWP(-1,$j));
  497. &adc ("edx",0);
  498. &mov ($j,1);
  499. &mov ("eax",&DWP(4,$inp)); # np[1]
  500. &jmp (&label("3rdmadd"));
  501. }
  502. &set_label("common_tail",16);
  503. &mov ($np,$_np); # load modulus pointer
  504. &mov ($rp,$_rp); # load result pointer
  505. &lea ($tp,&DWP($frame,"esp")); # [$ap and $bp are zapped]
  506. &mov ("eax",&DWP(0,$tp)); # tp[0]
  507. &mov ($j,$num); # j=num-1
  508. &xor ($i,$i); # i=0 and clear CF!
  509. &set_label("sub",16);
  510. &sbb ("eax",&DWP(0,$np,$i,4));
  511. &mov (&DWP(0,$rp,$i,4),"eax"); # rp[i]=tp[i]-np[i]
  512. &dec ($j); # doesn't affect CF!
  513. &mov ("eax",&DWP(4,$tp,$i,4)); # tp[i+1]
  514. &lea ($i,&DWP(1,$i)); # i++
  515. &jge (&label("sub"));
  516. &sbb ("eax",0); # handle upmost overflow bit
  517. &mov ("edx",-1);
  518. &xor ("edx","eax");
  519. &jmp (&label("copy"));
  520. &set_label("copy",16); # conditional copy
  521. &mov ($tp,&DWP($frame,"esp",$num,4));
  522. &mov ($np,&DWP(0,$rp,$num,4));
  523. &mov (&DWP($frame,"esp",$num,4),$j); # zap temporary vector
  524. &and ($tp,"eax");
  525. &and ($np,"edx");
  526. &or ($np,$tp);
  527. &mov (&DWP(0,$rp,$num,4),$np);
  528. &dec ($num);
  529. &jge (&label("copy"));
  530. &mov ("esp",$_sp); # pull saved stack pointer
  531. &mov ("eax",1);
  532. &set_label("just_leave");
  533. &function_end("bn_mul_mont");
  534. &asciz("Montgomery Multiplication for x86, CRYPTOGAMS by <appro\@openssl.org>");
  535. &asm_finish();
  536. close STDOUT or die "error closing STDOUT: $!";