bn_asm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /* crypto/bn/bn_asm.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(BN_LLONG) || defined(BN_UMULT_HIGH)
  67. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  68. {
  69. BN_ULONG c1=0;
  70. assert(num >= 0);
  71. if (num <= 0) return(c1);
  72. while (num&~3)
  73. {
  74. mul_add(rp[0],ap[0],w,c1);
  75. mul_add(rp[1],ap[1],w,c1);
  76. mul_add(rp[2],ap[2],w,c1);
  77. mul_add(rp[3],ap[3],w,c1);
  78. ap+=4; rp+=4; num-=4;
  79. }
  80. if (num)
  81. {
  82. mul_add(rp[0],ap[0],w,c1); if (--num==0) return c1;
  83. mul_add(rp[1],ap[1],w,c1); if (--num==0) return c1;
  84. mul_add(rp[2],ap[2],w,c1); return c1;
  85. }
  86. return(c1);
  87. }
  88. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  89. {
  90. BN_ULONG c1=0;
  91. assert(num >= 0);
  92. if (num <= 0) return(c1);
  93. while (num&~3)
  94. {
  95. mul(rp[0],ap[0],w,c1);
  96. mul(rp[1],ap[1],w,c1);
  97. mul(rp[2],ap[2],w,c1);
  98. mul(rp[3],ap[3],w,c1);
  99. ap+=4; rp+=4; num-=4;
  100. }
  101. if (num)
  102. {
  103. mul(rp[0],ap[0],w,c1); if (--num == 0) return c1;
  104. mul(rp[1],ap[1],w,c1); if (--num == 0) return c1;
  105. mul(rp[2],ap[2],w,c1);
  106. }
  107. return(c1);
  108. }
  109. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  110. {
  111. assert(n >= 0);
  112. if (n <= 0) return;
  113. while (n&~3)
  114. {
  115. sqr(r[0],r[1],a[0]);
  116. sqr(r[2],r[3],a[1]);
  117. sqr(r[4],r[5],a[2]);
  118. sqr(r[6],r[7],a[3]);
  119. a+=4; r+=8; n-=4;
  120. }
  121. if (n)
  122. {
  123. sqr(r[0],r[1],a[0]); if (--n == 0) return;
  124. sqr(r[2],r[3],a[1]); if (--n == 0) return;
  125. sqr(r[4],r[5],a[2]);
  126. }
  127. }
  128. #else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
  129. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  130. {
  131. BN_ULONG c=0;
  132. BN_ULONG bl,bh;
  133. assert(num >= 0);
  134. if (num <= 0) return((BN_ULONG)0);
  135. bl=LBITS(w);
  136. bh=HBITS(w);
  137. for (;;)
  138. {
  139. mul_add(rp[0],ap[0],bl,bh,c);
  140. if (--num == 0) break;
  141. mul_add(rp[1],ap[1],bl,bh,c);
  142. if (--num == 0) break;
  143. mul_add(rp[2],ap[2],bl,bh,c);
  144. if (--num == 0) break;
  145. mul_add(rp[3],ap[3],bl,bh,c);
  146. if (--num == 0) break;
  147. ap+=4;
  148. rp+=4;
  149. }
  150. return(c);
  151. }
  152. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  153. {
  154. BN_ULONG carry=0;
  155. BN_ULONG bl,bh;
  156. assert(num >= 0);
  157. if (num <= 0) return((BN_ULONG)0);
  158. bl=LBITS(w);
  159. bh=HBITS(w);
  160. for (;;)
  161. {
  162. mul(rp[0],ap[0],bl,bh,carry);
  163. if (--num == 0) break;
  164. mul(rp[1],ap[1],bl,bh,carry);
  165. if (--num == 0) break;
  166. mul(rp[2],ap[2],bl,bh,carry);
  167. if (--num == 0) break;
  168. mul(rp[3],ap[3],bl,bh,carry);
  169. if (--num == 0) break;
  170. ap+=4;
  171. rp+=4;
  172. }
  173. return(carry);
  174. }
  175. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  176. {
  177. assert(n >= 0);
  178. if (n <= 0) return;
  179. for (;;)
  180. {
  181. sqr64(r[0],r[1],a[0]);
  182. if (--n == 0) break;
  183. sqr64(r[2],r[3],a[1]);
  184. if (--n == 0) break;
  185. sqr64(r[4],r[5],a[2]);
  186. if (--n == 0) break;
  187. sqr64(r[6],r[7],a[3]);
  188. if (--n == 0) break;
  189. a+=4;
  190. r+=8;
  191. }
  192. }
  193. #endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
  194. #if defined(BN_LLONG) && defined(BN_DIV2W)
  195. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  196. {
  197. return((BN_ULONG)(((((BN_ULLONG)h)<<BN_BITS2)|l)/(BN_ULLONG)d));
  198. }
  199. #else
  200. /* Divide h,l by d and return the result. */
  201. /* I need to test this some more :-( */
  202. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  203. {
  204. BN_ULONG dh,dl,q,ret=0,th,tl,t;
  205. int i,count=2;
  206. if (d == 0) return(BN_MASK2);
  207. i=BN_num_bits_word(d);
  208. assert((i == BN_BITS2) || (h > (BN_ULONG)1<<i));
  209. i=BN_BITS2-i;
  210. if (h >= d) h-=d;
  211. if (i)
  212. {
  213. d<<=i;
  214. h=(h<<i)|(l>>(BN_BITS2-i));
  215. l<<=i;
  216. }
  217. dh=(d&BN_MASK2h)>>BN_BITS4;
  218. dl=(d&BN_MASK2l);
  219. for (;;)
  220. {
  221. if ((h>>BN_BITS4) == dh)
  222. q=BN_MASK2l;
  223. else
  224. q=h/dh;
  225. th=q*dh;
  226. tl=dl*q;
  227. for (;;)
  228. {
  229. t=h-th;
  230. if ((t&BN_MASK2h) ||
  231. ((tl) <= (
  232. (t<<BN_BITS4)|
  233. ((l&BN_MASK2h)>>BN_BITS4))))
  234. break;
  235. q--;
  236. th-=dh;
  237. tl-=dl;
  238. }
  239. t=(tl>>BN_BITS4);
  240. tl=(tl<<BN_BITS4)&BN_MASK2h;
  241. th+=t;
  242. if (l < tl) th++;
  243. l-=tl;
  244. if (h < th)
  245. {
  246. h+=d;
  247. q--;
  248. }
  249. h-=th;
  250. if (--count == 0) break;
  251. ret=q<<BN_BITS4;
  252. h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
  253. l=(l&BN_MASK2l)<<BN_BITS4;
  254. }
  255. ret|=q;
  256. return(ret);
  257. }
  258. #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
  259. #ifdef BN_LLONG
  260. BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  261. {
  262. BN_ULLONG ll=0;
  263. assert(n >= 0);
  264. if (n <= 0) return((BN_ULONG)0);
  265. for (;;)
  266. {
  267. ll+=(BN_ULLONG)a[0]+b[0];
  268. r[0]=(BN_ULONG)ll&BN_MASK2;
  269. ll>>=BN_BITS2;
  270. if (--n <= 0) break;
  271. ll+=(BN_ULLONG)a[1]+b[1];
  272. r[1]=(BN_ULONG)ll&BN_MASK2;
  273. ll>>=BN_BITS2;
  274. if (--n <= 0) break;
  275. ll+=(BN_ULLONG)a[2]+b[2];
  276. r[2]=(BN_ULONG)ll&BN_MASK2;
  277. ll>>=BN_BITS2;
  278. if (--n <= 0) break;
  279. ll+=(BN_ULLONG)a[3]+b[3];
  280. r[3]=(BN_ULONG)ll&BN_MASK2;
  281. ll>>=BN_BITS2;
  282. if (--n <= 0) break;
  283. a+=4;
  284. b+=4;
  285. r+=4;
  286. }
  287. return((BN_ULONG)ll);
  288. }
  289. #else /* !BN_LLONG */
  290. BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  291. {
  292. BN_ULONG c,l,t;
  293. assert(n >= 0);
  294. if (n <= 0) return((BN_ULONG)0);
  295. c=0;
  296. for (;;)
  297. {
  298. t=a[0];
  299. t=(t+c)&BN_MASK2;
  300. c=(t < c);
  301. l=(t+b[0])&BN_MASK2;
  302. c+=(l < t);
  303. r[0]=l;
  304. if (--n <= 0) break;
  305. t=a[1];
  306. t=(t+c)&BN_MASK2;
  307. c=(t < c);
  308. l=(t+b[1])&BN_MASK2;
  309. c+=(l < t);
  310. r[1]=l;
  311. if (--n <= 0) break;
  312. t=a[2];
  313. t=(t+c)&BN_MASK2;
  314. c=(t < c);
  315. l=(t+b[2])&BN_MASK2;
  316. c+=(l < t);
  317. r[2]=l;
  318. if (--n <= 0) break;
  319. t=a[3];
  320. t=(t+c)&BN_MASK2;
  321. c=(t < c);
  322. l=(t+b[3])&BN_MASK2;
  323. c+=(l < t);
  324. r[3]=l;
  325. if (--n <= 0) break;
  326. a+=4;
  327. b+=4;
  328. r+=4;
  329. }
  330. return((BN_ULONG)c);
  331. }
  332. #endif /* !BN_LLONG */
  333. BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  334. {
  335. BN_ULONG t1,t2;
  336. int c=0;
  337. assert(n >= 0);
  338. if (n <= 0) return((BN_ULONG)0);
  339. for (;;)
  340. {
  341. t1=a[0]; t2=b[0];
  342. r[0]=(t1-t2-c)&BN_MASK2;
  343. if (t1 != t2) c=(t1 < t2);
  344. if (--n <= 0) break;
  345. t1=a[1]; t2=b[1];
  346. r[1]=(t1-t2-c)&BN_MASK2;
  347. if (t1 != t2) c=(t1 < t2);
  348. if (--n <= 0) break;
  349. t1=a[2]; t2=b[2];
  350. r[2]=(t1-t2-c)&BN_MASK2;
  351. if (t1 != t2) c=(t1 < t2);
  352. if (--n <= 0) break;
  353. t1=a[3]; t2=b[3];
  354. r[3]=(t1-t2-c)&BN_MASK2;
  355. if (t1 != t2) c=(t1 < t2);
  356. if (--n <= 0) break;
  357. a+=4;
  358. b+=4;
  359. r+=4;
  360. }
  361. return(c);
  362. }
  363. #ifdef BN_MUL_COMBA
  364. #undef bn_mul_comba8
  365. #undef bn_mul_comba4
  366. #undef bn_sqr_comba8
  367. #undef bn_sqr_comba4
  368. /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
  369. /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
  370. /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
  371. /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
  372. #ifdef BN_LLONG
  373. #define mul_add_c(a,b,c0,c1,c2) \
  374. t=(BN_ULLONG)a*b; \
  375. t1=(BN_ULONG)Lw(t); \
  376. t2=(BN_ULONG)Hw(t); \
  377. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  378. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  379. #define mul_add_c2(a,b,c0,c1,c2) \
  380. t=(BN_ULLONG)a*b; \
  381. tt=(t+t)&BN_MASK; \
  382. if (tt < t) c2++; \
  383. t1=(BN_ULONG)Lw(tt); \
  384. t2=(BN_ULONG)Hw(tt); \
  385. c0=(c0+t1)&BN_MASK2; \
  386. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  387. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  388. #define sqr_add_c(a,i,c0,c1,c2) \
  389. t=(BN_ULLONG)a[i]*a[i]; \
  390. t1=(BN_ULONG)Lw(t); \
  391. t2=(BN_ULONG)Hw(t); \
  392. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  393. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  394. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  395. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  396. #elif defined(BN_UMULT_HIGH)
  397. #define mul_add_c(a,b,c0,c1,c2) { \
  398. BN_ULONG ta=(a),tb=(b); \
  399. t1 = ta * tb; \
  400. t2 = BN_UMULT_HIGH(ta,tb); \
  401. c0 += t1; t2 += (c0<t1)?1:0; \
  402. c1 += t2; c2 += (c1<t2)?1:0; \
  403. }
  404. #define mul_add_c2(a,b,c0,c1,c2) { \
  405. BN_ULONG ta=(a),tb=(b),t0; \
  406. t1 = BN_UMULT_HIGH(ta,tb); \
  407. t0 = ta * tb; \
  408. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  409. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  410. c0 += t1; t2 += (c0<t1)?1:0; \
  411. c1 += t2; c2 += (c1<t2)?1:0; \
  412. }
  413. #define sqr_add_c(a,i,c0,c1,c2) { \
  414. BN_ULONG ta=(a)[i]; \
  415. t1 = ta * ta; \
  416. t2 = BN_UMULT_HIGH(ta,ta); \
  417. c0 += t1; t2 += (c0<t1)?1:0; \
  418. c1 += t2; c2 += (c1<t2)?1:0; \
  419. }
  420. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  421. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  422. #else /* !BN_LLONG */
  423. #define mul_add_c(a,b,c0,c1,c2) \
  424. t1=LBITS(a); t2=HBITS(a); \
  425. bl=LBITS(b); bh=HBITS(b); \
  426. mul64(t1,t2,bl,bh); \
  427. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  428. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  429. #define mul_add_c2(a,b,c0,c1,c2) \
  430. t1=LBITS(a); t2=HBITS(a); \
  431. bl=LBITS(b); bh=HBITS(b); \
  432. mul64(t1,t2,bl,bh); \
  433. if (t2 & BN_TBIT) c2++; \
  434. t2=(t2+t2)&BN_MASK2; \
  435. if (t1 & BN_TBIT) t2++; \
  436. t1=(t1+t1)&BN_MASK2; \
  437. c0=(c0+t1)&BN_MASK2; \
  438. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  439. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  440. #define sqr_add_c(a,i,c0,c1,c2) \
  441. sqr64(t1,t2,(a)[i]); \
  442. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  443. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  444. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  445. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  446. #endif /* !BN_LLONG */
  447. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  448. {
  449. #ifdef BN_LLONG
  450. BN_ULLONG t;
  451. #else
  452. BN_ULONG bl,bh;
  453. #endif
  454. BN_ULONG t1,t2;
  455. BN_ULONG c1,c2,c3;
  456. c1=0;
  457. c2=0;
  458. c3=0;
  459. mul_add_c(a[0],b[0],c1,c2,c3);
  460. r[0]=c1;
  461. c1=0;
  462. mul_add_c(a[0],b[1],c2,c3,c1);
  463. mul_add_c(a[1],b[0],c2,c3,c1);
  464. r[1]=c2;
  465. c2=0;
  466. mul_add_c(a[2],b[0],c3,c1,c2);
  467. mul_add_c(a[1],b[1],c3,c1,c2);
  468. mul_add_c(a[0],b[2],c3,c1,c2);
  469. r[2]=c3;
  470. c3=0;
  471. mul_add_c(a[0],b[3],c1,c2,c3);
  472. mul_add_c(a[1],b[2],c1,c2,c3);
  473. mul_add_c(a[2],b[1],c1,c2,c3);
  474. mul_add_c(a[3],b[0],c1,c2,c3);
  475. r[3]=c1;
  476. c1=0;
  477. mul_add_c(a[4],b[0],c2,c3,c1);
  478. mul_add_c(a[3],b[1],c2,c3,c1);
  479. mul_add_c(a[2],b[2],c2,c3,c1);
  480. mul_add_c(a[1],b[3],c2,c3,c1);
  481. mul_add_c(a[0],b[4],c2,c3,c1);
  482. r[4]=c2;
  483. c2=0;
  484. mul_add_c(a[0],b[5],c3,c1,c2);
  485. mul_add_c(a[1],b[4],c3,c1,c2);
  486. mul_add_c(a[2],b[3],c3,c1,c2);
  487. mul_add_c(a[3],b[2],c3,c1,c2);
  488. mul_add_c(a[4],b[1],c3,c1,c2);
  489. mul_add_c(a[5],b[0],c3,c1,c2);
  490. r[5]=c3;
  491. c3=0;
  492. mul_add_c(a[6],b[0],c1,c2,c3);
  493. mul_add_c(a[5],b[1],c1,c2,c3);
  494. mul_add_c(a[4],b[2],c1,c2,c3);
  495. mul_add_c(a[3],b[3],c1,c2,c3);
  496. mul_add_c(a[2],b[4],c1,c2,c3);
  497. mul_add_c(a[1],b[5],c1,c2,c3);
  498. mul_add_c(a[0],b[6],c1,c2,c3);
  499. r[6]=c1;
  500. c1=0;
  501. mul_add_c(a[0],b[7],c2,c3,c1);
  502. mul_add_c(a[1],b[6],c2,c3,c1);
  503. mul_add_c(a[2],b[5],c2,c3,c1);
  504. mul_add_c(a[3],b[4],c2,c3,c1);
  505. mul_add_c(a[4],b[3],c2,c3,c1);
  506. mul_add_c(a[5],b[2],c2,c3,c1);
  507. mul_add_c(a[6],b[1],c2,c3,c1);
  508. mul_add_c(a[7],b[0],c2,c3,c1);
  509. r[7]=c2;
  510. c2=0;
  511. mul_add_c(a[7],b[1],c3,c1,c2);
  512. mul_add_c(a[6],b[2],c3,c1,c2);
  513. mul_add_c(a[5],b[3],c3,c1,c2);
  514. mul_add_c(a[4],b[4],c3,c1,c2);
  515. mul_add_c(a[3],b[5],c3,c1,c2);
  516. mul_add_c(a[2],b[6],c3,c1,c2);
  517. mul_add_c(a[1],b[7],c3,c1,c2);
  518. r[8]=c3;
  519. c3=0;
  520. mul_add_c(a[2],b[7],c1,c2,c3);
  521. mul_add_c(a[3],b[6],c1,c2,c3);
  522. mul_add_c(a[4],b[5],c1,c2,c3);
  523. mul_add_c(a[5],b[4],c1,c2,c3);
  524. mul_add_c(a[6],b[3],c1,c2,c3);
  525. mul_add_c(a[7],b[2],c1,c2,c3);
  526. r[9]=c1;
  527. c1=0;
  528. mul_add_c(a[7],b[3],c2,c3,c1);
  529. mul_add_c(a[6],b[4],c2,c3,c1);
  530. mul_add_c(a[5],b[5],c2,c3,c1);
  531. mul_add_c(a[4],b[6],c2,c3,c1);
  532. mul_add_c(a[3],b[7],c2,c3,c1);
  533. r[10]=c2;
  534. c2=0;
  535. mul_add_c(a[4],b[7],c3,c1,c2);
  536. mul_add_c(a[5],b[6],c3,c1,c2);
  537. mul_add_c(a[6],b[5],c3,c1,c2);
  538. mul_add_c(a[7],b[4],c3,c1,c2);
  539. r[11]=c3;
  540. c3=0;
  541. mul_add_c(a[7],b[5],c1,c2,c3);
  542. mul_add_c(a[6],b[6],c1,c2,c3);
  543. mul_add_c(a[5],b[7],c1,c2,c3);
  544. r[12]=c1;
  545. c1=0;
  546. mul_add_c(a[6],b[7],c2,c3,c1);
  547. mul_add_c(a[7],b[6],c2,c3,c1);
  548. r[13]=c2;
  549. c2=0;
  550. mul_add_c(a[7],b[7],c3,c1,c2);
  551. r[14]=c3;
  552. r[15]=c1;
  553. }
  554. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  555. {
  556. #ifdef BN_LLONG
  557. BN_ULLONG t;
  558. #else
  559. BN_ULONG bl,bh;
  560. #endif
  561. BN_ULONG t1,t2;
  562. BN_ULONG c1,c2,c3;
  563. c1=0;
  564. c2=0;
  565. c3=0;
  566. mul_add_c(a[0],b[0],c1,c2,c3);
  567. r[0]=c1;
  568. c1=0;
  569. mul_add_c(a[0],b[1],c2,c3,c1);
  570. mul_add_c(a[1],b[0],c2,c3,c1);
  571. r[1]=c2;
  572. c2=0;
  573. mul_add_c(a[2],b[0],c3,c1,c2);
  574. mul_add_c(a[1],b[1],c3,c1,c2);
  575. mul_add_c(a[0],b[2],c3,c1,c2);
  576. r[2]=c3;
  577. c3=0;
  578. mul_add_c(a[0],b[3],c1,c2,c3);
  579. mul_add_c(a[1],b[2],c1,c2,c3);
  580. mul_add_c(a[2],b[1],c1,c2,c3);
  581. mul_add_c(a[3],b[0],c1,c2,c3);
  582. r[3]=c1;
  583. c1=0;
  584. mul_add_c(a[3],b[1],c2,c3,c1);
  585. mul_add_c(a[2],b[2],c2,c3,c1);
  586. mul_add_c(a[1],b[3],c2,c3,c1);
  587. r[4]=c2;
  588. c2=0;
  589. mul_add_c(a[2],b[3],c3,c1,c2);
  590. mul_add_c(a[3],b[2],c3,c1,c2);
  591. r[5]=c3;
  592. c3=0;
  593. mul_add_c(a[3],b[3],c1,c2,c3);
  594. r[6]=c1;
  595. r[7]=c2;
  596. }
  597. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  598. {
  599. #ifdef BN_LLONG
  600. BN_ULLONG t,tt;
  601. #else
  602. BN_ULONG bl,bh;
  603. #endif
  604. BN_ULONG t1,t2;
  605. BN_ULONG c1,c2,c3;
  606. c1=0;
  607. c2=0;
  608. c3=0;
  609. sqr_add_c(a,0,c1,c2,c3);
  610. r[0]=c1;
  611. c1=0;
  612. sqr_add_c2(a,1,0,c2,c3,c1);
  613. r[1]=c2;
  614. c2=0;
  615. sqr_add_c(a,1,c3,c1,c2);
  616. sqr_add_c2(a,2,0,c3,c1,c2);
  617. r[2]=c3;
  618. c3=0;
  619. sqr_add_c2(a,3,0,c1,c2,c3);
  620. sqr_add_c2(a,2,1,c1,c2,c3);
  621. r[3]=c1;
  622. c1=0;
  623. sqr_add_c(a,2,c2,c3,c1);
  624. sqr_add_c2(a,3,1,c2,c3,c1);
  625. sqr_add_c2(a,4,0,c2,c3,c1);
  626. r[4]=c2;
  627. c2=0;
  628. sqr_add_c2(a,5,0,c3,c1,c2);
  629. sqr_add_c2(a,4,1,c3,c1,c2);
  630. sqr_add_c2(a,3,2,c3,c1,c2);
  631. r[5]=c3;
  632. c3=0;
  633. sqr_add_c(a,3,c1,c2,c3);
  634. sqr_add_c2(a,4,2,c1,c2,c3);
  635. sqr_add_c2(a,5,1,c1,c2,c3);
  636. sqr_add_c2(a,6,0,c1,c2,c3);
  637. r[6]=c1;
  638. c1=0;
  639. sqr_add_c2(a,7,0,c2,c3,c1);
  640. sqr_add_c2(a,6,1,c2,c3,c1);
  641. sqr_add_c2(a,5,2,c2,c3,c1);
  642. sqr_add_c2(a,4,3,c2,c3,c1);
  643. r[7]=c2;
  644. c2=0;
  645. sqr_add_c(a,4,c3,c1,c2);
  646. sqr_add_c2(a,5,3,c3,c1,c2);
  647. sqr_add_c2(a,6,2,c3,c1,c2);
  648. sqr_add_c2(a,7,1,c3,c1,c2);
  649. r[8]=c3;
  650. c3=0;
  651. sqr_add_c2(a,7,2,c1,c2,c3);
  652. sqr_add_c2(a,6,3,c1,c2,c3);
  653. sqr_add_c2(a,5,4,c1,c2,c3);
  654. r[9]=c1;
  655. c1=0;
  656. sqr_add_c(a,5,c2,c3,c1);
  657. sqr_add_c2(a,6,4,c2,c3,c1);
  658. sqr_add_c2(a,7,3,c2,c3,c1);
  659. r[10]=c2;
  660. c2=0;
  661. sqr_add_c2(a,7,4,c3,c1,c2);
  662. sqr_add_c2(a,6,5,c3,c1,c2);
  663. r[11]=c3;
  664. c3=0;
  665. sqr_add_c(a,6,c1,c2,c3);
  666. sqr_add_c2(a,7,5,c1,c2,c3);
  667. r[12]=c1;
  668. c1=0;
  669. sqr_add_c2(a,7,6,c2,c3,c1);
  670. r[13]=c2;
  671. c2=0;
  672. sqr_add_c(a,7,c3,c1,c2);
  673. r[14]=c3;
  674. r[15]=c1;
  675. }
  676. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  677. {
  678. #ifdef BN_LLONG
  679. BN_ULLONG t,tt;
  680. #else
  681. BN_ULONG bl,bh;
  682. #endif
  683. BN_ULONG t1,t2;
  684. BN_ULONG c1,c2,c3;
  685. c1=0;
  686. c2=0;
  687. c3=0;
  688. sqr_add_c(a,0,c1,c2,c3);
  689. r[0]=c1;
  690. c1=0;
  691. sqr_add_c2(a,1,0,c2,c3,c1);
  692. r[1]=c2;
  693. c2=0;
  694. sqr_add_c(a,1,c3,c1,c2);
  695. sqr_add_c2(a,2,0,c3,c1,c2);
  696. r[2]=c3;
  697. c3=0;
  698. sqr_add_c2(a,3,0,c1,c2,c3);
  699. sqr_add_c2(a,2,1,c1,c2,c3);
  700. r[3]=c1;
  701. c1=0;
  702. sqr_add_c(a,2,c2,c3,c1);
  703. sqr_add_c2(a,3,1,c2,c3,c1);
  704. r[4]=c2;
  705. c2=0;
  706. sqr_add_c2(a,3,2,c3,c1,c2);
  707. r[5]=c3;
  708. c3=0;
  709. sqr_add_c(a,3,c1,c2,c3);
  710. r[6]=c1;
  711. r[7]=c2;
  712. }
  713. #else /* !BN_MUL_COMBA */
  714. /* hmm... is it faster just to do a multiply? */
  715. #undef bn_sqr_comba4
  716. void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
  717. {
  718. BN_ULONG t[8];
  719. bn_sqr_normal(r,a,4,t);
  720. }
  721. #undef bn_sqr_comba8
  722. void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
  723. {
  724. BN_ULONG t[16];
  725. bn_sqr_normal(r,a,8,t);
  726. }
  727. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  728. {
  729. r[4]=bn_mul_words( &(r[0]),a,4,b[0]);
  730. r[5]=bn_mul_add_words(&(r[1]),a,4,b[1]);
  731. r[6]=bn_mul_add_words(&(r[2]),a,4,b[2]);
  732. r[7]=bn_mul_add_words(&(r[3]),a,4,b[3]);
  733. }
  734. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  735. {
  736. r[ 8]=bn_mul_words( &(r[0]),a,8,b[0]);
  737. r[ 9]=bn_mul_add_words(&(r[1]),a,8,b[1]);
  738. r[10]=bn_mul_add_words(&(r[2]),a,8,b[2]);
  739. r[11]=bn_mul_add_words(&(r[3]),a,8,b[3]);
  740. r[12]=bn_mul_add_words(&(r[4]),a,8,b[4]);
  741. r[13]=bn_mul_add_words(&(r[5]),a,8,b[5]);
  742. r[14]=bn_mul_add_words(&(r[6]),a,8,b[6]);
  743. r[15]=bn_mul_add_words(&(r[7]),a,8,b[7]);
  744. }
  745. #endif /* !BN_MUL_COMBA */