3 * $Id: ec.c,v 1.5 2004/03/21 22:52:06 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.5 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
37 * Projective coordinates for prime curves
39 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
40 * Simple (non-projective) curves over prime fields now seem to work.
42 * Revision 1.4 2003/05/15 23:25:59 mdw
43 * Make elliptic curve stuff build.
45 * Revision 1.3 2002/01/13 13:48:44 mdw
48 * Revision 1.2 2001/05/07 17:29:44 mdw
49 * Treat projective coordinates as an internal representation. Various
50 * minor interface changes.
52 * Revision 1.1 2001/04/29 18:12:33 mdw
57 /*----- Header files ------------------------------------------------------*/
62 /*----- Trivial wrappers --------------------------------------------------*/
64 /* --- @ec_create@ --- *
66 * Arguments: @ec *p@ = pointer to an elliptic-curve point
68 * Returns: The argument @p@.
70 * Use: Initializes a new point. The initial value is the additive
71 * identity (which is universal for all curves).
74 ec
*ec_create(ec
*p
) { EC_CREATE(p
); return (p
); }
76 /* --- @ec_destroy@ --- *
78 * Arguments: @ec *p@ = pointer to an elliptic-curve point
82 * Use: Destroys a point, making it invalid.
85 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
87 /* --- @ec_atinf@ --- *
89 * Arguments: @const ec *p@ = pointer to a point
91 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
95 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
97 /* --- @ec_setinf@ --- *
99 * Arguments: @ec *p@ = pointer to a point
101 * Returns: The argument @p@.
103 * Use: Sets the given point to be the point %$O$% at infinity.
106 ec
*ec_setinf(ec
*p
) { EC_SETINF(p
); return (p
); }
108 /* --- @ec_copy@ --- *
110 * Arguments: @ec *d@ = pointer to destination point
111 * @const ec *p@ = pointer to source point
113 * Returns: The destination @d@.
115 * Use: Creates a copy of an elliptic curve point.
118 ec
*ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); return (d
); }
120 /*----- Standard curve operations -----------------------------------------*/
122 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
124 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
125 * @ec *d@ = pointer to the destination
126 * @const ec *p@ = pointer to a source point
128 * Returns: The destination @d@.
130 * Use: An identity operation if your curve has no internal
131 * representation. (The field internal representation is still
135 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
141 d
->x
= F_IN(f
, d
->x
, p
->x
);
142 d
->y
= F_IN(f
, d
->y
, p
->y
);
143 mp_drop(d
->z
); d
->z
= 0;
148 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
154 d
->x
= F_OUT(f
, d
->x
, p
->x
);
155 d
->y
= F_OUT(f
, d
->y
, p
->y
);
156 mp_drop(d
->z
); d
->z
= 0;
161 ec
*ec_idfix(ec_curve
*c
, ec
*d
, const ec
*p
)
167 /* --- @ec_projin@, @ec_projout@ --- *
169 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
170 * @ec *d@ = pointer to the destination
171 * @const ec *p@ = pointer to a source point
173 * Returns: The destination @d@.
175 * Use: Conversion functions if your curve operations use a
176 * projective representation.
179 ec
*ec_projin(ec_curve
*c
, ec
*d
, const ec
*p
)
185 d
->x
= F_IN(f
, d
->x
, p
->x
);
186 d
->y
= F_IN(f
, d
->y
, p
->y
);
187 mp_drop(d
->z
); d
->z
= MP_COPY(f
->one
);
192 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
199 z
= F_INV(f
, MP_NEW
, p
->z
);
200 zz
= F_SQR(f
, MP_NEW
, z
);
201 z
= F_MUL(f
, z
, zz
, z
);
202 x
= F_MUL(f
, d
->x
, p
->x
, zz
);
203 y
= F_MUL(f
, d
->y
, p
->y
, z
);
207 d
->x
= F_OUT(f
, x
, x
);
208 d
->y
= F_OUT(f
, y
, y
);
214 ec
*ec_projfix(ec_curve
*c
, ec
*d
, const ec
*p
)
218 else if (d
->z
== c
->f
->one
)
223 z
= F_INV(f
, MP_NEW
, p
->z
);
224 zz
= F_SQR(f
, MP_NEW
, z
);
225 z
= F_MUL(f
, z
, zz
, z
);
226 d
->x
= F_MUL(f
, d
->x
, p
->x
, zz
);
227 d
->y
= F_MUL(f
, d
->y
, p
->y
, z
);
231 d
->z
= MP_COPY(f
->one
);
236 /* --- @ec_stdsub@ --- *
238 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
239 * @ec *d@ = pointer to the destination
240 * @const ec *p, *q@ = the operand points
242 * Returns: The destination @d@.
244 * Use: Standard point subtraction operation, in terms of negation
245 * and addition. This isn't as efficient as a ready-made
246 * subtraction operator.
249 ec
*ec_stdsub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
259 /*----- Creating curves ---------------------------------------------------*/
261 /* --- @ec_destroycurve@ --- *
263 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
267 * Use: Destroys a description of an elliptic curve.
270 void ec_destroycurve(ec_curve
*c
) { c
->ops
->destroy(c
); }
272 /*----- Real arithmetic ---------------------------------------------------*/
274 /* --- @ec_find@ --- *
276 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
277 * @ec *d@ = pointer to the destination point
278 * @mp *x@ = a possible x-coordinate
280 * Returns: Zero if OK, nonzero if there isn't a point there.
282 * Use: Finds a point on an elliptic curve with a given x-coordinate.
285 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
287 x
= F_IN(c
->f
, MP_NEW
, x
);
288 if ((d
= EC_FIND(c
, d
, x
)) != 0)
294 /* --- @ec_neg@ --- *
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 operand point
300 * Returns: The destination point.
302 * Use: Computes the negation of the given point.
305 ec
*ec_neg(ec_curve
*c
, ec
*d
, const ec
*p
)
309 return (EC_OUT(c
, d
, d
));
312 /* --- @ec_add@ --- *
314 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
315 * @ec *d@ = pointer to the destination point
316 * @const ec *p, *q@ = pointers to the operand points
320 * Use: Adds two points on an elliptic curve.
323 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
325 ec pp
= EC_INIT
, qq
= EC_INIT
;
328 EC_ADD(c
, d
, &pp
, &qq
);
335 /* --- @ec_sub@ --- *
337 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
338 * @ec *d@ = pointer to the destination point
339 * @const ec *p, *q@ = pointers to the operand points
341 * Returns: The destination @d@.
343 * Use: Subtracts one point from another on an elliptic curve.
346 ec
*ec_sub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
351 EC_SUB(c
, d
, &qq
, &qq
);
358 /* --- @ec_dbl@ --- *
360 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
361 * @ec *d@ = pointer to the destination point
362 * @const ec *p@ = pointer to the operand point
366 * Use: Doubles a point on an elliptic curve.
369 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
373 return (EC_OUT(c
, d
, d
));
376 /* --- @ec_check@ --- *
378 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
379 * @const ec *p@ = pointer to the point
381 * Returns: Zero if OK, nonzero if this is an invalid point.
383 * Use: Checks that a point is actually on an elliptic curve.
386 int ec_check(ec_curve
*c
, const ec
*p
)
394 rc
= EC_CHECK(c
, &t
);
399 /* --- @ec_imul@, @ec_mul@ --- *
401 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
402 * @ec *d@ = pointer to the destination point
403 * @const ec *p@ = pointer to the generator point
404 * @mp *n@ = integer multiplier
406 * Returns: The destination @d@.
408 * Use: Multiplies a point by a scalar, returning %$n p$%. The
409 * @imul@ variant uses internal representations for argument
413 ec
*ec_imul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
418 if (t
.x
&& (n
->f
& MP_BURN
))
427 if (MP_LEN(n
) < EXP_THRESH
)
428 EXP_SIMPLE(*d
, t
, n
);
430 EXP_WINDOW(*d
, t
, n
);
436 ec
*ec_mul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
440 return (EC_OUT(c
, d
, d
));
443 /*----- That's all, folks -------------------------------------------------*/