2
0

x86-mont.pl 16 KB

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