x86-mont.pl 17 KB

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