3 * $Id: ec.c,v 1.4 2003/05/15 23:25:59 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.4 2003/05/15 23:25:59 mdw
34 * Make elliptic curve stuff build.
36 * Revision 1.3 2002/01/13 13:48:44 mdw
39 * Revision 1.2 2001/05/07 17:29:44 mdw
40 * Treat projective coordinates as an internal representation. Various
41 * minor interface changes.
43 * Revision 1.1 2001/04/29 18:12:33 mdw
48 /*----- Header files ------------------------------------------------------*/
53 /*----- Trivial wrappers --------------------------------------------------*/
55 /* --- @ec_create@ --- *
57 * Arguments: @ec *p@ = pointer to an elliptic-curve point
59 * Returns: The argument @p@.
61 * Use: Initializes a new point. The initial value is the additive
62 * identity (which is universal for all curves).
65 ec *ec_create(ec *p) { EC_CREATE(p); return (p); }
67 /* --- @ec_destroy@ --- *
69 * Arguments: @ec *p@ = pointer to an elliptic-curve point
73 * Use: Destroys a point, making it invalid.
76 void ec_destroy(ec *p) { EC_DESTROY(p); }
78 /* --- @ec_atinf@ --- *
80 * Arguments: @const ec *p@ = pointer to a point
82 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
86 int ec_atinf(const ec *p) { return (EC_ATINF(p)); }
88 /* --- @ec_setinf@ --- *
90 * Arguments: @ec *p@ = pointer to a point
92 * Returns: The argument @p@.
94 * Use: Sets the given point to be the point %$O$% at infinity.
97 ec *ec_setinf(ec *p) { EC_SETINF(p); return (p); }
99 /* --- @ec_copy@ --- *
101 * Arguments: @ec *d@ = pointer to destination point
102 * @const ec *p@ = pointer to source point
104 * Returns: The destination @d@.
106 * Use: Creates a copy of an elliptic curve point.
109 ec *ec_copy(ec *d, const ec *p) { EC_COPY(d, p); return (d); }
111 /*----- Standard curve operations -----------------------------------------*/
113 /* --- @ec_idin@, @ec_idout@ --- *
115 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
116 * @ec *d@ = pointer to the destination
117 * @const ec *p@ = pointer to a source point
119 * Returns: The destination @d@.
121 * Use: An identity operation if your curve has no internal
122 * representation. (The field internal representation is still
126 ec *ec_idin(ec_curve *c, ec *d, const ec *p)
132 d->x = F_IN(f, d->x, p->x);
133 d->y = F_IN(f, d->y, p->y);
134 mp_drop(d->z); d->z = 0;
139 ec *ec_idout(ec_curve *c, ec *d, const ec *p)
145 d->x = F_OUT(f, d->x, p->x);
146 d->y = F_OUT(f, d->y, p->y);
147 mp_drop(d->z); d->z = 0;
152 /* --- @ec_projin@, @ec_projout@ --- *
154 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
155 * @ec *d@ = pointer to the destination
156 * @const ec *p@ = pointer to a source point
158 * Returns: The destination @d@.
160 * Use: Conversion functions if your curve operations use a
161 * projective representation.
164 ec *ec_projin(ec_curve *c, ec *d, const ec *p)
170 d->x = F_IN(f, d->x, p->x);
171 d->y = F_IN(f, d->y, p->y);
172 mp_drop(d->z); d->z = MP_COPY(f->one);
177 ec *ec_projout(ec_curve *c, ec *d, const ec *p)
184 z = F_INV(f, MP_NEW, p->z);
185 x = F_MUL(f, d->x, p->x, z);
186 y = F_MUL(f, d->y, p->y, z);
189 d->x = F_OUT(f, x, x);
190 d->y = F_OUT(f, y, y);
196 /* --- @ec_stdsub@ --- *
198 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
199 * @ec *d@ = pointer to the destination
200 * @const ec *p, *q@ = the operand points
202 * Returns: The destination @d@.
204 * Use: Standard point subtraction operation, in terms of negation
205 * and addition. This isn't as efficient as a ready-made
206 * subtraction operator.
209 ec *ec_stdsub(ec_curve *c, ec *d, const ec *p, const ec *q)
218 /*----- Creating curves ---------------------------------------------------*/
220 /* --- @ec_destroycurve@ --- *
222 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
226 * Use: Destroys a description of an elliptic curve.
229 void ec_destroycurve(ec_curve *c) { c->ops->destroy(c); }
231 /*----- Real arithmetic ---------------------------------------------------*/
233 /* --- @ec_find@ --- *
235 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
236 * @ec *d@ = pointer to the destination point
237 * @mp *x@ = a possible x-coordinate
239 * Returns: Zero if OK, nonzero if there isn't a point there.
241 * Use: Finds a point on an elliptic curve with a given x-coordinate.
244 ec *ec_find(ec_curve *c, ec *d, mp *x)
246 x = F_IN(c->f, MP_NEW, x);
247 if ((d = EC_FIND(c, d, x)) != 0)
253 /* --- @ec_add@ --- *
255 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
256 * @ec *d@ = pointer to the destination point
257 * @const ec *p, *q@ = pointers to the operand points
261 * Use: Adds two points on an elliptic curve.
264 ec *ec_add(ec_curve *c, ec *d, const ec *p, const ec *q)
266 ec pp = EC_INIT, qq = EC_INIT;
269 EC_ADD(c, d, &pp, &qq);
276 /* --- @ec_dbl@ --- *
278 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
279 * @ec *d@ = pointer to the destination point
280 * @const ec *p@ = pointer to the operand point
284 * Use: Doubles a point on an elliptic curve.
287 ec *ec_dbl(ec_curve *c, ec *d, const ec *p)
291 return (EC_OUT(c, d, d));
294 /* --- @ec_imul@, @ec_mul@ --- *
296 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
297 * @ec *d@ = pointer to the destination point
298 * @const ec *p@ = pointer to the generator point
299 * @mp *n@ = integer multiplier
301 * Returns: The destination @d@.
303 * Use: Multiplies a point by a scalar, returning %$n p$%. The
304 * @imul@ variant uses internal representations for argument
308 ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n)
313 if (t.x && (n->f & MP_BURN))
319 else if (MP_LEN(n) < EXP_THRESH)
320 EXP_SIMPLE(*d, t, n);
322 EXP_WINDOW(*d, t, n);
326 ec *ec_mul(ec_curve *c, ec *d, const ec *p, mp *n)
330 return (EC_OUT(c, d, d));
333 /*----- That's all, folks -------------------------------------------------*/