bnspeed.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* unused */
  2. /* crypto/bn/bnspeed.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 1000000
  61. #undef PROG
  62. #define PROG bnspeed_main
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <signal.h>
  66. #include <string.h>
  67. #include <openssl/crypto.h>
  68. #include <openssl/err.h>
  69. #if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
  70. # define TIMES
  71. #endif
  72. #ifndef _IRIX
  73. # include <time.h>
  74. #endif
  75. #ifdef TIMES
  76. # include <sys/types.h>
  77. # include <sys/times.h>
  78. #endif
  79. /*
  80. * Depending on the VMS version, the tms structure is perhaps defined. The
  81. * __TMS macro will show if it was. If it wasn't defined, we should undefine
  82. * TIMES, since that tells the rest of the program how things should be
  83. * handled. -- Richard Levitte
  84. */
  85. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
  86. # undef TIMES
  87. #endif
  88. #ifndef TIMES
  89. # include <sys/timeb.h>
  90. #endif
  91. #if defined(sun) || defined(__ultrix)
  92. # define _POSIX_SOURCE
  93. # include <limits.h>
  94. # include <sys/param.h>
  95. #endif
  96. #include <openssl/bn.h>
  97. #include <openssl/x509.h>
  98. /* The following if from times(3) man page. It may need to be changed */
  99. #ifndef HZ
  100. # ifndef CLK_TCK
  101. # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
  102. # define HZ 100.0
  103. # else /* _BSD_CLK_TCK_ */
  104. # define HZ ((double)_BSD_CLK_TCK_)
  105. # endif
  106. # else /* CLK_TCK */
  107. # define HZ ((double)CLK_TCK)
  108. # endif
  109. #endif
  110. #undef BUFSIZE
  111. #define BUFSIZE ((long)1024*8)
  112. int run = 0;
  113. static double Time_F(int s);
  114. #define START 0
  115. #define STOP 1
  116. static double Time_F(int s)
  117. {
  118. double ret;
  119. #ifdef TIMES
  120. static struct tms tstart, tend;
  121. if (s == START) {
  122. times(&tstart);
  123. return (0);
  124. } else {
  125. times(&tend);
  126. ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
  127. return ((ret < 1e-3) ? 1e-3 : ret);
  128. }
  129. #else /* !times() */
  130. static struct timeb tstart, tend;
  131. long i;
  132. if (s == START) {
  133. ftime(&tstart);
  134. return (0);
  135. } else {
  136. ftime(&tend);
  137. i = (long)tend.millitm - (long)tstart.millitm;
  138. ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1000.0;
  139. return ((ret < 0.001) ? 0.001 : ret);
  140. }
  141. #endif
  142. }
  143. #define NUM_SIZES 5
  144. static int sizes[NUM_SIZES] = { 128, 256, 512, 1024, 2048 };
  145. /*
  146. * static int sizes[NUM_SIZES]={59,179,299,419,539};
  147. */
  148. void do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
  149. int main(int argc, char **argv)
  150. {
  151. BN_CTX *ctx;
  152. BIGNUM a, b, c;
  153. ctx = BN_CTX_new();
  154. BN_init(&a);
  155. BN_init(&b);
  156. BN_init(&c);
  157. do_mul(&a, &b, &c, ctx);
  158. }
  159. void do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  160. {
  161. int i, j, k;
  162. double tm;
  163. long num;
  164. for (i = 0; i < NUM_SIZES; i++) {
  165. num = BASENUM;
  166. if (i)
  167. num /= (i * 3);
  168. BN_rand(a, sizes[i], 1, 0);
  169. for (j = i; j < NUM_SIZES; j++) {
  170. BN_rand(b, sizes[j], 1, 0);
  171. Time_F(START);
  172. for (k = 0; k < num; k++)
  173. BN_mul(r, b, a, ctx);
  174. tm = Time_F(STOP);
  175. printf("mul %4d x %4d -> %8.3fms\n", sizes[i], sizes[j],
  176. tm * 1000.0 / num);
  177. }
  178. }
  179. for (i = 0; i < NUM_SIZES; i++) {
  180. num = BASENUM;
  181. if (i)
  182. num /= (i * 3);
  183. BN_rand(a, sizes[i], 1, 0);
  184. Time_F(START);
  185. for (k = 0; k < num; k++)
  186. BN_sqr(r, a, ctx);
  187. tm = Time_F(STOP);
  188. printf("sqr %4d x %4d -> %8.3fms\n", sizes[i], sizes[i],
  189. tm * 1000.0 / num);
  190. }
  191. for (i = 0; i < NUM_SIZES; i++) {
  192. num = BASENUM / 10;
  193. if (i)
  194. num /= (i * 3);
  195. BN_rand(a, sizes[i] - 1, 1, 0);
  196. for (j = i; j < NUM_SIZES; j++) {
  197. BN_rand(b, sizes[j], 1, 0);
  198. Time_F(START);
  199. for (k = 0; k < 100000; k++)
  200. BN_div(r, NULL, b, a, ctx);
  201. tm = Time_F(STOP);
  202. printf("div %4d / %4d -> %8.3fms\n", sizes[j], sizes[i] - 1,
  203. tm * 1000.0 / num);
  204. }
  205. }
  206. }