2
0

bn_mul.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include "internal/cryptlib.h"
  11. #include "bn_lcl.h"
  12. #if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
  13. /*
  14. * Here follows specialised variants of bn_add_words() and bn_sub_words().
  15. * They have the property performing operations on arrays of different sizes.
  16. * The sizes of those arrays is expressed through cl, which is the common
  17. * length ( basically, min(len(a),len(b)) ), and dl, which is the delta
  18. * between the two lengths, calculated as len(a)-len(b). All lengths are the
  19. * number of BN_ULONGs... For the operations that require a result array as
  20. * parameter, it must have the length cl+abs(dl). These functions should
  21. * probably end up in bn_asm.c as soon as there are assembler counterparts
  22. * for the systems that use assembler files.
  23. */
  24. BN_ULONG bn_sub_part_words(BN_ULONG *r,
  25. const BN_ULONG *a, const BN_ULONG *b,
  26. int cl, int dl)
  27. {
  28. BN_ULONG c, t;
  29. assert(cl >= 0);
  30. c = bn_sub_words(r, a, b, cl);
  31. if (dl == 0)
  32. return c;
  33. r += cl;
  34. a += cl;
  35. b += cl;
  36. if (dl < 0) {
  37. for (;;) {
  38. t = b[0];
  39. r[0] = (0 - t - c) & BN_MASK2;
  40. if (t != 0)
  41. c = 1;
  42. if (++dl >= 0)
  43. break;
  44. t = b[1];
  45. r[1] = (0 - t - c) & BN_MASK2;
  46. if (t != 0)
  47. c = 1;
  48. if (++dl >= 0)
  49. break;
  50. t = b[2];
  51. r[2] = (0 - t - c) & BN_MASK2;
  52. if (t != 0)
  53. c = 1;
  54. if (++dl >= 0)
  55. break;
  56. t = b[3];
  57. r[3] = (0 - t - c) & BN_MASK2;
  58. if (t != 0)
  59. c = 1;
  60. if (++dl >= 0)
  61. break;
  62. b += 4;
  63. r += 4;
  64. }
  65. } else {
  66. int save_dl = dl;
  67. while (c) {
  68. t = a[0];
  69. r[0] = (t - c) & BN_MASK2;
  70. if (t != 0)
  71. c = 0;
  72. if (--dl <= 0)
  73. break;
  74. t = a[1];
  75. r[1] = (t - c) & BN_MASK2;
  76. if (t != 0)
  77. c = 0;
  78. if (--dl <= 0)
  79. break;
  80. t = a[2];
  81. r[2] = (t - c) & BN_MASK2;
  82. if (t != 0)
  83. c = 0;
  84. if (--dl <= 0)
  85. break;
  86. t = a[3];
  87. r[3] = (t - c) & BN_MASK2;
  88. if (t != 0)
  89. c = 0;
  90. if (--dl <= 0)
  91. break;
  92. save_dl = dl;
  93. a += 4;
  94. r += 4;
  95. }
  96. if (dl > 0) {
  97. if (save_dl > dl) {
  98. switch (save_dl - dl) {
  99. case 1:
  100. r[1] = a[1];
  101. if (--dl <= 0)
  102. break;
  103. /* fall thru */
  104. case 2:
  105. r[2] = a[2];
  106. if (--dl <= 0)
  107. break;
  108. /* fall thru */
  109. case 3:
  110. r[3] = a[3];
  111. if (--dl <= 0)
  112. break;
  113. }
  114. a += 4;
  115. r += 4;
  116. }
  117. }
  118. if (dl > 0) {
  119. for (;;) {
  120. r[0] = a[0];
  121. if (--dl <= 0)
  122. break;
  123. r[1] = a[1];
  124. if (--dl <= 0)
  125. break;
  126. r[2] = a[2];
  127. if (--dl <= 0)
  128. break;
  129. r[3] = a[3];
  130. if (--dl <= 0)
  131. break;
  132. a += 4;
  133. r += 4;
  134. }
  135. }
  136. }
  137. return c;
  138. }
  139. #endif
  140. #ifdef BN_RECURSION
  141. /*
  142. * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
  143. * Computer Programming, Vol. 2)
  144. */
  145. /*-
  146. * r is 2*n2 words in size,
  147. * a and b are both n2 words in size.
  148. * n2 must be a power of 2.
  149. * We multiply and return the result.
  150. * t must be 2*n2 words in size
  151. * We calculate
  152. * a[0]*b[0]
  153. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  154. * a[1]*b[1]
  155. */
  156. /* dnX may not be positive, but n2/2+dnX has to be */
  157. void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  158. int dna, int dnb, BN_ULONG *t)
  159. {
  160. int n = n2 / 2, c1, c2;
  161. int tna = n + dna, tnb = n + dnb;
  162. unsigned int neg, zero;
  163. BN_ULONG ln, lo, *p;
  164. # ifdef BN_MUL_COMBA
  165. # if 0
  166. if (n2 == 4) {
  167. bn_mul_comba4(r, a, b);
  168. return;
  169. }
  170. # endif
  171. /*
  172. * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
  173. * [steve]
  174. */
  175. if (n2 == 8 && dna == 0 && dnb == 0) {
  176. bn_mul_comba8(r, a, b);
  177. return;
  178. }
  179. # endif /* BN_MUL_COMBA */
  180. /* Else do normal multiply */
  181. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  182. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  183. if ((dna + dnb) < 0)
  184. memset(&r[2 * n2 + dna + dnb], 0,
  185. sizeof(BN_ULONG) * -(dna + dnb));
  186. return;
  187. }
  188. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  189. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  190. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  191. zero = neg = 0;
  192. switch (c1 * 3 + c2) {
  193. case -4:
  194. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  195. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  196. break;
  197. case -3:
  198. zero = 1;
  199. break;
  200. case -2:
  201. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  202. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  203. neg = 1;
  204. break;
  205. case -1:
  206. case 0:
  207. case 1:
  208. zero = 1;
  209. break;
  210. case 2:
  211. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  212. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  213. neg = 1;
  214. break;
  215. case 3:
  216. zero = 1;
  217. break;
  218. case 4:
  219. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  220. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  221. break;
  222. }
  223. # ifdef BN_MUL_COMBA
  224. if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
  225. * extra args to do this well */
  226. if (!zero)
  227. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  228. else
  229. memset(&t[n2], 0, sizeof(*t) * 8);
  230. bn_mul_comba4(r, a, b);
  231. bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
  232. } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
  233. * take extra args to do
  234. * this well */
  235. if (!zero)
  236. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  237. else
  238. memset(&t[n2], 0, sizeof(*t) * 16);
  239. bn_mul_comba8(r, a, b);
  240. bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
  241. } else
  242. # endif /* BN_MUL_COMBA */
  243. {
  244. p = &(t[n2 * 2]);
  245. if (!zero)
  246. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  247. else
  248. memset(&t[n2], 0, sizeof(*t) * n2);
  249. bn_mul_recursive(r, a, b, n, 0, 0, p);
  250. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
  251. }
  252. /*-
  253. * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  254. * r[10] holds (a[0]*b[0])
  255. * r[32] holds (b[1]*b[1])
  256. */
  257. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  258. if (neg) { /* if t[32] is negative */
  259. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  260. } else {
  261. /* Might have a carry */
  262. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  263. }
  264. /*-
  265. * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  266. * r[10] holds (a[0]*b[0])
  267. * r[32] holds (b[1]*b[1])
  268. * c1 holds the carry bits
  269. */
  270. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  271. if (c1) {
  272. p = &(r[n + n2]);
  273. lo = *p;
  274. ln = (lo + c1) & BN_MASK2;
  275. *p = ln;
  276. /*
  277. * The overflow will stop before we over write words we should not
  278. * overwrite
  279. */
  280. if (ln < (BN_ULONG)c1) {
  281. do {
  282. p++;
  283. lo = *p;
  284. ln = (lo + 1) & BN_MASK2;
  285. *p = ln;
  286. } while (ln == 0);
  287. }
  288. }
  289. }
  290. /*
  291. * n+tn is the word length t needs to be n*4 is size, as does r
  292. */
  293. /* tnX may not be negative but less than n */
  294. void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
  295. int tna, int tnb, BN_ULONG *t)
  296. {
  297. int i, j, n2 = n * 2;
  298. int c1, c2, neg;
  299. BN_ULONG ln, lo, *p;
  300. if (n < 8) {
  301. bn_mul_normal(r, a, n + tna, b, n + tnb);
  302. return;
  303. }
  304. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  305. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  306. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  307. neg = 0;
  308. switch (c1 * 3 + c2) {
  309. case -4:
  310. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  311. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  312. break;
  313. case -3:
  314. case -2:
  315. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  316. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  317. neg = 1;
  318. break;
  319. case -1:
  320. case 0:
  321. case 1:
  322. case 2:
  323. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  324. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  325. neg = 1;
  326. break;
  327. case 3:
  328. case 4:
  329. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  330. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  331. break;
  332. }
  333. /*
  334. * The zero case isn't yet implemented here. The speedup would probably
  335. * be negligible.
  336. */
  337. # if 0
  338. if (n == 4) {
  339. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  340. bn_mul_comba4(r, a, b);
  341. bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
  342. memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));
  343. } else
  344. # endif
  345. if (n == 8) {
  346. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  347. bn_mul_comba8(r, a, b);
  348. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  349. memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
  350. } else {
  351. p = &(t[n2 * 2]);
  352. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  353. bn_mul_recursive(r, a, b, n, 0, 0, p);
  354. i = n / 2;
  355. /*
  356. * If there is only a bottom half to the number, just do it
  357. */
  358. if (tna > tnb)
  359. j = tna - i;
  360. else
  361. j = tnb - i;
  362. if (j == 0) {
  363. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
  364. i, tna - i, tnb - i, p);
  365. memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
  366. } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
  367. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
  368. i, tna - i, tnb - i, p);
  369. memset(&(r[n2 + tna + tnb]), 0,
  370. sizeof(BN_ULONG) * (n2 - tna - tnb));
  371. } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
  372. memset(&r[n2], 0, sizeof(*r) * n2);
  373. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
  374. && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  375. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  376. } else {
  377. for (;;) {
  378. i /= 2;
  379. /*
  380. * these simplified conditions work exclusively because
  381. * difference between tna and tnb is 1 or 0
  382. */
  383. if (i < tna || i < tnb) {
  384. bn_mul_part_recursive(&(r[n2]),
  385. &(a[n]), &(b[n]),
  386. i, tna - i, tnb - i, p);
  387. break;
  388. } else if (i == tna || i == tnb) {
  389. bn_mul_recursive(&(r[n2]),
  390. &(a[n]), &(b[n]),
  391. i, tna - i, tnb - i, p);
  392. break;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. /*-
  399. * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  400. * r[10] holds (a[0]*b[0])
  401. * r[32] holds (b[1]*b[1])
  402. */
  403. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  404. if (neg) { /* if t[32] is negative */
  405. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  406. } else {
  407. /* Might have a carry */
  408. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  409. }
  410. /*-
  411. * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  412. * r[10] holds (a[0]*b[0])
  413. * r[32] holds (b[1]*b[1])
  414. * c1 holds the carry bits
  415. */
  416. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  417. if (c1) {
  418. p = &(r[n + n2]);
  419. lo = *p;
  420. ln = (lo + c1) & BN_MASK2;
  421. *p = ln;
  422. /*
  423. * The overflow will stop before we over write words we should not
  424. * overwrite
  425. */
  426. if (ln < (BN_ULONG)c1) {
  427. do {
  428. p++;
  429. lo = *p;
  430. ln = (lo + 1) & BN_MASK2;
  431. *p = ln;
  432. } while (ln == 0);
  433. }
  434. }
  435. }
  436. /*-
  437. * a and b must be the same size, which is n2.
  438. * r needs to be n2 words and t needs to be n2*2
  439. */
  440. void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  441. BN_ULONG *t)
  442. {
  443. int n = n2 / 2;
  444. bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
  445. if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
  446. bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
  447. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  448. bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
  449. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  450. } else {
  451. bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
  452. bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
  453. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  454. bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
  455. }
  456. }
  457. #endif /* BN_RECURSION */
  458. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  459. {
  460. int ret = bn_mul_fixed_top(r, a, b, ctx);
  461. bn_correct_top(r);
  462. bn_check_top(r);
  463. return ret;
  464. }
  465. int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  466. {
  467. int ret = 0;
  468. int top, al, bl;
  469. BIGNUM *rr;
  470. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  471. int i;
  472. #endif
  473. #ifdef BN_RECURSION
  474. BIGNUM *t = NULL;
  475. int j = 0, k;
  476. #endif
  477. bn_check_top(a);
  478. bn_check_top(b);
  479. bn_check_top(r);
  480. al = a->top;
  481. bl = b->top;
  482. if ((al == 0) || (bl == 0)) {
  483. BN_zero(r);
  484. return 1;
  485. }
  486. top = al + bl;
  487. BN_CTX_start(ctx);
  488. if ((r == a) || (r == b)) {
  489. if ((rr = BN_CTX_get(ctx)) == NULL)
  490. goto err;
  491. } else
  492. rr = r;
  493. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  494. i = al - bl;
  495. #endif
  496. #ifdef BN_MUL_COMBA
  497. if (i == 0) {
  498. # if 0
  499. if (al == 4) {
  500. if (bn_wexpand(rr, 8) == NULL)
  501. goto err;
  502. rr->top = 8;
  503. bn_mul_comba4(rr->d, a->d, b->d);
  504. goto end;
  505. }
  506. # endif
  507. if (al == 8) {
  508. if (bn_wexpand(rr, 16) == NULL)
  509. goto err;
  510. rr->top = 16;
  511. bn_mul_comba8(rr->d, a->d, b->d);
  512. goto end;
  513. }
  514. }
  515. #endif /* BN_MUL_COMBA */
  516. #ifdef BN_RECURSION
  517. if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
  518. if (i >= -1 && i <= 1) {
  519. /*
  520. * Find out the power of two lower or equal to the longest of the
  521. * two numbers
  522. */
  523. if (i >= 0) {
  524. j = BN_num_bits_word((BN_ULONG)al);
  525. }
  526. if (i == -1) {
  527. j = BN_num_bits_word((BN_ULONG)bl);
  528. }
  529. j = 1 << (j - 1);
  530. assert(j <= al || j <= bl);
  531. k = j + j;
  532. t = BN_CTX_get(ctx);
  533. if (t == NULL)
  534. goto err;
  535. if (al > j || bl > j) {
  536. if (bn_wexpand(t, k * 4) == NULL)
  537. goto err;
  538. if (bn_wexpand(rr, k * 4) == NULL)
  539. goto err;
  540. bn_mul_part_recursive(rr->d, a->d, b->d,
  541. j, al - j, bl - j, t->d);
  542. } else { /* al <= j || bl <= j */
  543. if (bn_wexpand(t, k * 2) == NULL)
  544. goto err;
  545. if (bn_wexpand(rr, k * 2) == NULL)
  546. goto err;
  547. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  548. }
  549. rr->top = top;
  550. goto end;
  551. }
  552. }
  553. #endif /* BN_RECURSION */
  554. if (bn_wexpand(rr, top) == NULL)
  555. goto err;
  556. rr->top = top;
  557. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  558. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  559. end:
  560. #endif
  561. rr->neg = a->neg ^ b->neg;
  562. rr->flags |= BN_FLG_FIXED_TOP;
  563. if (r != rr && BN_copy(r, rr) == NULL)
  564. goto err;
  565. ret = 1;
  566. err:
  567. bn_check_top(r);
  568. BN_CTX_end(ctx);
  569. return ret;
  570. }
  571. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
  572. {
  573. BN_ULONG *rr;
  574. if (na < nb) {
  575. int itmp;
  576. BN_ULONG *ltmp;
  577. itmp = na;
  578. na = nb;
  579. nb = itmp;
  580. ltmp = a;
  581. a = b;
  582. b = ltmp;
  583. }
  584. rr = &(r[na]);
  585. if (nb <= 0) {
  586. (void)bn_mul_words(r, a, na, 0);
  587. return;
  588. } else
  589. rr[0] = bn_mul_words(r, a, na, b[0]);
  590. for (;;) {
  591. if (--nb <= 0)
  592. return;
  593. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  594. if (--nb <= 0)
  595. return;
  596. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  597. if (--nb <= 0)
  598. return;
  599. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  600. if (--nb <= 0)
  601. return;
  602. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  603. rr += 4;
  604. r += 4;
  605. b += 4;
  606. }
  607. }
  608. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
  609. {
  610. bn_mul_words(r, a, n, b[0]);
  611. for (;;) {
  612. if (--n <= 0)
  613. return;
  614. bn_mul_add_words(&(r[1]), a, n, b[1]);
  615. if (--n <= 0)
  616. return;
  617. bn_mul_add_words(&(r[2]), a, n, b[2]);
  618. if (--n <= 0)
  619. return;
  620. bn_mul_add_words(&(r[3]), a, n, b[3]);
  621. if (--n <= 0)
  622. return;
  623. bn_mul_add_words(&(r[4]), a, n, b[4]);
  624. r += 4;
  625. b += 4;
  626. }
  627. }