159191Skris/* unused */
259191Skris
355714Skris/* crypto/bn/bnspeed.c */
455714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
555714Skris * All rights reserved.
655714Skris *
755714Skris * This package is an SSL implementation written
855714Skris * by Eric Young (eay@cryptsoft.com).
955714Skris * The implementation was written so as to conform with Netscapes SSL.
10280304Sjkim *
1155714Skris * This library is free for commercial and non-commercial use as long as
1255714Skris * the following conditions are aheared to.  The following conditions
1355714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1455714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1555714Skris * included with this distribution is covered by the same copyright terms
1655714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17280304Sjkim *
1855714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1955714Skris * the code are not to be removed.
2055714Skris * If this package is used in a product, Eric Young should be given attribution
2155714Skris * as the author of the parts of the library used.
2255714Skris * This can be in the form of a textual message at program startup or
2355714Skris * in documentation (online or textual) provided with the package.
24280304Sjkim *
2555714Skris * Redistribution and use in source and binary forms, with or without
2655714Skris * modification, are permitted provided that the following conditions
2755714Skris * are met:
2855714Skris * 1. Redistributions of source code must retain the copyright
2955714Skris *    notice, this list of conditions and the following disclaimer.
3055714Skris * 2. Redistributions in binary form must reproduce the above copyright
3155714Skris *    notice, this list of conditions and the following disclaimer in the
3255714Skris *    documentation and/or other materials provided with the distribution.
3355714Skris * 3. All advertising materials mentioning features or use of this software
3455714Skris *    must display the following acknowledgement:
3555714Skris *    "This product includes cryptographic software written by
3655714Skris *     Eric Young (eay@cryptsoft.com)"
3755714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3855714Skris *    being used are not cryptographic related :-).
39280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
4055714Skris *    the apps directory (application code) you must include an acknowledgement:
4155714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42280304Sjkim *
4355714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4455714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4555714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4655714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4755714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4855714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4955714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5055714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5155714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5255714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5355714Skris * SUCH DAMAGE.
54280304Sjkim *
5555714Skris * The licence and distribution terms for any publically available version or
5655714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5755714Skris * copied and put under another distribution licence
5855714Skris * [including the GNU Public Licence.]
5955714Skris */
6055714Skris
6155714Skris/* most of this code has been pilfered from my libdes speed.c program */
6255714Skris
63280304Sjkim#define BASENUM 1000000
6455714Skris#undef PROG
6555714Skris#define PROG bnspeed_main
6655714Skris
6755714Skris#include <stdio.h>
6855714Skris#include <stdlib.h>
6955714Skris#include <signal.h>
7055714Skris#include <string.h>
7155714Skris#include <openssl/crypto.h>
7255714Skris#include <openssl/err.h>
7355714Skris
74109998Smarkm#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
75280304Sjkim# define TIMES
7655714Skris#endif
7755714Skris
7855714Skris#ifndef _IRIX
79280304Sjkim# include <time.h>
8055714Skris#endif
8155714Skris#ifdef TIMES
82280304Sjkim# include <sys/types.h>
83280304Sjkim# include <sys/times.h>
8455714Skris#endif
8555714Skris
86280304Sjkim/*
87280304Sjkim * Depending on the VMS version, the tms structure is perhaps defined. The
88280304Sjkim * __TMS macro will show if it was.  If it wasn't defined, we should undefine
89280304Sjkim * TIMES, since that tells the rest of the program how things should be
90280304Sjkim * handled.  -- Richard Levitte
91280304Sjkim */
92109998Smarkm#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
93280304Sjkim# undef TIMES
9455714Skris#endif
9555714Skris
9655714Skris#ifndef TIMES
97280304Sjkim# include <sys/timeb.h>
9855714Skris#endif
9955714Skris
10055714Skris#if defined(sun) || defined(__ultrix)
101280304Sjkim# define _POSIX_SOURCE
102280304Sjkim# include <limits.h>
103280304Sjkim# include <sys/param.h>
10455714Skris#endif
10555714Skris
10655714Skris#include <openssl/bn.h>
10755714Skris#include <openssl/x509.h>
10855714Skris
10955714Skris/* The following if from times(3) man page.  It may need to be changed */
11055714Skris#ifndef HZ
11155714Skris# ifndef CLK_TCK
112280304Sjkim#  ifndef _BSD_CLK_TCK_         /* FreeBSD hack */
113280304Sjkim#   define HZ   100.0
114280304Sjkim#  else                         /* _BSD_CLK_TCK_ */
11555714Skris#   define HZ ((double)_BSD_CLK_TCK_)
11655714Skris#  endif
117280304Sjkim# else                          /* CLK_TCK */
11855714Skris#  define HZ ((double)CLK_TCK)
11955714Skris# endif
12055714Skris#endif
12155714Skris
12255714Skris#undef BUFSIZE
123280304Sjkim#define BUFSIZE ((long)1024*8)
124280304Sjkimint run = 0;
12555714Skris
12655714Skrisstatic double Time_F(int s);
127280304Sjkim#define START   0
128280304Sjkim#define STOP    1
12955714Skris
13055714Skrisstatic double Time_F(int s)
131280304Sjkim{
132280304Sjkim    double ret;
13355714Skris#ifdef TIMES
134280304Sjkim    static struct tms tstart, tend;
13555714Skris
136280304Sjkim    if (s == START) {
137280304Sjkim        times(&tstart);
138280304Sjkim        return (0);
139280304Sjkim    } else {
140280304Sjkim        times(&tend);
141280304Sjkim        ret = ((double)(tend.tms_utime - tstart.tms_utime)) / HZ;
142280304Sjkim        return ((ret < 1e-3) ? 1e-3 : ret);
143280304Sjkim    }
144280304Sjkim#else                           /* !times() */
145280304Sjkim    static struct timeb tstart, tend;
146280304Sjkim    long i;
14755714Skris
148280304Sjkim    if (s == START) {
149280304Sjkim        ftime(&tstart);
150280304Sjkim        return (0);
151280304Sjkim    } else {
152280304Sjkim        ftime(&tend);
153280304Sjkim        i = (long)tend.millitm - (long)tstart.millitm;
154280304Sjkim        ret = ((double)(tend.time - tstart.time)) + ((double)i) / 1000.0;
155280304Sjkim        return ((ret < 0.001) ? 0.001 : ret);
156280304Sjkim    }
15755714Skris#endif
158280304Sjkim}
15955714Skris
160280304Sjkim#define NUM_SIZES       5
161280304Sjkimstatic int sizes[NUM_SIZES] = { 128, 256, 512, 1024, 2048 };
16255714Skris
163280304Sjkim/*
164280304Sjkim * static int sizes[NUM_SIZES]={59,179,299,419,539};
165280304Sjkim */
16655714Skris
167280304Sjkimvoid do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
168280304Sjkim
16955714Skrisint main(int argc, char **argv)
170280304Sjkim{
171280304Sjkim    BN_CTX *ctx;
172280304Sjkim    BIGNUM a, b, c;
17355714Skris
174280304Sjkim    ctx = BN_CTX_new();
175280304Sjkim    BN_init(&a);
176280304Sjkim    BN_init(&b);
177280304Sjkim    BN_init(&c);
17855714Skris
179280304Sjkim    do_mul(&a, &b, &c, ctx);
180280304Sjkim}
18155714Skris
18255714Skrisvoid do_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
183280304Sjkim{
184280304Sjkim    int i, j, k;
185280304Sjkim    double tm;
186280304Sjkim    long num;
18755714Skris
188280304Sjkim    for (i = 0; i < NUM_SIZES; i++) {
189280304Sjkim        num = BASENUM;
190280304Sjkim        if (i)
191280304Sjkim            num /= (i * 3);
192280304Sjkim        BN_rand(a, sizes[i], 1, 0);
193280304Sjkim        for (j = i; j < NUM_SIZES; j++) {
194280304Sjkim            BN_rand(b, sizes[j], 1, 0);
195280304Sjkim            Time_F(START);
196280304Sjkim            for (k = 0; k < num; k++)
197280304Sjkim                BN_mul(r, b, a, ctx);
198280304Sjkim            tm = Time_F(STOP);
199280304Sjkim            printf("mul %4d x %4d -> %8.3fms\n", sizes[i], sizes[j],
200280304Sjkim                   tm * 1000.0 / num);
201280304Sjkim        }
202280304Sjkim    }
20355714Skris
204280304Sjkim    for (i = 0; i < NUM_SIZES; i++) {
205280304Sjkim        num = BASENUM;
206280304Sjkim        if (i)
207280304Sjkim            num /= (i * 3);
208280304Sjkim        BN_rand(a, sizes[i], 1, 0);
209280304Sjkim        Time_F(START);
210280304Sjkim        for (k = 0; k < num; k++)
211280304Sjkim            BN_sqr(r, a, ctx);
212280304Sjkim        tm = Time_F(STOP);
213280304Sjkim        printf("sqr %4d x %4d -> %8.3fms\n", sizes[i], sizes[i],
214280304Sjkim               tm * 1000.0 / num);
215280304Sjkim    }
21655714Skris
217280304Sjkim    for (i = 0; i < NUM_SIZES; i++) {
218280304Sjkim        num = BASENUM / 10;
219280304Sjkim        if (i)
220280304Sjkim            num /= (i * 3);
221280304Sjkim        BN_rand(a, sizes[i] - 1, 1, 0);
222280304Sjkim        for (j = i; j < NUM_SIZES; j++) {
223280304Sjkim            BN_rand(b, sizes[j], 1, 0);
224280304Sjkim            Time_F(START);
225280304Sjkim            for (k = 0; k < 100000; k++)
226280304Sjkim                BN_div(r, NULL, b, a, ctx);
227280304Sjkim            tm = Time_F(STOP);
228280304Sjkim            printf("div %4d / %4d -> %8.3fms\n", sizes[j], sizes[i] - 1,
229280304Sjkim                   tm * 1000.0 / num);
230280304Sjkim        }
231280304Sjkim    }
232280304Sjkim}
233