security/nss/lib/freebl/ecl/ec_naf.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 #include "ecl-priv.h"
     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;
    15 	for (i = 0; i < e; i++) {
    16 		a *= 2;
    17 	}
    18 	return a;
    19 }
    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;
    34 	twowm1 = ec_twoTo(w - 1);
    35 	mask = 2 * twowm1 - 1;
    37 	MP_DIGITS(&k) = 0;
    38 	MP_CHECKOK(mp_init_copy(&k, in));
    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;
    48 			/* Subtract off out[i].  Note mp_sub_d only works with
    49 			 * unsigned digits */
    50 			if (out[i] >= 0) {
    51 				mp_sub_d(&k, out[i], &k);
    52 			} else {
    53 				mp_add_d(&k, -(out[i]), &k);
    54 			}
    55 		} else {
    56 			out[i] = 0;
    57 		}
    58 		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;
    69 }

mercurial