bn_asm.c 27 KB

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