3 * $Id: ec.c,v 1.9 2004/04/01 21:28:41 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.9 2004/04/01 21:28:41 mdw
34 * Normal basis support (translates to poly basis internally). Rewrite
35 * EC and prime group table generators in awk, so that they can reuse data
36 * for repeated constants.
38 * Revision 1.8 2004/04/01 12:50:09 mdw
39 * Add cyclic group abstraction, with test code. Separate off exponentation
40 * functions for better static linking. Fix a buttload of bugs on the way.
41 * Generally ensure that negative exponents do inversion correctly. Add
42 * table of standard prime-field subgroups. (Binary field subgroups are
43 * currently unimplemented but easy to add if anyone ever finds a good one.)
45 * Revision 1.7 2004/03/27 17:54:11 mdw
46 * Standard curves and curve checking.
48 * Revision 1.6 2004/03/23 15:19:32 mdw
49 * Test elliptic curves more thoroughly.
51 * Revision 1.5 2004/03/21 22:52:06 mdw
52 * Merge and close elliptic curve branch.
54 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
55 * Projective coordinates for prime curves
57 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
58 * Simple (non-projective) curves over prime fields now seem to work.
60 * Revision 1.4 2003/05/15 23:25:59 mdw
61 * Make elliptic curve stuff build.
63 * Revision 1.3 2002/01/13 13:48:44 mdw
66 * Revision 1.2 2001/05/07 17:29:44 mdw
67 * Treat projective coordinates as an internal representation. Various
68 * minor interface changes.
70 * Revision 1.1 2001/04/29 18:12:33 mdw
75 /*----- Header files ------------------------------------------------------*/
79 /*----- Trivial wrappers --------------------------------------------------*/
81 /* --- @ec_samep@ --- *
83 * Arguments: @ec_curve *c, *d@ = two elliptic curves
85 * Returns: Nonzero if the curves are identical (not just isomorphic).
87 * Use: Checks for sameness of curves. This function does the full
88 * check, not just the curve-type-specific check done by the
89 * @sampep@ field operation.
92 int ec_samep(ec_curve
*c
, ec_curve
*d
)
94 return (field_samep(c
->f
, d
->f
) && c
->ops
== d
->ops
&& EC_SAMEP(c
, d
));
97 /* --- @ec_create@ --- *
99 * Arguments: @ec *p@ = pointer to an elliptic-curve point
101 * Returns: The argument @p@.
103 * Use: Initializes a new point. The initial value is the additive
104 * identity (which is universal for all curves).
107 ec
*ec_create(ec
*p
) { EC_CREATE(p
); return (p
); }
109 /* --- @ec_destroy@ --- *
111 * Arguments: @ec *p@ = pointer to an elliptic-curve point
115 * Use: Destroys a point, making it invalid.
118 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
120 /* --- @ec_atinf@ --- *
122 * Arguments: @const ec *p@ = pointer to a point
124 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
128 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
130 /* --- @ec_setinf@ --- *
132 * Arguments: @ec *p@ = pointer to a point
134 * Returns: The argument @p@.
136 * Use: Sets the given point to be the point %$O$% at infinity.
139 ec
*ec_setinf(ec
*p
) { EC_SETINF(p
); return (p
); }
141 /* --- @ec_copy@ --- *
143 * Arguments: @ec *d@ = pointer to destination point
144 * @const ec *p@ = pointer to source point
146 * Returns: The destination @d@.
148 * Use: Creates a copy of an elliptic curve point.
151 ec
*ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); return (d
); }
155 * Arguments: @const ec *p, *q@ = two points
157 * Returns: Nonzero if the points are equal. Compares external-format
161 int ec_eq(const ec
*p
, const ec
*q
) { return (EC_EQ(p
, q
)); }
163 /*----- Standard curve operations -----------------------------------------*/
165 /* --- @ec_stdsamep@ --- *
167 * Arguments: @ec_curve *c, *d@ = two elliptic curves
169 * Returns: Nonzero if the curves are identical (not just isomorphic).
171 * Use: Simple sameness check on @a@ and @b@ curve members.
174 int ec_stdsamep(ec_curve
*c
, ec_curve
*d
)
176 return (MP_EQ(c
->a
, d
->a
) && MP_EQ(c
->b
, d
->b
));
179 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
181 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
182 * @ec *d@ = pointer to the destination
183 * @const ec *p@ = pointer to a source point
185 * Returns: The destination @d@.
187 * Use: An identity operation if your curve has no internal
188 * representation. (The field internal representation is still
192 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
198 d
->x
= F_IN(f
, d
->x
, p
->x
);
199 d
->y
= F_IN(f
, d
->y
, p
->y
);
200 mp_drop(d
->z
); d
->z
= 0;
205 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
211 d
->x
= F_OUT(f
, d
->x
, p
->x
);
212 d
->y
= F_OUT(f
, d
->y
, p
->y
);
213 mp_drop(d
->z
); d
->z
= 0;
218 ec
*ec_idfix(ec_curve
*c
, ec
*d
, const ec
*p
)
224 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
226 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
227 * @ec *d@ = pointer to the destination
228 * @const ec *p@ = pointer to a source point
230 * Returns: The destination @d@.
232 * Use: Conversion functions if your curve operations use a
233 * projective representation.
236 ec
*ec_projin(ec_curve
*c
, ec
*d
, const ec
*p
)
242 d
->x
= F_IN(f
, d
->x
, p
->x
);
243 d
->y
= F_IN(f
, d
->y
, p
->y
);
244 mp_drop(d
->z
); d
->z
= MP_COPY(f
->one
);
249 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
256 z
= F_INV(f
, MP_NEW
, p
->z
);
257 zz
= F_SQR(f
, MP_NEW
, z
);
258 z
= F_MUL(f
, z
, zz
, z
);
259 x
= F_MUL(f
, d
->x
, p
->x
, zz
);
260 y
= F_MUL(f
, d
->y
, p
->y
, z
);
264 d
->x
= F_OUT(f
, x
, x
);
265 d
->y
= F_OUT(f
, y
, y
);
271 ec
*ec_projfix(ec_curve
*c
, ec
*d
, const ec
*p
)
275 else if (d
->z
== c
->f
->one
)
280 z
= F_INV(f
, MP_NEW
, p
->z
);
281 zz
= F_SQR(f
, MP_NEW
, z
);
282 z
= F_MUL(f
, z
, zz
, z
);
283 d
->x
= F_MUL(f
, d
->x
, p
->x
, zz
);
284 d
->y
= F_MUL(f
, d
->y
, p
->y
, z
);
288 d
->z
= MP_COPY(f
->one
);
293 /* --- @ec_stdsub@ --- *
295 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
296 * @ec *d@ = pointer to the destination
297 * @const ec *p, *q@ = the operand points
299 * Returns: The destination @d@.
301 * Use: Standard point subtraction operation, in terms of negation
302 * and addition. This isn't as efficient as a ready-made
303 * subtraction operator.
306 ec
*ec_stdsub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
316 /*----- Creating curves ---------------------------------------------------*/
318 /* --- @ec_destroycurve@ --- *
320 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
324 * Use: Destroys a description of an elliptic curve.
327 void ec_destroycurve(ec_curve
*c
) { c
->ops
->destroy(c
); }
329 /*----- Real arithmetic ---------------------------------------------------*/
331 /* --- @ec_find@ --- *
333 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
334 * @ec *d@ = pointer to the destination point
335 * @mp *x@ = a possible x-coordinate
337 * Returns: Zero if OK, nonzero if there isn't a point there.
339 * Use: Finds a point on an elliptic curve with a given x-coordinate.
342 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
344 x
= F_IN(c
->f
, MP_NEW
, x
);
345 if ((d
= EC_FIND(c
, d
, x
)) != 0)
351 /* --- @ec_neg@ --- *
353 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
354 * @ec *d@ = pointer to the destination point
355 * @const ec *p@ = pointer to the operand point
357 * Returns: The destination point.
359 * Use: Computes the negation of the given point.
362 ec
*ec_neg(ec_curve
*c
, ec
*d
, const ec
*p
)
366 return (EC_OUT(c
, d
, d
));
369 /* --- @ec_add@ --- *
371 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
372 * @ec *d@ = pointer to the destination point
373 * @const ec *p, *q@ = pointers to the operand points
377 * Use: Adds two points on an elliptic curve.
380 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
382 ec pp
= EC_INIT
, qq
= EC_INIT
;
385 EC_ADD(c
, d
, &pp
, &qq
);
392 /* --- @ec_sub@ --- *
394 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
395 * @ec *d@ = pointer to the destination point
396 * @const ec *p, *q@ = pointers to the operand points
398 * Returns: The destination @d@.
400 * Use: Subtracts one point from another on an elliptic curve.
403 ec
*ec_sub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
405 ec pp
= EC_INIT
, qq
= EC_INIT
;
408 EC_SUB(c
, d
, &pp
, &qq
);
415 /* --- @ec_dbl@ --- *
417 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
418 * @ec *d@ = pointer to the destination point
419 * @const ec *p@ = pointer to the operand point
423 * Use: Doubles a point on an elliptic curve.
426 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
430 return (EC_OUT(c
, d
, d
));
433 /* --- @ec_check@ --- *
435 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
436 * @const ec *p@ = pointer to the point
438 * Returns: Zero if OK, nonzero if this is an invalid point.
440 * Use: Checks that a point is actually on an elliptic curve.
443 int ec_check(ec_curve
*c
, const ec
*p
)
451 rc
= EC_CHECK(c
, &t
);
456 /* --- @ec_rand@ --- *
458 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
459 * @ec *d@ = pointer to the destination point
460 * @grand *r@ = random number source
462 * Returns: The destination @d@.
464 * Use: Finds a random point on the given curve.
467 ec
*ec_rand(ec_curve
*c
, ec
*d
, grand
*r
)
470 do x
= F_RAND(c
->f
, x
, r
); while (!EC_FIND(c
, d
, x
));
472 if (grand_range(r
, 2)) EC_NEG(c
, d
, d
);
473 return (EC_OUT(c
, d
, d
));
476 /*----- That's all, folks -------------------------------------------------*/