2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14
* The Original Code is the elliptic curve math library for prime
15
* field curves using floating point operations.
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.
22
* Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
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.
41
#define ECFP_BSIZE 192
42
#define ECFP_NUMDOUBLES 8
44
#include "ecp_fpinc.c"
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. */
50
ecfp192_singleReduce(double *d, const EC_group_fp * group)
54
ECFP_ASSERT(group->doubleBitSize == 24);
55
ECFP_ASSERT(group->primeBitSize == 192);
56
ECFP_ASSERT(group->numDoubles == 8);
58
q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_192;
59
q += group->bitSize_alpha;
60
q -= group->bitSize_alpha;
62
d[ECFP_NUMDOUBLES - 1] -= q;
63
d[0] += q * ecfp_twom192;
64
d[2] += q * ecfp_twom128;
65
ecfp_positiveTidy(d, group);
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 */
74
ecfp_reduce_192(double *r, double *x, const EC_group_fp * group)
76
double x8, x9, x10, q;
78
ECFP_ASSERT(group->doubleBitSize == 24);
79
ECFP_ASSERT(group->primeBitSize == 192);
80
ECFP_ASSERT(group->numDoubles == 8);
82
/* Tidy just the upper portion, the lower part can wait */
83
ecfp_tidyUpper(x, group);
85
x8 = x[8] + x[14] * ecfp_twom128; /* adds bits 16-40 */
86
x9 = x[9] + x[15] * ecfp_twom128; /* adds bits 16-40 */
88
/* Tidy up, or we won't have enough bits later to add it in */
90
q = x8 + group->alpha[9];
95
q = x9 + group->alpha[10];
96
q -= group->alpha[10];
100
r[7] = x[7] + x[15] * ecfp_twom192 + x[13] * ecfp_twom128; /* adds
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
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;
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;
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;
127
r[ECFP_NUMDOUBLES - 1] -= q;
128
r[0] += q * ecfp_twom192;
129
r[2] += q * ecfp_twom128;
131
/* Tidy the result */
132
ecfp_tidyShort(r, group);
135
/* Sets group to use optimized calculations in this file */
137
ec_group_set_nistp192_fp(ECGroup *group)
141
/* Allocate memory for floating point group data */
142
fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
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;
153
fpg->ecfp_singleReduce = &ecfp192_singleReduce;
154
fpg->ecfp_reduce = &ecfp_reduce_192;
155
fpg->ecfp_tidy = &ecfp_tidy;
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;
167
group->point_mul = &ec_GFp_point_mul_wNAF_fp;
168
group->points_mul = &ec_pts_mul_basic;
170
group->extra_free = &ec_GFp_extra_free_fp;
172
ec_set_fp_precision(fpg);
173
fpg->bitSize_alpha = ECFP_TWO192 * fpg->alpha[0];