bn_asm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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_LOHI)
  397. #define mul_add_c(a,b,c0,c1,c2) { \
  398. BN_ULONG ta=(a),tb=(b); \
  399. BN_UMULT_LOHI(t1,t2,ta,tb); \
  400. c0 += t1; t2 += (c0<t1)?1:0; \
  401. c1 += t2; c2 += (c1<t2)?1:0; \
  402. }
  403. #define mul_add_c2(a,b,c0,c1,c2) { \
  404. BN_ULONG ta=(a),tb=(b),t0; \
  405. BN_UMULT_LOHI(t0,t1,ta,tb); \
  406. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  407. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  408. c0 += t1; t2 += (c0<t1)?1:0; \
  409. c1 += t2; c2 += (c1<t2)?1:0; \
  410. }
  411. #define sqr_add_c(a,i,c0,c1,c2) { \
  412. BN_ULONG ta=(a)[i]; \
  413. BN_UMULT_LOHI(t1,t2,ta,ta); \
  414. c0 += t1; t2 += (c0<t1)?1:0; \
  415. c1 += t2; c2 += (c1<t2)?1:0; \
  416. }
  417. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  418. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  419. #elif defined(BN_UMULT_HIGH)
  420. #define mul_add_c(a,b,c0,c1,c2) { \
  421. BN_ULONG ta=(a),tb=(b); \
  422. t1 = ta * tb; \
  423. t2 = BN_UMULT_HIGH(ta,tb); \
  424. c0 += t1; t2 += (c0<t1)?1:0; \
  425. c1 += t2; c2 += (c1<t2)?1:0; \
  426. }
  427. #define mul_add_c2(a,b,c0,c1,c2) { \
  428. BN_ULONG ta=(a),tb=(b),t0; \
  429. t1 = BN_UMULT_HIGH(ta,tb); \
  430. t0 = ta * tb; \
  431. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  432. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  433. c0 += t1; t2 += (c0<t1)?1:0; \
  434. c1 += t2; c2 += (c1<t2)?1:0; \
  435. }
  436. #define sqr_add_c(a,i,c0,c1,c2) { \
  437. BN_ULONG ta=(a)[i]; \
  438. t1 = ta * ta; \
  439. t2 = BN_UMULT_HIGH(ta,ta); \
  440. c0 += t1; t2 += (c0<t1)?1:0; \
  441. c1 += t2; c2 += (c1<t2)?1:0; \
  442. }
  443. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  444. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  445. #else /* !BN_LLONG */
  446. #define mul_add_c(a,b,c0,c1,c2) \
  447. t1=LBITS(a); t2=HBITS(a); \
  448. bl=LBITS(b); bh=HBITS(b); \
  449. mul64(t1,t2,bl,bh); \
  450. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  451. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  452. #define mul_add_c2(a,b,c0,c1,c2) \
  453. t1=LBITS(a); t2=HBITS(a); \
  454. bl=LBITS(b); bh=HBITS(b); \
  455. mul64(t1,t2,bl,bh); \
  456. if (t2 & BN_TBIT) c2++; \
  457. t2=(t2+t2)&BN_MASK2; \
  458. if (t1 & BN_TBIT) t2++; \
  459. t1=(t1+t1)&BN_MASK2; \
  460. c0=(c0+t1)&BN_MASK2; \
  461. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  462. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  463. #define sqr_add_c(a,i,c0,c1,c2) \
  464. sqr64(t1,t2,(a)[i]); \
  465. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  466. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  467. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  468. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  469. #endif /* !BN_LLONG */
  470. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  471. {
  472. #ifdef BN_LLONG
  473. BN_ULLONG t;
  474. #else
  475. BN_ULONG bl,bh;
  476. #endif
  477. BN_ULONG t1,t2;
  478. BN_ULONG c1,c2,c3;
  479. c1=0;
  480. c2=0;
  481. c3=0;
  482. mul_add_c(a[0],b[0],c1,c2,c3);
  483. r[0]=c1;
  484. c1=0;
  485. mul_add_c(a[0],b[1],c2,c3,c1);
  486. mul_add_c(a[1],b[0],c2,c3,c1);
  487. r[1]=c2;
  488. c2=0;
  489. mul_add_c(a[2],b[0],c3,c1,c2);
  490. mul_add_c(a[1],b[1],c3,c1,c2);
  491. mul_add_c(a[0],b[2],c3,c1,c2);
  492. r[2]=c3;
  493. c3=0;
  494. mul_add_c(a[0],b[3],c1,c2,c3);
  495. mul_add_c(a[1],b[2],c1,c2,c3);
  496. mul_add_c(a[2],b[1],c1,c2,c3);
  497. mul_add_c(a[3],b[0],c1,c2,c3);
  498. r[3]=c1;
  499. c1=0;
  500. mul_add_c(a[4],b[0],c2,c3,c1);
  501. mul_add_c(a[3],b[1],c2,c3,c1);
  502. mul_add_c(a[2],b[2],c2,c3,c1);
  503. mul_add_c(a[1],b[3],c2,c3,c1);
  504. mul_add_c(a[0],b[4],c2,c3,c1);
  505. r[4]=c2;
  506. c2=0;
  507. mul_add_c(a[0],b[5],c3,c1,c2);
  508. mul_add_c(a[1],b[4],c3,c1,c2);
  509. mul_add_c(a[2],b[3],c3,c1,c2);
  510. mul_add_c(a[3],b[2],c3,c1,c2);
  511. mul_add_c(a[4],b[1],c3,c1,c2);
  512. mul_add_c(a[5],b[0],c3,c1,c2);
  513. r[5]=c3;
  514. c3=0;
  515. mul_add_c(a[6],b[0],c1,c2,c3);
  516. mul_add_c(a[5],b[1],c1,c2,c3);
  517. mul_add_c(a[4],b[2],c1,c2,c3);
  518. mul_add_c(a[3],b[3],c1,c2,c3);
  519. mul_add_c(a[2],b[4],c1,c2,c3);
  520. mul_add_c(a[1],b[5],c1,c2,c3);
  521. mul_add_c(a[0],b[6],c1,c2,c3);
  522. r[6]=c1;
  523. c1=0;
  524. mul_add_c(a[0],b[7],c2,c3,c1);
  525. mul_add_c(a[1],b[6],c2,c3,c1);
  526. mul_add_c(a[2],b[5],c2,c3,c1);
  527. mul_add_c(a[3],b[4],c2,c3,c1);
  528. mul_add_c(a[4],b[3],c2,c3,c1);
  529. mul_add_c(a[5],b[2],c2,c3,c1);
  530. mul_add_c(a[6],b[1],c2,c3,c1);
  531. mul_add_c(a[7],b[0],c2,c3,c1);
  532. r[7]=c2;
  533. c2=0;
  534. mul_add_c(a[7],b[1],c3,c1,c2);
  535. mul_add_c(a[6],b[2],c3,c1,c2);
  536. mul_add_c(a[5],b[3],c3,c1,c2);
  537. mul_add_c(a[4],b[4],c3,c1,c2);
  538. mul_add_c(a[3],b[5],c3,c1,c2);
  539. mul_add_c(a[2],b[6],c3,c1,c2);
  540. mul_add_c(a[1],b[7],c3,c1,c2);
  541. r[8]=c3;
  542. c3=0;
  543. mul_add_c(a[2],b[7],c1,c2,c3);
  544. mul_add_c(a[3],b[6],c1,c2,c3);
  545. mul_add_c(a[4],b[5],c1,c2,c3);
  546. mul_add_c(a[5],b[4],c1,c2,c3);
  547. mul_add_c(a[6],b[3],c1,c2,c3);
  548. mul_add_c(a[7],b[2],c1,c2,c3);
  549. r[9]=c1;
  550. c1=0;
  551. mul_add_c(a[7],b[3],c2,c3,c1);
  552. mul_add_c(a[6],b[4],c2,c3,c1);
  553. mul_add_c(a[5],b[5],c2,c3,c1);
  554. mul_add_c(a[4],b[6],c2,c3,c1);
  555. mul_add_c(a[3],b[7],c2,c3,c1);
  556. r[10]=c2;
  557. c2=0;
  558. mul_add_c(a[4],b[7],c3,c1,c2);
  559. mul_add_c(a[5],b[6],c3,c1,c2);
  560. mul_add_c(a[6],b[5],c3,c1,c2);
  561. mul_add_c(a[7],b[4],c3,c1,c2);
  562. r[11]=c3;
  563. c3=0;
  564. mul_add_c(a[7],b[5],c1,c2,c3);
  565. mul_add_c(a[6],b[6],c1,c2,c3);
  566. mul_add_c(a[5],b[7],c1,c2,c3);
  567. r[12]=c1;
  568. c1=0;
  569. mul_add_c(a[6],b[7],c2,c3,c1);
  570. mul_add_c(a[7],b[6],c2,c3,c1);
  571. r[13]=c2;
  572. c2=0;
  573. mul_add_c(a[7],b[7],c3,c1,c2);
  574. r[14]=c3;
  575. r[15]=c1;
  576. }
  577. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  578. {
  579. #ifdef BN_LLONG
  580. BN_ULLONG t;
  581. #else
  582. BN_ULONG bl,bh;
  583. #endif
  584. BN_ULONG t1,t2;
  585. BN_ULONG c1,c2,c3;
  586. c1=0;
  587. c2=0;
  588. c3=0;
  589. mul_add_c(a[0],b[0],c1,c2,c3);
  590. r[0]=c1;
  591. c1=0;
  592. mul_add_c(a[0],b[1],c2,c3,c1);
  593. mul_add_c(a[1],b[0],c2,c3,c1);
  594. r[1]=c2;
  595. c2=0;
  596. mul_add_c(a[2],b[0],c3,c1,c2);
  597. mul_add_c(a[1],b[1],c3,c1,c2);
  598. mul_add_c(a[0],b[2],c3,c1,c2);
  599. r[2]=c3;
  600. c3=0;
  601. mul_add_c(a[0],b[3],c1,c2,c3);
  602. mul_add_c(a[1],b[2],c1,c2,c3);
  603. mul_add_c(a[2],b[1],c1,c2,c3);
  604. mul_add_c(a[3],b[0],c1,c2,c3);
  605. r[3]=c1;
  606. c1=0;
  607. mul_add_c(a[3],b[1],c2,c3,c1);
  608. mul_add_c(a[2],b[2],c2,c3,c1);
  609. mul_add_c(a[1],b[3],c2,c3,c1);
  610. r[4]=c2;
  611. c2=0;
  612. mul_add_c(a[2],b[3],c3,c1,c2);
  613. mul_add_c(a[3],b[2],c3,c1,c2);
  614. r[5]=c3;
  615. c3=0;
  616. mul_add_c(a[3],b[3],c1,c2,c3);
  617. r[6]=c1;
  618. r[7]=c2;
  619. }
  620. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  621. {
  622. #ifdef BN_LLONG
  623. BN_ULLONG t,tt;
  624. #else
  625. BN_ULONG bl,bh;
  626. #endif
  627. BN_ULONG t1,t2;
  628. BN_ULONG c1,c2,c3;
  629. c1=0;
  630. c2=0;
  631. c3=0;
  632. sqr_add_c(a,0,c1,c2,c3);
  633. r[0]=c1;
  634. c1=0;
  635. sqr_add_c2(a,1,0,c2,c3,c1);
  636. r[1]=c2;
  637. c2=0;
  638. sqr_add_c(a,1,c3,c1,c2);
  639. sqr_add_c2(a,2,0,c3,c1,c2);
  640. r[2]=c3;
  641. c3=0;
  642. sqr_add_c2(a,3,0,c1,c2,c3);
  643. sqr_add_c2(a,2,1,c1,c2,c3);
  644. r[3]=c1;
  645. c1=0;
  646. sqr_add_c(a,2,c2,c3,c1);
  647. sqr_add_c2(a,3,1,c2,c3,c1);
  648. sqr_add_c2(a,4,0,c2,c3,c1);
  649. r[4]=c2;
  650. c2=0;
  651. sqr_add_c2(a,5,0,c3,c1,c2);
  652. sqr_add_c2(a,4,1,c3,c1,c2);
  653. sqr_add_c2(a,3,2,c3,c1,c2);
  654. r[5]=c3;
  655. c3=0;
  656. sqr_add_c(a,3,c1,c2,c3);
  657. sqr_add_c2(a,4,2,c1,c2,c3);
  658. sqr_add_c2(a,5,1,c1,c2,c3);
  659. sqr_add_c2(a,6,0,c1,c2,c3);
  660. r[6]=c1;
  661. c1=0;
  662. sqr_add_c2(a,7,0,c2,c3,c1);
  663. sqr_add_c2(a,6,1,c2,c3,c1);
  664. sqr_add_c2(a,5,2,c2,c3,c1);
  665. sqr_add_c2(a,4,3,c2,c3,c1);
  666. r[7]=c2;
  667. c2=0;
  668. sqr_add_c(a,4,c3,c1,c2);
  669. sqr_add_c2(a,5,3,c3,c1,c2);
  670. sqr_add_c2(a,6,2,c3,c1,c2);
  671. sqr_add_c2(a,7,1,c3,c1,c2);
  672. r[8]=c3;
  673. c3=0;
  674. sqr_add_c2(a,7,2,c1,c2,c3);
  675. sqr_add_c2(a,6,3,c1,c2,c3);
  676. sqr_add_c2(a,5,4,c1,c2,c3);
  677. r[9]=c1;
  678. c1=0;
  679. sqr_add_c(a,5,c2,c3,c1);
  680. sqr_add_c2(a,6,4,c2,c3,c1);
  681. sqr_add_c2(a,7,3,c2,c3,c1);
  682. r[10]=c2;
  683. c2=0;
  684. sqr_add_c2(a,7,4,c3,c1,c2);
  685. sqr_add_c2(a,6,5,c3,c1,c2);
  686. r[11]=c3;
  687. c3=0;
  688. sqr_add_c(a,6,c1,c2,c3);
  689. sqr_add_c2(a,7,5,c1,c2,c3);
  690. r[12]=c1;
  691. c1=0;
  692. sqr_add_c2(a,7,6,c2,c3,c1);
  693. r[13]=c2;
  694. c2=0;
  695. sqr_add_c(a,7,c3,c1,c2);
  696. r[14]=c3;
  697. r[15]=c1;
  698. }
  699. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  700. {
  701. #ifdef BN_LLONG
  702. BN_ULLONG t,tt;
  703. #else
  704. BN_ULONG bl,bh;
  705. #endif
  706. BN_ULONG t1,t2;
  707. BN_ULONG c1,c2,c3;
  708. c1=0;
  709. c2=0;
  710. c3=0;
  711. sqr_add_c(a,0,c1,c2,c3);
  712. r[0]=c1;
  713. c1=0;
  714. sqr_add_c2(a,1,0,c2,c3,c1);
  715. r[1]=c2;
  716. c2=0;
  717. sqr_add_c(a,1,c3,c1,c2);
  718. sqr_add_c2(a,2,0,c3,c1,c2);
  719. r[2]=c3;
  720. c3=0;
  721. sqr_add_c2(a,3,0,c1,c2,c3);
  722. sqr_add_c2(a,2,1,c1,c2,c3);
  723. r[3]=c1;
  724. c1=0;
  725. sqr_add_c(a,2,c2,c3,c1);
  726. sqr_add_c2(a,3,1,c2,c3,c1);
  727. r[4]=c2;
  728. c2=0;
  729. sqr_add_c2(a,3,2,c3,c1,c2);
  730. r[5]=c3;
  731. c3=0;
  732. sqr_add_c(a,3,c1,c2,c3);
  733. r[6]=c1;
  734. r[7]=c2;
  735. }
  736. #else /* !BN_MUL_COMBA */
  737. /* hmm... is it faster just to do a multiply? */
  738. #undef bn_sqr_comba4
  739. void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
  740. {
  741. BN_ULONG t[8];
  742. bn_sqr_normal(r,a,4,t);
  743. }
  744. #undef bn_sqr_comba8
  745. void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
  746. {
  747. BN_ULONG t[16];
  748. bn_sqr_normal(r,a,8,t);
  749. }
  750. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  751. {
  752. r[4]=bn_mul_words( &(r[0]),a,4,b[0]);
  753. r[5]=bn_mul_add_words(&(r[1]),a,4,b[1]);
  754. r[6]=bn_mul_add_words(&(r[2]),a,4,b[2]);
  755. r[7]=bn_mul_add_words(&(r[3]),a,4,b[3]);
  756. }
  757. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  758. {
  759. r[ 8]=bn_mul_words( &(r[0]),a,8,b[0]);
  760. r[ 9]=bn_mul_add_words(&(r[1]),a,8,b[1]);
  761. r[10]=bn_mul_add_words(&(r[2]),a,8,b[2]);
  762. r[11]=bn_mul_add_words(&(r[3]),a,8,b[3]);
  763. r[12]=bn_mul_add_words(&(r[4]),a,8,b[4]);
  764. r[13]=bn_mul_add_words(&(r[5]),a,8,b[5]);
  765. r[14]=bn_mul_add_words(&(r[6]),a,8,b[6]);
  766. r[15]=bn_mul_add_words(&(r[7]),a,8,b[7]);
  767. }
  768. #endif /* !BN_MUL_COMBA */