bn_mul.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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 = bn_mul_fixed_top(r, a, b, ctx);
  874. bn_correct_top(r);
  875. bn_check_top(r);
  876. return ret;
  877. }
  878. int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  879. {
  880. int ret = 0;
  881. int top, al, bl;
  882. BIGNUM *rr;
  883. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  884. int i;
  885. #endif
  886. #ifdef BN_RECURSION
  887. BIGNUM *t = NULL;
  888. int j = 0, k;
  889. #endif
  890. #ifdef BN_COUNT
  891. fprintf(stderr, "BN_mul %d * %d\n", a->top, b->top);
  892. #endif
  893. bn_check_top(a);
  894. bn_check_top(b);
  895. bn_check_top(r);
  896. al = a->top;
  897. bl = b->top;
  898. if ((al == 0) || (bl == 0)) {
  899. BN_zero(r);
  900. return (1);
  901. }
  902. top = al + bl;
  903. BN_CTX_start(ctx);
  904. if ((r == a) || (r == b)) {
  905. if ((rr = BN_CTX_get(ctx)) == NULL)
  906. goto err;
  907. } else
  908. rr = r;
  909. rr->neg = a->neg ^ b->neg;
  910. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  911. i = al - bl;
  912. #endif
  913. #ifdef BN_MUL_COMBA
  914. if (i == 0) {
  915. # if 0
  916. if (al == 4) {
  917. if (bn_wexpand(rr, 8) == NULL)
  918. goto err;
  919. rr->top = 8;
  920. bn_mul_comba4(rr->d, a->d, b->d);
  921. goto end;
  922. }
  923. # endif
  924. if (al == 8) {
  925. if (bn_wexpand(rr, 16) == NULL)
  926. goto err;
  927. rr->top = 16;
  928. bn_mul_comba8(rr->d, a->d, b->d);
  929. goto end;
  930. }
  931. }
  932. #endif /* BN_MUL_COMBA */
  933. #ifdef BN_RECURSION
  934. if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
  935. if (i >= -1 && i <= 1) {
  936. /*
  937. * Find out the power of two lower or equal to the longest of the
  938. * two numbers
  939. */
  940. if (i >= 0) {
  941. j = BN_num_bits_word((BN_ULONG)al);
  942. }
  943. if (i == -1) {
  944. j = BN_num_bits_word((BN_ULONG)bl);
  945. }
  946. j = 1 << (j - 1);
  947. assert(j <= al || j <= bl);
  948. k = j + j;
  949. t = BN_CTX_get(ctx);
  950. if (t == NULL)
  951. goto err;
  952. if (al > j || bl > j) {
  953. if (bn_wexpand(t, k * 4) == NULL)
  954. goto err;
  955. if (bn_wexpand(rr, k * 4) == NULL)
  956. goto err;
  957. bn_mul_part_recursive(rr->d, a->d, b->d,
  958. j, al - j, bl - j, t->d);
  959. } else { /* al <= j || bl <= j */
  960. if (bn_wexpand(t, k * 2) == NULL)
  961. goto err;
  962. if (bn_wexpand(rr, k * 2) == NULL)
  963. goto err;
  964. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  965. }
  966. rr->top = top;
  967. goto end;
  968. }
  969. }
  970. #endif /* BN_RECURSION */
  971. if (bn_wexpand(rr, top) == NULL)
  972. goto err;
  973. rr->top = top;
  974. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  975. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  976. end:
  977. #endif
  978. rr->flags |= BN_FLG_FIXED_TOP;
  979. if (r != rr && BN_copy(r, rr) == NULL)
  980. goto err;
  981. ret = 1;
  982. err:
  983. bn_check_top(r);
  984. BN_CTX_end(ctx);
  985. return (ret);
  986. }
  987. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
  988. {
  989. BN_ULONG *rr;
  990. #ifdef BN_COUNT
  991. fprintf(stderr, " bn_mul_normal %d * %d\n", na, nb);
  992. #endif
  993. if (na < nb) {
  994. int itmp;
  995. BN_ULONG *ltmp;
  996. itmp = na;
  997. na = nb;
  998. nb = itmp;
  999. ltmp = a;
  1000. a = b;
  1001. b = ltmp;
  1002. }
  1003. rr = &(r[na]);
  1004. if (nb <= 0) {
  1005. (void)bn_mul_words(r, a, na, 0);
  1006. return;
  1007. } else
  1008. rr[0] = bn_mul_words(r, a, na, b[0]);
  1009. for (;;) {
  1010. if (--nb <= 0)
  1011. return;
  1012. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  1013. if (--nb <= 0)
  1014. return;
  1015. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  1016. if (--nb <= 0)
  1017. return;
  1018. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  1019. if (--nb <= 0)
  1020. return;
  1021. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  1022. rr += 4;
  1023. r += 4;
  1024. b += 4;
  1025. }
  1026. }
  1027. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
  1028. {
  1029. #ifdef BN_COUNT
  1030. fprintf(stderr, " bn_mul_low_normal %d * %d\n", n, n);
  1031. #endif
  1032. bn_mul_words(r, a, n, b[0]);
  1033. for (;;) {
  1034. if (--n <= 0)
  1035. return;
  1036. bn_mul_add_words(&(r[1]), a, n, b[1]);
  1037. if (--n <= 0)
  1038. return;
  1039. bn_mul_add_words(&(r[2]), a, n, b[2]);
  1040. if (--n <= 0)
  1041. return;
  1042. bn_mul_add_words(&(r[3]), a, n, b[3]);
  1043. if (--n <= 0)
  1044. return;
  1045. bn_mul_add_words(&(r[4]), a, n, b[4]);
  1046. r += 4;
  1047. b += 4;
  1048. }
  1049. }