bn_mul.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* crypto/bn/bn_mul.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(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
  67. /*
  68. * Here follows specialised variants of bn_add_words() and bn_sub_words().
  69. * They have the property performing operations on arrays of different sizes.
  70. * The sizes of those arrays is expressed through cl, which is the common
  71. * length ( basicall, min(len(a),len(b)) ), and dl, which is the delta
  72. * between the two lengths, calculated as len(a)-len(b). All lengths are the
  73. * number of BN_ULONGs... For the operations that require a result array as
  74. * parameter, it must have the length cl+abs(dl). These functions should
  75. * probably end up in bn_asm.c as soon as there are assembler counterparts
  76. * for the systems that use assembler files.
  77. */
  78. BN_ULONG bn_sub_part_words(BN_ULONG *r,
  79. const BN_ULONG *a, const BN_ULONG *b,
  80. int cl, int dl)
  81. {
  82. BN_ULONG c, t;
  83. assert(cl >= 0);
  84. c = bn_sub_words(r, a, b, cl);
  85. if (dl == 0)
  86. return c;
  87. r += cl;
  88. a += cl;
  89. b += cl;
  90. if (dl < 0) {
  91. # ifdef BN_COUNT
  92. fprintf(stderr, " bn_sub_part_words %d + %d (dl < 0, c = %d)\n", cl,
  93. dl, c);
  94. # endif
  95. for (;;) {
  96. t = b[0];
  97. r[0] = (0 - t - c) & BN_MASK2;
  98. if (t != 0)
  99. c = 1;
  100. if (++dl >= 0)
  101. break;
  102. t = b[1];
  103. r[1] = (0 - t - c) & BN_MASK2;
  104. if (t != 0)
  105. c = 1;
  106. if (++dl >= 0)
  107. break;
  108. t = b[2];
  109. r[2] = (0 - t - c) & BN_MASK2;
  110. if (t != 0)
  111. c = 1;
  112. if (++dl >= 0)
  113. break;
  114. t = b[3];
  115. r[3] = (0 - t - c) & BN_MASK2;
  116. if (t != 0)
  117. c = 1;
  118. if (++dl >= 0)
  119. break;
  120. b += 4;
  121. r += 4;
  122. }
  123. } else {
  124. int save_dl = dl;
  125. # ifdef BN_COUNT
  126. fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c = %d)\n", cl,
  127. dl, c);
  128. # endif
  129. while (c) {
  130. t = a[0];
  131. r[0] = (t - c) & BN_MASK2;
  132. if (t != 0)
  133. c = 0;
  134. if (--dl <= 0)
  135. break;
  136. t = a[1];
  137. r[1] = (t - c) & BN_MASK2;
  138. if (t != 0)
  139. c = 0;
  140. if (--dl <= 0)
  141. break;
  142. t = a[2];
  143. r[2] = (t - c) & BN_MASK2;
  144. if (t != 0)
  145. c = 0;
  146. if (--dl <= 0)
  147. break;
  148. t = a[3];
  149. r[3] = (t - c) & BN_MASK2;
  150. if (t != 0)
  151. c = 0;
  152. if (--dl <= 0)
  153. break;
  154. save_dl = dl;
  155. a += 4;
  156. r += 4;
  157. }
  158. if (dl > 0) {
  159. # ifdef BN_COUNT
  160. fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c == 0)\n",
  161. cl, dl);
  162. # endif
  163. if (save_dl > dl) {
  164. switch (save_dl - dl) {
  165. case 1:
  166. r[1] = a[1];
  167. if (--dl <= 0)
  168. break;
  169. case 2:
  170. r[2] = a[2];
  171. if (--dl <= 0)
  172. break;
  173. case 3:
  174. r[3] = a[3];
  175. if (--dl <= 0)
  176. break;
  177. }
  178. a += 4;
  179. r += 4;
  180. }
  181. }
  182. if (dl > 0) {
  183. # ifdef BN_COUNT
  184. fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, copy)\n",
  185. cl, dl);
  186. # endif
  187. for (;;) {
  188. r[0] = a[0];
  189. if (--dl <= 0)
  190. break;
  191. r[1] = a[1];
  192. if (--dl <= 0)
  193. break;
  194. r[2] = a[2];
  195. if (--dl <= 0)
  196. break;
  197. r[3] = a[3];
  198. if (--dl <= 0)
  199. break;
  200. a += 4;
  201. r += 4;
  202. }
  203. }
  204. }
  205. return c;
  206. }
  207. #endif
  208. BN_ULONG bn_add_part_words(BN_ULONG *r,
  209. const BN_ULONG *a, const BN_ULONG *b,
  210. int cl, int dl)
  211. {
  212. BN_ULONG c, l, t;
  213. assert(cl >= 0);
  214. c = bn_add_words(r, a, b, cl);
  215. if (dl == 0)
  216. return c;
  217. r += cl;
  218. a += cl;
  219. b += cl;
  220. if (dl < 0) {
  221. int save_dl = dl;
  222. #ifdef BN_COUNT
  223. fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c = %d)\n", cl,
  224. dl, c);
  225. #endif
  226. while (c) {
  227. l = (c + b[0]) & BN_MASK2;
  228. c = (l < c);
  229. r[0] = l;
  230. if (++dl >= 0)
  231. break;
  232. l = (c + b[1]) & BN_MASK2;
  233. c = (l < c);
  234. r[1] = l;
  235. if (++dl >= 0)
  236. break;
  237. l = (c + b[2]) & BN_MASK2;
  238. c = (l < c);
  239. r[2] = l;
  240. if (++dl >= 0)
  241. break;
  242. l = (c + b[3]) & BN_MASK2;
  243. c = (l < c);
  244. r[3] = l;
  245. if (++dl >= 0)
  246. break;
  247. save_dl = dl;
  248. b += 4;
  249. r += 4;
  250. }
  251. if (dl < 0) {
  252. #ifdef BN_COUNT
  253. fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c == 0)\n",
  254. cl, dl);
  255. #endif
  256. if (save_dl < dl) {
  257. switch (dl - save_dl) {
  258. case 1:
  259. r[1] = b[1];
  260. if (++dl >= 0)
  261. break;
  262. case 2:
  263. r[2] = b[2];
  264. if (++dl >= 0)
  265. break;
  266. case 3:
  267. r[3] = b[3];
  268. if (++dl >= 0)
  269. break;
  270. }
  271. b += 4;
  272. r += 4;
  273. }
  274. }
  275. if (dl < 0) {
  276. #ifdef BN_COUNT
  277. fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, copy)\n",
  278. cl, dl);
  279. #endif
  280. for (;;) {
  281. r[0] = b[0];
  282. if (++dl >= 0)
  283. break;
  284. r[1] = b[1];
  285. if (++dl >= 0)
  286. break;
  287. r[2] = b[2];
  288. if (++dl >= 0)
  289. break;
  290. r[3] = b[3];
  291. if (++dl >= 0)
  292. break;
  293. b += 4;
  294. r += 4;
  295. }
  296. }
  297. } else {
  298. int save_dl = dl;
  299. #ifdef BN_COUNT
  300. fprintf(stderr, " bn_add_part_words %d + %d (dl > 0)\n", cl, dl);
  301. #endif
  302. while (c) {
  303. t = (a[0] + c) & BN_MASK2;
  304. c = (t < c);
  305. r[0] = t;
  306. if (--dl <= 0)
  307. break;
  308. t = (a[1] + c) & BN_MASK2;
  309. c = (t < c);
  310. r[1] = t;
  311. if (--dl <= 0)
  312. break;
  313. t = (a[2] + c) & BN_MASK2;
  314. c = (t < c);
  315. r[2] = t;
  316. if (--dl <= 0)
  317. break;
  318. t = (a[3] + c) & BN_MASK2;
  319. c = (t < c);
  320. r[3] = t;
  321. if (--dl <= 0)
  322. break;
  323. save_dl = dl;
  324. a += 4;
  325. r += 4;
  326. }
  327. #ifdef BN_COUNT
  328. fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, c == 0)\n", cl,
  329. dl);
  330. #endif
  331. if (dl > 0) {
  332. if (save_dl > dl) {
  333. switch (save_dl - dl) {
  334. case 1:
  335. r[1] = a[1];
  336. if (--dl <= 0)
  337. break;
  338. case 2:
  339. r[2] = a[2];
  340. if (--dl <= 0)
  341. break;
  342. case 3:
  343. r[3] = a[3];
  344. if (--dl <= 0)
  345. break;
  346. }
  347. a += 4;
  348. r += 4;
  349. }
  350. }
  351. if (dl > 0) {
  352. #ifdef BN_COUNT
  353. fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, copy)\n",
  354. cl, dl);
  355. #endif
  356. for (;;) {
  357. r[0] = a[0];
  358. if (--dl <= 0)
  359. break;
  360. r[1] = a[1];
  361. if (--dl <= 0)
  362. break;
  363. r[2] = a[2];
  364. if (--dl <= 0)
  365. break;
  366. r[3] = a[3];
  367. if (--dl <= 0)
  368. break;
  369. a += 4;
  370. r += 4;
  371. }
  372. }
  373. }
  374. return c;
  375. }
  376. #ifdef BN_RECURSION
  377. /*
  378. * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
  379. * Computer Programming, Vol. 2)
  380. */
  381. /*-
  382. * r is 2*n2 words in size,
  383. * a and b are both n2 words in size.
  384. * n2 must be a power of 2.
  385. * We multiply and return the result.
  386. * t must be 2*n2 words in size
  387. * We calculate
  388. * a[0]*b[0]
  389. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  390. * a[1]*b[1]
  391. */
  392. /* dnX may not be positive, but n2/2+dnX has to be */
  393. void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  394. int dna, int dnb, BN_ULONG *t)
  395. {
  396. int n = n2 / 2, c1, c2;
  397. int tna = n + dna, tnb = n + dnb;
  398. unsigned int neg, zero;
  399. BN_ULONG ln, lo, *p;
  400. # ifdef BN_COUNT
  401. fprintf(stderr, " bn_mul_recursive %d%+d * %d%+d\n", n2, dna, n2, dnb);
  402. # endif
  403. # ifdef BN_MUL_COMBA
  404. # if 0
  405. if (n2 == 4) {
  406. bn_mul_comba4(r, a, b);
  407. return;
  408. }
  409. # endif
  410. /*
  411. * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
  412. * [steve]
  413. */
  414. if (n2 == 8 && dna == 0 && dnb == 0) {
  415. bn_mul_comba8(r, a, b);
  416. return;
  417. }
  418. # endif /* BN_MUL_COMBA */
  419. /* Else do normal multiply */
  420. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  421. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  422. if ((dna + dnb) < 0)
  423. memset(&r[2 * n2 + dna + dnb], 0,
  424. sizeof(BN_ULONG) * -(dna + dnb));
  425. return;
  426. }
  427. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  428. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  429. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  430. zero = neg = 0;
  431. switch (c1 * 3 + c2) {
  432. case -4:
  433. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  434. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  435. break;
  436. case -3:
  437. zero = 1;
  438. break;
  439. case -2:
  440. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  441. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  442. neg = 1;
  443. break;
  444. case -1:
  445. case 0:
  446. case 1:
  447. zero = 1;
  448. break;
  449. case 2:
  450. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  451. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  452. neg = 1;
  453. break;
  454. case 3:
  455. zero = 1;
  456. break;
  457. case 4:
  458. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  459. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  460. break;
  461. }
  462. # ifdef BN_MUL_COMBA
  463. if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
  464. * extra args to do this well */
  465. if (!zero)
  466. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  467. else
  468. memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG));
  469. bn_mul_comba4(r, a, b);
  470. bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
  471. } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
  472. * take extra args to do
  473. * this well */
  474. if (!zero)
  475. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  476. else
  477. memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG));
  478. bn_mul_comba8(r, a, b);
  479. bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
  480. } else
  481. # endif /* BN_MUL_COMBA */
  482. {
  483. p = &(t[n2 * 2]);
  484. if (!zero)
  485. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  486. else
  487. memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  488. bn_mul_recursive(r, a, b, n, 0, 0, p);
  489. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
  490. }
  491. /*-
  492. * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  493. * r[10] holds (a[0]*b[0])
  494. * r[32] holds (b[1]*b[1])
  495. */
  496. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  497. if (neg) { /* if t[32] is negative */
  498. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  499. } else {
  500. /* Might have a carry */
  501. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  502. }
  503. /*-
  504. * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  505. * r[10] holds (a[0]*b[0])
  506. * r[32] holds (b[1]*b[1])
  507. * c1 holds the carry bits
  508. */
  509. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  510. if (c1) {
  511. p = &(r[n + n2]);
  512. lo = *p;
  513. ln = (lo + c1) & BN_MASK2;
  514. *p = ln;
  515. /*
  516. * The overflow will stop before we over write words we should not
  517. * overwrite
  518. */
  519. if (ln < (BN_ULONG)c1) {
  520. do {
  521. p++;
  522. lo = *p;
  523. ln = (lo + 1) & BN_MASK2;
  524. *p = ln;
  525. } while (ln == 0);
  526. }
  527. }
  528. }
  529. /*
  530. * n+tn is the word length t needs to be n*4 is size, as does r
  531. */
  532. /* tnX may not be negative but less than n */
  533. void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
  534. int tna, int tnb, BN_ULONG *t)
  535. {
  536. int i, j, n2 = n * 2;
  537. int c1, c2, neg;
  538. BN_ULONG ln, lo, *p;
  539. # ifdef BN_COUNT
  540. fprintf(stderr, " bn_mul_part_recursive (%d%+d) * (%d%+d)\n",
  541. n, tna, n, tnb);
  542. # endif
  543. if (n < 8) {
  544. bn_mul_normal(r, a, n + tna, b, n + tnb);
  545. return;
  546. }
  547. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  548. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  549. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  550. neg = 0;
  551. switch (c1 * 3 + c2) {
  552. case -4:
  553. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  554. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  555. break;
  556. case -3:
  557. /* break; */
  558. case -2:
  559. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  560. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  561. neg = 1;
  562. break;
  563. case -1:
  564. case 0:
  565. case 1:
  566. /* break; */
  567. case 2:
  568. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  569. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  570. neg = 1;
  571. break;
  572. case 3:
  573. /* break; */
  574. case 4:
  575. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  576. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  577. break;
  578. }
  579. /*
  580. * The zero case isn't yet implemented here. The speedup would probably
  581. * be negligible.
  582. */
  583. # if 0
  584. if (n == 4) {
  585. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  586. bn_mul_comba4(r, a, b);
  587. bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
  588. memset(&(r[n2 + tn * 2]), 0, sizeof(BN_ULONG) * (n2 - tn * 2));
  589. } else
  590. # endif
  591. if (n == 8) {
  592. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  593. bn_mul_comba8(r, a, b);
  594. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  595. memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  596. } else {
  597. p = &(t[n2 * 2]);
  598. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  599. bn_mul_recursive(r, a, b, n, 0, 0, p);
  600. i = n / 2;
  601. /*
  602. * If there is only a bottom half to the number, just do it
  603. */
  604. if (tna > tnb)
  605. j = tna - i;
  606. else
  607. j = tnb - i;
  608. if (j == 0) {
  609. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
  610. i, tna - i, tnb - i, p);
  611. memset(&(r[n2 + i * 2]), 0, sizeof(BN_ULONG) * (n2 - i * 2));
  612. } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
  613. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
  614. i, tna - i, tnb - i, p);
  615. memset(&(r[n2 + tna + tnb]), 0,
  616. sizeof(BN_ULONG) * (n2 - tna - tnb));
  617. } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
  618. memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2);
  619. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
  620. && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  621. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  622. } else {
  623. for (;;) {
  624. i /= 2;
  625. /*
  626. * these simplified conditions work exclusively because
  627. * difference between tna and tnb is 1 or 0
  628. */
  629. if (i < tna || i < tnb) {
  630. bn_mul_part_recursive(&(r[n2]),
  631. &(a[n]), &(b[n]),
  632. i, tna - i, tnb - i, p);
  633. break;
  634. } else if (i == tna || i == tnb) {
  635. bn_mul_recursive(&(r[n2]),
  636. &(a[n]), &(b[n]),
  637. i, tna - i, tnb - i, p);
  638. break;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. /*-
  645. * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  646. * r[10] holds (a[0]*b[0])
  647. * r[32] holds (b[1]*b[1])
  648. */
  649. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  650. if (neg) { /* if t[32] is negative */
  651. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  652. } else {
  653. /* Might have a carry */
  654. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  655. }
  656. /*-
  657. * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  658. * r[10] holds (a[0]*b[0])
  659. * r[32] holds (b[1]*b[1])
  660. * c1 holds the carry bits
  661. */
  662. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  663. if (c1) {
  664. p = &(r[n + n2]);
  665. lo = *p;
  666. ln = (lo + c1) & BN_MASK2;
  667. *p = ln;
  668. /*
  669. * The overflow will stop before we over write words we should not
  670. * overwrite
  671. */
  672. if (ln < (BN_ULONG)c1) {
  673. do {
  674. p++;
  675. lo = *p;
  676. ln = (lo + 1) & BN_MASK2;
  677. *p = ln;
  678. } while (ln == 0);
  679. }
  680. }
  681. }
  682. /*-
  683. * a and b must be the same size, which is n2.
  684. * r needs to be n2 words and t needs to be n2*2
  685. */
  686. void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  687. BN_ULONG *t)
  688. {
  689. int n = n2 / 2;
  690. # ifdef BN_COUNT
  691. fprintf(stderr, " bn_mul_low_recursive %d * %d\n", n2, n2);
  692. # endif
  693. bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
  694. if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
  695. bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
  696. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  697. bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
  698. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  699. } else {
  700. bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
  701. bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
  702. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  703. bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
  704. }
  705. }
  706. /*-
  707. * a and b must be the same size, which is n2.
  708. * r needs to be n2 words and t needs to be n2*2
  709. * l is the low words of the output.
  710. * t needs to be n2*3
  711. */
  712. void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2,
  713. BN_ULONG *t)
  714. {
  715. int i, n;
  716. int c1, c2;
  717. int neg, oneg, zero;
  718. BN_ULONG ll, lc, *lp, *mp;
  719. # ifdef BN_COUNT
  720. fprintf(stderr, " bn_mul_high %d * %d\n", n2, n2);
  721. # endif
  722. n = n2 / 2;
  723. /* Calculate (al-ah)*(bh-bl) */
  724. neg = zero = 0;
  725. c1 = bn_cmp_words(&(a[0]), &(a[n]), n);
  726. c2 = bn_cmp_words(&(b[n]), &(b[0]), n);
  727. switch (c1 * 3 + c2) {
  728. case -4:
  729. bn_sub_words(&(r[0]), &(a[n]), &(a[0]), n);
  730. bn_sub_words(&(r[n]), &(b[0]), &(b[n]), n);
  731. break;
  732. case -3:
  733. zero = 1;
  734. break;
  735. case -2:
  736. bn_sub_words(&(r[0]), &(a[n]), &(a[0]), n);
  737. bn_sub_words(&(r[n]), &(b[n]), &(b[0]), n);
  738. neg = 1;
  739. break;
  740. case -1:
  741. case 0:
  742. case 1:
  743. zero = 1;
  744. break;
  745. case 2:
  746. bn_sub_words(&(r[0]), &(a[0]), &(a[n]), n);
  747. bn_sub_words(&(r[n]), &(b[0]), &(b[n]), n);
  748. neg = 1;
  749. break;
  750. case 3:
  751. zero = 1;
  752. break;
  753. case 4:
  754. bn_sub_words(&(r[0]), &(a[0]), &(a[n]), n);
  755. bn_sub_words(&(r[n]), &(b[n]), &(b[0]), n);
  756. break;
  757. }
  758. oneg = neg;
  759. /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */
  760. /* r[10] = (a[1]*b[1]) */
  761. # ifdef BN_MUL_COMBA
  762. if (n == 8) {
  763. bn_mul_comba8(&(t[0]), &(r[0]), &(r[n]));
  764. bn_mul_comba8(r, &(a[n]), &(b[n]));
  765. } else
  766. # endif
  767. {
  768. bn_mul_recursive(&(t[0]), &(r[0]), &(r[n]), n, 0, 0, &(t[n2]));
  769. bn_mul_recursive(r, &(a[n]), &(b[n]), n, 0, 0, &(t[n2]));
  770. }
  771. /*-
  772. * s0 == low(al*bl)
  773. * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl)
  774. * We know s0 and s1 so the only unknown is high(al*bl)
  775. * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl))
  776. * high(al*bl) == s1 - (r[0]+l[0]+t[0])
  777. */
  778. if (l != NULL) {
  779. lp = &(t[n2 + n]);
  780. c1 = (int)(bn_add_words(lp, &(r[0]), &(l[0]), n));
  781. } else {
  782. c1 = 0;
  783. lp = &(r[0]);
  784. }
  785. if (neg)
  786. neg = (int)(bn_sub_words(&(t[n2]), lp, &(t[0]), n));
  787. else {
  788. bn_add_words(&(t[n2]), lp, &(t[0]), n);
  789. neg = 0;
  790. }
  791. if (l != NULL) {
  792. bn_sub_words(&(t[n2 + n]), &(l[n]), &(t[n2]), n);
  793. } else {
  794. lp = &(t[n2 + n]);
  795. mp = &(t[n2]);
  796. for (i = 0; i < n; i++)
  797. lp[i] = ((~mp[i]) + 1) & BN_MASK2;
  798. }
  799. /*-
  800. * s[0] = low(al*bl)
  801. * t[3] = high(al*bl)
  802. * t[10] = (a[0]-a[1])*(b[1]-b[0]) neg is the sign
  803. * r[10] = (a[1]*b[1])
  804. */
  805. /*-
  806. * R[10] = al*bl
  807. * R[21] = al*bl + ah*bh + (a[0]-a[1])*(b[1]-b[0])
  808. * R[32] = ah*bh
  809. */
  810. /*-
  811. * R[1]=t[3]+l[0]+r[0](+-)t[0] (have carry/borrow)
  812. * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow)
  813. * R[3]=r[1]+(carry/borrow)
  814. */
  815. if (l != NULL) {
  816. lp = &(t[n2]);
  817. c1 = (int)(bn_add_words(lp, &(t[n2 + n]), &(l[0]), n));
  818. } else {
  819. lp = &(t[n2 + n]);
  820. c1 = 0;
  821. }
  822. c1 += (int)(bn_add_words(&(t[n2]), lp, &(r[0]), n));
  823. if (oneg)
  824. c1 -= (int)(bn_sub_words(&(t[n2]), &(t[n2]), &(t[0]), n));
  825. else
  826. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), &(t[0]), n));
  827. c2 = (int)(bn_add_words(&(r[0]), &(r[0]), &(t[n2 + n]), n));
  828. c2 += (int)(bn_add_words(&(r[0]), &(r[0]), &(r[n]), n));
  829. if (oneg)
  830. c2 -= (int)(bn_sub_words(&(r[0]), &(r[0]), &(t[n]), n));
  831. else
  832. c2 += (int)(bn_add_words(&(r[0]), &(r[0]), &(t[n]), n));
  833. if (c1 != 0) { /* Add starting at r[0], could be +ve or -ve */
  834. i = 0;
  835. if (c1 > 0) {
  836. lc = c1;
  837. do {
  838. ll = (r[i] + lc) & BN_MASK2;
  839. r[i++] = ll;
  840. lc = (lc > ll);
  841. } while (lc);
  842. } else {
  843. lc = -c1;
  844. do {
  845. ll = r[i];
  846. r[i++] = (ll - lc) & BN_MASK2;
  847. lc = (lc > ll);
  848. } while (lc);
  849. }
  850. }
  851. if (c2 != 0) { /* Add starting at r[1] */
  852. i = n;
  853. if (c2 > 0) {
  854. lc = c2;
  855. do {
  856. ll = (r[i] + lc) & BN_MASK2;
  857. r[i++] = ll;
  858. lc = (lc > ll);
  859. } while (lc);
  860. } else {
  861. lc = -c2;
  862. do {
  863. ll = r[i];
  864. r[i++] = (ll - lc) & BN_MASK2;
  865. lc = (lc > ll);
  866. } while (lc);
  867. }
  868. }
  869. }
  870. #endif /* BN_RECURSION */
  871. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  872. {
  873. int ret = 0;
  874. int top, al, bl;
  875. BIGNUM *rr;
  876. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  877. int i;
  878. #endif
  879. #ifdef BN_RECURSION
  880. BIGNUM *t = NULL;
  881. int j = 0, k;
  882. #endif
  883. #ifdef BN_COUNT
  884. fprintf(stderr, "BN_mul %d * %d\n", a->top, b->top);
  885. #endif
  886. bn_check_top(a);
  887. bn_check_top(b);
  888. bn_check_top(r);
  889. al = a->top;
  890. bl = b->top;
  891. if ((al == 0) || (bl == 0)) {
  892. BN_zero(r);
  893. return (1);
  894. }
  895. top = al + bl;
  896. BN_CTX_start(ctx);
  897. if ((r == a) || (r == b)) {
  898. if ((rr = BN_CTX_get(ctx)) == NULL)
  899. goto err;
  900. } else
  901. rr = r;
  902. rr->neg = a->neg ^ b->neg;
  903. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  904. i = al - bl;
  905. #endif
  906. #ifdef BN_MUL_COMBA
  907. if (i == 0) {
  908. # if 0
  909. if (al == 4) {
  910. if (bn_wexpand(rr, 8) == NULL)
  911. goto err;
  912. rr->top = 8;
  913. bn_mul_comba4(rr->d, a->d, b->d);
  914. goto end;
  915. }
  916. # endif
  917. if (al == 8) {
  918. if (bn_wexpand(rr, 16) == NULL)
  919. goto err;
  920. rr->top = 16;
  921. bn_mul_comba8(rr->d, a->d, b->d);
  922. goto end;
  923. }
  924. }
  925. #endif /* BN_MUL_COMBA */
  926. #ifdef BN_RECURSION
  927. if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
  928. if (i >= -1 && i <= 1) {
  929. /*
  930. * Find out the power of two lower or equal to the longest of the
  931. * two numbers
  932. */
  933. if (i >= 0) {
  934. j = BN_num_bits_word((BN_ULONG)al);
  935. }
  936. if (i == -1) {
  937. j = BN_num_bits_word((BN_ULONG)bl);
  938. }
  939. j = 1 << (j - 1);
  940. assert(j <= al || j <= bl);
  941. k = j + j;
  942. t = BN_CTX_get(ctx);
  943. if (t == NULL)
  944. goto err;
  945. if (al > j || bl > j) {
  946. if (bn_wexpand(t, k * 4) == NULL)
  947. goto err;
  948. if (bn_wexpand(rr, k * 4) == NULL)
  949. goto err;
  950. bn_mul_part_recursive(rr->d, a->d, b->d,
  951. j, al - j, bl - j, t->d);
  952. } else { /* al <= j || bl <= j */
  953. if (bn_wexpand(t, k * 2) == NULL)
  954. goto err;
  955. if (bn_wexpand(rr, k * 2) == NULL)
  956. goto err;
  957. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  958. }
  959. rr->top = top;
  960. goto end;
  961. }
  962. # if 0
  963. if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  964. BIGNUM *tmp_bn = (BIGNUM *)b;
  965. if (bn_wexpand(tmp_bn, al) == NULL)
  966. goto err;
  967. tmp_bn->d[bl] = 0;
  968. bl++;
  969. i--;
  970. } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {
  971. BIGNUM *tmp_bn = (BIGNUM *)a;
  972. if (bn_wexpand(tmp_bn, bl) == NULL)
  973. goto err;
  974. tmp_bn->d[al] = 0;
  975. al++;
  976. i++;
  977. }
  978. if (i == 0) {
  979. /* symmetric and > 4 */
  980. /* 16 or larger */
  981. j = BN_num_bits_word((BN_ULONG)al);
  982. j = 1 << (j - 1);
  983. k = j + j;
  984. t = BN_CTX_get(ctx);
  985. if (al == j) { /* exact multiple */
  986. if (bn_wexpand(t, k * 2) == NULL)
  987. goto err;
  988. if (bn_wexpand(rr, k * 2) == NULL)
  989. goto err;
  990. bn_mul_recursive(rr->d, a->d, b->d, al, t->d);
  991. } else {
  992. if (bn_wexpand(t, k * 4) == NULL)
  993. goto err;
  994. if (bn_wexpand(rr, k * 4) == NULL)
  995. goto err;
  996. bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);
  997. }
  998. rr->top = top;
  999. goto end;
  1000. }
  1001. # endif
  1002. }
  1003. #endif /* BN_RECURSION */
  1004. if (bn_wexpand(rr, top) == NULL)
  1005. goto err;
  1006. rr->top = top;
  1007. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  1008. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  1009. end:
  1010. #endif
  1011. bn_correct_top(rr);
  1012. if (r != rr)
  1013. BN_copy(r, rr);
  1014. ret = 1;
  1015. err:
  1016. bn_check_top(r);
  1017. BN_CTX_end(ctx);
  1018. return (ret);
  1019. }
  1020. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
  1021. {
  1022. BN_ULONG *rr;
  1023. #ifdef BN_COUNT
  1024. fprintf(stderr, " bn_mul_normal %d * %d\n", na, nb);
  1025. #endif
  1026. if (na < nb) {
  1027. int itmp;
  1028. BN_ULONG *ltmp;
  1029. itmp = na;
  1030. na = nb;
  1031. nb = itmp;
  1032. ltmp = a;
  1033. a = b;
  1034. b = ltmp;
  1035. }
  1036. rr = &(r[na]);
  1037. if (nb <= 0) {
  1038. (void)bn_mul_words(r, a, na, 0);
  1039. return;
  1040. } else
  1041. rr[0] = bn_mul_words(r, a, na, b[0]);
  1042. for (;;) {
  1043. if (--nb <= 0)
  1044. return;
  1045. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  1046. if (--nb <= 0)
  1047. return;
  1048. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  1049. if (--nb <= 0)
  1050. return;
  1051. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  1052. if (--nb <= 0)
  1053. return;
  1054. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  1055. rr += 4;
  1056. r += 4;
  1057. b += 4;
  1058. }
  1059. }
  1060. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
  1061. {
  1062. #ifdef BN_COUNT
  1063. fprintf(stderr, " bn_mul_low_normal %d * %d\n", n, n);
  1064. #endif
  1065. bn_mul_words(r, a, n, b[0]);
  1066. for (;;) {
  1067. if (--n <= 0)
  1068. return;
  1069. bn_mul_add_words(&(r[1]), a, n, b[1]);
  1070. if (--n <= 0)
  1071. return;
  1072. bn_mul_add_words(&(r[2]), a, n, b[2]);
  1073. if (--n <= 0)
  1074. return;
  1075. bn_mul_add_words(&(r[3]), a, n, b[3]);
  1076. if (--n <= 0)
  1077. return;
  1078. bn_mul_add_words(&(r[4]), a, n, b[4]);
  1079. r += 4;
  1080. b += 4;
  1081. }
  1082. }