3 * $Id: ec.h,v 1.2 2001/05/07 17:29:44 mdw Exp $
5 * Elliptic curve definitions
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2001/05/07 17:29:44 mdw
34 * Treat projective coordinates as an internal representation. Various
35 * minor interface changes.
37 * Revision 1.1 2001/04/29 18:12:33 mdw
49 /*----- Header files ------------------------------------------------------*/
54 /*----- Data structures ---------------------------------------------------*/
56 typedef struct ec_curve
{
57 const struct ec_ops
*ops
; /* Curve operations */
58 field
*f
; /* Underlying field structure */
62 mp
*x
, *y
; /* Point coordinates */
63 mp
*z
; /* Common denominator (or null) */
66 typedef struct ec_ops
{
67 void (*destroy
)(ec_curve */
*c*/
);
68 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
69 ec
*(*out
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
70 ec
*(*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
71 ec
*(*add
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
72 ec
*(*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
75 #define EC_DESTROY(c) (c)->ops->destroy((c))
77 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
78 #define EC_OUT(c, d, p) (c)->ops->in((c), (d), (p))
80 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
81 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
82 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
84 /*----- Simple memory management things -----------------------------------*/
86 /* --- @ec_create@ --- *
88 * Arguments: @ec *p@ = pointer to an elliptic-curve point
90 * Returns: The argument @p@.
92 * Use: Initializes a new point. The initial value is the additive
93 * identity (which is universal for all curves).
96 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
98 #define EC_CREATE(p) do { \
100 _p->x = _p->y = _p->z = MP_NEW; \
103 extern ec
*ec_create(ec */
*p*/
);
105 /* --- @ec_destroy@ --- *
107 * Arguments: @ec *p@ = pointer to an elliptic-curve point
111 * Use: Destroys a point, making it invalid.
114 #define EC_DESTROY(p) do { \
116 if (!EC_ATINF(_p)) { \
119 if (_p->z) MP_DROP(_p->z); \
123 extern void ec_destroy(ec */
*p*/
);
125 /* --- @ec_atinf@ --- *
127 * Arguments: @const ec *p@ = pointer to a point
129 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
133 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
135 extern int ec_atinf(const ec */
*p*/
);
137 /* --- @ec_setinf@ --- *
139 * Arguments: @ec *p@ = pointer to a point
141 * Returns: The argument @p@.
143 * Use: Sets the given point to be the point %$O$% at infinity.
146 #define EC_SETINF(p) do { \
148 if (!EC_ATINF(_p)) { \
151 if (_p->z) MP_DROP(_p->z); \
152 _p->x = _p->y = _p->z = MP_NEW; \
158 extern ec
*ec_setinf(ec */
*p*/
);
160 /* --- @ec_copy@ --- *
162 * Arguments: @ec *d@ = pointer to destination point
163 * @const ec *p@ = pointer to source point
165 * Returns: The destination @d@.
167 * Use: Creates a copy of an elliptic curve point.
170 #define EC_COPY(d, p) do { \
172 const ec *_p = (p); \
176 _d->x = _d->y = _d->z = MP_NEW; \
185 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
187 /*----- Interesting arithmetic --------------------------------------------*/
189 /* --- @ec_denorm@ --- *
191 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
192 * @ec *d@ = pointer to the destination point
193 * @const ec *p@ = pointer to the source point
197 * Use: Denormalizes the given point, converting to internal
198 * representations and setting the denominator to 1.
201 extern void ec_denorm(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
203 /* --- @ec_norm@ --- *
205 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
206 * @ec *d@ = pointer to the destination point
207 * @const ec *p@ = pointer to the source point
211 * Use: Normalizes the given point, by dividing through by the
212 * denominator and returning to external representation.
215 extern void ec_norm(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
217 /* --- @ec_find@ --- *
219 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
220 * @ec *d@ = pointer to the destination point
221 * @mp *x@ = a possible x-coordinate
223 * Returns: Zero if OK, nonzero if there isn't a point there.
225 * Use: Finds a point on an elliptic curve with a given x-coordinate.
228 extern void ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
230 /* --- @ec_add@ --- *
232 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
233 * @ec *d@ = pointer to the destination point
234 * @const ec *p, *q@ = pointers to the operand points
236 * Returns: The destination @d@.
238 * Use: Adds two points on an elliptic curve.
241 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
242 const ec */
*p*/
, const ec */
*q*/
);
244 /* --- @ec_dbl@ --- *
246 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
247 * @ec *d@ = pointer to the destination point
248 * @const ec *p@ = pointer to the operand point
250 * Returns: The destination @d@.
252 * Use: Doubles a point on an elliptic curve.
255 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
257 /* --- @ec_mul@ --- *
259 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
260 * @ec *d@ = pointer to the destination point
261 * @const ec *p@ = pointer to the generator point
262 * @mp *n@ = integer multiplier
264 * Returns: The destination @d@.
266 * Use: Multiplies a point by a scalar, returning %$n p$%.
269 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
271 /*----- Standard curve operations -----------------------------------------*/
273 /* --- @ec_idin@, @ec_idout@ --- *
275 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
276 * @ec *d@ = pointer to the destination
277 * @const ec *p@ = pointer to a source point
279 * Returns: The destination @d@.
281 * Use: An identity operation if your curve has no internal
282 * representation. (The field internal representation is still
286 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
287 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
289 /* --- @ec_projin@, @ec_projout@ --- *
291 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
292 * @ec *d@ = pointer to the destination
293 * @const ec *p@ = pointer to a source point
295 * Returns: The destination @d@.
297 * Use: Conversion functions if your curve operations use a
298 * projective representation.
301 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
302 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
304 /*----- Creating curves ---------------------------------------------------*/
306 /* --- @ec_prime@ --- *
308 * Arguments: @field *f@ = the underyling field for this elliptic curve
309 * @mp *a, *b@ = the coefficients for this curve
311 * Returns: A pointer to the curve.
313 * Use: Creates a curve structure for an elliptic curve defined over
317 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
319 /* --- @ec_bin@ --- *
321 * Arguments: @field *f@ = the underlying field for this elliptic curve
322 * @mp *a, *b@ = the coefficients for this curve
324 * Returns: A pointer to the curve.
326 * Use: Creates a curve structure for a non-supersingular elliptic
327 * curve defined over a binary field.
330 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
332 /*----- That's all, folks -------------------------------------------------*/