mo-586.pl 16 KB

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