]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/ckutils/giantBench/giantBench.c
2 * Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include "giantIntegers.h"
26 #include "ckutilities.h"
27 #include "feeFunctions.h"
30 #include "ckutilsPlatform.h"
35 #define MIN_SIZE_DEF 4 /* min giant size in bytes */
36 #define MAX_SIZE_DEF 32 /* max in bytes */
37 #define LOOP_NOTIFY 100
40 static void usage(char **argv
)
42 printf("usage: %s [options]\n", argv
[0]);
43 printf(" Options:\n");
44 printf(" l=loops (default = %d)\n", LOOPS_DEF
);
45 printf(" n=maxBytes (default = %d\n", MIN_SIZE_DEF
);
46 printf(" x=maxBytes (default = %d\n", MAX_SIZE_DEF
);
47 printf(" o (use old 16-bit CryptKit\n");
54 * Fill buffer with random data.
56 static void fillData(unsigned bufSize
,
65 intCount
= bufSize
>> 2;
67 for(i
=0; i
<intCount
; i
++) {
71 residue
= bufSize
& 0x3;
72 cp
= (unsigned char *)ip
;
73 for(i
=0; i
<residue
; i
++) {
74 *cp
++ = (unsigned char)RAND();
79 * fill a pre-allocd giant with specified number of bytes of random
80 * data. *Buf is mallocd and uninitialized and will change here.
82 static void genGiant(giant g
,
88 fillData(numBytes
, buf
);
89 deserializeGiant(buf
, g
, numBytes
);
91 /* set random sign; deserializeGiant() is always positive */
97 /* avoid zero data - too many pitfalls with mod and div */
105 * Init giant arrays with random data.
107 static void initRandGiants(unsigned numBytes
,
115 for(i
=0; i
<numGiants
; i
++) {
116 genGiant(g1
[i
], numBytes
, buf
);
117 genGiant(g2
[i
], numBytes
, buf
);
122 * Individual tests. API is identical for all tests.
124 * loops : number of ops to perform.
125 * g1, g2 : arrays of giants with random data and sign. Tests may modify
126 * these. Size of array = 'loops'. Capacity big enough for all
128 * Return : total microseconds to do 'loops' ops.
131 static int mulgTest(unsigned loops
,
139 PLAT_GET_TIME(startTime
);
140 for(loop
=0; loop
<loops
; loop
++) {
143 PLAT_GET_TIME(endTime
);
144 return PLAT_GET_NS(startTime
, endTime
);
147 static int squareTest(unsigned loops
,
155 PLAT_GET_TIME(startTime
);
156 for(loop
=0; loop
<loops
; loop
++) {
159 PLAT_GET_TIME(endTime
);
160 return PLAT_GET_NS(startTime
, endTime
);
164 int main(int argc
, char **argv
)
170 unsigned char *buf
; // random data
174 unsigned mulgElapsed
;
177 int loops
= LOOPS_DEF
;
180 unsigned maxSize
= MAX_SIZE_DEF
;
181 unsigned minSize
= MIN_SIZE_DEF
;
187 argc
= ccommand(&argv
);
190 for(arg
=1; arg
<argc
; arg
++) {
194 maxSize
= atoi(&argp
[2]);
197 minSize
= atoi(&argp
[2]);
200 loops
= atoi(&argp
[2]);
206 seed
= atoi(&argp
[2]);
214 buf
= malloc(maxSize
);
219 seed
= (unsigned)tim
;
224 * Scratch giants, big enough for anything. Malloc here, init with
225 * random data before each test
226 * note these mallocs will be too big in the useOld case...
228 g1
= malloc(sizeof(giant
) * loops
);
229 g2
= malloc(sizeof(giant
) * loops
);
230 if((g1
== NULL
) || (g2
== NULL
)) {
231 printf("malloc error\n");
235 numDigits
= ((2 * maxSize
) + 1) / 2;
238 numDigits
= BYTES_TO_GIANT_DIGITS(2 * maxSize
);
240 for(i
=0; i
<loops
; i
++) {
241 g1
[i
] = newGiant(numDigits
);
242 g2
[i
] = newGiant(numDigits
);
243 if((g1
[i
] == NULL
) || (g2
[i
] == NULL
)) {
244 printf("malloc error\n");
249 printf("Starting giants test: seed %d\n", seed
);
250 for(numBytes
=minSize
; numBytes
<=maxSize
; numBytes
*=2) {
252 initRandGiants(numBytes
,
258 mulgElapsed
= mulgTest(loops
, g1
, g2
);
259 initRandGiants(numBytes
,
265 sqrElapsed
= squareTest(loops
, g1
, g2
);
266 printf(" bits : %4d mulg : %3d ns gsquare : %3d ns\n",