3 * $Id: ec.c,v 1.8 2004/04/01 12:50:09 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.8 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.7 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
43 * Revision 1.6 2004/03/23 15:19:32 mdw
44 * Test elliptic curves more thoroughly.
46 * Revision 1.5 2004/03/21 22:52:06 mdw
47 * Merge and close elliptic curve branch.
49 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
50 * Projective coordinates for prime curves
52 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
53 * Simple (non-projective) curves over prime fields now seem to work.
55 * Revision 1.4 2003/05/15 23:25:59 mdw
56 * Make elliptic curve stuff build.
58 * Revision 1.3 2002/01/13 13:48:44 mdw
61 * Revision 1.2 2001/05/07 17:29:44 mdw
62 * Treat projective coordinates as an internal representation. Various
63 * minor interface changes.
65 * Revision 1.1 2001/04/29 18:12:33 mdw
70 /*----- Header files ------------------------------------------------------*/
74 /*----- Trivial wrappers --------------------------------------------------*/
76 /* --- @ec_samep@ --- *
78 * Arguments: @ec_curve *c, *d@ = two elliptic curves
80 * Returns: Nonzero if the curves are identical (not just isomorphic).
82 * Use: Checks for sameness of curves. This function does the full
83 * check, not just the curve-type-specific check done by the
84 * @sampep@ field operation.
87 int ec_samep(ec_curve
*c
, ec_curve
*d
)
89 return (field_samep(c
->f
, d
->f
) && c
->ops
== d
->ops
&& EC_SAMEP(c
, d
));
92 /* --- @ec_create@ --- *
94 * Arguments: @ec *p@ = pointer to an elliptic-curve point
96 * Returns: The argument @p@.
98 * Use: Initializes a new point. The initial value is the additive
99 * identity (which is universal for all curves).
102 ec
*ec_create(ec
*p
) { EC_CREATE(p
); return (p
); }
104 /* --- @ec_destroy@ --- *
106 * Arguments: @ec *p@ = pointer to an elliptic-curve point
110 * Use: Destroys a point, making it invalid.
113 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
115 /* --- @ec_atinf@ --- *
117 * Arguments: @const ec *p@ = pointer to a point
119 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
123 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
125 /* --- @ec_setinf@ --- *
127 * Arguments: @ec *p@ = pointer to a point
129 * Returns: The argument @p@.
131 * Use: Sets the given point to be the point %$O$% at infinity.
134 ec
*ec_setinf(ec
*p
) { EC_SETINF(p
); return (p
); }
136 /* --- @ec_copy@ --- *
138 * Arguments: @ec *d@ = pointer to destination point
139 * @const ec *p@ = pointer to source point
141 * Returns: The destination @d@.
143 * Use: Creates a copy of an elliptic curve point.
146 ec
*ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); return (d
); }
150 * Arguments: @const ec *p, *q@ = two points
152 * Returns: Nonzero if the points are equal. Compares external-format
156 int ec_eq(const ec
*p
, const ec
*q
) { return (EC_EQ(p
, q
)); }
158 /*----- Standard curve operations -----------------------------------------*/
160 /* --- @ec_stdsamep@ --- *
162 * Arguments: @ec_curve *c, *d@ = two elliptic curves
164 * Returns: Nonzero if the curves are identical (not just isomorphic).
166 * Use: Simple sameness check on @a@ and @b@ curve members.
169 int ec_stdsamep(ec_curve
*c
, ec_curve
*d
)
171 return (MP_EQ(c
->a
, d
->a
) && MP_EQ(c
->b
, d
->b
));
174 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
176 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
177 * @ec *d@ = pointer to the destination
178 * @const ec *p@ = pointer to a source point
180 * Returns: The destination @d@.
182 * Use: An identity operation if your curve has no internal
183 * representation. (The field internal representation is still
187 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
193 d
->x
= F_IN(f
, d
->x
, p
->x
);
194 d
->y
= F_IN(f
, d
->y
, p
->y
);
195 mp_drop(d
->z
); d
->z
= 0;
200 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
206 d
->x
= F_OUT(f
, d
->x
, p
->x
);
207 d
->y
= F_OUT(f
, d
->y
, p
->y
);
208 mp_drop(d
->z
); d
->z
= 0;
213 ec
*ec_idfix(ec_curve
*c
, ec
*d
, const ec
*p
)
219 /* --- @ec_projin@, @ec_projout@ --- *
221 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
222 * @ec *d@ = pointer to the destination
223 * @const ec *p@ = pointer to a source point
225 * Returns: The destination @d@.
227 * Use: Conversion functions if your curve operations use a
228 * projective representation.
231 ec
*ec_projin(ec_curve
*c
, ec
*d
, const ec
*p
)
237 d
->x
= F_IN(f
, d
->x
, p
->x
);
238 d
->y
= F_IN(f
, d
->y
, p
->y
);
239 mp_drop(d
->z
); d
->z
= MP_COPY(f
->one
);
244 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
251 z
= F_INV(f
, MP_NEW
, p
->z
);
252 zz
= F_SQR(f
, MP_NEW
, z
);
253 z
= F_MUL(f
, z
, zz
, z
);
254 x
= F_MUL(f
, d
->x
, p
->x
, zz
);
255 y
= F_MUL(f
, d
->y
, p
->y
, z
);
259 d
->x
= F_OUT(f
, x
, x
);
260 d
->y
= F_OUT(f
, y
, y
);
266 ec
*ec_projfix(ec_curve
*c
, ec
*d
, const ec
*p
)
270 else if (d
->z
== c
->f
->one
)
275 z
= F_INV(f
, MP_NEW
, p
->z
);
276 zz
= F_SQR(f
, MP_NEW
, z
);
277 z
= F_MUL(f
, z
, zz
, z
);
278 d
->x
= F_MUL(f
, d
->x
, p
->x
, zz
);
279 d
->y
= F_MUL(f
, d
->y
, p
->y
, z
);
283 d
->z
= MP_COPY(f
->one
);
288 /* --- @ec_stdsub@ --- *
290 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
291 * @ec *d@ = pointer to the destination
292 * @const ec *p, *q@ = the operand points
294 * Returns: The destination @d@.
296 * Use: Standard point subtraction operation, in terms of negation
297 * and addition. This isn't as efficient as a ready-made
298 * subtraction operator.
301 ec
*ec_stdsub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
311 /*----- Creating curves ---------------------------------------------------*/
313 /* --- @ec_destroycurve@ --- *
315 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
319 * Use: Destroys a description of an elliptic curve.
322 void ec_destroycurve(ec_curve
*c
) { c
->ops
->destroy(c
); }
324 /*----- Real arithmetic ---------------------------------------------------*/
326 /* --- @ec_find@ --- *
328 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
329 * @ec *d@ = pointer to the destination point
330 * @mp *x@ = a possible x-coordinate
332 * Returns: Zero if OK, nonzero if there isn't a point there.
334 * Use: Finds a point on an elliptic curve with a given x-coordinate.
337 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
339 x
= F_IN(c
->f
, MP_NEW
, x
);
340 if ((d
= EC_FIND(c
, d
, x
)) != 0)
346 /* --- @ec_neg@ --- *
348 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
349 * @ec *d@ = pointer to the destination point
350 * @const ec *p@ = pointer to the operand point
352 * Returns: The destination point.
354 * Use: Computes the negation of the given point.
357 ec
*ec_neg(ec_curve
*c
, ec
*d
, const ec
*p
)
361 return (EC_OUT(c
, d
, d
));
364 /* --- @ec_add@ --- *
366 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
367 * @ec *d@ = pointer to the destination point
368 * @const ec *p, *q@ = pointers to the operand points
372 * Use: Adds two points on an elliptic curve.
375 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
377 ec pp
= EC_INIT
, qq
= EC_INIT
;
380 EC_ADD(c
, d
, &pp
, &qq
);
387 /* --- @ec_sub@ --- *
389 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
390 * @ec *d@ = pointer to the destination point
391 * @const ec *p, *q@ = pointers to the operand points
393 * Returns: The destination @d@.
395 * Use: Subtracts one point from another on an elliptic curve.
398 ec
*ec_sub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
400 ec pp
= EC_INIT
, qq
= EC_INIT
;
403 EC_SUB(c
, d
, &pp
, &qq
);
410 /* --- @ec_dbl@ --- *
412 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
413 * @ec *d@ = pointer to the destination point
414 * @const ec *p@ = pointer to the operand point
418 * Use: Doubles a point on an elliptic curve.
421 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
425 return (EC_OUT(c
, d
, d
));
428 /* --- @ec_check@ --- *
430 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
431 * @const ec *p@ = pointer to the point
433 * Returns: Zero if OK, nonzero if this is an invalid point.
435 * Use: Checks that a point is actually on an elliptic curve.
438 int ec_check(ec_curve
*c
, const ec
*p
)
446 rc
= EC_CHECK(c
, &t
);
451 /* --- @ec_rand@ --- *
453 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
454 * @ec *d@ = pointer to the destination point
455 * @grand *r@ = random number source
457 * Returns: The destination @d@.
459 * Use: Finds a random point on the given curve.
462 ec
*ec_rand(ec_curve
*c
, ec
*d
, grand
*r
)
465 do x
= F_RAND(c
->f
, x
, r
); while (!EC_FIND(c
, d
, x
));
467 if (grand_range(r
, 2)) EC_NEG(c
, d
, d
);
468 return (EC_OUT(c
, d
, d
));
471 /*----- That's all, folks -------------------------------------------------*/