x86_64-gcc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright 2002-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 "../bn_local.h"
  10. #if !(defined(__GNUC__) && __GNUC__>=2)
  11. # include "../bn_asm.c" /* kind of dirty hack for Sun Studio */
  12. #else
  13. /*-
  14. * x86_64 BIGNUM accelerator version 0.1, December 2002.
  15. *
  16. * Implemented by Andy Polyakov <appro@openssl.org> for the OpenSSL
  17. * project.
  18. *
  19. * Rights for redistribution and usage in source and binary forms are
  20. * granted according to the OpenSSL license. Warranty of any kind is
  21. * disclaimed.
  22. *
  23. * Q. Version 0.1? It doesn't sound like Andy, he used to assign real
  24. * versions, like 1.0...
  25. * A. Well, that's because this code is basically a quick-n-dirty
  26. * proof-of-concept hack. As you can see it's implemented with
  27. * inline assembler, which means that you're bound to GCC and that
  28. * there might be enough room for further improvement.
  29. *
  30. * Q. Why inline assembler?
  31. * A. x86_64 features own ABI which I'm not familiar with. This is
  32. * why I decided to let the compiler take care of subroutine
  33. * prologue/epilogue as well as register allocation. For reference.
  34. * Win64 implements different ABI for AMD64, different from Linux.
  35. *
  36. * Q. How much faster does it get?
  37. * A. 'apps/openssl speed rsa dsa' output with no-asm:
  38. *
  39. * sign verify sign/s verify/s
  40. * rsa 512 bits 0.0006s 0.0001s 1683.8 18456.2
  41. * rsa 1024 bits 0.0028s 0.0002s 356.0 6407.0
  42. * rsa 2048 bits 0.0172s 0.0005s 58.0 1957.8
  43. * rsa 4096 bits 0.1155s 0.0018s 8.7 555.6
  44. * sign verify sign/s verify/s
  45. * dsa 512 bits 0.0005s 0.0006s 2100.8 1768.3
  46. * dsa 1024 bits 0.0014s 0.0018s 692.3 559.2
  47. * dsa 2048 bits 0.0049s 0.0061s 204.7 165.0
  48. *
  49. * 'apps/openssl speed rsa dsa' output with this module:
  50. *
  51. * sign verify sign/s verify/s
  52. * rsa 512 bits 0.0004s 0.0000s 2767.1 33297.9
  53. * rsa 1024 bits 0.0012s 0.0001s 867.4 14674.7
  54. * rsa 2048 bits 0.0061s 0.0002s 164.0 5270.0
  55. * rsa 4096 bits 0.0384s 0.0006s 26.1 1650.8
  56. * sign verify sign/s verify/s
  57. * dsa 512 bits 0.0002s 0.0003s 4442.2 3786.3
  58. * dsa 1024 bits 0.0005s 0.0007s 1835.1 1497.4
  59. * dsa 2048 bits 0.0016s 0.0020s 620.4 504.6
  60. *
  61. * For the reference. IA-32 assembler implementation performs
  62. * very much like 64-bit code compiled with no-asm on the same
  63. * machine.
  64. */
  65. # undef mul
  66. # undef mul_add
  67. /*-
  68. * "m"(a), "+m"(r) is the way to favor DirectPath µ-code;
  69. * "g"(0) let the compiler to decide where does it
  70. * want to keep the value of zero;
  71. */
  72. # define mul_add(r,a,word,carry) do { \
  73. register BN_ULONG high,low; \
  74. asm ("mulq %3" \
  75. : "=a"(low),"=d"(high) \
  76. : "a"(word),"m"(a) \
  77. : "cc"); \
  78. asm ("addq %2,%0; adcq %3,%1" \
  79. : "+r"(carry),"+d"(high)\
  80. : "a"(low),"g"(0) \
  81. : "cc"); \
  82. asm ("addq %2,%0; adcq %3,%1" \
  83. : "+m"(r),"+d"(high) \
  84. : "r"(carry),"g"(0) \
  85. : "cc"); \
  86. carry=high; \
  87. } while (0)
  88. # define mul(r,a,word,carry) do { \
  89. register BN_ULONG high,low; \
  90. asm ("mulq %3" \
  91. : "=a"(low),"=d"(high) \
  92. : "a"(word),"g"(a) \
  93. : "cc"); \
  94. asm ("addq %2,%0; adcq %3,%1" \
  95. : "+r"(carry),"+d"(high)\
  96. : "a"(low),"g"(0) \
  97. : "cc"); \
  98. (r)=carry, carry=high; \
  99. } while (0)
  100. # undef sqr
  101. # define sqr(r0,r1,a) \
  102. asm ("mulq %2" \
  103. : "=a"(r0),"=d"(r1) \
  104. : "a"(a) \
  105. : "cc");
  106. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
  107. BN_ULONG w)
  108. {
  109. BN_ULONG c1 = 0;
  110. if (num <= 0)
  111. return c1;
  112. while (num & ~3) {
  113. mul_add(rp[0], ap[0], w, c1);
  114. mul_add(rp[1], ap[1], w, c1);
  115. mul_add(rp[2], ap[2], w, c1);
  116. mul_add(rp[3], ap[3], w, c1);
  117. ap += 4;
  118. rp += 4;
  119. num -= 4;
  120. }
  121. if (num) {
  122. mul_add(rp[0], ap[0], w, c1);
  123. if (--num == 0)
  124. return c1;
  125. mul_add(rp[1], ap[1], w, c1);
  126. if (--num == 0)
  127. return c1;
  128. mul_add(rp[2], ap[2], w, c1);
  129. return c1;
  130. }
  131. return c1;
  132. }
  133. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  134. {
  135. BN_ULONG c1 = 0;
  136. if (num <= 0)
  137. return c1;
  138. while (num & ~3) {
  139. mul(rp[0], ap[0], w, c1);
  140. mul(rp[1], ap[1], w, c1);
  141. mul(rp[2], ap[2], w, c1);
  142. mul(rp[3], ap[3], w, c1);
  143. ap += 4;
  144. rp += 4;
  145. num -= 4;
  146. }
  147. if (num) {
  148. mul(rp[0], ap[0], w, c1);
  149. if (--num == 0)
  150. return c1;
  151. mul(rp[1], ap[1], w, c1);
  152. if (--num == 0)
  153. return c1;
  154. mul(rp[2], ap[2], w, c1);
  155. }
  156. return c1;
  157. }
  158. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  159. {
  160. if (n <= 0)
  161. return;
  162. while (n & ~3) {
  163. sqr(r[0], r[1], a[0]);
  164. sqr(r[2], r[3], a[1]);
  165. sqr(r[4], r[5], a[2]);
  166. sqr(r[6], r[7], a[3]);
  167. a += 4;
  168. r += 8;
  169. n -= 4;
  170. }
  171. if (n) {
  172. sqr(r[0], r[1], a[0]);
  173. if (--n == 0)
  174. return;
  175. sqr(r[2], r[3], a[1]);
  176. if (--n == 0)
  177. return;
  178. sqr(r[4], r[5], a[2]);
  179. }
  180. }
  181. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  182. {
  183. BN_ULONG ret, waste;
  184. asm("divq %4":"=a"(ret), "=d"(waste)
  185. : "a"(l), "d"(h), "r"(d)
  186. : "cc");
  187. return ret;
  188. }
  189. BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  190. int n)
  191. {
  192. BN_ULONG ret;
  193. size_t i = 0;
  194. if (n <= 0)
  195. return 0;
  196. asm volatile (" subq %0,%0 \n" /* clear carry */
  197. " jmp 1f \n"
  198. ".p2align 4 \n"
  199. "1: movq (%4,%2,8),%0 \n"
  200. " adcq (%5,%2,8),%0 \n"
  201. " movq %0,(%3,%2,8) \n"
  202. " lea 1(%2),%2 \n"
  203. " dec %1 \n"
  204. " jnz 1b \n"
  205. " sbbq %0,%0 \n"
  206. :"=&r" (ret), "+c"(n), "+r"(i)
  207. :"r"(rp), "r"(ap), "r"(bp)
  208. :"cc", "memory");
  209. return ret & 1;
  210. }
  211. # ifndef SIMICS
  212. BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  213. int n)
  214. {
  215. BN_ULONG ret;
  216. size_t i = 0;
  217. if (n <= 0)
  218. return 0;
  219. asm volatile (" subq %0,%0 \n" /* clear borrow */
  220. " jmp 1f \n"
  221. ".p2align 4 \n"
  222. "1: movq (%4,%2,8),%0 \n"
  223. " sbbq (%5,%2,8),%0 \n"
  224. " movq %0,(%3,%2,8) \n"
  225. " lea 1(%2),%2 \n"
  226. " dec %1 \n"
  227. " jnz 1b \n"
  228. " sbbq %0,%0 \n"
  229. :"=&r" (ret), "+c"(n), "+r"(i)
  230. :"r"(rp), "r"(ap), "r"(bp)
  231. :"cc", "memory");
  232. return ret & 1;
  233. }
  234. # else
  235. /* Simics 1.4<7 has buggy sbbq:-( */
  236. # define BN_MASK2 0xffffffffffffffffL
  237. BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
  238. {
  239. BN_ULONG t1, t2;
  240. int c = 0;
  241. if (n <= 0)
  242. return (BN_ULONG)0;
  243. for (;;) {
  244. t1 = a[0];
  245. t2 = b[0];
  246. r[0] = (t1 - t2 - c) & BN_MASK2;
  247. if (t1 != t2)
  248. c = (t1 < t2);
  249. if (--n <= 0)
  250. break;
  251. t1 = a[1];
  252. t2 = b[1];
  253. r[1] = (t1 - t2 - c) & BN_MASK2;
  254. if (t1 != t2)
  255. c = (t1 < t2);
  256. if (--n <= 0)
  257. break;
  258. t1 = a[2];
  259. t2 = b[2];
  260. r[2] = (t1 - t2 - c) & BN_MASK2;
  261. if (t1 != t2)
  262. c = (t1 < t2);
  263. if (--n <= 0)
  264. break;
  265. t1 = a[3];
  266. t2 = b[3];
  267. r[3] = (t1 - t2 - c) & BN_MASK2;
  268. if (t1 != t2)
  269. c = (t1 < t2);
  270. if (--n <= 0)
  271. break;
  272. a += 4;
  273. b += 4;
  274. r += 4;
  275. }
  276. return c;
  277. }
  278. # endif
  279. /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
  280. /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
  281. /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
  282. /*
  283. * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
  284. * c=(c2,c1,c0)
  285. */
  286. /*
  287. * Keep in mind that carrying into high part of multiplication result
  288. * can not overflow, because it cannot be all-ones.
  289. */
  290. # if 0
  291. /* original macros are kept for reference purposes */
  292. # define mul_add_c(a,b,c0,c1,c2) do { \
  293. BN_ULONG ta = (a), tb = (b); \
  294. BN_ULONG lo, hi; \
  295. BN_UMULT_LOHI(lo,hi,ta,tb); \
  296. c0 += lo; hi += (c0<lo)?1:0; \
  297. c1 += hi; c2 += (c1<hi)?1:0; \
  298. } while(0)
  299. # define mul_add_c2(a,b,c0,c1,c2) do { \
  300. BN_ULONG ta = (a), tb = (b); \
  301. BN_ULONG lo, hi, tt; \
  302. BN_UMULT_LOHI(lo,hi,ta,tb); \
  303. c0 += lo; tt = hi+((c0<lo)?1:0); \
  304. c1 += tt; c2 += (c1<tt)?1:0; \
  305. c0 += lo; hi += (c0<lo)?1:0; \
  306. c1 += hi; c2 += (c1<hi)?1:0; \
  307. } while(0)
  308. # define sqr_add_c(a,i,c0,c1,c2) do { \
  309. BN_ULONG ta = (a)[i]; \
  310. BN_ULONG lo, hi; \
  311. BN_UMULT_LOHI(lo,hi,ta,ta); \
  312. c0 += lo; hi += (c0<lo)?1:0; \
  313. c1 += hi; c2 += (c1<hi)?1:0; \
  314. } while(0)
  315. # else
  316. # define mul_add_c(a,b,c0,c1,c2) do { \
  317. BN_ULONG t1,t2; \
  318. asm ("mulq %3" \
  319. : "=a"(t1),"=d"(t2) \
  320. : "a"(a),"m"(b) \
  321. : "cc"); \
  322. asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
  323. : "+r"(c0),"+r"(c1),"+r"(c2) \
  324. : "r"(t1),"r"(t2),"g"(0) \
  325. : "cc"); \
  326. } while (0)
  327. # define sqr_add_c(a,i,c0,c1,c2) do { \
  328. BN_ULONG t1,t2; \
  329. asm ("mulq %2" \
  330. : "=a"(t1),"=d"(t2) \
  331. : "a"(a[i]) \
  332. : "cc"); \
  333. asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
  334. : "+r"(c0),"+r"(c1),"+r"(c2) \
  335. : "r"(t1),"r"(t2),"g"(0) \
  336. : "cc"); \
  337. } while (0)
  338. # define mul_add_c2(a,b,c0,c1,c2) do { \
  339. BN_ULONG t1,t2; \
  340. asm ("mulq %3" \
  341. : "=a"(t1),"=d"(t2) \
  342. : "a"(a),"m"(b) \
  343. : "cc"); \
  344. asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
  345. : "+r"(c0),"+r"(c1),"+r"(c2) \
  346. : "r"(t1),"r"(t2),"g"(0) \
  347. : "cc"); \
  348. asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
  349. : "+r"(c0),"+r"(c1),"+r"(c2) \
  350. : "r"(t1),"r"(t2),"g"(0) \
  351. : "cc"); \
  352. } while (0)
  353. # endif
  354. # define sqr_add_c2(a,i,j,c0,c1,c2) \
  355. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  356. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  357. {
  358. BN_ULONG c1, c2, c3;
  359. c1 = 0;
  360. c2 = 0;
  361. c3 = 0;
  362. mul_add_c(a[0], b[0], c1, c2, c3);
  363. r[0] = c1;
  364. c1 = 0;
  365. mul_add_c(a[0], b[1], c2, c3, c1);
  366. mul_add_c(a[1], b[0], c2, c3, c1);
  367. r[1] = c2;
  368. c2 = 0;
  369. mul_add_c(a[2], b[0], c3, c1, c2);
  370. mul_add_c(a[1], b[1], c3, c1, c2);
  371. mul_add_c(a[0], b[2], c3, c1, c2);
  372. r[2] = c3;
  373. c3 = 0;
  374. mul_add_c(a[0], b[3], c1, c2, c3);
  375. mul_add_c(a[1], b[2], c1, c2, c3);
  376. mul_add_c(a[2], b[1], c1, c2, c3);
  377. mul_add_c(a[3], b[0], c1, c2, c3);
  378. r[3] = c1;
  379. c1 = 0;
  380. mul_add_c(a[4], b[0], c2, c3, c1);
  381. mul_add_c(a[3], b[1], c2, c3, c1);
  382. mul_add_c(a[2], b[2], c2, c3, c1);
  383. mul_add_c(a[1], b[3], c2, c3, c1);
  384. mul_add_c(a[0], b[4], c2, c3, c1);
  385. r[4] = c2;
  386. c2 = 0;
  387. mul_add_c(a[0], b[5], c3, c1, c2);
  388. mul_add_c(a[1], b[4], c3, c1, c2);
  389. mul_add_c(a[2], b[3], c3, c1, c2);
  390. mul_add_c(a[3], b[2], c3, c1, c2);
  391. mul_add_c(a[4], b[1], c3, c1, c2);
  392. mul_add_c(a[5], b[0], c3, c1, c2);
  393. r[5] = c3;
  394. c3 = 0;
  395. mul_add_c(a[6], b[0], c1, c2, c3);
  396. mul_add_c(a[5], b[1], c1, c2, c3);
  397. mul_add_c(a[4], b[2], c1, c2, c3);
  398. mul_add_c(a[3], b[3], c1, c2, c3);
  399. mul_add_c(a[2], b[4], c1, c2, c3);
  400. mul_add_c(a[1], b[5], c1, c2, c3);
  401. mul_add_c(a[0], b[6], c1, c2, c3);
  402. r[6] = c1;
  403. c1 = 0;
  404. mul_add_c(a[0], b[7], c2, c3, c1);
  405. mul_add_c(a[1], b[6], c2, c3, c1);
  406. mul_add_c(a[2], b[5], c2, c3, c1);
  407. mul_add_c(a[3], b[4], c2, c3, c1);
  408. mul_add_c(a[4], b[3], c2, c3, c1);
  409. mul_add_c(a[5], b[2], c2, c3, c1);
  410. mul_add_c(a[6], b[1], c2, c3, c1);
  411. mul_add_c(a[7], b[0], c2, c3, c1);
  412. r[7] = c2;
  413. c2 = 0;
  414. mul_add_c(a[7], b[1], c3, c1, c2);
  415. mul_add_c(a[6], b[2], c3, c1, c2);
  416. mul_add_c(a[5], b[3], c3, c1, c2);
  417. mul_add_c(a[4], b[4], c3, c1, c2);
  418. mul_add_c(a[3], b[5], c3, c1, c2);
  419. mul_add_c(a[2], b[6], c3, c1, c2);
  420. mul_add_c(a[1], b[7], c3, c1, c2);
  421. r[8] = c3;
  422. c3 = 0;
  423. mul_add_c(a[2], b[7], c1, c2, c3);
  424. mul_add_c(a[3], b[6], c1, c2, c3);
  425. mul_add_c(a[4], b[5], c1, c2, c3);
  426. mul_add_c(a[5], b[4], c1, c2, c3);
  427. mul_add_c(a[6], b[3], c1, c2, c3);
  428. mul_add_c(a[7], b[2], c1, c2, c3);
  429. r[9] = c1;
  430. c1 = 0;
  431. mul_add_c(a[7], b[3], c2, c3, c1);
  432. mul_add_c(a[6], b[4], c2, c3, c1);
  433. mul_add_c(a[5], b[5], c2, c3, c1);
  434. mul_add_c(a[4], b[6], c2, c3, c1);
  435. mul_add_c(a[3], b[7], c2, c3, c1);
  436. r[10] = c2;
  437. c2 = 0;
  438. mul_add_c(a[4], b[7], c3, c1, c2);
  439. mul_add_c(a[5], b[6], c3, c1, c2);
  440. mul_add_c(a[6], b[5], c3, c1, c2);
  441. mul_add_c(a[7], b[4], c3, c1, c2);
  442. r[11] = c3;
  443. c3 = 0;
  444. mul_add_c(a[7], b[5], c1, c2, c3);
  445. mul_add_c(a[6], b[6], c1, c2, c3);
  446. mul_add_c(a[5], b[7], c1, c2, c3);
  447. r[12] = c1;
  448. c1 = 0;
  449. mul_add_c(a[6], b[7], c2, c3, c1);
  450. mul_add_c(a[7], b[6], c2, c3, c1);
  451. r[13] = c2;
  452. c2 = 0;
  453. mul_add_c(a[7], b[7], c3, c1, c2);
  454. r[14] = c3;
  455. r[15] = c1;
  456. }
  457. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  458. {
  459. BN_ULONG c1, c2, c3;
  460. c1 = 0;
  461. c2 = 0;
  462. c3 = 0;
  463. mul_add_c(a[0], b[0], c1, c2, c3);
  464. r[0] = c1;
  465. c1 = 0;
  466. mul_add_c(a[0], b[1], c2, c3, c1);
  467. mul_add_c(a[1], b[0], c2, c3, c1);
  468. r[1] = c2;
  469. c2 = 0;
  470. mul_add_c(a[2], b[0], c3, c1, c2);
  471. mul_add_c(a[1], b[1], c3, c1, c2);
  472. mul_add_c(a[0], b[2], c3, c1, c2);
  473. r[2] = c3;
  474. c3 = 0;
  475. mul_add_c(a[0], b[3], c1, c2, c3);
  476. mul_add_c(a[1], b[2], c1, c2, c3);
  477. mul_add_c(a[2], b[1], c1, c2, c3);
  478. mul_add_c(a[3], b[0], c1, c2, c3);
  479. r[3] = c1;
  480. c1 = 0;
  481. mul_add_c(a[3], b[1], c2, c3, c1);
  482. mul_add_c(a[2], b[2], c2, c3, c1);
  483. mul_add_c(a[1], b[3], c2, c3, c1);
  484. r[4] = c2;
  485. c2 = 0;
  486. mul_add_c(a[2], b[3], c3, c1, c2);
  487. mul_add_c(a[3], b[2], c3, c1, c2);
  488. r[5] = c3;
  489. c3 = 0;
  490. mul_add_c(a[3], b[3], c1, c2, c3);
  491. r[6] = c1;
  492. r[7] = c2;
  493. }
  494. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  495. {
  496. BN_ULONG c1, c2, c3;
  497. c1 = 0;
  498. c2 = 0;
  499. c3 = 0;
  500. sqr_add_c(a, 0, c1, c2, c3);
  501. r[0] = c1;
  502. c1 = 0;
  503. sqr_add_c2(a, 1, 0, c2, c3, c1);
  504. r[1] = c2;
  505. c2 = 0;
  506. sqr_add_c(a, 1, c3, c1, c2);
  507. sqr_add_c2(a, 2, 0, c3, c1, c2);
  508. r[2] = c3;
  509. c3 = 0;
  510. sqr_add_c2(a, 3, 0, c1, c2, c3);
  511. sqr_add_c2(a, 2, 1, c1, c2, c3);
  512. r[3] = c1;
  513. c1 = 0;
  514. sqr_add_c(a, 2, c2, c3, c1);
  515. sqr_add_c2(a, 3, 1, c2, c3, c1);
  516. sqr_add_c2(a, 4, 0, c2, c3, c1);
  517. r[4] = c2;
  518. c2 = 0;
  519. sqr_add_c2(a, 5, 0, c3, c1, c2);
  520. sqr_add_c2(a, 4, 1, c3, c1, c2);
  521. sqr_add_c2(a, 3, 2, c3, c1, c2);
  522. r[5] = c3;
  523. c3 = 0;
  524. sqr_add_c(a, 3, c1, c2, c3);
  525. sqr_add_c2(a, 4, 2, c1, c2, c3);
  526. sqr_add_c2(a, 5, 1, c1, c2, c3);
  527. sqr_add_c2(a, 6, 0, c1, c2, c3);
  528. r[6] = c1;
  529. c1 = 0;
  530. sqr_add_c2(a, 7, 0, c2, c3, c1);
  531. sqr_add_c2(a, 6, 1, c2, c3, c1);
  532. sqr_add_c2(a, 5, 2, c2, c3, c1);
  533. sqr_add_c2(a, 4, 3, c2, c3, c1);
  534. r[7] = c2;
  535. c2 = 0;
  536. sqr_add_c(a, 4, c3, c1, c2);
  537. sqr_add_c2(a, 5, 3, c3, c1, c2);
  538. sqr_add_c2(a, 6, 2, c3, c1, c2);
  539. sqr_add_c2(a, 7, 1, c3, c1, c2);
  540. r[8] = c3;
  541. c3 = 0;
  542. sqr_add_c2(a, 7, 2, c1, c2, c3);
  543. sqr_add_c2(a, 6, 3, c1, c2, c3);
  544. sqr_add_c2(a, 5, 4, c1, c2, c3);
  545. r[9] = c1;
  546. c1 = 0;
  547. sqr_add_c(a, 5, c2, c3, c1);
  548. sqr_add_c2(a, 6, 4, c2, c3, c1);
  549. sqr_add_c2(a, 7, 3, c2, c3, c1);
  550. r[10] = c2;
  551. c2 = 0;
  552. sqr_add_c2(a, 7, 4, c3, c1, c2);
  553. sqr_add_c2(a, 6, 5, c3, c1, c2);
  554. r[11] = c3;
  555. c3 = 0;
  556. sqr_add_c(a, 6, c1, c2, c3);
  557. sqr_add_c2(a, 7, 5, c1, c2, c3);
  558. r[12] = c1;
  559. c1 = 0;
  560. sqr_add_c2(a, 7, 6, c2, c3, c1);
  561. r[13] = c2;
  562. c2 = 0;
  563. sqr_add_c(a, 7, c3, c1, c2);
  564. r[14] = c3;
  565. r[15] = c1;
  566. }
  567. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  568. {
  569. BN_ULONG c1, c2, c3;
  570. c1 = 0;
  571. c2 = 0;
  572. c3 = 0;
  573. sqr_add_c(a, 0, c1, c2, c3);
  574. r[0] = c1;
  575. c1 = 0;
  576. sqr_add_c2(a, 1, 0, c2, c3, c1);
  577. r[1] = c2;
  578. c2 = 0;
  579. sqr_add_c(a, 1, c3, c1, c2);
  580. sqr_add_c2(a, 2, 0, c3, c1, c2);
  581. r[2] = c3;
  582. c3 = 0;
  583. sqr_add_c2(a, 3, 0, c1, c2, c3);
  584. sqr_add_c2(a, 2, 1, c1, c2, c3);
  585. r[3] = c1;
  586. c1 = 0;
  587. sqr_add_c(a, 2, c2, c3, c1);
  588. sqr_add_c2(a, 3, 1, c2, c3, c1);
  589. r[4] = c2;
  590. c2 = 0;
  591. sqr_add_c2(a, 3, 2, c3, c1, c2);
  592. r[5] = c3;
  593. c3 = 0;
  594. sqr_add_c(a, 3, c1, c2, c3);
  595. r[6] = c1;
  596. r[7] = c2;
  597. }
  598. #endif