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 /*----- Header files ------------------------------------------------------*/
34 /*----- Trivial wrappers --------------------------------------------------*/
36 /* --- @ec_samep@ --- *
38 * Arguments: @ec_curve *c, *d@ = two elliptic curves
40 * Returns: Nonzero if the curves are identical (not just isomorphic).
42 * Use: Checks for sameness of curves. This function does the full
43 * check, not just the curve-type-specific check done by the
44 * @sampep@ field operation.
47 int ec_samep(ec_curve
*c
, ec_curve
*d
)
49 return (c
== d
|| (field_samep(c
->f
, d
->f
) &&
50 c
->ops
== d
->ops
&& EC_SAMEP(c
, d
)));
53 /* --- @ec_create@ --- *
55 * Arguments: @ec *p@ = pointer to an elliptic-curve point
57 * Returns: The argument @p@.
59 * Use: Initializes a new point. The initial value is the additive
60 * identity (which is universal for all curves).
63 ec
*ec_create(ec
*p
) { EC_CREATE(p
); return (p
); }
65 /* --- @ec_destroy@ --- *
67 * Arguments: @ec *p@ = pointer to an elliptic-curve point
71 * Use: Destroys a point, making it invalid.
74 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
76 /* --- @ec_atinf@ --- *
78 * Arguments: @const ec *p@ = pointer to a point
80 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
84 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
86 /* --- @ec_setinf@ --- *
88 * Arguments: @ec *p@ = pointer to a point
90 * Returns: The argument @p@.
92 * Use: Sets the given point to be the point %$O$% at infinity.
95 ec
*ec_setinf(ec
*p
) { EC_SETINF(p
); return (p
); }
97 /* --- @ec_copy@ --- *
99 * Arguments: @ec *d@ = pointer to destination point
100 * @const ec *p@ = pointer to source point
102 * Returns: The destination @d@.
104 * Use: Creates a copy of an elliptic curve point.
107 ec
*ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); return (d
); }
111 * Arguments: @const ec *p, *q@ = two points
113 * Returns: Nonzero if the points are equal. Compares external-format
117 int ec_eq(const ec
*p
, const ec
*q
) { return (EC_EQ(p
, q
)); }
119 /*----- Standard curve operations -----------------------------------------*/
121 /* --- @ec_stdsamep@ --- *
123 * Arguments: @ec_curve *c, *d@ = two elliptic curves
125 * Returns: Nonzero if the curves are identical (not just isomorphic).
127 * Use: Simple sameness check on @a@ and @b@ curve members.
130 int ec_stdsamep(ec_curve
*c
, ec_curve
*d
)
131 { return (MP_EQ(c
->a
, d
->a
) && MP_EQ(c
->b
, d
->b
)); }
133 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
135 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
136 * @ec *d@ = pointer to the destination
137 * @const ec *p@ = pointer to a source point
139 * Returns: The destination @d@.
141 * Use: An identity operation if your curve has no internal
142 * representation. (The field internal representation is still
146 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
152 d
->x
= F_IN(f
, d
->x
, p
->x
);
153 d
->y
= F_IN(f
, d
->y
, p
->y
);
154 mp_drop(d
->z
); d
->z
= 0;
159 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
165 d
->x
= F_OUT(f
, d
->x
, p
->x
);
166 d
->y
= F_OUT(f
, d
->y
, p
->y
);
167 mp_drop(d
->z
); d
->z
= 0;
172 ec
*ec_idfix(ec_curve
*c
, ec
*d
, const ec
*p
)
173 { EC_COPY(d
, p
); return (d
); }
175 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
177 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
178 * @ec *d@ = pointer to the destination
179 * @const ec *p@ = pointer to a source point
181 * Returns: The destination @d@.
183 * Use: Conversion functions if your curve operations use a
184 * projective representation.
187 ec
*ec_projin(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
= MP_COPY(f
->one
);
200 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
207 if (p
->z
== f
->one
) {
208 d
->x
= F_OUT(f
, d
->x
, p
->x
);
209 d
->y
= F_OUT(f
, d
->y
, p
->y
);
211 z
= F_INV(f
, MP_NEW
, p
->z
);
212 zz
= F_SQR(f
, MP_NEW
, z
);
213 z
= F_MUL(f
, z
, zz
, z
);
214 x
= F_MUL(f
, d
->x
, p
->x
, zz
);
215 y
= F_MUL(f
, d
->y
, p
->y
, z
);
218 d
->x
= F_OUT(f
, x
, x
);
219 d
->y
= F_OUT(f
, y
, y
);
227 ec
*ec_projfix(ec_curve
*c
, ec
*d
, const ec
*p
)
231 else if (p
->z
== c
->f
->one
)
236 z
= F_INV(f
, MP_NEW
, p
->z
);
237 zz
= F_SQR(f
, MP_NEW
, z
);
238 z
= F_MUL(f
, z
, zz
, z
);
239 d
->x
= F_MUL(f
, d
->x
, p
->x
, zz
);
240 d
->y
= F_MUL(f
, d
->y
, p
->y
, z
);
244 d
->z
= MP_COPY(f
->one
);
249 /* --- @ec_stdsub@ --- *
251 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
252 * @ec *d@ = pointer to the destination
253 * @const ec *p, *q@ = the operand points
255 * Returns: The destination @d@.
257 * Use: Standard point subtraction operation, in terms of negation
258 * and addition. This isn't as efficient as a ready-made
259 * subtraction operator.
262 ec
*ec_stdsub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
272 /*----- Creating curves ---------------------------------------------------*/
274 /* --- @ec_destroycurve@ --- *
276 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
280 * Use: Destroys a description of an elliptic curve.
283 void ec_destroycurve(ec_curve
*c
) { c
->ops
->destroy(c
); }
285 /*----- Real arithmetic ---------------------------------------------------*/
287 /* --- @ec_find@ --- *
289 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
290 * @ec *d@ = pointer to the destination point
291 * @mp *x@ = a possible x-coordinate
293 * Returns: Zero if OK, nonzero if there isn't a point there.
295 * Use: Finds a point on an elliptic curve with a given x-coordinate.
298 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
300 x
= F_IN(c
->f
, MP_NEW
, x
);
301 if ((d
= EC_FIND(c
, d
, x
)) != 0)
307 /* --- @ec_neg@ --- *
309 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
310 * @ec *d@ = pointer to the destination point
311 * @const ec *p@ = pointer to the operand point
313 * Returns: The destination point.
315 * Use: Computes the negation of the given point.
318 ec
*ec_neg(ec_curve
*c
, ec
*d
, const ec
*p
)
319 { EC_IN(c
, d
, p
); EC_NEG(c
, d
, d
); return (EC_OUT(c
, d
, d
)); }
321 /* --- @ec_add@ --- *
323 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
324 * @ec *d@ = pointer to the destination point
325 * @const ec *p, *q@ = pointers to the operand points
329 * Use: Adds two points on an elliptic curve.
332 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
334 ec pp
= EC_INIT
, qq
= EC_INIT
;
337 EC_ADD(c
, d
, &pp
, &qq
);
344 /* --- @ec_sub@ --- *
346 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
347 * @ec *d@ = pointer to the destination point
348 * @const ec *p, *q@ = pointers to the operand points
350 * Returns: The destination @d@.
352 * Use: Subtracts one point from another on an elliptic curve.
355 ec
*ec_sub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
357 ec pp
= EC_INIT
, qq
= EC_INIT
;
360 EC_SUB(c
, d
, &pp
, &qq
);
367 /* --- @ec_dbl@ --- *
369 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
370 * @ec *d@ = pointer to the destination point
371 * @const ec *p@ = pointer to the operand point
375 * Use: Doubles a point on an elliptic curve.
378 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
379 { EC_IN(c
, d
, p
); EC_DBL(c
, d
, d
); return (EC_OUT(c
, d
, d
)); }
381 /* --- @ec_check@ --- *
383 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
384 * @const ec *p@ = pointer to the point
386 * Returns: Zero if OK, nonzero if this is an invalid point.
388 * Use: Checks that a point is actually on an elliptic curve.
391 int ec_check(ec_curve
*c
, const ec
*p
)
399 rc
= EC_CHECK(c
, &t
);
404 /* --- @ec_rand@ --- *
406 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
407 * @ec *d@ = pointer to the destination point
408 * @grand *r@ = random number source
410 * Returns: The destination @d@.
412 * Use: Finds a random point on the given curve.
415 ec
*ec_rand(ec_curve
*c
, ec
*d
, grand
*r
)
418 do x
= F_RAND(c
->f
, x
, r
); while (!EC_FIND(c
, d
, x
));
420 if (grand_range(r
, 2)) EC_NEG(c
, d
, d
);
421 return (EC_OUT(c
, d
, d
));
424 /*----- That's all, folks -------------------------------------------------*/