bn_asm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* crypto/bn/bn_asm.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #ifndef BN_DEBUG
  59. # undef NDEBUG /* avoid conflicting definitions */
  60. # define NDEBUG
  61. #endif
  62. #include <stdio.h>
  63. #include <assert.h>
  64. #include "cryptlib.h"
  65. #include "bn_lcl.h"
  66. #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
  67. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  68. {
  69. BN_ULONG c1=0;
  70. assert(num >= 0);
  71. if (num <= 0) return(c1);
  72. #ifndef OPENSSL_SMALL_FOOTPRINT
  73. while (num&~3)
  74. {
  75. mul_add(rp[0],ap[0],w,c1);
  76. mul_add(rp[1],ap[1],w,c1);
  77. mul_add(rp[2],ap[2],w,c1);
  78. mul_add(rp[3],ap[3],w,c1);
  79. ap+=4; rp+=4; num-=4;
  80. }
  81. #endif
  82. while (num)
  83. {
  84. mul_add(rp[0],ap[0],w,c1);
  85. ap++; rp++; num--;
  86. }
  87. return(c1);
  88. }
  89. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  90. {
  91. BN_ULONG c1=0;
  92. assert(num >= 0);
  93. if (num <= 0) return(c1);
  94. #ifndef OPENSSL_SMALL_FOOTPRINT
  95. while (num&~3)
  96. {
  97. mul(rp[0],ap[0],w,c1);
  98. mul(rp[1],ap[1],w,c1);
  99. mul(rp[2],ap[2],w,c1);
  100. mul(rp[3],ap[3],w,c1);
  101. ap+=4; rp+=4; num-=4;
  102. }
  103. #endif
  104. while (num)
  105. {
  106. mul(rp[0],ap[0],w,c1);
  107. ap++; rp++; num--;
  108. }
  109. return(c1);
  110. }
  111. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  112. {
  113. assert(n >= 0);
  114. if (n <= 0) return;
  115. #ifndef OPENSSL_SMALL_FOOTPRINT
  116. while (n&~3)
  117. {
  118. sqr(r[0],r[1],a[0]);
  119. sqr(r[2],r[3],a[1]);
  120. sqr(r[4],r[5],a[2]);
  121. sqr(r[6],r[7],a[3]);
  122. a+=4; r+=8; n-=4;
  123. }
  124. #endif
  125. while (n)
  126. {
  127. sqr(r[0],r[1],a[0]);
  128. a++; r+=2; n--;
  129. }
  130. }
  131. #else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
  132. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  133. {
  134. BN_ULONG c=0;
  135. BN_ULONG bl,bh;
  136. assert(num >= 0);
  137. if (num <= 0) return((BN_ULONG)0);
  138. bl=LBITS(w);
  139. bh=HBITS(w);
  140. #ifndef OPENSSL_SMALL_FOOTPRINT
  141. while (num&~3)
  142. {
  143. mul_add(rp[0],ap[0],bl,bh,c);
  144. mul_add(rp[1],ap[1],bl,bh,c);
  145. mul_add(rp[2],ap[2],bl,bh,c);
  146. mul_add(rp[3],ap[3],bl,bh,c);
  147. ap+=4; rp+=4; num-=4;
  148. }
  149. #endif
  150. while (num)
  151. {
  152. mul_add(rp[0],ap[0],bl,bh,c);
  153. ap++; rp++; num--;
  154. }
  155. return(c);
  156. }
  157. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  158. {
  159. BN_ULONG carry=0;
  160. BN_ULONG bl,bh;
  161. assert(num >= 0);
  162. if (num <= 0) return((BN_ULONG)0);
  163. bl=LBITS(w);
  164. bh=HBITS(w);
  165. #ifndef OPENSSL_SMALL_FOOTPRINT
  166. while (num&~3)
  167. {
  168. mul(rp[0],ap[0],bl,bh,carry);
  169. mul(rp[1],ap[1],bl,bh,carry);
  170. mul(rp[2],ap[2],bl,bh,carry);
  171. mul(rp[3],ap[3],bl,bh,carry);
  172. ap+=4; rp+=4; num-=4;
  173. }
  174. #endif
  175. while (num)
  176. {
  177. mul(rp[0],ap[0],bl,bh,carry);
  178. ap++; rp++; num--;
  179. }
  180. return(carry);
  181. }
  182. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  183. {
  184. assert(n >= 0);
  185. if (n <= 0) return;
  186. #ifndef OPENSSL_SMALL_FOOTPRINT
  187. while (n&~3)
  188. {
  189. sqr64(r[0],r[1],a[0]);
  190. sqr64(r[2],r[3],a[1]);
  191. sqr64(r[4],r[5],a[2]);
  192. sqr64(r[6],r[7],a[3]);
  193. a+=4; r+=8; n-=4;
  194. }
  195. #endif
  196. while (n)
  197. {
  198. sqr64(r[0],r[1],a[0]);
  199. a++; r+=2; n--;
  200. }
  201. }
  202. #endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
  203. #if defined(BN_LLONG) && defined(BN_DIV2W)
  204. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  205. {
  206. return((BN_ULONG)(((((BN_ULLONG)h)<<BN_BITS2)|l)/(BN_ULLONG)d));
  207. }
  208. #else
  209. /* Divide h,l by d and return the result. */
  210. /* I need to test this some more :-( */
  211. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  212. {
  213. BN_ULONG dh,dl,q,ret=0,th,tl,t;
  214. int i,count=2;
  215. if (d == 0) return(BN_MASK2);
  216. i=BN_num_bits_word(d);
  217. assert((i == BN_BITS2) || (h <= (BN_ULONG)1<<i));
  218. i=BN_BITS2-i;
  219. if (h >= d) h-=d;
  220. if (i)
  221. {
  222. d<<=i;
  223. h=(h<<i)|(l>>(BN_BITS2-i));
  224. l<<=i;
  225. }
  226. dh=(d&BN_MASK2h)>>BN_BITS4;
  227. dl=(d&BN_MASK2l);
  228. for (;;)
  229. {
  230. if ((h>>BN_BITS4) == dh)
  231. q=BN_MASK2l;
  232. else
  233. q=h/dh;
  234. th=q*dh;
  235. tl=dl*q;
  236. for (;;)
  237. {
  238. t=h-th;
  239. if ((t&BN_MASK2h) ||
  240. ((tl) <= (
  241. (t<<BN_BITS4)|
  242. ((l&BN_MASK2h)>>BN_BITS4))))
  243. break;
  244. q--;
  245. th-=dh;
  246. tl-=dl;
  247. }
  248. t=(tl>>BN_BITS4);
  249. tl=(tl<<BN_BITS4)&BN_MASK2h;
  250. th+=t;
  251. if (l < tl) th++;
  252. l-=tl;
  253. if (h < th)
  254. {
  255. h+=d;
  256. q--;
  257. }
  258. h-=th;
  259. if (--count == 0) break;
  260. ret=q<<BN_BITS4;
  261. h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
  262. l=(l&BN_MASK2l)<<BN_BITS4;
  263. }
  264. ret|=q;
  265. return(ret);
  266. }
  267. #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
  268. #ifdef BN_LLONG
  269. BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  270. {
  271. BN_ULLONG ll=0;
  272. assert(n >= 0);
  273. if (n <= 0) return((BN_ULONG)0);
  274. #ifndef OPENSSL_SMALL_FOOTPRINT
  275. while (n&~3)
  276. {
  277. ll+=(BN_ULLONG)a[0]+b[0];
  278. r[0]=(BN_ULONG)ll&BN_MASK2;
  279. ll>>=BN_BITS2;
  280. ll+=(BN_ULLONG)a[1]+b[1];
  281. r[1]=(BN_ULONG)ll&BN_MASK2;
  282. ll>>=BN_BITS2;
  283. ll+=(BN_ULLONG)a[2]+b[2];
  284. r[2]=(BN_ULONG)ll&BN_MASK2;
  285. ll>>=BN_BITS2;
  286. ll+=(BN_ULLONG)a[3]+b[3];
  287. r[3]=(BN_ULONG)ll&BN_MASK2;
  288. ll>>=BN_BITS2;
  289. a+=4; b+=4; r+=4; n-=4;
  290. }
  291. #endif
  292. while (n)
  293. {
  294. ll+=(BN_ULLONG)a[0]+b[0];
  295. r[0]=(BN_ULONG)ll&BN_MASK2;
  296. ll>>=BN_BITS2;
  297. a++; b++; r++; n--;
  298. }
  299. return((BN_ULONG)ll);
  300. }
  301. #else /* !BN_LLONG */
  302. BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  303. {
  304. BN_ULONG c,l,t;
  305. assert(n >= 0);
  306. if (n <= 0) return((BN_ULONG)0);
  307. c=0;
  308. #ifndef OPENSSL_SMALL_FOOTPRINT
  309. while (n&~3)
  310. {
  311. t=a[0];
  312. t=(t+c)&BN_MASK2;
  313. c=(t < c);
  314. l=(t+b[0])&BN_MASK2;
  315. c+=(l < t);
  316. r[0]=l;
  317. t=a[1];
  318. t=(t+c)&BN_MASK2;
  319. c=(t < c);
  320. l=(t+b[1])&BN_MASK2;
  321. c+=(l < t);
  322. r[1]=l;
  323. t=a[2];
  324. t=(t+c)&BN_MASK2;
  325. c=(t < c);
  326. l=(t+b[2])&BN_MASK2;
  327. c+=(l < t);
  328. r[2]=l;
  329. t=a[3];
  330. t=(t+c)&BN_MASK2;
  331. c=(t < c);
  332. l=(t+b[3])&BN_MASK2;
  333. c+=(l < t);
  334. r[3]=l;
  335. a+=4; b+=4; r+=4; n-=4;
  336. }
  337. #endif
  338. while(n)
  339. {
  340. t=a[0];
  341. t=(t+c)&BN_MASK2;
  342. c=(t < c);
  343. l=(t+b[0])&BN_MASK2;
  344. c+=(l < t);
  345. r[0]=l;
  346. a++; b++; r++; n--;
  347. }
  348. return((BN_ULONG)c);
  349. }
  350. #endif /* !BN_LLONG */
  351. BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  352. {
  353. BN_ULONG t1,t2;
  354. int c=0;
  355. assert(n >= 0);
  356. if (n <= 0) return((BN_ULONG)0);
  357. #ifndef OPENSSL_SMALL_FOOTPRINT
  358. while (n&~3)
  359. {
  360. t1=a[0]; t2=b[0];
  361. r[0]=(t1-t2-c)&BN_MASK2;
  362. if (t1 != t2) c=(t1 < t2);
  363. t1=a[1]; t2=b[1];
  364. r[1]=(t1-t2-c)&BN_MASK2;
  365. if (t1 != t2) c=(t1 < t2);
  366. t1=a[2]; t2=b[2];
  367. r[2]=(t1-t2-c)&BN_MASK2;
  368. if (t1 != t2) c=(t1 < t2);
  369. t1=a[3]; t2=b[3];
  370. r[3]=(t1-t2-c)&BN_MASK2;
  371. if (t1 != t2) c=(t1 < t2);
  372. a+=4; b+=4; r+=4; n-=4;
  373. }
  374. #endif
  375. while (n)
  376. {
  377. t1=a[0]; t2=b[0];
  378. r[0]=(t1-t2-c)&BN_MASK2;
  379. if (t1 != t2) c=(t1 < t2);
  380. a++; b++; r++; n--;
  381. }
  382. return(c);
  383. }
  384. #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
  385. #undef bn_mul_comba8
  386. #undef bn_mul_comba4
  387. #undef bn_sqr_comba8
  388. #undef bn_sqr_comba4
  389. /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
  390. /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
  391. /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
  392. /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
  393. #ifdef BN_LLONG
  394. #define mul_add_c(a,b,c0,c1,c2) \
  395. t=(BN_ULLONG)a*b; \
  396. t1=(BN_ULONG)Lw(t); \
  397. t2=(BN_ULONG)Hw(t); \
  398. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  399. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  400. #define mul_add_c2(a,b,c0,c1,c2) \
  401. t=(BN_ULLONG)a*b; \
  402. tt=(t+t)&BN_MASK; \
  403. if (tt < t) c2++; \
  404. t1=(BN_ULONG)Lw(tt); \
  405. t2=(BN_ULONG)Hw(tt); \
  406. c0=(c0+t1)&BN_MASK2; \
  407. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  408. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  409. #define sqr_add_c(a,i,c0,c1,c2) \
  410. t=(BN_ULLONG)a[i]*a[i]; \
  411. t1=(BN_ULONG)Lw(t); \
  412. t2=(BN_ULONG)Hw(t); \
  413. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  414. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  415. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  416. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  417. #elif defined(BN_UMULT_LOHI)
  418. #define mul_add_c(a,b,c0,c1,c2) { \
  419. BN_ULONG ta=(a),tb=(b); \
  420. BN_UMULT_LOHI(t1,t2,ta,tb); \
  421. c0 += t1; t2 += (c0<t1)?1:0; \
  422. c1 += t2; c2 += (c1<t2)?1:0; \
  423. }
  424. #define mul_add_c2(a,b,c0,c1,c2) { \
  425. BN_ULONG ta=(a),tb=(b),t0; \
  426. BN_UMULT_LOHI(t0,t1,ta,tb); \
  427. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  428. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  429. c0 += t1; t2 += (c0<t1)?1:0; \
  430. c1 += t2; c2 += (c1<t2)?1:0; \
  431. }
  432. #define sqr_add_c(a,i,c0,c1,c2) { \
  433. BN_ULONG ta=(a)[i]; \
  434. BN_UMULT_LOHI(t1,t2,ta,ta); \
  435. c0 += t1; t2 += (c0<t1)?1:0; \
  436. c1 += t2; c2 += (c1<t2)?1:0; \
  437. }
  438. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  439. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  440. #elif defined(BN_UMULT_HIGH)
  441. #define mul_add_c(a,b,c0,c1,c2) { \
  442. BN_ULONG ta=(a),tb=(b); \
  443. t1 = ta * tb; \
  444. t2 = BN_UMULT_HIGH(ta,tb); \
  445. c0 += t1; t2 += (c0<t1)?1:0; \
  446. c1 += t2; c2 += (c1<t2)?1:0; \
  447. }
  448. #define mul_add_c2(a,b,c0,c1,c2) { \
  449. BN_ULONG ta=(a),tb=(b),t0; \
  450. t1 = BN_UMULT_HIGH(ta,tb); \
  451. t0 = ta * tb; \
  452. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  453. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  454. c0 += t1; t2 += (c0<t1)?1:0; \
  455. c1 += t2; c2 += (c1<t2)?1:0; \
  456. }
  457. #define sqr_add_c(a,i,c0,c1,c2) { \
  458. BN_ULONG ta=(a)[i]; \
  459. t1 = ta * ta; \
  460. t2 = BN_UMULT_HIGH(ta,ta); \
  461. c0 += t1; t2 += (c0<t1)?1:0; \
  462. c1 += t2; c2 += (c1<t2)?1:0; \
  463. }
  464. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  465. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  466. #else /* !BN_LLONG */
  467. #define mul_add_c(a,b,c0,c1,c2) \
  468. t1=LBITS(a); t2=HBITS(a); \
  469. bl=LBITS(b); bh=HBITS(b); \
  470. mul64(t1,t2,bl,bh); \
  471. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  472. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  473. #define mul_add_c2(a,b,c0,c1,c2) \
  474. t1=LBITS(a); t2=HBITS(a); \
  475. bl=LBITS(b); bh=HBITS(b); \
  476. mul64(t1,t2,bl,bh); \
  477. if (t2 & BN_TBIT) c2++; \
  478. t2=(t2+t2)&BN_MASK2; \
  479. if (t1 & BN_TBIT) t2++; \
  480. t1=(t1+t1)&BN_MASK2; \
  481. c0=(c0+t1)&BN_MASK2; \
  482. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  483. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  484. #define sqr_add_c(a,i,c0,c1,c2) \
  485. sqr64(t1,t2,(a)[i]); \
  486. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  487. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  488. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  489. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  490. #endif /* !BN_LLONG */
  491. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  492. {
  493. #ifdef BN_LLONG
  494. BN_ULLONG t;
  495. #else
  496. BN_ULONG bl,bh;
  497. #endif
  498. BN_ULONG t1,t2;
  499. BN_ULONG c1,c2,c3;
  500. c1=0;
  501. c2=0;
  502. c3=0;
  503. mul_add_c(a[0],b[0],c1,c2,c3);
  504. r[0]=c1;
  505. c1=0;
  506. mul_add_c(a[0],b[1],c2,c3,c1);
  507. mul_add_c(a[1],b[0],c2,c3,c1);
  508. r[1]=c2;
  509. c2=0;
  510. mul_add_c(a[2],b[0],c3,c1,c2);
  511. mul_add_c(a[1],b[1],c3,c1,c2);
  512. mul_add_c(a[0],b[2],c3,c1,c2);
  513. r[2]=c3;
  514. c3=0;
  515. mul_add_c(a[0],b[3],c1,c2,c3);
  516. mul_add_c(a[1],b[2],c1,c2,c3);
  517. mul_add_c(a[2],b[1],c1,c2,c3);
  518. mul_add_c(a[3],b[0],c1,c2,c3);
  519. r[3]=c1;
  520. c1=0;
  521. mul_add_c(a[4],b[0],c2,c3,c1);
  522. mul_add_c(a[3],b[1],c2,c3,c1);
  523. mul_add_c(a[2],b[2],c2,c3,c1);
  524. mul_add_c(a[1],b[3],c2,c3,c1);
  525. mul_add_c(a[0],b[4],c2,c3,c1);
  526. r[4]=c2;
  527. c2=0;
  528. mul_add_c(a[0],b[5],c3,c1,c2);
  529. mul_add_c(a[1],b[4],c3,c1,c2);
  530. mul_add_c(a[2],b[3],c3,c1,c2);
  531. mul_add_c(a[3],b[2],c3,c1,c2);
  532. mul_add_c(a[4],b[1],c3,c1,c2);
  533. mul_add_c(a[5],b[0],c3,c1,c2);
  534. r[5]=c3;
  535. c3=0;
  536. mul_add_c(a[6],b[0],c1,c2,c3);
  537. mul_add_c(a[5],b[1],c1,c2,c3);
  538. mul_add_c(a[4],b[2],c1,c2,c3);
  539. mul_add_c(a[3],b[3],c1,c2,c3);
  540. mul_add_c(a[2],b[4],c1,c2,c3);
  541. mul_add_c(a[1],b[5],c1,c2,c3);
  542. mul_add_c(a[0],b[6],c1,c2,c3);
  543. r[6]=c1;
  544. c1=0;
  545. mul_add_c(a[0],b[7],c2,c3,c1);
  546. mul_add_c(a[1],b[6],c2,c3,c1);
  547. mul_add_c(a[2],b[5],c2,c3,c1);
  548. mul_add_c(a[3],b[4],c2,c3,c1);
  549. mul_add_c(a[4],b[3],c2,c3,c1);
  550. mul_add_c(a[5],b[2],c2,c3,c1);
  551. mul_add_c(a[6],b[1],c2,c3,c1);
  552. mul_add_c(a[7],b[0],c2,c3,c1);
  553. r[7]=c2;
  554. c2=0;
  555. mul_add_c(a[7],b[1],c3,c1,c2);
  556. mul_add_c(a[6],b[2],c3,c1,c2);
  557. mul_add_c(a[5],b[3],c3,c1,c2);
  558. mul_add_c(a[4],b[4],c3,c1,c2);
  559. mul_add_c(a[3],b[5],c3,c1,c2);
  560. mul_add_c(a[2],b[6],c3,c1,c2);
  561. mul_add_c(a[1],b[7],c3,c1,c2);
  562. r[8]=c3;
  563. c3=0;
  564. mul_add_c(a[2],b[7],c1,c2,c3);
  565. mul_add_c(a[3],b[6],c1,c2,c3);
  566. mul_add_c(a[4],b[5],c1,c2,c3);
  567. mul_add_c(a[5],b[4],c1,c2,c3);
  568. mul_add_c(a[6],b[3],c1,c2,c3);
  569. mul_add_c(a[7],b[2],c1,c2,c3);
  570. r[9]=c1;
  571. c1=0;
  572. mul_add_c(a[7],b[3],c2,c3,c1);
  573. mul_add_c(a[6],b[4],c2,c3,c1);
  574. mul_add_c(a[5],b[5],c2,c3,c1);
  575. mul_add_c(a[4],b[6],c2,c3,c1);
  576. mul_add_c(a[3],b[7],c2,c3,c1);
  577. r[10]=c2;
  578. c2=0;
  579. mul_add_c(a[4],b[7],c3,c1,c2);
  580. mul_add_c(a[5],b[6],c3,c1,c2);
  581. mul_add_c(a[6],b[5],c3,c1,c2);
  582. mul_add_c(a[7],b[4],c3,c1,c2);
  583. r[11]=c3;
  584. c3=0;
  585. mul_add_c(a[7],b[5],c1,c2,c3);
  586. mul_add_c(a[6],b[6],c1,c2,c3);
  587. mul_add_c(a[5],b[7],c1,c2,c3);
  588. r[12]=c1;
  589. c1=0;
  590. mul_add_c(a[6],b[7],c2,c3,c1);
  591. mul_add_c(a[7],b[6],c2,c3,c1);
  592. r[13]=c2;
  593. c2=0;
  594. mul_add_c(a[7],b[7],c3,c1,c2);
  595. r[14]=c3;
  596. r[15]=c1;
  597. }
  598. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  599. {
  600. #ifdef BN_LLONG
  601. BN_ULLONG t;
  602. #else
  603. BN_ULONG bl,bh;
  604. #endif
  605. BN_ULONG t1,t2;
  606. BN_ULONG c1,c2,c3;
  607. c1=0;
  608. c2=0;
  609. c3=0;
  610. mul_add_c(a[0],b[0],c1,c2,c3);
  611. r[0]=c1;
  612. c1=0;
  613. mul_add_c(a[0],b[1],c2,c3,c1);
  614. mul_add_c(a[1],b[0],c2,c3,c1);
  615. r[1]=c2;
  616. c2=0;
  617. mul_add_c(a[2],b[0],c3,c1,c2);
  618. mul_add_c(a[1],b[1],c3,c1,c2);
  619. mul_add_c(a[0],b[2],c3,c1,c2);
  620. r[2]=c3;
  621. c3=0;
  622. mul_add_c(a[0],b[3],c1,c2,c3);
  623. mul_add_c(a[1],b[2],c1,c2,c3);
  624. mul_add_c(a[2],b[1],c1,c2,c3);
  625. mul_add_c(a[3],b[0],c1,c2,c3);
  626. r[3]=c1;
  627. c1=0;
  628. mul_add_c(a[3],b[1],c2,c3,c1);
  629. mul_add_c(a[2],b[2],c2,c3,c1);
  630. mul_add_c(a[1],b[3],c2,c3,c1);
  631. r[4]=c2;
  632. c2=0;
  633. mul_add_c(a[2],b[3],c3,c1,c2);
  634. mul_add_c(a[3],b[2],c3,c1,c2);
  635. r[5]=c3;
  636. c3=0;
  637. mul_add_c(a[3],b[3],c1,c2,c3);
  638. r[6]=c1;
  639. r[7]=c2;
  640. }
  641. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  642. {
  643. #ifdef BN_LLONG
  644. BN_ULLONG t,tt;
  645. #else
  646. BN_ULONG bl,bh;
  647. #endif
  648. BN_ULONG t1,t2;
  649. BN_ULONG c1,c2,c3;
  650. c1=0;
  651. c2=0;
  652. c3=0;
  653. sqr_add_c(a,0,c1,c2,c3);
  654. r[0]=c1;
  655. c1=0;
  656. sqr_add_c2(a,1,0,c2,c3,c1);
  657. r[1]=c2;
  658. c2=0;
  659. sqr_add_c(a,1,c3,c1,c2);
  660. sqr_add_c2(a,2,0,c3,c1,c2);
  661. r[2]=c3;
  662. c3=0;
  663. sqr_add_c2(a,3,0,c1,c2,c3);
  664. sqr_add_c2(a,2,1,c1,c2,c3);
  665. r[3]=c1;
  666. c1=0;
  667. sqr_add_c(a,2,c2,c3,c1);
  668. sqr_add_c2(a,3,1,c2,c3,c1);
  669. sqr_add_c2(a,4,0,c2,c3,c1);
  670. r[4]=c2;
  671. c2=0;
  672. sqr_add_c2(a,5,0,c3,c1,c2);
  673. sqr_add_c2(a,4,1,c3,c1,c2);
  674. sqr_add_c2(a,3,2,c3,c1,c2);
  675. r[5]=c3;
  676. c3=0;
  677. sqr_add_c(a,3,c1,c2,c3);
  678. sqr_add_c2(a,4,2,c1,c2,c3);
  679. sqr_add_c2(a,5,1,c1,c2,c3);
  680. sqr_add_c2(a,6,0,c1,c2,c3);
  681. r[6]=c1;
  682. c1=0;
  683. sqr_add_c2(a,7,0,c2,c3,c1);
  684. sqr_add_c2(a,6,1,c2,c3,c1);
  685. sqr_add_c2(a,5,2,c2,c3,c1);
  686. sqr_add_c2(a,4,3,c2,c3,c1);
  687. r[7]=c2;
  688. c2=0;
  689. sqr_add_c(a,4,c3,c1,c2);
  690. sqr_add_c2(a,5,3,c3,c1,c2);
  691. sqr_add_c2(a,6,2,c3,c1,c2);
  692. sqr_add_c2(a,7,1,c3,c1,c2);
  693. r[8]=c3;
  694. c3=0;
  695. sqr_add_c2(a,7,2,c1,c2,c3);
  696. sqr_add_c2(a,6,3,c1,c2,c3);
  697. sqr_add_c2(a,5,4,c1,c2,c3);
  698. r[9]=c1;
  699. c1=0;
  700. sqr_add_c(a,5,c2,c3,c1);
  701. sqr_add_c2(a,6,4,c2,c3,c1);
  702. sqr_add_c2(a,7,3,c2,c3,c1);
  703. r[10]=c2;
  704. c2=0;
  705. sqr_add_c2(a,7,4,c3,c1,c2);
  706. sqr_add_c2(a,6,5,c3,c1,c2);
  707. r[11]=c3;
  708. c3=0;
  709. sqr_add_c(a,6,c1,c2,c3);
  710. sqr_add_c2(a,7,5,c1,c2,c3);
  711. r[12]=c1;
  712. c1=0;
  713. sqr_add_c2(a,7,6,c2,c3,c1);
  714. r[13]=c2;
  715. c2=0;
  716. sqr_add_c(a,7,c3,c1,c2);
  717. r[14]=c3;
  718. r[15]=c1;
  719. }
  720. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  721. {
  722. #ifdef BN_LLONG
  723. BN_ULLONG t,tt;
  724. #else
  725. BN_ULONG bl,bh;
  726. #endif
  727. BN_ULONG t1,t2;
  728. BN_ULONG c1,c2,c3;
  729. c1=0;
  730. c2=0;
  731. c3=0;
  732. sqr_add_c(a,0,c1,c2,c3);
  733. r[0]=c1;
  734. c1=0;
  735. sqr_add_c2(a,1,0,c2,c3,c1);
  736. r[1]=c2;
  737. c2=0;
  738. sqr_add_c(a,1,c3,c1,c2);
  739. sqr_add_c2(a,2,0,c3,c1,c2);
  740. r[2]=c3;
  741. c3=0;
  742. sqr_add_c2(a,3,0,c1,c2,c3);
  743. sqr_add_c2(a,2,1,c1,c2,c3);
  744. r[3]=c1;
  745. c1=0;
  746. sqr_add_c(a,2,c2,c3,c1);
  747. sqr_add_c2(a,3,1,c2,c3,c1);
  748. r[4]=c2;
  749. c2=0;
  750. sqr_add_c2(a,3,2,c3,c1,c2);
  751. r[5]=c3;
  752. c3=0;
  753. sqr_add_c(a,3,c1,c2,c3);
  754. r[6]=c1;
  755. r[7]=c2;
  756. }
  757. #ifdef OPENSSL_NO_ASM
  758. #ifdef OPENSSL_BN_ASM_MONT
  759. #include <alloca.h>
  760. /*
  761. * This is essentially reference implementation, which may or may not
  762. * result in performance improvement. E.g. on IA-32 this routine was
  763. * observed to give 40% faster rsa1024 private key operations and 10%
  764. * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
  765. * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
  766. * reference implementation, one to be used as starting point for
  767. * platform-specific assembler. Mentioned numbers apply to compiler
  768. * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
  769. * can vary not only from platform to platform, but even for compiler
  770. * versions. Assembler vs. assembler improvement coefficients can
  771. * [and are known to] differ and are to be documented elsewhere.
  772. */
  773. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0p, int num)
  774. {
  775. BN_ULONG c0,c1,ml,*tp,n0;
  776. #ifdef mul64
  777. BN_ULONG mh;
  778. #endif
  779. volatile BN_ULONG *vp;
  780. int i=0,j;
  781. #if 0 /* template for platform-specific implementation */
  782. if (ap==bp) return bn_sqr_mont(rp,ap,np,n0p,num);
  783. #endif
  784. vp = tp = alloca((num+2)*sizeof(BN_ULONG));
  785. n0 = *n0p;
  786. tp[num] = bn_mul_words(tp,ap,num,bp[0]);
  787. tp[num+1] = 0;
  788. goto enter;
  789. for(i=0;i<num;i++)
  790. {
  791. c0 = bn_mul_add_words(tp,ap,num,bp[i]);
  792. c1 = (tp[num] + c0)&BN_MASK2;
  793. tp[num] = c1;
  794. tp[num+1] = (c1<c0?1:0);
  795. enter:
  796. c1 = tp[0];
  797. ml = (c1*n0)&BN_MASK2;
  798. c0 = 0;
  799. #ifdef mul64
  800. mh = HBITS(ml);
  801. ml = LBITS(ml);
  802. mul_add(c1,np[0],ml,mh,c0);
  803. #else
  804. mul_add(c1,ml,np[0],c0);
  805. #endif
  806. for(j=1;j<num;j++)
  807. {
  808. c1 = tp[j];
  809. #ifdef mul64
  810. mul_add(c1,np[j],ml,mh,c0);
  811. #else
  812. mul_add(c1,ml,np[j],c0);
  813. #endif
  814. tp[j-1] = c1&BN_MASK2;
  815. }
  816. c1 = (tp[num] + c0)&BN_MASK2;
  817. tp[num-1] = c1;
  818. tp[num] = tp[num+1] + (c1<c0?1:0);
  819. }
  820. if (tp[num]!=0 || tp[num-1]>=np[num-1])
  821. {
  822. c0 = bn_sub_words(rp,tp,np,num);
  823. if (tp[num]!=0 || c0==0)
  824. {
  825. for(i=0;i<num+2;i++) vp[i] = 0;
  826. return 1;
  827. }
  828. }
  829. for(i=0;i<num;i++) rp[i] = tp[i], vp[i] = 0;
  830. vp[num] = 0;
  831. vp[num+1] = 0;
  832. return 1;
  833. }
  834. #else
  835. /*
  836. * Return value of 0 indicates that multiplication/convolution was not
  837. * performed to signal the caller to fall down to alternative/original
  838. * code-path.
  839. */
  840. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num)
  841. { return 0; }
  842. #endif /* OPENSSL_BN_ASM_MONT */
  843. #endif
  844. #else /* !BN_MUL_COMBA */
  845. /* hmm... is it faster just to do a multiply? */
  846. #undef bn_sqr_comba4
  847. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  848. {
  849. BN_ULONG t[8];
  850. bn_sqr_normal(r,a,4,t);
  851. }
  852. #undef bn_sqr_comba8
  853. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  854. {
  855. BN_ULONG t[16];
  856. bn_sqr_normal(r,a,8,t);
  857. }
  858. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  859. {
  860. r[4]=bn_mul_words( &(r[0]),a,4,b[0]);
  861. r[5]=bn_mul_add_words(&(r[1]),a,4,b[1]);
  862. r[6]=bn_mul_add_words(&(r[2]),a,4,b[2]);
  863. r[7]=bn_mul_add_words(&(r[3]),a,4,b[3]);
  864. }
  865. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  866. {
  867. r[ 8]=bn_mul_words( &(r[0]),a,8,b[0]);
  868. r[ 9]=bn_mul_add_words(&(r[1]),a,8,b[1]);
  869. r[10]=bn_mul_add_words(&(r[2]),a,8,b[2]);
  870. r[11]=bn_mul_add_words(&(r[3]),a,8,b[3]);
  871. r[12]=bn_mul_add_words(&(r[4]),a,8,b[4]);
  872. r[13]=bn_mul_add_words(&(r[5]),a,8,b[5]);
  873. r[14]=bn_mul_add_words(&(r[6]),a,8,b[6]);
  874. r[15]=bn_mul_add_words(&(r[7]),a,8,b[7]);
  875. }
  876. #ifdef OPENSSL_NO_ASM
  877. #ifdef OPENSSL_BN_ASM_MONT
  878. #include <alloca.h>
  879. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0p, int num)
  880. {
  881. BN_ULONG c0,c1,*tp,n0=*n0p;
  882. volatile BN_ULONG *vp;
  883. int i=0,j;
  884. vp = tp = alloca((num+2)*sizeof(BN_ULONG));
  885. for(i=0;i<=num;i++) tp[i]=0;
  886. for(i=0;i<num;i++)
  887. {
  888. c0 = bn_mul_add_words(tp,ap,num,bp[i]);
  889. c1 = (tp[num] + c0)&BN_MASK2;
  890. tp[num] = c1;
  891. tp[num+1] = (c1<c0?1:0);
  892. c0 = bn_mul_add_words(tp,np,num,tp[0]*n0);
  893. c1 = (tp[num] + c0)&BN_MASK2;
  894. tp[num] = c1;
  895. tp[num+1] += (c1<c0?1:0);
  896. for(j=0;j<=num;j++) tp[j]=tp[j+1];
  897. }
  898. if (tp[num]!=0 || tp[num-1]>=np[num-1])
  899. {
  900. c0 = bn_sub_words(rp,tp,np,num);
  901. if (tp[num]!=0 || c0==0)
  902. {
  903. for(i=0;i<num+2;i++) vp[i] = 0;
  904. return 1;
  905. }
  906. }
  907. for(i=0;i<num;i++) rp[i] = tp[i], vp[i] = 0;
  908. vp[num] = 0;
  909. vp[num+1] = 0;
  910. return 1;
  911. }
  912. #else
  913. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num)
  914. { return 0; }
  915. #endif /* OPENSSL_BN_ASM_MONT */
  916. #endif
  917. #endif /* !BN_MUL_COMBA */