1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
* Copyright (c) 1997 Stanford University
*
* The use of this software for revenue-generating purposes may require a
* license from the owners of the underlying intellectual property.
* Specifically, the SRP protocol may not be used for revenue-generating
* purposes without license.
*
* Within that constraint, permission to use, copy, modify, and distribute
* this software and its documentation for any purpose is hereby granted
* without fee, provided that the above copyright notices and this permission
* notice appear in all copies of the software and related documentation.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <sys/time.h>
#include "t_defines.h"
#include "t_pwd.h"
static unsigned char testmod[] =
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
static unsigned char testbase[] = { 0x80, 0x00 };
static unsigned char testexp[] = { 0x31, 0x41, 0x59 };
static unsigned char modulus[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22,
0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B,
0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B,
0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5,
0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
static unsigned char smallbase[] = { 2 };
static unsigned char bigbase[] = {
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x02, 0x46, 0x8A, 0xCE, 0x13, 0x57, 0x9B, 0xDF,
0x03, 0x69, 0xCF, 0x25, 0x8B, 0xE1, 0x47, 0xAD,
0x04, 0x8C, 0x15, 0x9D, 0x26, 0xAE, 0x37, 0xBF,
0x05, 0xAF, 0x49, 0xE3, 0x8D, 0x27, 0xC1, 0x6B,
0x06, 0xC2, 0x8E, 0x4A, 0x17, 0xD3, 0x9F, 0x5B,
0x07, 0xE5, 0xC3, 0xA1, 0x8F, 0x6D, 0x4B, 0x29,
0x08, 0x19, 0x2A, 0x3B, 0x4C, 0x5D, 0x6E, 0x7F,
0x09, 0x2B, 0x4D, 0x6F, 0x81, 0xA3, 0xC5, 0xE7,
0x0A, 0x4E, 0x82, 0xC6, 0x1B, 0x5F, 0x93, 0xD7,
0x0B, 0x61, 0xC7, 0x2D, 0x8e, 0xE9, 0x4F, 0xA5,
0x0C, 0x84, 0x1D, 0x95, 0x2E, 0xA6, 0x3F, 0xB7,
0x0D, 0xA7, 0x41, 0xEB, 0x85, 0x2F, 0xC9, 0x63,
0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87,
0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2B, 0x1A, 0x0F
};
static unsigned char smallexp[] = { 0xFD, 0xB9, 0x75, 0x31 };
static unsigned char bigexp[] = {
0xF7, 0xE6, 0xD5, 0xC4, 0xB3, 0xA2, 0x91, 0x80,
0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01,
0x08, 0x19, 0x2A, 0x3B, 0x4C, 0x5D, 0x6E, 0x7F
};
#define NRUNS 10
#define PERF_NORM 671 /* Standardized to 167MHz UltraSparc-1 */
double
bench_modexp(b, e, m)
BigInteger b, e, m;
{
BigInteger r;
struct timeval before, after;
unsigned long elapsedus;
int i;
r = BigIntegerFromInt(0);
gettimeofday(&before, NULL);
for(i = 0; i < NRUNS; ++i)
BigIntegerModExp(r, b, e, m);
gettimeofday(&after, NULL);
elapsedus = 1000000 * (after.tv_sec - before.tv_sec) +
after.tv_usec - before.tv_usec;
return (double) elapsedus / NRUNS / 1000000;
}
main(argc, argv)
int argc;
char **argv;
{
BigInteger b, m, e, r;
char hexbuf[MAXHEXPARAMLEN];
double t;
int tb, te, tg;
printf("math library test: ");
printf("0x%s ^ ", t_tohex(hexbuf, testbase, sizeof(testbase)));
b = BigIntegerFromBytes(testbase, sizeof(testbase));
printf("0x%s mod ", t_tohex(hexbuf, testexp, sizeof(testexp)));
e = BigIntegerFromBytes(testexp, sizeof(testexp));
printf("0x%s = ", t_tohex(hexbuf, testmod, sizeof(testmod)));
m = BigIntegerFromBytes(testmod, sizeof(testmod));
r = BigIntegerFromInt(0);
BigIntegerModExp(r, b, e, m);
BigIntegerToHex(r, hexbuf);
printf("0x%s\n", hexbuf);
printf("(correct result is 0x1cebdf2e340234)\n\n");
BigIntegerFree(b);
BigIntegerFree(e);
BigIntegerFree(m);
BigIntegerFree(r);
m = BigIntegerFromBytes(modulus, sizeof(modulus));
b = BigIntegerFromBytes(smallbase, sizeof(smallbase));
e = BigIntegerFromBytes(bigexp, sizeof(bigexp));
tg = (int) (1000 * bench_modexp(b, e, m));
printf("t[g] = %d ms\n", tg);
BigIntegerFree(b);
b = BigIntegerFromBytes(bigbase, sizeof(bigbase));
tb = (int) (1000 * bench_modexp(b, e, m));
printf("t[b] = %d ms\n", tb);
BigIntegerFree(e);
e = BigIntegerFromBytes(smallexp, sizeof(smallexp));
te = (int) (1000 * bench_modexp(b, e, m));
printf("t[e] = %d ms\n", te);
printf("\nSRPbench total: %d\n", tb + tg + te);
printf("Performance index: %.3f\n", (double) PERF_NORM / (tb + tg + te));
BigIntegerFree(b);
BigIntegerFree(e);
BigIntegerFree(m);
return 0;
}
|