1*c40a6cd7SToomas Soome /*
2f9fbec18Smcpowers * ***** BEGIN LICENSE BLOCK *****
3f9fbec18Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4f9fbec18Smcpowers *
5f9fbec18Smcpowers * The contents of this file are subject to the Mozilla Public License Version
6f9fbec18Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with
7f9fbec18Smcpowers * the License. You may obtain a copy of the License at
8f9fbec18Smcpowers * http://www.mozilla.org/MPL/
9f9fbec18Smcpowers *
10f9fbec18Smcpowers * Software distributed under the License is distributed on an "AS IS" basis,
11f9fbec18Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12f9fbec18Smcpowers * for the specific language governing rights and limitations under the
13f9fbec18Smcpowers * License.
14f9fbec18Smcpowers *
15f9fbec18Smcpowers * The Original Code is the elliptic curve math library.
16f9fbec18Smcpowers *
17f9fbec18Smcpowers * The Initial Developer of the Original Code is
18f9fbec18Smcpowers * Sun Microsystems, Inc.
19f9fbec18Smcpowers * Portions created by the Initial Developer are Copyright (C) 2003
20f9fbec18Smcpowers * the Initial Developer. All Rights Reserved.
21f9fbec18Smcpowers *
22f9fbec18Smcpowers * Contributor(s):
23f9fbec18Smcpowers * Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
24f9fbec18Smcpowers *
25f9fbec18Smcpowers * Alternatively, the contents of this file may be used under the terms of
26f9fbec18Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
27f9fbec18Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28f9fbec18Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
29f9fbec18Smcpowers * of those above. If you wish to allow use of your version of this file only
30f9fbec18Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
31f9fbec18Smcpowers * use your version of this file under the terms of the MPL, indicate your
32f9fbec18Smcpowers * decision by deleting the provisions above and replace them with the notice
33f9fbec18Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
34f9fbec18Smcpowers * the provisions above, a recipient may use your version of this file under
35f9fbec18Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
36f9fbec18Smcpowers *
37f9fbec18Smcpowers * ***** END LICENSE BLOCK ***** */
38f9fbec18Smcpowers /*
39f9fbec18Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40f9fbec18Smcpowers * Use is subject to license terms.
41f9fbec18Smcpowers *
42f9fbec18Smcpowers * Sun elects to use this software under the MPL license.
43f9fbec18Smcpowers */
44f9fbec18Smcpowers
45f9fbec18Smcpowers #include "ecl-priv.h"
46f9fbec18Smcpowers
47*c40a6cd7SToomas Soome /* Returns 2^e as an integer. This is meant to be used for small powers of
48f9fbec18Smcpowers * two. */
49f9fbec18Smcpowers int
ec_twoTo(int e)50f9fbec18Smcpowers ec_twoTo(int e)
51f9fbec18Smcpowers {
52f9fbec18Smcpowers int a = 1;
53f9fbec18Smcpowers int i;
54f9fbec18Smcpowers
55f9fbec18Smcpowers for (i = 0; i < e; i++) {
56f9fbec18Smcpowers a *= 2;
57f9fbec18Smcpowers }
58f9fbec18Smcpowers return a;
59f9fbec18Smcpowers }
60f9fbec18Smcpowers
61f9fbec18Smcpowers /* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
62*c40a6cd7SToomas Soome * be an array of signed char's to output to, bitsize should be the number
63f9fbec18Smcpowers * of bits of out, in is the original scalar, and w is the window size.
64f9fbec18Smcpowers * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
65f9fbec18Smcpowers * Menezes, "Software implementation of elliptic curve cryptography over
66f9fbec18Smcpowers * binary fields", Proc. CHES 2000. */
67f9fbec18Smcpowers mp_err
ec_compute_wNAF(signed char * out,int bitsize,const mp_int * in,int w)68f9fbec18Smcpowers ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in, int w)
69f9fbec18Smcpowers {
70f9fbec18Smcpowers mp_int k;
71f9fbec18Smcpowers mp_err res = MP_OKAY;
72f9fbec18Smcpowers int i, twowm1, mask;
73f9fbec18Smcpowers
74f9fbec18Smcpowers twowm1 = ec_twoTo(w - 1);
75f9fbec18Smcpowers mask = 2 * twowm1 - 1;
76f9fbec18Smcpowers
77f9fbec18Smcpowers MP_DIGITS(&k) = 0;
78f9fbec18Smcpowers MP_CHECKOK(mp_init_copy(&k, in));
79f9fbec18Smcpowers
80f9fbec18Smcpowers i = 0;
81f9fbec18Smcpowers /* Compute wNAF form */
82f9fbec18Smcpowers while (mp_cmp_z(&k) > 0) {
83f9fbec18Smcpowers if (mp_isodd(&k)) {
84f9fbec18Smcpowers out[i] = MP_DIGIT(&k, 0) & mask;
85f9fbec18Smcpowers if (out[i] >= twowm1)
86f9fbec18Smcpowers out[i] -= 2 * twowm1;
87f9fbec18Smcpowers
88f9fbec18Smcpowers /* Subtract off out[i]. Note mp_sub_d only works with
89f9fbec18Smcpowers * unsigned digits */
90f9fbec18Smcpowers if (out[i] >= 0) {
91f9fbec18Smcpowers mp_sub_d(&k, out[i], &k);
92f9fbec18Smcpowers } else {
93f9fbec18Smcpowers mp_add_d(&k, -(out[i]), &k);
94f9fbec18Smcpowers }
95f9fbec18Smcpowers } else {
96f9fbec18Smcpowers out[i] = 0;
97f9fbec18Smcpowers }
98f9fbec18Smcpowers mp_div_2(&k, &k);
99f9fbec18Smcpowers i++;
100f9fbec18Smcpowers }
101f9fbec18Smcpowers /* Zero out the remaining elements of the out array. */
102f9fbec18Smcpowers for (; i < bitsize + 1; i++) {
103f9fbec18Smcpowers out[i] = 0;
104f9fbec18Smcpowers }
105f9fbec18Smcpowers CLEANUP:
106f9fbec18Smcpowers mp_clear(&k);
107f9fbec18Smcpowers return res;
108f9fbec18Smcpowers
109f9fbec18Smcpowers }
110