~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/freebl/ecl/ec2_233.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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