bnspeed.c 6.5 KB

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