1/* 2 * Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25#include <security_cryptkit/giantPortCommon.h> 26#include <security_cryptkit/feeDebug.h> 27#include <security_cryptkit/feeFunctions.h> 28#include <stdlib.h> 29#include "ckutilsPlatform.h" 30#include <stdio.h> 31#include <time.h> 32 33#define LOOPS_DEF 10000 34#define MIN_SIZE_DEF 1 /* mix digits for vectorMultiply test */ 35#define MAX_SIZE_DEF 8 /* max digits */ 36 37 38static void usage(char **argv) 39{ 40 printf("usage: %s [options]\n", argv[0]); 41 printf(" Options:\n"); 42 printf(" l=loops (default = %d)\n", LOOPS_DEF); 43 printf(" n=maxDigits (default = %d)\n", MIN_SIZE_DEF); 44 printf(" x=maxDigits (default = %d)\n", MAX_SIZE_DEF); 45 printf(" s=seed\n"); 46 printf(" h(elp)\n"); 47 exit(1); 48} 49 50/* 51 * Fill buffer with random data. Assumes giantDigits is native int size. 52 */ 53static void randDigits(unsigned numDigits, 54 giantDigit *digits) 55{ 56 int i; 57 58 for(i=0; i<numDigits; i++) { 59 /* RAND() only returns 31 bits on Unix.... */ 60 digits[i]= RAND() + ((RAND() & 1) << 31); 61 } 62} 63 64 65 66int main(int argc, char **argv) 67{ 68 int arg; 69 char *argp; 70 giantDigit *digit1; // mallocd arrays 71 giantDigit *digit2; 72 giantDigit *vect1; 73 giantDigit *vect2; 74 giantDigit *dig1p; // ptr into mallocd arrays 75 giantDigit *dig2p; 76 giantDigit *vect1p; 77 giantDigit *vect2p; 78 unsigned numDigits; 79 unsigned i; 80 PLAT_TIME startTime; 81 PLAT_TIME endTime; 82 unsigned elapsed; 83 giantDigit scr1; // op result 84 giantDigit scr2; // op result 85 int loops = LOOPS_DEF; 86 int seedSpec = 0; 87 unsigned seed = 0; 88 unsigned maxSize = MAX_SIZE_DEF; 89 unsigned minSize = MIN_SIZE_DEF; 90 91 initCryptKit(); 92 93 #if macintosh 94 argc = ccommand(&argv); 95 #endif 96 97 for(arg=1; arg<argc; arg++) { 98 argp = argv[arg]; 99 switch(argp[0]) { 100 case 'x': 101 maxSize = atoi(&argp[2]); 102 break; 103 case 'n': 104 minSize = atoi(&argp[2]); 105 break; 106 case 'l': 107 loops = atoi(&argp[2]); 108 break; 109 case 's': 110 seed = atoi(&argp[2]); 111 seedSpec = 1; 112 break; 113 case 'h': 114 default: 115 usage(argv); 116 } 117 } 118 119 if(!seedSpec) { 120 unsigned long tim; 121 time(&tim); 122 seed = (unsigned)tim; 123 } 124 SRAND(seed); 125 126 /* 127 * Scratch digits, big enough for anything. Malloc here, init with 128 * random data before each test. 129 */ 130 digit1 = malloc(sizeof(giantDigit) * loops * 2); 131 digit2 = malloc(sizeof(giantDigit) * loops * 2); 132 133 /* vect1 and vect2 are arrays of giantDigit arrays */ 134 vect1 = malloc(sizeof(giantDigit) * loops * maxSize); 135 vect2 = malloc(sizeof(giantDigit) * loops * maxSize); 136 137 if((digit1 == NULL) || (digit1 == NULL) || 138 (vect1 == NULL) || (vect2 == NULL)) { 139 printf("malloc error\n"); 140 exit(1); 141 } 142 143 printf("Starting giantAsm test: seed %d\n", seed); 144 145 /* giantAddDigits test */ 146 randDigits(loops, digit1); 147 randDigits(loops, digit2); 148 dig1p = digit1; 149 dig2p = digit2; 150 PLAT_GET_TIME(startTime); 151 for(i=0; i<loops; i++) { 152 scr1 = giantAddDigits(*dig1p++, *dig2p++, &scr2); 153 } 154 PLAT_GET_TIME(endTime); 155 elapsed = PLAT_GET_NS(startTime, endTime); 156 printf("giantAddDigits: %f ns\n", 157 (double)elapsed / (double)loops); 158 159 /* giantAddDouble test */ 160 randDigits(loops, digit1); 161 randDigits(loops * 2, digit2); 162 dig1p = digit1; 163 dig2p = digit2; 164 PLAT_GET_TIME(startTime); 165 for(i=0; i<loops; i++) { 166 giantAddDouble(dig2p, dig2p+1, *dig1p++); 167 dig2p += 2; 168 } 169 PLAT_GET_TIME(endTime); 170 elapsed = PLAT_GET_NS(startTime, endTime); 171 printf("giantAddDouble: %f ns\n", 172 (double)elapsed / (double)loops); 173 174 /* giantSubDigits test */ 175 randDigits(loops, digit1); 176 randDigits(loops, digit2); 177 dig1p = digit1; 178 dig2p = digit2; 179 PLAT_GET_TIME(startTime); 180 for(i=0; i<loops; i++) { 181 scr1 = giantSubDigits(*dig1p++, *dig2p++, &scr2); 182 } 183 PLAT_GET_TIME(endTime); 184 elapsed = PLAT_GET_NS(startTime, endTime); 185 printf("giantSubDigits: %f ns\n", 186 (double)elapsed / (double)loops); 187 188 /* giantMulDigits test */ 189 randDigits(loops, digit1); 190 randDigits(loops, digit2); 191 dig1p = digit1; 192 dig2p = digit2; 193 PLAT_GET_TIME(startTime); 194 for(i=0; i<loops; i++) { 195 giantMulDigits(*dig1p++, *dig2p++, &scr1, &scr2); 196 } 197 PLAT_GET_TIME(endTime); 198 elapsed = PLAT_GET_NS(startTime, endTime); 199 printf("giantMulDigits: %f ns\n", 200 (double)elapsed / (double)loops); 201 202 printf("\nvectorMultiply:\n"); 203 for(numDigits=minSize; numDigits<=maxSize; numDigits*=2) { 204 205 randDigits(loops, digit1); // plierDigit 206 randDigits(loops * numDigits, vect1); // candVector 207 randDigits(loops * numDigits, vect2); // prodVector 208 dig1p = digit1; 209 vect1p = vect1; 210 vect2p = vect2; 211 212 PLAT_GET_TIME(startTime); 213 for(i=0; i<loops; i++) { 214 scr1 = VectorMultiply(*dig1p++, // plierDigit 215 vect1p, // candVector 216 numDigits, 217 vect2p); // prodVector 218 vect1p += numDigits; 219 vect2p += numDigits; 220 } 221 PLAT_GET_TIME(endTime); 222 elapsed = PLAT_GET_NS(startTime, endTime); 223 printf(" bits = %4d : %f ns\n", 224 numDigits * GIANT_BITS_PER_DIGIT, 225 (double)elapsed / (double)loops); 226 227 } /* for numDigits */ 228 return 0; 229} 230