mul.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bn.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include "internal.h"
  60. #define BN_MUL_RECURSIVE_SIZE_NORMAL 16
  61. #define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
  62. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) {
  63. BN_ULONG *rr;
  64. if (na < nb) {
  65. int itmp;
  66. BN_ULONG *ltmp;
  67. itmp = na;
  68. na = nb;
  69. nb = itmp;
  70. ltmp = a;
  71. a = b;
  72. b = ltmp;
  73. }
  74. rr = &(r[na]);
  75. if (nb <= 0) {
  76. (void)bn_mul_words(r, a, na, 0);
  77. return;
  78. } else {
  79. rr[0] = bn_mul_words(r, a, na, b[0]);
  80. }
  81. for (;;) {
  82. if (--nb <= 0) {
  83. return;
  84. }
  85. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  86. if (--nb <= 0) {
  87. return;
  88. }
  89. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  90. if (--nb <= 0) {
  91. return;
  92. }
  93. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  94. if (--nb <= 0) {
  95. return;
  96. }
  97. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  98. rr += 4;
  99. r += 4;
  100. b += 4;
  101. }
  102. }
  103. #if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
  104. /* Here follows specialised variants of bn_add_words() and bn_sub_words(). They
  105. * have the property performing operations on arrays of different sizes. The
  106. * sizes of those arrays is expressed through cl, which is the common length (
  107. * basicall, min(len(a),len(b)) ), and dl, which is the delta between the two
  108. * lengths, calculated as len(a)-len(b). All lengths are the number of
  109. * BN_ULONGs... For the operations that require a result array as parameter,
  110. * it must have the length cl+abs(dl). These functions should probably end up
  111. * in bn_asm.c as soon as there are assembler counterparts for the systems that
  112. * use assembler files. */
  113. static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
  114. const BN_ULONG *b, int cl, int dl) {
  115. BN_ULONG c, t;
  116. assert(cl >= 0);
  117. c = bn_sub_words(r, a, b, cl);
  118. if (dl == 0) {
  119. return c;
  120. }
  121. r += cl;
  122. a += cl;
  123. b += cl;
  124. if (dl < 0) {
  125. for (;;) {
  126. t = b[0];
  127. r[0] = (0 - t - c) & BN_MASK2;
  128. if (t != 0) {
  129. c = 1;
  130. }
  131. if (++dl >= 0) {
  132. break;
  133. }
  134. t = b[1];
  135. r[1] = (0 - t - c) & BN_MASK2;
  136. if (t != 0) {
  137. c = 1;
  138. }
  139. if (++dl >= 0) {
  140. break;
  141. }
  142. t = b[2];
  143. r[2] = (0 - t - c) & BN_MASK2;
  144. if (t != 0) {
  145. c = 1;
  146. }
  147. if (++dl >= 0) {
  148. break;
  149. }
  150. t = b[3];
  151. r[3] = (0 - t - c) & BN_MASK2;
  152. if (t != 0) {
  153. c = 1;
  154. }
  155. if (++dl >= 0) {
  156. break;
  157. }
  158. b += 4;
  159. r += 4;
  160. }
  161. } else {
  162. int save_dl = dl;
  163. while (c) {
  164. t = a[0];
  165. r[0] = (t - c) & BN_MASK2;
  166. if (t != 0) {
  167. c = 0;
  168. }
  169. if (--dl <= 0) {
  170. break;
  171. }
  172. t = a[1];
  173. r[1] = (t - c) & BN_MASK2;
  174. if (t != 0) {
  175. c = 0;
  176. }
  177. if (--dl <= 0) {
  178. break;
  179. }
  180. t = a[2];
  181. r[2] = (t - c) & BN_MASK2;
  182. if (t != 0) {
  183. c = 0;
  184. }
  185. if (--dl <= 0) {
  186. break;
  187. }
  188. t = a[3];
  189. r[3] = (t - c) & BN_MASK2;
  190. if (t != 0) {
  191. c = 0;
  192. }
  193. if (--dl <= 0) {
  194. break;
  195. }
  196. save_dl = dl;
  197. a += 4;
  198. r += 4;
  199. }
  200. if (dl > 0) {
  201. if (save_dl > dl) {
  202. switch (save_dl - dl) {
  203. case 1:
  204. r[1] = a[1];
  205. if (--dl <= 0) {
  206. break;
  207. }
  208. case 2:
  209. r[2] = a[2];
  210. if (--dl <= 0) {
  211. break;
  212. }
  213. case 3:
  214. r[3] = a[3];
  215. if (--dl <= 0) {
  216. break;
  217. }
  218. }
  219. a += 4;
  220. r += 4;
  221. }
  222. }
  223. if (dl > 0) {
  224. for (;;) {
  225. r[0] = a[0];
  226. if (--dl <= 0) {
  227. break;
  228. }
  229. r[1] = a[1];
  230. if (--dl <= 0) {
  231. break;
  232. }
  233. r[2] = a[2];
  234. if (--dl <= 0) {
  235. break;
  236. }
  237. r[3] = a[3];
  238. if (--dl <= 0) {
  239. break;
  240. }
  241. a += 4;
  242. r += 4;
  243. }
  244. }
  245. }
  246. return c;
  247. }
  248. #else
  249. /* On other platforms the function is defined in asm. */
  250. BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  251. int cl, int dl);
  252. #endif
  253. /* Karatsuba recursive multiplication algorithm
  254. * (cf. Knuth, The Art of Computer Programming, Vol. 2) */
  255. /* r is 2*n2 words in size,
  256. * a and b are both n2 words in size.
  257. * n2 must be a power of 2.
  258. * We multiply and return the result.
  259. * t must be 2*n2 words in size
  260. * We calculate
  261. * a[0]*b[0]
  262. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  263. * a[1]*b[1]
  264. */
  265. /* dnX may not be positive, but n2/2+dnX has to be */
  266. static void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  267. int dna, int dnb, BN_ULONG *t) {
  268. int n = n2 / 2, c1, c2;
  269. int tna = n + dna, tnb = n + dnb;
  270. unsigned int neg, zero;
  271. BN_ULONG ln, lo, *p;
  272. /* Only call bn_mul_comba 8 if n2 == 8 and the
  273. * two arrays are complete [steve]
  274. */
  275. if (n2 == 8 && dna == 0 && dnb == 0) {
  276. bn_mul_comba8(r, a, b);
  277. return;
  278. }
  279. /* Else do normal multiply */
  280. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  281. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  282. if ((dna + dnb) < 0) {
  283. memset(&r[2 * n2 + dna + dnb], 0, sizeof(BN_ULONG) * -(dna + dnb));
  284. }
  285. return;
  286. }
  287. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  288. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  289. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  290. zero = neg = 0;
  291. switch (c1 * 3 + c2) {
  292. case -4:
  293. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  294. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  295. break;
  296. case -3:
  297. zero = 1;
  298. break;
  299. case -2:
  300. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  301. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  302. neg = 1;
  303. break;
  304. case -1:
  305. case 0:
  306. case 1:
  307. zero = 1;
  308. break;
  309. case 2:
  310. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  311. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  312. neg = 1;
  313. break;
  314. case 3:
  315. zero = 1;
  316. break;
  317. case 4:
  318. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  319. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  320. break;
  321. }
  322. if (n == 4 && dna == 0 && dnb == 0) {
  323. /* XXX: bn_mul_comba4 could take extra args to do this well */
  324. if (!zero) {
  325. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  326. } else {
  327. memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG));
  328. }
  329. bn_mul_comba4(r, a, b);
  330. bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
  331. } else if (n == 8 && dna == 0 && dnb == 0) {
  332. /* XXX: bn_mul_comba8 could take extra args to do this well */
  333. if (!zero) {
  334. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  335. } else {
  336. memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG));
  337. }
  338. bn_mul_comba8(r, a, b);
  339. bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
  340. } else {
  341. p = &(t[n2 * 2]);
  342. if (!zero) {
  343. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  344. } else {
  345. memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  346. }
  347. bn_mul_recursive(r, a, b, n, 0, 0, p);
  348. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
  349. }
  350. /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  351. * r[10] holds (a[0]*b[0])
  352. * r[32] holds (b[1]*b[1]) */
  353. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  354. if (neg) {
  355. /* if t[32] is negative */
  356. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  357. } else {
  358. /* Might have a carry */
  359. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  360. }
  361. /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  362. * r[10] holds (a[0]*b[0])
  363. * r[32] holds (b[1]*b[1])
  364. * c1 holds the carry bits */
  365. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  366. if (c1) {
  367. p = &(r[n + n2]);
  368. lo = *p;
  369. ln = (lo + c1) & BN_MASK2;
  370. *p = ln;
  371. /* The overflow will stop before we over write
  372. * words we should not overwrite */
  373. if (ln < (BN_ULONG)c1) {
  374. do {
  375. p++;
  376. lo = *p;
  377. ln = (lo + 1) & BN_MASK2;
  378. *p = ln;
  379. } while (ln == 0);
  380. }
  381. }
  382. }
  383. /* n+tn is the word length
  384. * t needs to be n*4 is size, as does r */
  385. /* tnX may not be negative but less than n */
  386. static void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
  387. int tna, int tnb, BN_ULONG *t) {
  388. int i, j, n2 = n * 2;
  389. int c1, c2, neg;
  390. BN_ULONG ln, lo, *p;
  391. if (n < 8) {
  392. bn_mul_normal(r, a, n + tna, b, n + tnb);
  393. return;
  394. }
  395. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  396. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  397. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  398. neg = 0;
  399. switch (c1 * 3 + c2) {
  400. case -4:
  401. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  402. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  403. break;
  404. case -3:
  405. /* break; */
  406. case -2:
  407. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  408. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  409. neg = 1;
  410. break;
  411. case -1:
  412. case 0:
  413. case 1:
  414. /* break; */
  415. case 2:
  416. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  417. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  418. neg = 1;
  419. break;
  420. case 3:
  421. /* break; */
  422. case 4:
  423. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  424. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  425. break;
  426. }
  427. if (n == 8) {
  428. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  429. bn_mul_comba8(r, a, b);
  430. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  431. memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  432. } else {
  433. p = &(t[n2 * 2]);
  434. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  435. bn_mul_recursive(r, a, b, n, 0, 0, p);
  436. i = n / 2;
  437. /* If there is only a bottom half to the number,
  438. * just do it */
  439. if (tna > tnb) {
  440. j = tna - i;
  441. } else {
  442. j = tnb - i;
  443. }
  444. if (j == 0) {
  445. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
  446. memset(&(r[n2 + i * 2]), 0, sizeof(BN_ULONG) * (n2 - i * 2));
  447. } else if (j > 0) {
  448. /* eg, n == 16, i == 8 and tn == 11 */
  449. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
  450. memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  451. } else {
  452. /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
  453. memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2);
  454. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
  455. tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  456. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  457. } else {
  458. for (;;) {
  459. i /= 2;
  460. /* these simplified conditions work
  461. * exclusively because difference
  462. * between tna and tnb is 1 or 0 */
  463. if (i < tna || i < tnb) {
  464. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i,
  465. tnb - i, p);
  466. break;
  467. } else if (i == tna || i == tnb) {
  468. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i,
  469. p);
  470. break;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  477. * r[10] holds (a[0]*b[0])
  478. * r[32] holds (b[1]*b[1])
  479. */
  480. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  481. if (neg) {
  482. /* if t[32] is negative */
  483. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  484. } else {
  485. /* Might have a carry */
  486. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  487. }
  488. /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  489. * r[10] holds (a[0]*b[0])
  490. * r[32] holds (b[1]*b[1])
  491. * c1 holds the carry bits */
  492. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  493. if (c1) {
  494. p = &(r[n + n2]);
  495. lo = *p;
  496. ln = (lo + c1) & BN_MASK2;
  497. *p = ln;
  498. /* The overflow will stop before we over write
  499. * words we should not overwrite */
  500. if (ln < (BN_ULONG)c1) {
  501. do {
  502. p++;
  503. lo = *p;
  504. ln = (lo + 1) & BN_MASK2;
  505. *p = ln;
  506. } while (ln == 0);
  507. }
  508. }
  509. }
  510. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  511. int ret = 0;
  512. int top, al, bl;
  513. BIGNUM *rr;
  514. int i;
  515. BIGNUM *t = NULL;
  516. int j = 0, k;
  517. al = a->top;
  518. bl = b->top;
  519. if ((al == 0) || (bl == 0)) {
  520. BN_zero(r);
  521. return 1;
  522. }
  523. top = al + bl;
  524. BN_CTX_start(ctx);
  525. if ((r == a) || (r == b)) {
  526. if ((rr = BN_CTX_get(ctx)) == NULL) {
  527. goto err;
  528. }
  529. } else {
  530. rr = r;
  531. }
  532. rr->neg = a->neg ^ b->neg;
  533. i = al - bl;
  534. if (i == 0) {
  535. if (al == 8) {
  536. if (bn_wexpand(rr, 16) == NULL) {
  537. goto err;
  538. }
  539. rr->top = 16;
  540. bn_mul_comba8(rr->d, a->d, b->d);
  541. goto end;
  542. }
  543. }
  544. static const int kMulNormalSize = 16;
  545. if (al >= kMulNormalSize && bl >= kMulNormalSize) {
  546. if (i >= -1 && i <= 1) {
  547. /* Find out the power of two lower or equal
  548. to the longest of the two numbers */
  549. if (i >= 0) {
  550. j = BN_num_bits_word((BN_ULONG)al);
  551. }
  552. if (i == -1) {
  553. j = BN_num_bits_word((BN_ULONG)bl);
  554. }
  555. j = 1 << (j - 1);
  556. assert(j <= al || j <= bl);
  557. k = j + j;
  558. t = BN_CTX_get(ctx);
  559. if (t == NULL) {
  560. goto err;
  561. }
  562. if (al > j || bl > j) {
  563. if (bn_wexpand(t, k * 4) == NULL) {
  564. goto err;
  565. }
  566. if (bn_wexpand(rr, k * 4) == NULL) {
  567. goto err;
  568. }
  569. bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  570. } else {
  571. /* al <= j || bl <= j */
  572. if (bn_wexpand(t, k * 2) == NULL) {
  573. goto err;
  574. }
  575. if (bn_wexpand(rr, k * 2) == NULL) {
  576. goto err;
  577. }
  578. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  579. }
  580. rr->top = top;
  581. goto end;
  582. }
  583. }
  584. if (bn_wexpand(rr, top) == NULL) {
  585. goto err;
  586. }
  587. rr->top = top;
  588. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  589. end:
  590. bn_correct_top(rr);
  591. if (r != rr && !BN_copy(r, rr)) {
  592. goto err;
  593. }
  594. ret = 1;
  595. err:
  596. BN_CTX_end(ctx);
  597. return ret;
  598. }
  599. /* tmp must have 2*n words */
  600. static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) {
  601. int i, j, max;
  602. const BN_ULONG *ap;
  603. BN_ULONG *rp;
  604. max = n * 2;
  605. ap = a;
  606. rp = r;
  607. rp[0] = rp[max - 1] = 0;
  608. rp++;
  609. j = n;
  610. if (--j > 0) {
  611. ap++;
  612. rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
  613. rp += 2;
  614. }
  615. for (i = n - 2; i > 0; i--) {
  616. j--;
  617. ap++;
  618. rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
  619. rp += 2;
  620. }
  621. bn_add_words(r, r, r, max);
  622. /* There will not be a carry */
  623. bn_sqr_words(tmp, a, n);
  624. bn_add_words(r, r, tmp, max);
  625. }
  626. /* r is 2*n words in size,
  627. * a and b are both n words in size. (There's not actually a 'b' here ...)
  628. * n must be a power of 2.
  629. * We multiply and return the result.
  630. * t must be 2*n words in size
  631. * We calculate
  632. * a[0]*b[0]
  633. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  634. * a[1]*b[1]
  635. */
  636. static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) {
  637. int n = n2 / 2;
  638. int zero, c1;
  639. BN_ULONG ln, lo, *p;
  640. if (n2 == 4) {
  641. bn_sqr_comba4(r, a);
  642. return;
  643. } else if (n2 == 8) {
  644. bn_sqr_comba8(r, a);
  645. return;
  646. }
  647. if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  648. bn_sqr_normal(r, a, n2, t);
  649. return;
  650. }
  651. /* r=(a[0]-a[1])*(a[1]-a[0]) */
  652. c1 = bn_cmp_words(a, &(a[n]), n);
  653. zero = 0;
  654. if (c1 > 0) {
  655. bn_sub_words(t, a, &(a[n]), n);
  656. } else if (c1 < 0) {
  657. bn_sub_words(t, &(a[n]), a, n);
  658. } else {
  659. zero = 1;
  660. }
  661. /* The result will always be negative unless it is zero */
  662. p = &(t[n2 * 2]);
  663. if (!zero) {
  664. bn_sqr_recursive(&(t[n2]), t, n, p);
  665. } else {
  666. memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  667. }
  668. bn_sqr_recursive(r, a, n, p);
  669. bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
  670. /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
  671. * r[10] holds (a[0]*b[0])
  672. * r[32] holds (b[1]*b[1]) */
  673. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  674. /* t[32] is negative */
  675. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  676. /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
  677. * r[10] holds (a[0]*a[0])
  678. * r[32] holds (a[1]*a[1])
  679. * c1 holds the carry bits */
  680. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  681. if (c1) {
  682. p = &(r[n + n2]);
  683. lo = *p;
  684. ln = (lo + c1) & BN_MASK2;
  685. *p = ln;
  686. /* The overflow will stop before we over write
  687. * words we should not overwrite */
  688. if (ln < (BN_ULONG)c1) {
  689. do {
  690. p++;
  691. lo = *p;
  692. ln = (lo + 1) & BN_MASK2;
  693. *p = ln;
  694. } while (ln == 0);
  695. }
  696. }
  697. }
  698. int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
  699. BN_ULONG ll;
  700. w &= BN_MASK2;
  701. if (!bn->top) {
  702. return 1;
  703. }
  704. if (w == 0) {
  705. BN_zero(bn);
  706. return 1;
  707. }
  708. ll = bn_mul_words(bn->d, bn->d, bn->top, w);
  709. if (ll) {
  710. if (bn_wexpand(bn, bn->top + 1) == NULL) {
  711. return 0;
  712. }
  713. bn->d[bn->top++] = ll;
  714. }
  715. return 1;
  716. }
  717. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
  718. int max, al;
  719. int ret = 0;
  720. BIGNUM *tmp, *rr;
  721. al = a->top;
  722. if (al <= 0) {
  723. r->top = 0;
  724. r->neg = 0;
  725. return 1;
  726. }
  727. BN_CTX_start(ctx);
  728. rr = (a != r) ? r : BN_CTX_get(ctx);
  729. tmp = BN_CTX_get(ctx);
  730. if (!rr || !tmp) {
  731. goto err;
  732. }
  733. max = 2 * al; /* Non-zero (from above) */
  734. if (bn_wexpand(rr, max) == NULL) {
  735. goto err;
  736. }
  737. if (al == 4) {
  738. bn_sqr_comba4(rr->d, a->d);
  739. } else if (al == 8) {
  740. bn_sqr_comba8(rr->d, a->d);
  741. } else {
  742. if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  743. BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
  744. bn_sqr_normal(rr->d, a->d, al, t);
  745. } else {
  746. int j, k;
  747. j = BN_num_bits_word((BN_ULONG)al);
  748. j = 1 << (j - 1);
  749. k = j + j;
  750. if (al == j) {
  751. if (bn_wexpand(tmp, k * 2) == NULL) {
  752. goto err;
  753. }
  754. bn_sqr_recursive(rr->d, a->d, al, tmp->d);
  755. } else {
  756. if (bn_wexpand(tmp, max) == NULL) {
  757. goto err;
  758. }
  759. bn_sqr_normal(rr->d, a->d, al, tmp->d);
  760. }
  761. }
  762. }
  763. rr->neg = 0;
  764. /* If the most-significant half of the top word of 'a' is zero, then
  765. * the square of 'a' will max-1 words. */
  766. if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) {
  767. rr->top = max - 1;
  768. } else {
  769. rr->top = max;
  770. }
  771. if (rr != r && !BN_copy(r, rr)) {
  772. goto err;
  773. }
  774. ret = 1;
  775. err:
  776. BN_CTX_end(ctx);
  777. return ret;
  778. }