1N/A/*
1N/A * ***** BEGIN LICENSE BLOCK *****
1N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1N/A *
1N/A * The contents of this file are subject to the Mozilla Public License Version
1N/A * 1.1 (the "License"); you may not use this file except in compliance with
1N/A * the License. You may obtain a copy of the License at
1N/A * http://www.mozilla.org/MPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS IS" basis,
1N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1N/A * for the specific language governing rights and limitations under the
1N/A * License.
1N/A *
1N/A * The Original Code is the elliptic curve math library.
1N/A *
1N/A * The Initial Developer of the Original Code is
1N/A * Sun Microsystems, Inc.
1N/A * Portions created by the Initial Developer are Copyright (C) 2003
1N/A * the Initial Developer. All Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A * Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
1N/A *
1N/A * Alternatively, the contents of this file may be used under the terms of
1N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
1N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1N/A * in which case the provisions of the GPL or the LGPL are applicable instead
1N/A * of those above. If you wish to allow use of your version of this file only
1N/A * under the terms of either the GPL or the LGPL, and not to allow others to
1N/A * use your version of this file under the terms of the MPL, indicate your
1N/A * decision by deleting the provisions above and replace them with the notice
1N/A * and other provisions required by the GPL or the LGPL. If you do not delete
1N/A * the provisions above, a recipient may use your version of this file under
1N/A * the terms of any one of the MPL, the GPL or the LGPL.
1N/A *
1N/A * ***** END LICENSE BLOCK ***** */
1N/A/*
1N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A * Sun elects to use this software under the MPL license.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include "ecl-priv.h"
1N/A
1N/A/* Returns 2^e as an integer. This is meant to be used for small powers of
1N/A * two. */
1N/Aint
1N/Aec_twoTo(int e)
1N/A{
1N/A int a = 1;
1N/A int i;
1N/A
1N/A for (i = 0; i < e; i++) {
1N/A a *= 2;
1N/A }
1N/A return a;
1N/A}
1N/A
1N/A/* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
1N/A * be an array of signed char's to output to, bitsize should be the number
1N/A * of bits of out, in is the original scalar, and w is the window size.
1N/A * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
1N/A * Menezes, "Software implementation of elliptic curve cryptography over
1N/A * binary fields", Proc. CHES 2000. */
1N/Amp_err
1N/Aec_compute_wNAF(signed char *out, int bitsize, const mp_int *in, int w)
1N/A{
1N/A mp_int k;
1N/A mp_err res = MP_OKAY;
1N/A int i, twowm1, mask;
1N/A
1N/A twowm1 = ec_twoTo(w - 1);
1N/A mask = 2 * twowm1 - 1;
1N/A
1N/A MP_DIGITS(&k) = 0;
1N/A MP_CHECKOK(mp_init_copy(&k, in));
1N/A
1N/A i = 0;
1N/A /* Compute wNAF form */
1N/A while (mp_cmp_z(&k) > 0) {
1N/A if (mp_isodd(&k)) {
1N/A out[i] = MP_DIGIT(&k, 0) & mask;
1N/A if (out[i] >= twowm1)
1N/A out[i] -= 2 * twowm1;
1N/A
1N/A /* Subtract off out[i]. Note mp_sub_d only works with
1N/A * unsigned digits */
1N/A if (out[i] >= 0) {
1N/A mp_sub_d(&k, out[i], &k);
1N/A } else {
1N/A mp_add_d(&k, -(out[i]), &k);
1N/A }
1N/A } else {
1N/A out[i] = 0;
1N/A }
1N/A mp_div_2(&k, &k);
1N/A i++;
1N/A }
1N/A /* Zero out the remaining elements of the out array. */
1N/A for (; i < bitsize + 1; i++) {
1N/A out[i] = 0;
1N/A }
1N/A CLEANUP:
1N/A mp_clear(&k);
1N/A return res;
1N/A
1N/A}