You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

542 lines
16 KiB

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