#include "giantIntegers.h"
#include "ckutilities.h"
#include "feeFunctions.h"
#include "feeDebug.h"
#include <stdlib.h>
#include "ckutilsPlatform.h"
#include <stdio.h>
#include <time.h>
#define LOOPS_DEF 100
#define MIN_SIZE_DEF 4
#define MAX_SIZE_DEF 32
#define LOOP_NOTIFY 100
static void usage(char **argv)
{
printf("usage: %s [options]\n", argv[0]);
printf(" Options:\n");
printf(" l=loops (default = %d)\n", LOOPS_DEF);
printf(" n=maxBytes (default = %d\n", MIN_SIZE_DEF);
printf(" x=maxBytes (default = %d\n", MAX_SIZE_DEF);
printf(" o (use old 16-bit CryptKit\n");
printf(" s=seed\n");
printf(" h(elp)\n");
exit(1);
}
static void fillData(unsigned bufSize,
unsigned char *buf)
{
unsigned *ip;
unsigned intCount;
unsigned residue;
unsigned char *cp;
int i;
intCount = bufSize >> 2;
ip = (unsigned *)buf;
for(i=0; i<intCount; i++) {
*ip++ = RAND();
}
residue = bufSize & 0x3;
cp = (unsigned char *)ip;
for(i=0; i<residue; i++) {
*cp++ = (unsigned char)RAND();
}
}
static void genGiant(giant g,
unsigned numBytes,
unsigned char *buf)
{
int i;
fillData(numBytes, buf);
deserializeGiant(buf, g, numBytes);
i = RAND();
if(i & 1) {
g->sign = -g->sign;
}
while(isZero(g)) {
g->sign = 1;
g->n[0] = RAND();
}
}
static void initRandGiants(unsigned numBytes,
unsigned char *buf,
unsigned numGiants,
giant *g1,
giant *g2)
{
int i;
for(i=0; i<numGiants; i++) {
genGiant(g1[i], numBytes, buf);
genGiant(g2[i], numBytes, buf);
}
}
static int mulgTest(unsigned loops,
giant *g1,
giant *g2)
{
int loop;
PLAT_TIME startTime;
PLAT_TIME endTime;
PLAT_GET_TIME(startTime);
for(loop=0; loop<loops; loop++) {
mulg(*g1++, *g2++);
}
PLAT_GET_TIME(endTime);
return PLAT_GET_NS(startTime, endTime);
}
static int squareTest(unsigned loops,
giant *g1,
giant *g2)
{
int loop;
PLAT_TIME startTime;
PLAT_TIME endTime;
PLAT_GET_TIME(startTime);
for(loop=0; loop<loops; loop++) {
gsquare(*g1++);
}
PLAT_GET_TIME(endTime);
return PLAT_GET_NS(startTime, endTime);
}
int main(int argc, char **argv)
{
int arg;
char *argp;
giant *g1;
giant *g2; unsigned char *buf; unsigned numDigits;
unsigned i;
unsigned numBytes;
unsigned mulgElapsed;
unsigned sqrElapsed;
int loops = LOOPS_DEF;
int seedSpec = 0;
unsigned seed = 0;
unsigned maxSize = MAX_SIZE_DEF;
unsigned minSize = MIN_SIZE_DEF;
int useOld = 0;
initCryptKit();
#if macintosh
argc = ccommand(&argv);
#endif
for(arg=1; arg<argc; arg++) {
argp = argv[arg];
switch(argp[0]) {
case 'x':
maxSize = atoi(&argp[2]);
break;
case 'n':
minSize = atoi(&argp[2]);
break;
case 'l':
loops = atoi(&argp[2]);
break;
case 'o':
useOld = 1;
break;
case 's':
seed = atoi(&argp[2]);
seedSpec = 1;
break;
case 'h':
default:
usage(argv);
}
}
buf = malloc(maxSize);
if(!seedSpec) {
unsigned long tim;
time(&tim);
seed = (unsigned)tim;
}
SRAND(seed);
g1 = malloc(sizeof(giant) * loops);
g2 = malloc(sizeof(giant) * loops);
if((g1 == NULL) || (g2 == NULL)) {
printf("malloc error\n");
exit(1);
}
if(useOld) {
numDigits = ((2 * maxSize) + 1) / 2;
}
else {
numDigits = BYTES_TO_GIANT_DIGITS(2 * maxSize);
}
for(i=0; i<loops; i++) {
g1[i] = newGiant(numDigits);
g2[i] = newGiant(numDigits);
if((g1[i] == NULL) || (g2[i] == NULL)) {
printf("malloc error\n");
exit(1);
}
}
printf("Starting giants test: seed %d\n", seed);
for(numBytes=minSize; numBytes<=maxSize; numBytes*=2) {
initRandGiants(numBytes,
buf,
loops,
g1,
g2);
mulgElapsed = mulgTest(loops, g1, g2);
initRandGiants(numBytes,
buf,
loops,
g1,
g2);
sqrElapsed = squareTest(loops, g1, g2);
printf(" bits : %4d mulg : %3d ns gsquare : %3d ns\n",
numBytes * 8,
mulgElapsed / loops,
sqrElapsed / loops);
}
return 0;
}