expspeed.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /* determine timings for modexp, modmul, modsqr, gcd, Kronecker symbol,
  63. * modular inverse, or modular square roots */
  64. #define TEST_EXP
  65. #undef TEST_MUL
  66. #undef TEST_SQR
  67. #undef TEST_GCD
  68. #undef TEST_KRON
  69. #undef TEST_INV
  70. #undef TEST_SQRT
  71. #define P_MOD_64 9 /* least significant 6 bits for prime to be used for BN_sqrt timings */
  72. #if defined(TEST_EXP) + defined(TEST_MUL) + defined(TEST_SQR) + defined(TEST_GCD) + defined(TEST_KRON) + defined(TEST_INV) +defined(TEST_SQRT) != 1
  73. # error "choose one test"
  74. #endif
  75. #if defined(TEST_INV) || defined(TEST_SQRT)
  76. # define C_PRIME
  77. static void genprime_cb(int p, int n, void *arg);
  78. #endif
  79. #undef PROG
  80. #define PROG bnspeed_main
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <signal.h>
  84. #include <string.h>
  85. #include <openssl/crypto.h>
  86. #include <openssl/err.h>
  87. #include <openssl/rand.h>
  88. #if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
  89. #define TIMES
  90. #endif
  91. #ifndef _IRIX
  92. #include <time.h>
  93. #endif
  94. #ifdef TIMES
  95. #include <sys/types.h>
  96. #include <sys/times.h>
  97. #endif
  98. /* Depending on the VMS version, the tms structure is perhaps defined.
  99. The __TMS macro will show if it was. If it wasn't defined, we should
  100. undefine TIMES, since that tells the rest of the program how things
  101. should be handled. -- Richard Levitte */
  102. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
  103. #undef TIMES
  104. #endif
  105. #ifndef TIMES
  106. #include <sys/timeb.h>
  107. #endif
  108. #if defined(sun) || defined(__ultrix)
  109. #define _POSIX_SOURCE
  110. #include <limits.h>
  111. #include <sys/param.h>
  112. #endif
  113. #include <openssl/bn.h>
  114. #include <openssl/x509.h>
  115. /* The following if from times(3) man page. It may need to be changed */
  116. #ifndef HZ
  117. # ifndef CLK_TCK
  118. # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
  119. # define HZ 100.0
  120. # else /* _BSD_CLK_TCK_ */
  121. # define HZ ((double)_BSD_CLK_TCK_)
  122. # endif
  123. # else /* CLK_TCK */
  124. # define HZ ((double)CLK_TCK)
  125. # endif
  126. #endif
  127. #undef BUFSIZE
  128. #define BUFSIZE ((long)1024*8)
  129. int run=0;
  130. static double Time_F(int s);
  131. #define START 0
  132. #define STOP 1
  133. static double Time_F(int s)
  134. {
  135. double ret;
  136. #ifdef TIMES
  137. static struct tms tstart,tend;
  138. if (s == START)
  139. {
  140. times(&tstart);
  141. return(0);
  142. }
  143. else
  144. {
  145. times(&tend);
  146. ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
  147. return((ret < 1e-3)?1e-3:ret);
  148. }
  149. #else /* !times() */
  150. static struct timeb tstart,tend;
  151. long i;
  152. if (s == START)
  153. {
  154. ftime(&tstart);
  155. return(0);
  156. }
  157. else
  158. {
  159. ftime(&tend);
  160. i=(long)tend.millitm-(long)tstart.millitm;
  161. ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
  162. return((ret < 0.001)?0.001:ret);
  163. }
  164. #endif
  165. }
  166. #define NUM_SIZES 7
  167. #if NUM_START > NUM_SIZES
  168. # error "NUM_START > NUM_SIZES"
  169. #endif
  170. static int sizes[NUM_SIZES]={128,256,512,1024,2048,4096,8192};
  171. static int mul_c[NUM_SIZES]={8*8*8*8*8*8,8*8*8*8*8,8*8*8*8,8*8*8,8*8,8,1};
  172. /*static int sizes[NUM_SIZES]={59,179,299,419,539}; */
  173. #define RAND_SEED(string) { const char str[] = string; RAND_seed(string, sizeof str); }
  174. void do_mul_exp(BIGNUM *r,BIGNUM *a,BIGNUM *b,BIGNUM *c,BN_CTX *ctx);
  175. int main(int argc, char **argv)
  176. {
  177. BN_CTX *ctx;
  178. BIGNUM *a,*b,*c,*r;
  179. #if 1
  180. if (!CRYPTO_set_mem_debug_functions(0,0,0,0,0))
  181. abort();
  182. #endif
  183. ctx=BN_CTX_new();
  184. a=BN_new();
  185. b=BN_new();
  186. c=BN_new();
  187. r=BN_new();
  188. while (!RAND_status())
  189. /* not enough bits */
  190. RAND_SEED("I demand a manual recount!");
  191. do_mul_exp(r,a,b,c,ctx);
  192. return 0;
  193. }
  194. void do_mul_exp(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
  195. {
  196. int i,k;
  197. double tm;
  198. long num;
  199. num=BASENUM;
  200. for (i=NUM_START; i<NUM_SIZES; i++)
  201. {
  202. #ifdef C_PRIME
  203. # ifdef TEST_SQRT
  204. if (!BN_set_word(a, 64)) goto err;
  205. if (!BN_set_word(b, P_MOD_64)) goto err;
  206. # define ADD a
  207. # define REM b
  208. # else
  209. # define ADD NULL
  210. # define REM NULL
  211. # endif
  212. if (!BN_generate_prime(c,sizes[i],0,ADD,REM,genprime_cb,NULL)) goto err;
  213. putc('\n', stderr);
  214. fflush(stderr);
  215. #endif
  216. for (k=0; k<num; k++)
  217. {
  218. if (k%50 == 0) /* Average over num/50 different choices of random numbers. */
  219. {
  220. if (!BN_pseudo_rand(a,sizes[i],1,0)) goto err;
  221. if (!BN_pseudo_rand(b,sizes[i],1,0)) goto err;
  222. #ifndef C_PRIME
  223. if (!BN_pseudo_rand(c,sizes[i],1,1)) goto err;
  224. #endif
  225. #ifdef TEST_SQRT
  226. if (!BN_mod_sqr(a,a,c,ctx)) goto err;
  227. if (!BN_mod_sqr(b,b,c,ctx)) goto err;
  228. #else
  229. if (!BN_nnmod(a,a,c,ctx)) goto err;
  230. if (!BN_nnmod(b,b,c,ctx)) goto err;
  231. #endif
  232. if (k == 0)
  233. Time_F(START);
  234. }
  235. #if defined(TEST_EXP)
  236. if (!BN_mod_exp(r,a,b,c,ctx)) goto err;
  237. #elif defined(TEST_MUL)
  238. {
  239. int i = 0;
  240. for (i = 0; i < 50; i++)
  241. if (!BN_mod_mul(r,a,b,c,ctx)) goto err;
  242. }
  243. #elif defined(TEST_SQR)
  244. {
  245. int i = 0;
  246. for (i = 0; i < 50; i++)
  247. {
  248. if (!BN_mod_sqr(r,a,c,ctx)) goto err;
  249. if (!BN_mod_sqr(r,b,c,ctx)) goto err;
  250. }
  251. }
  252. #elif defined(TEST_GCD)
  253. if (!BN_gcd(r,a,b,ctx)) goto err;
  254. if (!BN_gcd(r,b,c,ctx)) goto err;
  255. if (!BN_gcd(r,c,a,ctx)) goto err;
  256. #elif defined(TEST_KRON)
  257. if (-2 == BN_kronecker(a,b,ctx)) goto err;
  258. if (-2 == BN_kronecker(b,c,ctx)) goto err;
  259. if (-2 == BN_kronecker(c,a,ctx)) goto err;
  260. #elif defined(TEST_INV)
  261. if (!BN_mod_inverse(r,a,c,ctx)) goto err;
  262. if (!BN_mod_inverse(r,b,c,ctx)) goto err;
  263. #else /* TEST_SQRT */
  264. if (!BN_mod_sqrt(r,a,c,ctx)) goto err;
  265. if (!BN_mod_sqrt(r,b,c,ctx)) goto err;
  266. #endif
  267. }
  268. tm=Time_F(STOP);
  269. printf(
  270. #if defined(TEST_EXP)
  271. "modexp %4d ^ %4d %% %4d"
  272. #elif defined(TEST_MUL)
  273. "50*modmul %4d %4d %4d"
  274. #elif defined(TEST_SQR)
  275. "100*modsqr %4d %4d %4d"
  276. #elif defined(TEST_GCD)
  277. "3*gcd %4d %4d %4d"
  278. #elif defined(TEST_KRON)
  279. "3*kronecker %4d %4d %4d"
  280. #elif defined(TEST_INV)
  281. "2*inv %4d %4d mod %4d"
  282. #else /* TEST_SQRT */
  283. "2*sqrt [prime == %d (mod 64)] %4d %4d mod %4d"
  284. #endif
  285. " -> %8.6fms %5.1f (%ld)\n",
  286. #ifdef TEST_SQRT
  287. P_MOD_64,
  288. #endif
  289. sizes[i],sizes[i],sizes[i],tm*1000.0/num,tm*mul_c[i]/num, num);
  290. num/=7;
  291. if (num <= 0) num=1;
  292. }
  293. return;
  294. err:
  295. ERR_print_errors_fp(stderr);
  296. }
  297. #ifdef C_PRIME
  298. static void genprime_cb(int p, int n, void *arg)
  299. {
  300. char c='*';
  301. if (p == 0) c='.';
  302. if (p == 1) c='+';
  303. if (p == 2) c='*';
  304. if (p == 3) c='\n';
  305. putc(c, stderr);
  306. fflush(stderr);
  307. (void)n;
  308. (void)arg;
  309. }
  310. #endif