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

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/freebl/ecl/ecp_fp192.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 prime
 
15
 * field curves using floating point operations.
 
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
 *      Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 */
 
37
 
 
38
#include "ecp_fp.h"
 
39
#include <stdlib.h>
 
40
 
 
41
#define ECFP_BSIZE 192
 
42
#define ECFP_NUMDOUBLES 8
 
43
 
 
44
#include "ecp_fpinc.c"
 
45
 
 
46
/* Performs a single step of reduction, just on the uppermost float
 
47
 * (assumes already tidied), and then retidies. Note, this does not
 
48
 * guarantee that the result will be less than p. */
 
49
void
 
50
ecfp192_singleReduce(double *d, const EC_group_fp * group)
 
51
{
 
52
        double q;
 
53
 
 
54
        ECFP_ASSERT(group->doubleBitSize == 24);
 
55
        ECFP_ASSERT(group->primeBitSize == 192);
 
56
        ECFP_ASSERT(group->numDoubles == 8);
 
57
 
 
58
        q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_192;
 
59
        q += group->bitSize_alpha;
 
60
        q -= group->bitSize_alpha;
 
61
 
 
62
        d[ECFP_NUMDOUBLES - 1] -= q;
 
63
        d[0] += q * ecfp_twom192;
 
64
        d[2] += q * ecfp_twom128;
 
65
        ecfp_positiveTidy(d, group);
 
66
}
 
67
 
 
68
/* 
 
69
 * Performs imperfect reduction.  This might leave some negative terms,
 
70
 * and one more reduction might be required for the result to be between 0 
 
71
 * and p-1. x should be be an array of at least 16, and r at least 8 x and 
 
72
 * r can be the same, but then the upper parts of r are not zeroed */
 
73
void
 
74
ecfp_reduce_192(double *r, double *x, const EC_group_fp * group)
 
75
{
 
76
        double x8, x9, x10, q;
 
77
 
 
78
        ECFP_ASSERT(group->doubleBitSize == 24);
 
79
        ECFP_ASSERT(group->primeBitSize == 192);
 
80
        ECFP_ASSERT(group->numDoubles == 8);
 
81
 
 
82
        /* Tidy just the upper portion, the lower part can wait */
 
83
        ecfp_tidyUpper(x, group);
 
84
 
 
85
        x8 = x[8] + x[14] * ecfp_twom128;       /* adds bits 16-40 */
 
86
        x9 = x[9] + x[15] * ecfp_twom128;       /* adds bits 16-40 */
 
87
 
 
88
        /* Tidy up, or we won't have enough bits later to add it in */
 
89
 
 
90
        q = x8 + group->alpha[9];
 
91
        q -= group->alpha[9];
 
92
        x8 -= q;
 
93
        x9 += q;
 
94
 
 
95
        q = x9 + group->alpha[10];
 
96
        q -= group->alpha[10];
 
97
        x9 -= q;
 
98
        x10 = x[10] + q;
 
99
 
 
100
        r[7] = x[7] + x[15] * ecfp_twom192 + x[13] * ecfp_twom128;      /* adds
 
101
                                                                                                                                 * bits
 
102
                                                                                                                                 * 0-40 */
 
103
        r[6] = x[6] + x[14] * ecfp_twom192 + x[12] * ecfp_twom128;
 
104
        r[5] = x[5] + x[13] * ecfp_twom192 + x[11] * ecfp_twom128;
 
105
        r[4] = x[4] + x[12] * ecfp_twom192 + x10 * ecfp_twom128;
 
106
        r[3] = x[3] + x[11] * ecfp_twom192 + x9 * ecfp_twom128; /* adds bits
 
107
                                                                                                                         * 0-40 */
 
108
        r[2] = x[2] + x10 * ecfp_twom192 + x8 * ecfp_twom128;
 
109
        r[1] = x[1] + x9 * ecfp_twom192;        /* adds bits 16-40 */
 
110
        r[0] = x[0] + x8 * ecfp_twom192;
 
111
 
 
112
        /* 
 
113
         * Tidy up just r[group->numDoubles-2] so that the number of
 
114
         * reductions is accurate plus or minus one.  (Rather than tidy all to 
 
115
         * make it totally accurate) */
 
116
        q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1];
 
117
        q -= group->alpha[ECFP_NUMDOUBLES - 1];
 
118
        r[ECFP_NUMDOUBLES - 2] -= q;
 
119
        r[ECFP_NUMDOUBLES - 1] += q;
 
120
 
 
121
        /* Tidy up the excess bits on r[group->numDoubles-1] using reduction */
 
122
        /* Use ecfp_beta so we get a positive res */
 
123
        q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_192;
 
124
        q += group->bitSize_alpha;
 
125
        q -= group->bitSize_alpha;
 
126
 
 
127
        r[ECFP_NUMDOUBLES - 1] -= q;
 
128
        r[0] += q * ecfp_twom192;
 
129
        r[2] += q * ecfp_twom128;
 
130
 
 
131
        /* Tidy the result */
 
132
        ecfp_tidyShort(r, group);
 
133
}
 
134
 
 
135
/* Sets group to use optimized calculations in this file */
 
136
mp_err
 
137
ec_group_set_nistp192_fp(ECGroup *group)
 
138
{
 
139
        EC_group_fp *fpg;
 
140
 
 
141
        /* Allocate memory for floating point group data */
 
142
        fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
 
143
        if (fpg == NULL) {
 
144
                return MP_MEM;
 
145
        }
 
146
 
 
147
        fpg->numDoubles = ECFP_NUMDOUBLES;
 
148
        fpg->primeBitSize = ECFP_BSIZE;
 
149
        fpg->orderBitSize = 192;
 
150
        fpg->doubleBitSize = 24;
 
151
        fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS;
 
152
        fpg->aIsM3 = 1;
 
153
        fpg->ecfp_singleReduce = &ecfp192_singleReduce;
 
154
        fpg->ecfp_reduce = &ecfp_reduce_192;
 
155
        fpg->ecfp_tidy = &ecfp_tidy;
 
156
 
 
157
        fpg->pt_add_jac_aff = &ecfp192_pt_add_jac_aff;
 
158
        fpg->pt_add_jac = &ecfp192_pt_add_jac;
 
159
        fpg->pt_add_jm_chud = &ecfp192_pt_add_jm_chud;
 
160
        fpg->pt_add_chud = &ecfp192_pt_add_chud;
 
161
        fpg->pt_dbl_jac = &ecfp192_pt_dbl_jac;
 
162
        fpg->pt_dbl_jm = &ecfp192_pt_dbl_jm;
 
163
        fpg->pt_dbl_aff2chud = &ecfp192_pt_dbl_aff2chud;
 
164
        fpg->precompute_chud = &ecfp192_precompute_chud;
 
165
        fpg->precompute_jac = &ecfp192_precompute_jac;
 
166
 
 
167
        group->point_mul = &ec_GFp_point_mul_wNAF_fp;
 
168
        group->points_mul = &ec_pts_mul_basic;
 
169
        group->extra1 = fpg;
 
170
        group->extra_free = &ec_GFp_extra_free_fp;
 
171
 
 
172
        ec_set_fp_precision(fpg);
 
173
        fpg->bitSize_alpha = ECFP_TWO192 * fpg->alpha[0];
 
174
 
 
175
        return MP_OKAY;
 
176
}