X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/Security/libsecurity_cryptkit/ckutils/atomTime/atomTime.c diff --git a/Security/libsecurity_cryptkit/ckutils/atomTime/atomTime.c b/Security/libsecurity_cryptkit/ckutils/atomTime/atomTime.c new file mode 100644 index 00000000..d4eec127 --- /dev/null +++ b/Security/libsecurity_cryptkit/ckutils/atomTime/atomTime.c @@ -0,0 +1,662 @@ +/* + * atomTime.c - measure performance of mulg, gsquare, feemod, + * gshift{left,right}, elliptic, ellMulProj + */ + +#include "ckconfig.h" +#include "ckutilsPlatform.h" +#include "CryptKitSA.h" +#include "ckutilities.h" /* needs private headers */ +#include "curveParams.h" /* ditto */ +#include "falloc.h" /* ditto */ +#include "elliptic.h" +#include "ellipticProj.h" +#include +#include +#include + +/* default loops for "fast" and "slow" ops respecitively */ +#define LOOPS_DEF_FAST 10000 +#define LOOPS_DEF_SLOW 100 + +#define NUM_BORROW 100 + +/* individual enables - normally all on, disable to zero in on one test */ +#define MULG_ENABLE 1 +#define GSQUARE_ENABLE 1 +#define FEEMOD_ENABLE 1 +#define BORROW_ENABLE 1 +#define SHIFT_ENABLE 1 +#define BINVAUX_ENABLE 1 +#define MAKE_RECIP_ENABLE 1 +#define MODGRECIP_ENABLE 1 +#define KEYGEN_ENABLE 1 +#define ELLIPTIC_ENABLE 1 +#define ELL_SIMPLE_ENABLE 1 + +static void usage(char **argv) +{ + printf("Usage: %s [l=loops_fast] [L=loops_slow] [q(uick)] [D=depth]\n", argv[0]); + exit(1); +} + +/* + * Fill numGiants with random data of length bits. + */ +static void genRandGiants(giant *giants, + unsigned numGiants, + unsigned bits, + feeRand rand) +{ + int i; + giant g; + unsigned char *rdata; + unsigned bytes = (bits + 7) / 8; + giantDigit mask = 0; + unsigned giantMsd = 0; // index of MSD + unsigned log2BitsPerDigit; + unsigned bitsPerDigit; + + /* just to satisfy compiler - make sure it's always called */ + if(giants == NULL) { + return; + } + + log2BitsPerDigit = GIANT_LOG2_BITS_PER_DIGIT; + bitsPerDigit = GIANT_BITS_PER_DIGIT; + + if((bits & 7) != 0) { + /* + * deserializeGiant() has a resolution of one byte. We + * need more resolution - that is, we'll be creating + * giants a little larger than we need, and we'll mask off + * some bits in the giants' m.s. digit. + * This assumes that data fills the giantDigits such + * that if bytes mod GIANT_BYTES_PER_DIGIT != 0, the + * empty byte(s) in the MSD are in the m.s. byte(s). + */ + giantMsd = bits >> log2BitsPerDigit; + mask = (1 << (bits & (bitsPerDigit - 1))) - 1; + } + rdata = fmalloc(bytes); + for(i=0; in[giantMsd] &= mask; + + /* + * We've zeroed out some bits; we might have to + * adjust the sign of the giant as well. Note that + * deserializeGiant always yields positive + * giants.... + */ + for(j=(g->sign - 1); j!=0; j--) { + if(g->n[j] == 0) { + (g->sign)--; + } + else { + break; + } + } + } + } + ffree(rdata); + return; +} + +#if CRYPTKIT_ELL_PROJ_ENABLE +/* + * Assumes the presence of numEllPoints items in *points, and that the + * x coordinate in each point is init'd to a random giant. Uses the x + * coords as seeds to make normalized points of the entire *points array. + */ +static void makePoints(pointProjStruct *points, + unsigned numEllPoints, + curveParams *cp) +{ + int i; + giant seed = newGiant(cp->maxDigits); + + for(i=0; i maxLoops) { + maxLoops = loopsSlow; + } + + /* + * Alloc array of giants big enough for squaring at the largest + * key size, enough of them for 'loops' mulgs + */ + cp = curveParamsForDepth(FEE_DEPTH_LARGEST); + numGiants = maxLoops * 2; // 2 giants per loop + if(loopsSlow > (maxLoops / 4)) { + /* findPointProj needs 4 giants per loop */ + numGiants *= 4; + } + #if CRYPTKIT_ELL_PROJ_ENABLE + numEllGiants = loopsSlow * 2; + numEllPoints = loopsSlow * 2; + #endif + giants = fmalloc(numGiants * sizeof(giant)); + if(giants == NULL) { + printf("malloc failure\n"); + exit(1); + } + for(i=0; imaxDigits); + if(giants[i] == NULL) { + printf("malloc failure\n"); + exit(1); + } + } + freeCurveParams(cp); + + #if CRYPTKIT_ELL_PROJ_ENABLE + /* + * Projective points - two per ellLoop. The giants come from + * giants[]. We reserve an extra giant per point. + * We're assuming that numEllPoints < (4 * numGiants). + */ + points = fmalloc(numEllPoints * sizeof(pointProjStruct)); + if(points == NULL) { + printf("malloc failure\n"); + exit(1); + } + j=0; + for(i=0; icurveType) { + case FCT_Montgomery: + curveType = "FCT_Montgomery"; + break; + case FCT_Weierstrass: + curveType = "FCT_Weierstrass"; + break; + case FCT_General: + curveType = "FCT_General"; + break; + default: + printf("***Unknown curveType!\n"); + exit(1); + } + + switch(cp->primeType) { + case FPT_General: + printf("depth=%d; FPT_General, %s; keysize=%d;\n", + depth, curveType, bitlen(cp->basePrime)); + break; + case FPT_Mersenne: + printf("depth=%d; FPT_Mersenne, %s; q=%d\n", + depth, curveType, cp->q); + break; + default: + printf("depth=%d; FPT_FEE, %s; q=%d k=%d\n", + depth, curveType, cp->q, cp->k); + break; + } + basePrimeLen = bitlen(cp->basePrime); + + /* + * mulg test + * bitlen(giant) <= bitlen(basePrime); + * giants[n+1] *= giants[n] + */ + #if MULG_ENABLE + genRandGiants(giants, numGiants, basePrimeLen, rand); + PLAT_GET_TIME(startTime); + for(i=0; imaxDigits); + } + for(j=0; jbasePrime, giants[i]); + } + PLAT_GET_TIME(endTime); + elapsed = PLAT_GET_US(startTime, endTime); + printf(" binvaux: %12.2f us per op\n", + elapsed / loopsSlow); + #endif /* BINVAUX_ENABLE */ + + /* + * make_recip test + * bitlen(giant) <= bitlen(basePrime); + * make_recip(giants[n], giants[n+1] + */ + #if MAKE_RECIP_ENABLE + genRandGiants(giants, numGiants, basePrimeLen, rand); + PLAT_GET_TIME(startTime); + for(i=0; imaxDigits); + recip = borrowGiant(cp->maxDigits); + genRandGiants(&modGiant, 1, basePrimeLen, rand); + make_recip(modGiant, recip); + + PLAT_GET_TIME(startTime); + for(i=0; imaxDigits); + PLAT_GET_TIME(startTime); + for(i=0; in[0] = 1; + z->sign = 1; + elliptic(giants[i], z, giants[i+1], cp); + } + PLAT_GET_TIME(endTime); + elapsed = PLAT_GET_US(startTime, endTime); + printf(" elliptic: %12.2f us per op\n", + elapsed / (loopsSlow / 2)); + #endif /* ELLIPTIC_ENABLE*/ + + /* + * elliptic_simple test + * bitlen(giant) <= bitlen(basePrime); + * giants[n] *= giants[n+1] (elliptic mult) + */ + #if ELL_SIMPLE_ENABLE + genRandGiants(giants, numGiants, basePrimeLen, rand); + PLAT_GET_TIME(startTime); + for(i=0; icurveType != FCT_Weierstrass) { + goto loopEnd; + } + + #if CRYPTKIT_ELL_PROJ_ENABLE + /* + * ellMulProj test + * bitlen(giant) <= bitlen(basePrime); + * point[n+1] = point[n] * giants[4n+3] + * + * note we're cooking up way more giants than we have to; + * we really only need the x's and k's. But what the heck. + */ + genRandGiants(giants, 4 * numEllPoints, basePrimeLen, rand); + makePoints(points, numEllPoints, cp); + PLAT_GET_TIME(startTime); + for(i=0; i