| OLD | NEW |
| (Empty) |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| 4 | |
| 5 #include "ecl-priv.h" | |
| 6 | |
| 7 /* Returns 2^e as an integer. This is meant to be used for small powers of | |
| 8 * two. */ | |
| 9 int | |
| 10 ec_twoTo(int e) | |
| 11 { | |
| 12 int a = 1; | |
| 13 int i; | |
| 14 | |
| 15 for (i = 0; i < e; i++) { | |
| 16 a *= 2; | |
| 17 } | |
| 18 return a; | |
| 19 } | |
| 20 | |
| 21 /* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should | |
| 22 * be an array of signed char's to output to, bitsize should be the number | |
| 23 * of bits of out, in is the original scalar, and w is the window size. | |
| 24 * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A. | |
| 25 * Menezes, "Software implementation of elliptic curve cryptography over | |
| 26 * binary fields", Proc. CHES 2000. */ | |
| 27 mp_err | |
| 28 ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in, int w) | |
| 29 { | |
| 30 mp_int k; | |
| 31 mp_err res = MP_OKAY; | |
| 32 int i, twowm1, mask; | |
| 33 | |
| 34 twowm1 = ec_twoTo(w - 1); | |
| 35 mask = 2 * twowm1 - 1; | |
| 36 | |
| 37 MP_DIGITS(&k) = 0; | |
| 38 MP_CHECKOK(mp_init_copy(&k, in)); | |
| 39 | |
| 40 i = 0; | |
| 41 /* Compute wNAF form */ | |
| 42 while (mp_cmp_z(&k) > 0) { | |
| 43 if (mp_isodd(&k)) { | |
| 44 out[i] = MP_DIGIT(&k, 0) & mask; | |
| 45 if (out[i] >= twowm1) | |
| 46 out[i] -= 2 * twowm1; | |
| 47 | |
| 48 /* Subtract off out[i]. Note mp_sub_d only works with | |
| 49 * unsigned digits */ | |
| 50 if (out[i] >= 0) { | |
| 51 MP_CHECKOK(mp_sub_d(&k, out[i], &k)); | |
| 52 } else { | |
| 53 MP_CHECKOK(mp_add_d(&k, -(out[i]), &k)); | |
| 54 } | |
| 55 } else { | |
| 56 out[i] = 0; | |
| 57 } | |
| 58 MP_CHECKOK(mp_div_2(&k, &k)); | |
| 59 i++; | |
| 60 } | |
| 61 /* Zero out the remaining elements of the out array. */ | |
| 62 for (; i < bitsize + 1; i++) { | |
| 63 out[i] = 0; | |
| 64 } | |
| 65 CLEANUP: | |
| 66 mp_clear(&k); | |
| 67 return res; | |
| 68 | |
| 69 } | |
| OLD | NEW |