3 * $Id: ec.c,v 1.4.4.2 2004/03/20 00:13:31 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.4.2 2004/03/20 00:13:31 mdw
34 * Projective coordinates for prime curves
36 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
37 * Simple (non-projective) curves over prime fields now seem to work.
39 * Revision 1.4 2003/05/15 23:25:59 mdw
40 * Make elliptic curve stuff build.
42 * Revision 1.3 2002/01/13 13:48:44 mdw
45 * Revision 1.2 2001/05/07 17:29:44 mdw
46 * Treat projective coordinates as an internal representation. Various
47 * minor interface changes.
49 * Revision 1.1 2001/04/29 18:12:33 mdw
54 /*----- Header files ------------------------------------------------------*/
59 /*----- Trivial wrappers --------------------------------------------------*/
61 /* --- @ec_create@ --- *
63 * Arguments: @ec *p@ = pointer to an elliptic-curve point
65 * Returns: The argument @p@.
67 * Use: Initializes a new point. The initial value is the additive
68 * identity (which is universal for all curves).
71 ec
*ec_create(ec
*p
) { EC_CREATE(p
); return (p
); }
73 /* --- @ec_destroy@ --- *
75 * Arguments: @ec *p@ = pointer to an elliptic-curve point
79 * Use: Destroys a point, making it invalid.
82 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
84 /* --- @ec_atinf@ --- *
86 * Arguments: @const ec *p@ = pointer to a point
88 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
92 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
94 /* --- @ec_setinf@ --- *
96 * Arguments: @ec *p@ = pointer to a point
98 * Returns: The argument @p@.
100 * Use: Sets the given point to be the point %$O$% at infinity.
103 ec
*ec_setinf(ec
*p
) { EC_SETINF(p
); return (p
); }
105 /* --- @ec_copy@ --- *
107 * Arguments: @ec *d@ = pointer to destination point
108 * @const ec *p@ = pointer to source point
110 * Returns: The destination @d@.
112 * Use: Creates a copy of an elliptic curve point.
115 ec
*ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); return (d
); }
117 /*----- Standard curve operations -----------------------------------------*/
119 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
121 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
122 * @ec *d@ = pointer to the destination
123 * @const ec *p@ = pointer to a source point
125 * Returns: The destination @d@.
127 * Use: An identity operation if your curve has no internal
128 * representation. (The field internal representation is still
132 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
138 d
->x
= F_IN(f
, d
->x
, p
->x
);
139 d
->y
= F_IN(f
, d
->y
, p
->y
);
140 mp_drop(d
->z
); d
->z
= 0;
145 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
151 d
->x
= F_OUT(f
, d
->x
, p
->x
);
152 d
->y
= F_OUT(f
, d
->y
, p
->y
);
153 mp_drop(d
->z
); d
->z
= 0;
158 ec
*ec_idfix(ec_curve
*c
, ec
*d
, const ec
*p
)
164 /* --- @ec_projin@, @ec_projout@ --- *
166 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
167 * @ec *d@ = pointer to the destination
168 * @const ec *p@ = pointer to a source point
170 * Returns: The destination @d@.
172 * Use: Conversion functions if your curve operations use a
173 * projective representation.
176 ec
*ec_projin(ec_curve
*c
, ec
*d
, const ec
*p
)
182 d
->x
= F_IN(f
, d
->x
, p
->x
);
183 d
->y
= F_IN(f
, d
->y
, p
->y
);
184 mp_drop(d
->z
); d
->z
= MP_COPY(f
->one
);
189 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
196 z
= F_INV(f
, MP_NEW
, p
->z
);
197 zz
= F_SQR(f
, MP_NEW
, z
);
198 z
= F_MUL(f
, z
, zz
, z
);
199 x
= F_MUL(f
, d
->x
, p
->x
, zz
);
200 y
= F_MUL(f
, d
->y
, p
->y
, z
);
204 d
->x
= F_OUT(f
, x
, x
);
205 d
->y
= F_OUT(f
, y
, y
);
211 ec
*ec_projfix(ec_curve
*c
, ec
*d
, const ec
*p
)
215 else if (d
->z
== c
->f
->one
)
220 z
= F_INV(f
, MP_NEW
, p
->z
);
221 zz
= F_SQR(f
, MP_NEW
, z
);
222 z
= F_MUL(f
, z
, zz
, z
);
223 d
->x
= F_MUL(f
, d
->x
, p
->x
, zz
);
224 d
->y
= F_MUL(f
, d
->y
, p
->y
, z
);
228 d
->z
= MP_COPY(f
->one
);
233 /* --- @ec_stdsub@ --- *
235 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
236 * @ec *d@ = pointer to the destination
237 * @const ec *p, *q@ = the operand points
239 * Returns: The destination @d@.
241 * Use: Standard point subtraction operation, in terms of negation
242 * and addition. This isn't as efficient as a ready-made
243 * subtraction operator.
246 ec
*ec_stdsub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
256 /*----- Creating curves ---------------------------------------------------*/
258 /* --- @ec_destroycurve@ --- *
260 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
264 * Use: Destroys a description of an elliptic curve.
267 void ec_destroycurve(ec_curve
*c
) { c
->ops
->destroy(c
); }
269 /*----- Real arithmetic ---------------------------------------------------*/
271 /* --- @ec_find@ --- *
273 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
274 * @ec *d@ = pointer to the destination point
275 * @mp *x@ = a possible x-coordinate
277 * Returns: Zero if OK, nonzero if there isn't a point there.
279 * Use: Finds a point on an elliptic curve with a given x-coordinate.
282 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
284 x
= F_IN(c
->f
, MP_NEW
, x
);
285 if ((d
= EC_FIND(c
, d
, x
)) != 0)
291 /* --- @ec_neg@ --- *
293 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
294 * @ec *d@ = pointer to the destination point
295 * @const ec *p@ = pointer to the operand point
297 * Returns: The destination point.
299 * Use: Computes the negation of the given point.
302 ec
*ec_neg(ec_curve
*c
, ec
*d
, const ec
*p
)
306 return (EC_OUT(c
, d
, d
));
309 /* --- @ec_add@ --- *
311 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
312 * @ec *d@ = pointer to the destination point
313 * @const ec *p, *q@ = pointers to the operand points
317 * Use: Adds two points on an elliptic curve.
320 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
322 ec pp
= EC_INIT
, qq
= EC_INIT
;
325 EC_ADD(c
, d
, &pp
, &qq
);
332 /* --- @ec_sub@ --- *
334 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
335 * @ec *d@ = pointer to the destination point
336 * @const ec *p, *q@ = pointers to the operand points
338 * Returns: The destination @d@.
340 * Use: Subtracts one point from another on an elliptic curve.
343 ec
*ec_sub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
348 EC_SUB(c
, d
, &qq
, &qq
);
355 /* --- @ec_dbl@ --- *
357 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
358 * @ec *d@ = pointer to the destination point
359 * @const ec *p@ = pointer to the operand point
363 * Use: Doubles a point on an elliptic curve.
366 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
370 return (EC_OUT(c
, d
, d
));
373 /* --- @ec_check@ --- *
375 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
376 * @const ec *p@ = pointer to the point
378 * Returns: Zero if OK, nonzero if this is an invalid point.
380 * Use: Checks that a point is actually on an elliptic curve.
383 int ec_check(ec_curve
*c
, const ec
*p
)
391 rc
= EC_CHECK(c
, &t
);
396 /* --- @ec_imul@, @ec_mul@ --- *
398 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
399 * @ec *d@ = pointer to the destination point
400 * @const ec *p@ = pointer to the generator point
401 * @mp *n@ = integer multiplier
403 * Returns: The destination @d@.
405 * Use: Multiplies a point by a scalar, returning %$n p$%. The
406 * @imul@ variant uses internal representations for argument
410 ec
*ec_imul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
415 if (t
.x
&& (n
->f
& MP_BURN
))
424 if (MP_LEN(n
) < EXP_THRESH
)
425 EXP_SIMPLE(*d
, t
, n
);
427 EXP_WINDOW(*d
, t
, n
);
433 ec
*ec_mul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
437 return (EC_OUT(c
, d
, d
));
440 /*----- That's all, folks -------------------------------------------------*/