3 * $Id: ec.h,v 1.1 2001/04/29 18:12:33 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.1 2001/04/29 18:12:33 mdw
45 /*----- Header files ------------------------------------------------------*/
50 /*----- Data structures ---------------------------------------------------*/
52 typedef struct ec_curve
{
53 const struct ec_ops
*ops
; /* Curve operations */
54 field
*f
; /* Underlying field structure */
58 mp
*x
, *y
; /* Point coordinates */
59 mp
*z
; /* Common denominator (or null) */
62 typedef struct ec_ops
{
63 void (*destroy
)(ec_curve */
*c*/
);
64 int (*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
65 void (*add
)(ec_curve */
*c*/
, ec */
*d*/
, ec */
*p*/
, ec */
*q*/
);
66 void (*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, ec */
*p*/
);
69 #define EC_DESTROY(c) (c)->ops->destroy((c))
71 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
72 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
73 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
75 /*----- Simple memory management things -----------------------------------*/
77 /* --- @ec_create@ --- *
79 * Arguments: @ec *p@ = pointer to an elliptic-curve point
83 * Use: Initializes a new point. The initial value is the additive
84 * identity (which is universal for all curves).
87 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
89 #define EC_CREATE(p) do { \
91 _p->x = _p->y = _p->z = MP_NEW; \
94 extern void ec_create(ec */
*p*/
);
96 /* --- @ec_destroy@ --- *
98 * Arguments: @ec *p@ = pointer to an elliptic-curve point
102 * Use: Destroys a point, making it invalid.
105 #define EC_DESTROY(p) do { \
107 if (!EC_ATINF(_p)) { \
110 if (_p->z) MP_DROP(_p->z); \
114 extern void ec_destroy(ec */
*p*/
);
116 /* --- @ec_atinf@ --- *
118 * Arguments: @const ec *p@ = pointer to a point
120 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
124 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
126 extern int ec_atinf(const ec */
*p*/
);
128 /* --- @ec_setinf@ --- *
130 * Arguments: @ec *p@ = pointer to a point
134 * Use: Sets the given point to be the point %$O$% at infinity.
137 #define EC_SETINF(p) do { \
139 if (!EC_ATINF(_p)) { \
142 if (_p->z) MP_DROP(_p->z); \
143 _p->x = _p->y = _p->z = MP_NEW; \
149 extern void ec_setinf(ec */
*p*/
);
151 /* --- @ec_copy@ --- *
153 * Arguments: @ec *d@ = pointer to destination point
154 * @const ec *p@ = pointer to source point
158 * Use: Creates a copy of an elliptic curve point.
161 #define EC_COPY(d, p) do { \
163 const ec *_p = (p); \
167 _d->x = _d->y = _d->z = MP_NEW; \
176 extern void ec_copy(ec */
*d*/
, const ec */
*p*/
);
178 /*----- Interesting arithmetic --------------------------------------------*/
180 /* --- @ec_denorm@ --- *
182 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
183 * @ec *d@ = pointer to the destination point
184 * @const ec *p@ = pointer to the source point
188 * Use: Denormalizes the given point, converting to internal
189 * representations and setting the denominator to 1.
192 extern void ec_denorm(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
194 /* --- @ec_norm@ --- *
196 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
197 * @ec *d@ = pointer to the destination point
198 * @const ec *p@ = pointer to the source point
202 * Use: Normalizes the given point, by dividing through by the
203 * denominator and returning to external representation.
206 extern void ec_norm(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
208 /* --- @ec_find@ --- *
210 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
211 * @ec *d@ = pointer to the destination point
212 * @mp *x@ = a possible x-coordinate
214 * Returns: Zero if OK, nonzero if there isn't a point there.
216 * Use: Finds a point on an elliptic curve with a given x-coordinate.
219 extern void ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
221 /* --- @ec_add@ --- *
223 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
224 * @ec *d@ = pointer to the destination point
225 * @const ec *p, *q@ = pointers to the operand points
229 * Use: Adds two points on an elliptic curve.
232 extern void ec_add(ec_curve */
*c*/
, ec */
*d*/
,
233 const ec */
*p*/
, const ec */
*q*/
);
235 /* --- @ec_dbl@ --- *
237 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
238 * @ec *d@ = pointer to the destination point
239 * @const ec *p@ = pointer to the operand point
243 * Use: Doubles a point on an elliptic curve.
246 extern void ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
248 /* --- @ec_mul@ --- *
250 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
251 * @ec *d@ = pointer to the destination point
252 * @const ec *p@ = pointer to the generator point
253 * @mp *n@ = integer multiplier
257 * Use: Multiplies a point by a scalar, returning %$n p$%.
260 extern void ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
262 /*----- Creating curves ---------------------------------------------------*/
264 /* --- @ec_prime@ --- *
266 * Arguments: @field *f@ = the underyling field for this elliptic curve
267 * @mp *a, *b@ = the coefficients for this curve
269 * Returns: A pointer to the curve.
271 * Use: Creates a curve structure for an elliptic curve defined over
275 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
277 /* --- @ec_bin@ --- *
279 * Arguments: @field *f@ = the underlying field for this elliptic curve
280 * @mp *a, *b@ = the coefficients for this curve
282 * Returns: A pointer to the curve.
284 * Use: Creates a curve structure for a non-supersingular elliptic
285 * curve defined over a binary field.
288 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
290 /*----- That's all, folks -------------------------------------------------*/