4272N/A/*
4272N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4272N/A * Use is subject to license terms.
4272N/A *
4272N/A * This library is free software; you can redistribute it and/or
4272N/A * modify it under the terms of the GNU Lesser General Public
4272N/A * License as published by the Free Software Foundation; either
4272N/A * version 2.1 of the License, or (at your option) any later version.
1674N/A *
4272N/A * This library is distributed in the hope that it will be useful,
4272N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
4272N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4272N/A * Lesser General Public License for more details.
1674N/A *
4272N/A * You should have received a copy of the GNU Lesser General Public License
4272N/A * along with this library; if not, write to the Free Software Foundation,
4272N/A * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1674N/A *
4272N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4272N/A * or visit www.oracle.com if you need additional information or have any
4272N/A * questions.
4272N/A */
4272N/A
4272N/A/* *********************************************************************
1674N/A *
1674N/A * The Original Code is the elliptic curve math library.
1674N/A *
1674N/A * The Initial Developer of the Original Code is
1674N/A * Sun Microsystems, Inc.
1674N/A * Portions created by the Initial Developer are Copyright (C) 2003
1674N/A * the Initial Developer. All Rights Reserved.
1674N/A *
1674N/A * Contributor(s):
1674N/A * Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
1674N/A *
1674N/A *********************************************************************** */
1674N/A
1674N/A#include "ecl-priv.h"
1674N/A
1674N/A/* Returns 2^e as an integer. This is meant to be used for small powers of
1674N/A * two. */
1674N/Aint
1674N/Aec_twoTo(int e)
1674N/A{
1674N/A int a = 1;
1674N/A int i;
1674N/A
1674N/A for (i = 0; i < e; i++) {
1674N/A a *= 2;
1674N/A }
1674N/A return a;
1674N/A}
1674N/A
1674N/A/* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
1674N/A * be an array of signed char's to output to, bitsize should be the number
1674N/A * of bits of out, in is the original scalar, and w is the window size.
1674N/A * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
1674N/A * Menezes, "Software implementation of elliptic curve cryptography over
1674N/A * binary fields", Proc. CHES 2000. */
1674N/Amp_err
1674N/Aec_compute_wNAF(signed char *out, int bitsize, const mp_int *in, int w)
1674N/A{
1674N/A mp_int k;
1674N/A mp_err res = MP_OKAY;
1674N/A int i, twowm1, mask;
1674N/A
1674N/A twowm1 = ec_twoTo(w - 1);
1674N/A mask = 2 * twowm1 - 1;
1674N/A
1674N/A MP_DIGITS(&k) = 0;
1674N/A MP_CHECKOK(mp_init_copy(&k, in));
1674N/A
1674N/A i = 0;
1674N/A /* Compute wNAF form */
1674N/A while (mp_cmp_z(&k) > 0) {
1674N/A if (mp_isodd(&k)) {
1674N/A out[i] = MP_DIGIT(&k, 0) & mask;
1674N/A if (out[i] >= twowm1)
1674N/A out[i] -= 2 * twowm1;
1674N/A
1674N/A /* Subtract off out[i]. Note mp_sub_d only works with
1674N/A * unsigned digits */
1674N/A if (out[i] >= 0) {
1674N/A mp_sub_d(&k, out[i], &k);
1674N/A } else {
1674N/A mp_add_d(&k, -(out[i]), &k);
1674N/A }
1674N/A } else {
1674N/A out[i] = 0;
1674N/A }
1674N/A mp_div_2(&k, &k);
1674N/A i++;
1674N/A }
1674N/A /* Zero out the remaining elements of the out array. */
1674N/A for (; i < bitsize + 1; i++) {
1674N/A out[i] = 0;
1674N/A }
1674N/A CLEANUP:
1674N/A mp_clear(&k);
1674N/A return res;
1674N/A
1674N/A}