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.
16
* The Initial Developer of the Original Code is Sun Microsystems, Inc.
17
* Portions created by Sun Microsystems, Inc. are Copyright (C) 2003
18
* Sun Microsystems, Inc. All Rights Reserved.
21
* Stephen Fung <fungstep@hotmail.com> and
22
* Douglas Stebila <douglas@stebila.ca>, 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.
45
/* MAX_FIELD_SIZE_DIGITS is the maximum size of field element supported */
46
#if defined(MP_USE_LONG_LONG_DIGIT) || defined(MP_USE_LONG_DIGIT)
47
#define ECL_SIXTY_FOUR_BIT
49
#define ECL_MAX_FIELD_SIZE_DIGITS 10
51
#define ECL_THIRTY_TWO_BIT
53
#define ECL_MAX_FIELD_SIZE_DIGITS 20
56
/* Gets the i'th bit in the binary representation of a. If i >= length(a),
57
* then return 0. (The above behaviour differs from mpl_get_bit, which
58
* causes an error if i >= length(a).) */
59
#define MP_GET_BIT(a, i) \
60
((i) >= mpl_significant_bits((a))) ? 0 : mpl_get_bit((a), (i))
63
typedef struct GFMethodStr GFMethod;
65
/* Indicates whether the structure was constructed from dynamic memory
66
* or statically created. */
68
/* Irreducible that defines the field. For prime fields, this is the
69
* prime p. For binary polynomial fields, this is the bitstring
70
* representation of the irreducible polynomial. */
72
/* For prime fields, the value irr_arr[0] is the number of bits in the
73
* field. For binary polynomial fields, the irreducible polynomial
74
* f(t) is represented as an array of unsigned int[], where f(t) is
75
* of the form: f(t) = t^p[0] + t^p[1] + ... + t^p[4] where m = p[0]
76
* > p[1] > ... > p[4] = 0. */
77
unsigned int irr_arr[5];
78
/* Field arithmetic methods. All methods (except field_enc and
79
* field_dec) are assumed to take field-encoded parameters and return
80
* field-encoded values. All methods (except field_enc and field_dec)
81
* are required to be implemented. */
82
mp_err (*field_add) (const mp_int *a, const mp_int *b, mp_int *r,
83
const GFMethod *meth);
84
mp_err (*field_neg) (const mp_int *a, mp_int *r, const GFMethod *meth);
85
mp_err (*field_sub) (const mp_int *a, const mp_int *b, mp_int *r,
86
const GFMethod *meth);
87
mp_err (*field_mod) (const mp_int *a, mp_int *r, const GFMethod *meth);
88
mp_err (*field_mul) (const mp_int *a, const mp_int *b, mp_int *r,
89
const GFMethod *meth);
90
mp_err (*field_sqr) (const mp_int *a, mp_int *r, const GFMethod *meth);
91
mp_err (*field_div) (const mp_int *a, const mp_int *b, mp_int *r,
92
const GFMethod *meth);
93
mp_err (*field_enc) (const mp_int *a, mp_int *r, const GFMethod *meth);
94
mp_err (*field_dec) (const mp_int *a, mp_int *r, const GFMethod *meth);
95
/* Extra storage for implementation-specific data. Any memory
96
* allocated to these extra fields will be cleared by extra_free. */
99
void (*extra_free) (GFMethod *meth);
102
/* Construct generic GFMethods. */
103
GFMethod *GFMethod_consGFp(const mp_int *irr);
104
GFMethod *GFMethod_consGFp_mont(const mp_int *irr);
105
GFMethod *GFMethod_consGF2m(const mp_int *irr,
106
const unsigned int irr_arr[5]);
107
/* Free the memory allocated (if any) to a GFMethod object. */
108
void GFMethod_free(GFMethod *meth);
111
/* Indicates whether the structure was constructed from dynamic memory
112
* or statically created. */
114
/* Field definition and arithmetic. */
116
/* Textual representation of curve name, if any. */
118
/* Curve parameters, field-encoded. */
119
mp_int curvea, curveb;
120
/* x and y coordinates of the base point, field-encoded. */
122
/* Order and cofactor of the base point. */
125
/* Point arithmetic methods. All methods are assumed to take
126
* field-encoded parameters and return field-encoded values. All
127
* methods (except base_point_mul and points_mul) are required to be
129
mp_err (*point_add) (const mp_int *px, const mp_int *py,
130
const mp_int *qx, const mp_int *qy, mp_int *rx,
131
mp_int *ry, const ECGroup *group);
132
mp_err (*point_sub) (const mp_int *px, const mp_int *py,
133
const mp_int *qx, const mp_int *qy, mp_int *rx,
134
mp_int *ry, const ECGroup *group);
135
mp_err (*point_dbl) (const mp_int *px, const mp_int *py, mp_int *rx,
136
mp_int *ry, const ECGroup *group);
137
mp_err (*point_mul) (const mp_int *n, const mp_int *px,
138
const mp_int *py, mp_int *rx, mp_int *ry,
139
const ECGroup *group);
140
mp_err (*base_point_mul) (const mp_int *n, mp_int *rx, mp_int *ry,
141
const ECGroup *group);
142
mp_err (*points_mul) (const mp_int *k1, const mp_int *k2,
143
const mp_int *px, const mp_int *py, mp_int *rx,
144
mp_int *ry, const ECGroup *group);
145
/* Extra storage for implementation-specific data. Any memory
146
* allocated to these extra fields will be cleared by extra_free. */
149
void (*extra_free) (ECGroup *group);
152
/* Wrapper functions for generic prime field arithmetic. */
153
mp_err ec_GFp_add(const mp_int *a, const mp_int *b, mp_int *r,
154
const GFMethod *meth);
155
mp_err ec_GFp_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
156
mp_err ec_GFp_sub(const mp_int *a, const mp_int *b, mp_int *r,
157
const GFMethod *meth);
158
mp_err ec_GFp_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
159
mp_err ec_GFp_mul(const mp_int *a, const mp_int *b, mp_int *r,
160
const GFMethod *meth);
161
mp_err ec_GFp_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
162
mp_err ec_GFp_div(const mp_int *a, const mp_int *b, mp_int *r,
163
const GFMethod *meth);
164
/* Wrapper functions for generic binary polynomial field arithmetic. */
165
mp_err ec_GF2m_add(const mp_int *a, const mp_int *b, mp_int *r,
166
const GFMethod *meth);
167
mp_err ec_GF2m_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
168
mp_err ec_GF2m_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
169
mp_err ec_GF2m_mul(const mp_int *a, const mp_int *b, mp_int *r,
170
const GFMethod *meth);
171
mp_err ec_GF2m_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
172
mp_err ec_GF2m_div(const mp_int *a, const mp_int *b, mp_int *r,
173
const GFMethod *meth);
175
/* Montgomery prime field arithmetic. */
176
mp_err ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
177
const GFMethod *meth);
178
mp_err ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
179
mp_err ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
180
const GFMethod *meth);
181
mp_err ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
182
mp_err ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
183
void ec_GFp_extra_free_mont(GFMethod *meth);
185
/* point multiplication */
186
mp_err ec_pts_mul_basic(const mp_int *k1, const mp_int *k2,
187
const mp_int *px, const mp_int *py, mp_int *rx,
188
mp_int *ry, const ECGroup *group);
189
mp_err ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2,
190
const mp_int *px, const mp_int *py, mp_int *rx,
191
mp_int *ry, const ECGroup *group);
193
/* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
194
* be an array of signed char's to output to, bitsize should be the number
195
* of bits of out, in is the original scalar, and w is the window size.
196
* NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
197
* Menezes, "Software implementation of elliptic curve cryptography over
198
* binary fields", Proc. CHES 2000. */
199
mp_err ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in,
202
/* Optimized field arithmetic */
203
mp_err ec_group_set_gfp192(ECGroup *group, ECCurveName);
204
mp_err ec_group_set_gfp224(ECGroup *group, ECCurveName);
205
mp_err ec_group_set_gf2m163(ECGroup *group, ECCurveName name);
206
mp_err ec_group_set_gf2m193(ECGroup *group, ECCurveName name);
207
mp_err ec_group_set_gf2m233(ECGroup *group, ECCurveName name);
209
/* Optimized floating-point arithmetic */
211
mp_err ec_group_set_secp160r1_fp(ECGroup *group);
212
mp_err ec_group_set_nistp192_fp(ECGroup *group);
213
mp_err ec_group_set_nistp224_fp(ECGroup *group);
216
#endif /* __ecl_priv_h_ */