michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mpi.h" michael@0: #include "mplogic.h" michael@0: #include "ecl.h" michael@0: #include "ecp.h" michael@0: #include "ecl-priv.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* Returns 2^e as an integer. This is meant to be used for small powers of michael@0: * two. */ michael@0: int ec_twoTo(int e); michael@0: michael@0: /* Number of bits of scalar to test */ michael@0: #define BITSIZE 160 michael@0: michael@0: /* Time k repetitions of operation op. */ michael@0: #define M_TimeOperation(op, k) { \ michael@0: double dStart, dNow, dUserTime; \ michael@0: struct rusage ru; \ michael@0: int i; \ michael@0: getrusage(RUSAGE_SELF, &ru); \ michael@0: dStart = (double)ru.ru_utime.tv_sec+(double)ru.ru_utime.tv_usec*0.000001; \ michael@0: for (i = 0; i < k; i++) { \ michael@0: { op; } \ michael@0: }; \ michael@0: getrusage(RUSAGE_SELF, &ru); \ michael@0: dNow = (double)ru.ru_utime.tv_sec+(double)ru.ru_utime.tv_usec*0.000001; \ michael@0: dUserTime = dNow-dStart; \ michael@0: if (dUserTime) printf(" %-45s\n k: %6i, t: %6.2f sec\n", #op, k, dUserTime); \ michael@0: } michael@0: michael@0: /* Tests wNAF computation. Non-adjacent-form is discussed in the paper: D. michael@0: * Hankerson, J. Hernandez and A. Menezes, "Software implementation of michael@0: * elliptic curve cryptography over binary fields", Proc. CHES 2000. */ michael@0: michael@0: mp_err michael@0: main(void) michael@0: { michael@0: signed char naf[BITSIZE + 1]; michael@0: ECGroup *group = NULL; michael@0: mp_int k; michael@0: mp_int *scalar; michael@0: int i, count; michael@0: int res; michael@0: int w = 5; michael@0: char s[1000]; michael@0: michael@0: /* Get a 160 bit scalar to compute wNAF from */ michael@0: group = ECGroup_fromName(ECCurve_SECG_PRIME_160R1); michael@0: scalar = &group->genx; michael@0: michael@0: /* Compute wNAF representation of scalar */ michael@0: ec_compute_wNAF(naf, BITSIZE, scalar, w); michael@0: michael@0: /* Verify correctness of representation */ michael@0: mp_init(&k); /* init k to 0 */ michael@0: michael@0: for (i = BITSIZE; i >= 0; i--) { michael@0: mp_add(&k, &k, &k); michael@0: /* digits in mp_???_d are unsigned */ michael@0: if (naf[i] >= 0) { michael@0: mp_add_d(&k, naf[i], &k); michael@0: } else { michael@0: mp_sub_d(&k, -naf[i], &k); michael@0: } michael@0: } michael@0: michael@0: if (mp_cmp(&k, scalar) != 0) { michael@0: printf("Error: incorrect NAF value.\n"); michael@0: MP_CHECKOK(mp_toradix(&k, s, 16)); michael@0: printf("NAF value %s\n", s); michael@0: MP_CHECKOK(mp_toradix(scalar, s, 16)); michael@0: printf("original value %s\n", s); michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: /* Verify digits of representation are valid */ michael@0: for (i = 0; i <= BITSIZE; i++) { michael@0: if (naf[i] % 2 == 0 && naf[i] != 0) { michael@0: printf("Error: Even non-zero digit found.\n"); michael@0: goto CLEANUP; michael@0: } michael@0: if (naf[i] < -(ec_twoTo(w - 1)) || naf[i] >= ec_twoTo(w - 1)) { michael@0: printf("Error: Magnitude of naf digit too large.\n"); michael@0: goto CLEANUP; michael@0: } michael@0: } michael@0: michael@0: /* Verify sparsity of representation */ michael@0: count = w - 1; michael@0: for (i = 0; i <= BITSIZE; i++) { michael@0: if (naf[i] != 0) { michael@0: if (count < w - 1) { michael@0: printf("Error: Sparsity failed.\n"); michael@0: goto CLEANUP; michael@0: } michael@0: count = 0; michael@0: } else michael@0: count++; michael@0: } michael@0: michael@0: /* Check timing */ michael@0: M_TimeOperation(ec_compute_wNAF(naf, BITSIZE, scalar, w), 10000); michael@0: michael@0: printf("Test passed.\n"); michael@0: CLEANUP: michael@0: ECGroup_free(group); michael@0: return MP_OKAY; michael@0: }