PageRenderTime 26ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/zpao/v8monkey
C | 259 lines | 193 code | 16 blank | 50 comment | 13 complexity | edd583886d27d130faddf5acc0b95c68 MD5 | raw file
  1/* 
  2 * ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with
  7 * the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  9 *
 10 * Software distributed under the License is distributed on an "AS IS" basis,
 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12 * for the specific language governing rights and limitations under the
 13 * License.
 14 *
 15 * The Original Code is the elliptic curve math library for binary polynomial field curves.
 16 *
 17 * The Initial Developer of the Original Code is
 18 * Sun Microsystems, Inc.
 19 * Portions created by the Initial Developer are Copyright (C) 2003
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
 24 *   Stephen Fung <fungstep@hotmail.com>, and
 25 *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
 26 *
 27 * Alternatively, the contents of this file may be used under the terms of
 28 * either the GNU General Public License Version 2 or later (the "GPL"), or
 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 30 * in which case the provisions of the GPL or the LGPL are applicable instead
 31 * of those above. If you wish to allow use of your version of this file only
 32 * under the terms of either the GPL or the LGPL, and not to allow others to
 33 * use your version of this file under the terms of the MPL, indicate your
 34 * decision by deleting the provisions above and replace them with the notice
 35 * and other provisions required by the GPL or the LGPL. If you do not delete
 36 * the provisions above, a recipient may use your version of this file under
 37 * the terms of any one of the MPL, the GPL or the LGPL.
 38 *
 39 * ***** END LICENSE BLOCK ***** */
 40
 41#include "ec2.h"
 42#include "mp_gf2m.h"
 43#include "mp_gf2m-priv.h"
 44#include "mpi.h"
 45#include "mpi-priv.h"
 46#include <stdlib.h>
 47
 48/* Fast reduction for polynomials over a 163-bit curve. Assumes reduction
 49 * polynomial with terms {163, 7, 6, 3, 0}. */
 50mp_err
 51ec_GF2m_163_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
 52{
 53	mp_err res = MP_OKAY;
 54	mp_digit *u, z;
 55
 56	if (a != r) {
 57		MP_CHECKOK(mp_copy(a, r));
 58	}
 59#ifdef ECL_SIXTY_FOUR_BIT
 60	if (MP_USED(r) < 6) {
 61		MP_CHECKOK(s_mp_pad(r, 6));
 62	}
 63	u = MP_DIGITS(r);
 64	MP_USED(r) = 6;
 65
 66	/* u[5] only has 6 significant bits */
 67	z = u[5];
 68	u[2] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
 69	z = u[4];
 70	u[2] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35);
 71	u[1] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
 72	z = u[3];
 73	u[1] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35);
 74	u[0] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
 75	z = u[2] >> 35;				/* z only has 29 significant bits */
 76	u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z;
 77	/* clear bits above 163 */
 78	u[5] = u[4] = u[3] = 0;
 79	u[2] ^= z << 35;
 80#else
 81	if (MP_USED(r) < 11) {
 82		MP_CHECKOK(s_mp_pad(r, 11));
 83	}
 84	u = MP_DIGITS(r);
 85	MP_USED(r) = 11;
 86
 87	/* u[11] only has 6 significant bits */
 88	z = u[10];
 89	u[5] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
 90	u[4] ^= (z << 29);
 91	z = u[9];
 92	u[5] ^= (z >> 28) ^ (z >> 29);
 93	u[4] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
 94	u[3] ^= (z << 29);
 95	z = u[8];
 96	u[4] ^= (z >> 28) ^ (z >> 29);
 97	u[3] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
 98	u[2] ^= (z << 29);
 99	z = u[7];
100	u[3] ^= (z >> 28) ^ (z >> 29);
101	u[2] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
102	u[1] ^= (z << 29);
103	z = u[6];
104	u[2] ^= (z >> 28) ^ (z >> 29);
105	u[1] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
106	u[0] ^= (z << 29);
107	z = u[5] >> 3;				/* z only has 29 significant bits */
108	u[1] ^= (z >> 25) ^ (z >> 26);
109	u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z;
110	/* clear bits above 163 */
111	u[11] = u[10] = u[9] = u[8] = u[7] = u[6] = 0;
112	u[5] ^= z << 3;
113#endif
114	s_mp_clamp(r);
115
116  CLEANUP:
117	return res;
118}
119
120/* Fast squaring for polynomials over a 163-bit curve. Assumes reduction
121 * polynomial with terms {163, 7, 6, 3, 0}. */
122mp_err
123ec_GF2m_163_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
124{
125	mp_err res = MP_OKAY;
126	mp_digit *u, *v;
127
128	v = MP_DIGITS(a);
129
130#ifdef ECL_SIXTY_FOUR_BIT
131	if (MP_USED(a) < 3) {
132		return mp_bsqrmod(a, meth->irr_arr, r);
133	}
134	if (MP_USED(r) < 6) {
135		MP_CHECKOK(s_mp_pad(r, 6));
136	}
137	MP_USED(r) = 6;
138#else
139	if (MP_USED(a) < 6) {
140		return mp_bsqrmod(a, meth->irr_arr, r);
141	}
142	if (MP_USED(r) < 12) {
143		MP_CHECKOK(s_mp_pad(r, 12));
144	}
145	MP_USED(r) = 12;
146#endif
147	u = MP_DIGITS(r);
148
149#ifdef ECL_THIRTY_TWO_BIT
150	u[11] = gf2m_SQR1(v[5]);
151	u[10] = gf2m_SQR0(v[5]);
152	u[9] = gf2m_SQR1(v[4]);
153	u[8] = gf2m_SQR0(v[4]);
154	u[7] = gf2m_SQR1(v[3]);
155	u[6] = gf2m_SQR0(v[3]);
156#endif
157	u[5] = gf2m_SQR1(v[2]);
158	u[4] = gf2m_SQR0(v[2]);
159	u[3] = gf2m_SQR1(v[1]);
160	u[2] = gf2m_SQR0(v[1]);
161	u[1] = gf2m_SQR1(v[0]);
162	u[0] = gf2m_SQR0(v[0]);
163	return ec_GF2m_163_mod(r, r, meth);
164
165  CLEANUP:
166	return res;
167}
168
169/* Fast multiplication for polynomials over a 163-bit curve. Assumes
170 * reduction polynomial with terms {163, 7, 6, 3, 0}. */
171mp_err
172ec_GF2m_163_mul(const mp_int *a, const mp_int *b, mp_int *r,
173				const GFMethod *meth)
174{
175	mp_err res = MP_OKAY;
176	mp_digit a2 = 0, a1 = 0, a0, b2 = 0, b1 = 0, b0;
177
178#ifdef ECL_THIRTY_TWO_BIT
179	mp_digit a5 = 0, a4 = 0, a3 = 0, b5 = 0, b4 = 0, b3 = 0;
180	mp_digit rm[6];
181#endif
182
183	if (a == b) {
184		return ec_GF2m_163_sqr(a, r, meth);
185	} else {
186		switch (MP_USED(a)) {
187#ifdef ECL_THIRTY_TWO_BIT
188		case 6:
189			a5 = MP_DIGIT(a, 5);
190		case 5:
191			a4 = MP_DIGIT(a, 4);
192		case 4:
193			a3 = MP_DIGIT(a, 3);
194#endif
195		case 3:
196			a2 = MP_DIGIT(a, 2);
197		case 2:
198			a1 = MP_DIGIT(a, 1);
199		default:
200			a0 = MP_DIGIT(a, 0);
201		}
202		switch (MP_USED(b)) {
203#ifdef ECL_THIRTY_TWO_BIT
204		case 6:
205			b5 = MP_DIGIT(b, 5);
206		case 5:
207			b4 = MP_DIGIT(b, 4);
208		case 4:
209			b3 = MP_DIGIT(b, 3);
210#endif
211		case 3:
212			b2 = MP_DIGIT(b, 2);
213		case 2:
214			b1 = MP_DIGIT(b, 1);
215		default:
216			b0 = MP_DIGIT(b, 0);
217		}
218#ifdef ECL_SIXTY_FOUR_BIT
219		MP_CHECKOK(s_mp_pad(r, 6));
220		s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0);
221		MP_USED(r) = 6;
222		s_mp_clamp(r);
223#else
224		MP_CHECKOK(s_mp_pad(r, 12));
225		s_bmul_3x3(MP_DIGITS(r) + 6, a5, a4, a3, b5, b4, b3);
226		s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0);
227		s_bmul_3x3(rm, a5 ^ a2, a4 ^ a1, a3 ^ a0, b5 ^ b2, b4 ^ b1,
228				   b3 ^ b0);
229		rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 11);
230		rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 10);
231		rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 9);
232		rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 8);
233		rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 7);
234		rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 6);
235		MP_DIGIT(r, 8) ^= rm[5];
236		MP_DIGIT(r, 7) ^= rm[4];
237		MP_DIGIT(r, 6) ^= rm[3];
238		MP_DIGIT(r, 5) ^= rm[2];
239		MP_DIGIT(r, 4) ^= rm[1];
240		MP_DIGIT(r, 3) ^= rm[0];
241		MP_USED(r) = 12;
242		s_mp_clamp(r);
243#endif
244		return ec_GF2m_163_mod(r, r, meth);
245	}
246
247  CLEANUP:
248	return res;
249}
250
251/* Wire in fast field arithmetic for 163-bit curves. */
252mp_err
253ec_group_set_gf2m163(ECGroup *group, ECCurveName name)
254{
255	group->meth->field_mod = &ec_GF2m_163_mod;
256	group->meth->field_mul = &ec_GF2m_163_mul;
257	group->meth->field_sqr = &ec_GF2m_163_sqr;
258	return MP_OKAY;
259}