expspeed.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* unused */
  2. /* crypto/bn/expspeed.c */
  3. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  4. * All rights reserved.
  5. *
  6. * This package is an SSL implementation written
  7. * by Eric Young (eay@cryptsoft.com).
  8. * The implementation was written so as to conform with Netscapes SSL.
  9. *
  10. * This library is free for commercial and non-commercial use as long as
  11. * the following conditions are aheared to. The following conditions
  12. * apply to all code found in this distribution, be it the RC4, RSA,
  13. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  14. * included with this distribution is covered by the same copyright terms
  15. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  16. *
  17. * Copyright remains Eric Young's, and as such any Copyright notices in
  18. * the code are not to be removed.
  19. * If this package is used in a product, Eric Young should be given attribution
  20. * as the author of the parts of the library used.
  21. * This can be in the form of a textual message at program startup or
  22. * in documentation (online or textual) provided with the package.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. * 1. Redistributions of source code must retain the copyright
  28. * notice, this list of conditions and the following disclaimer.
  29. * 2. Redistributions in binary form must reproduce the above copyright
  30. * notice, this list of conditions and the following disclaimer in the
  31. * documentation and/or other materials provided with the distribution.
  32. * 3. All advertising materials mentioning features or use of this software
  33. * must display the following acknowledgement:
  34. * "This product includes cryptographic software written by
  35. * Eric Young (eay@cryptsoft.com)"
  36. * The word 'cryptographic' can be left out if the rouines from the library
  37. * being used are not cryptographic related :-).
  38. * 4. If you include any Windows specific code (or a derivative thereof) from
  39. * the apps directory (application code) you must include an acknowledgement:
  40. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  43. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  46. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  47. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  48. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. *
  54. * The licence and distribution terms for any publically available version or
  55. * derivative of this code cannot be changed. i.e. this code cannot simply be
  56. * copied and put under another distribution licence
  57. * [including the GNU Public Licence.]
  58. */
  59. /* most of this code has been pilfered from my libdes speed.c program */
  60. #define BASENUM 5000
  61. #define NUM_START 0
  62. /*
  63. * determine timings for modexp, modmul, modsqr, gcd, Kronecker symbol,
  64. * modular inverse, or modular square roots
  65. */
  66. #define TEST_EXP
  67. #undef TEST_MUL
  68. #undef TEST_SQR
  69. #undef TEST_GCD
  70. #undef TEST_KRON
  71. #undef TEST_INV
  72. #undef TEST_SQRT
  73. #define P_MOD_64 9 /* least significant 6 bits for prime to be
  74. * used for BN_sqrt timings */
  75. #if defined(TEST_EXP) + defined(TEST_MUL) + defined(TEST_SQR) + defined(TEST_GCD) + defined(TEST_KRON) + defined(TEST_INV) +defined(TEST_SQRT) != 1
  76. # error "choose one test"
  77. #endif
  78. #if defined(TEST_INV) || defined(TEST_SQRT)
  79. # define C_PRIME
  80. static void genprime_cb(int p, int n, void *arg);
  81. #endif
  82. #undef PROG
  83. #define PROG bnspeed_main
  84. #include <stdio.h>
  85. #include <stdlib.h>
  86. #include <signal.h>
  87. #include <string.h>
  88. #include <openssl/crypto.h>
  89. #include <openssl/err.h>
  90. #include <openssl/rand.h>
  91. #if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
  92. # define TIMES
  93. #endif
  94. #ifndef _IRIX
  95. # include <time.h>
  96. #endif
  97. #ifdef TIMES
  98. # include <sys/types.h>
  99. # include <sys/times.h>
  100. #endif
  101. /*
  102. * Depending on the VMS version, the tms structure is perhaps defined. The
  103. * __TMS macro will show if it was. If it wasn't defined, we should undefine
  104. * TIMES, since that tells the rest of the program how things should be
  105. * handled. -- Richard Levitte
  106. */
  107. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
  108. # undef TIMES
  109. #endif
  110. #ifndef TIMES
  111. # include <sys/timeb.h>
  112. #endif
  113. #if defined(sun) || defined(__ultrix)
  114. # define _POSIX_SOURCE
  115. # include <limits.h>
  116. # include <sys/param.h>
  117. #endif
  118. #include <openssl/bn.h>
  119. #include <openssl/x509.h>
  120. /* The following if from times(3) man page. It may need to be changed */
  121. #ifndef HZ
  122. # ifndef CLK_TCK
  123. # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
  124. # define HZ 100.0
  125. # else /* _BSD_CLK_TCK_ */
  126. # define HZ ((double)_BSD_CLK_TCK_)
  127. # endif
  128. # else /* CLK_TCK */
  129. # define HZ ((double)CLK_TCK)
  130. # endif
  131. #endif
  132. #undef BUFSIZE
  133. #define BUFSIZE ((long)1024*8)
  134. int run = 0;
  135. static double Time_F(int s);
  136. #define START 0
  137. #define STOP 1
  138. static double Time_F(int s)
  139. {
  140. double ret;
  141. #ifdef TIMES
  142. static struct tms tstart, tend;
  143. if (s == START) {
  144. times(&tstart);
  145. return (0);
  146. } else {
  147. times(&tend);
  148. ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
  149. return ((ret < 1e-3) ? 1e-3 : ret);
  150. }
  151. #else /* !times() */
  152. static struct timeb tstart, tend;
  153. long i;
  154. if (s == START) {
  155. ftime(&tstart);
  156. return (0);
  157. } else {
  158. ftime(&tend);
  159. i = (long)tend.millitm - (long)tstart.millitm;
  160. ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1000.0;
  161. return ((ret < 0.001) ? 0.001 : ret);
  162. }
  163. #endif
  164. }
  165. #define NUM_SIZES 7
  166. #if NUM_START > NUM_SIZES
  167. # error "NUM_START > NUM_SIZES"
  168. #endif
  169. static int sizes[NUM_SIZES] = { 128, 256, 512, 1024, 2048, 4096, 8192 };
  170. static int mul_c[NUM_SIZES] =
  171. { 8 * 8 * 8 * 8 * 8 * 8, 8 * 8 * 8 * 8 * 8, 8 * 8 * 8 * 8, 8 * 8 * 8,
  172. 8 * 8, 8, 1
  173. };
  174. /*
  175. * static int sizes[NUM_SIZES]={59,179,299,419,539};
  176. */
  177. #define RAND_SEED(string) { const char str[] = string; RAND_seed(string, sizeof str); }
  178. void do_mul_exp(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx);
  179. int main(int argc, char **argv)
  180. {
  181. BN_CTX *ctx;
  182. BIGNUM *a, *b, *c, *r;
  183. #if 1
  184. if (!CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0))
  185. abort();
  186. #endif
  187. ctx = BN_CTX_new();
  188. a = BN_new();
  189. b = BN_new();
  190. c = BN_new();
  191. r = BN_new();
  192. while (!RAND_status())
  193. /* not enough bits */
  194. RAND_SEED("I demand a manual recount!");
  195. do_mul_exp(r, a, b, c, ctx);
  196. return 0;
  197. }
  198. void do_mul_exp(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
  199. {
  200. int i, k;
  201. double tm;
  202. long num;
  203. num = BASENUM;
  204. for (i = NUM_START; i < NUM_SIZES; i++) {
  205. #ifdef C_PRIME
  206. # ifdef TEST_SQRT
  207. if (!BN_set_word(a, 64))
  208. goto err;
  209. if (!BN_set_word(b, P_MOD_64))
  210. goto err;
  211. # define ADD a
  212. # define REM b
  213. # else
  214. # define ADD NULL
  215. # define REM NULL
  216. # endif
  217. if (!BN_generate_prime(c, sizes[i], 0, ADD, REM, genprime_cb, NULL))
  218. goto err;
  219. putc('\n', stderr);
  220. fflush(stderr);
  221. #endif
  222. for (k = 0; k < num; k++) {
  223. if (k % 50 == 0) { /* Average over num/50 different choices of
  224. * random numbers. */
  225. if (!BN_pseudo_rand(a, sizes[i], 1, 0))
  226. goto err;
  227. if (!BN_pseudo_rand(b, sizes[i], 1, 0))
  228. goto err;
  229. #ifndef C_PRIME
  230. if (!BN_pseudo_rand(c, sizes[i], 1, 1))
  231. goto err;
  232. #endif
  233. #ifdef TEST_SQRT
  234. if (!BN_mod_sqr(a, a, c, ctx))
  235. goto err;
  236. if (!BN_mod_sqr(b, b, c, ctx))
  237. goto err;
  238. #else
  239. if (!BN_nnmod(a, a, c, ctx))
  240. goto err;
  241. if (!BN_nnmod(b, b, c, ctx))
  242. goto err;
  243. #endif
  244. if (k == 0)
  245. Time_F(START);
  246. }
  247. #if defined(TEST_EXP)
  248. if (!BN_mod_exp(r, a, b, c, ctx))
  249. goto err;
  250. #elif defined(TEST_MUL)
  251. {
  252. int i = 0;
  253. for (i = 0; i < 50; i++)
  254. if (!BN_mod_mul(r, a, b, c, ctx))
  255. goto err;
  256. }
  257. #elif defined(TEST_SQR)
  258. {
  259. int i = 0;
  260. for (i = 0; i < 50; i++) {
  261. if (!BN_mod_sqr(r, a, c, ctx))
  262. goto err;
  263. if (!BN_mod_sqr(r, b, c, ctx))
  264. goto err;
  265. }
  266. }
  267. #elif defined(TEST_GCD)
  268. if (!BN_gcd(r, a, b, ctx))
  269. goto err;
  270. if (!BN_gcd(r, b, c, ctx))
  271. goto err;
  272. if (!BN_gcd(r, c, a, ctx))
  273. goto err;
  274. #elif defined(TEST_KRON)
  275. if (-2 == BN_kronecker(a, b, ctx))
  276. goto err;
  277. if (-2 == BN_kronecker(b, c, ctx))
  278. goto err;
  279. if (-2 == BN_kronecker(c, a, ctx))
  280. goto err;
  281. #elif defined(TEST_INV)
  282. if (!BN_mod_inverse(r, a, c, ctx))
  283. goto err;
  284. if (!BN_mod_inverse(r, b, c, ctx))
  285. goto err;
  286. #else /* TEST_SQRT */
  287. if (!BN_mod_sqrt(r, a, c, ctx))
  288. goto err;
  289. if (!BN_mod_sqrt(r, b, c, ctx))
  290. goto err;
  291. #endif
  292. }
  293. tm = Time_F(STOP);
  294. printf(
  295. #if defined(TEST_EXP)
  296. "modexp %4d ^ %4d %% %4d"
  297. #elif defined(TEST_MUL)
  298. "50*modmul %4d %4d %4d"
  299. #elif defined(TEST_SQR)
  300. "100*modsqr %4d %4d %4d"
  301. #elif defined(TEST_GCD)
  302. "3*gcd %4d %4d %4d"
  303. #elif defined(TEST_KRON)
  304. "3*kronecker %4d %4d %4d"
  305. #elif defined(TEST_INV)
  306. "2*inv %4d %4d mod %4d"
  307. #else /* TEST_SQRT */
  308. "2*sqrt [prime == %d (mod 64)] %4d %4d mod %4d"
  309. #endif
  310. " -> %8.6fms %5.1f (%ld)\n",
  311. #ifdef TEST_SQRT
  312. P_MOD_64,
  313. #endif
  314. sizes[i], sizes[i], sizes[i], tm * 1000.0 / num,
  315. tm * mul_c[i] / num, num);
  316. num /= 7;
  317. if (num <= 0)
  318. num = 1;
  319. }
  320. return;
  321. err:
  322. ERR_print_errors_fp(stderr);
  323. }
  324. #ifdef C_PRIME
  325. static void genprime_cb(int p, int n, void *arg)
  326. {
  327. char c = '*';
  328. if (p == 0)
  329. c = '.';
  330. if (p == 1)
  331. c = '+';
  332. if (p == 2)
  333. c = '*';
  334. if (p == 3)
  335. c = '\n';
  336. putc(c, stderr);
  337. fflush(stderr);
  338. (void)n;
  339. (void)arg;
  340. }
  341. #endif