f8b77f5e4d26490e9267ae6f21c9546bdde957de
3 * Elliptic curve definitions
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Trivial wrappers --------------------------------------------------*/
34 /* --- @ec_samep@ --- *
36 * Arguments: @ec_curve *c, *d@ = two elliptic curves
38 * Returns: Nonzero if the curves are identical (not just isomorphic).
40 * Use: Checks for sameness of curves. This function does the full
41 * check, not just the curve-type-specific check done by the
42 * @sampep@ field operation.
45 int ec_samep(ec_curve
*c
, ec_curve
*d
)
47 return (c
== d
|| (field_samep(c
->f
, d
->f
) &&
48 c
->ops
== d
->ops
&& EC_SAMEP(c
, d
)));
51 /* --- @ec_create@ --- *
53 * Arguments: @ec *p@ = pointer to an elliptic-curve point
55 * Returns: The argument @p@.
57 * Use: Initializes a new point. The initial value is the additive
58 * identity (which is universal for all curves).
61 ec
*ec_create(ec
*p
) { EC_CREATE(p
); return (p
); }
63 /* --- @ec_destroy@ --- *
65 * Arguments: @ec *p@ = pointer to an elliptic-curve point
69 * Use: Destroys a point, making it invalid.
72 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
74 /* --- @ec_atinf@ --- *
76 * Arguments: @const ec *p@ = pointer to a point
78 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
82 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
84 /* --- @ec_setinf@ --- *
86 * Arguments: @ec *p@ = pointer to a point
88 * Returns: The argument @p@.
90 * Use: Sets the given point to be the point %$O$% at infinity.
93 ec
*ec_setinf(ec
*p
) { EC_SETINF(p
); return (p
); }
95 /* --- @ec_copy@ --- *
97 * Arguments: @ec *d@ = pointer to destination point
98 * @const ec *p@ = pointer to source point
100 * Returns: The destination @d@.
102 * Use: Creates a copy of an elliptic curve point.
105 ec
*ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); return (d
); }
109 * Arguments: @const ec *p, *q@ = two points
111 * Returns: Nonzero if the points are equal. Compares external-format
115 int ec_eq(const ec
*p
, const ec
*q
) { return (EC_EQ(p
, q
)); }
117 /*----- Standard curve operations -----------------------------------------*/
119 /* --- @ec_stdsamep@ --- *
121 * Arguments: @ec_curve *c, *d@ = two elliptic curves
123 * Returns: Nonzero if the curves are identical (not just isomorphic).
125 * Use: Simple sameness check on @a@ and @b@ curve members.
128 int ec_stdsamep(ec_curve
*c
, ec_curve
*d
)
129 { return (MP_EQ(c
->a
, d
->a
) && MP_EQ(c
->b
, d
->b
)); }
131 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
133 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
134 * @ec *d@ = pointer to the destination
135 * @const ec *p@ = pointer to a source point
137 * Returns: The destination @d@.
139 * Use: An identity operation if your curve has no internal
140 * representation. (The field internal representation is still
144 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
150 d
->x
= F_IN(f
, d
->x
, p
->x
);
151 d
->y
= F_IN(f
, d
->y
, p
->y
);
152 mp_drop(d
->z
); d
->z
= 0;
157 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
163 d
->x
= F_OUT(f
, d
->x
, p
->x
);
164 d
->y
= F_OUT(f
, d
->y
, p
->y
);
165 mp_drop(d
->z
); d
->z
= 0;
170 ec
*ec_idfix(ec_curve
*c
, ec
*d
, const ec
*p
)
171 { EC_COPY(d
, p
); return (d
); }
173 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
175 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
176 * @ec *d@ = pointer to the destination
177 * @const ec *p@ = pointer to a source point
179 * Returns: The destination @d@.
181 * Use: Conversion functions if your curve operations use a
182 * projective representation.
185 ec
*ec_projin(ec_curve
*c
, ec
*d
, const ec
*p
)
191 d
->x
= F_IN(f
, d
->x
, p
->x
);
192 d
->y
= F_IN(f
, d
->y
, p
->y
);
193 mp_drop(d
->z
); d
->z
= MP_COPY(f
->one
);
198 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
205 if (p
->z
== f
->one
) {
206 d
->x
= F_OUT(f
, d
->x
, p
->x
);
207 d
->y
= F_OUT(f
, d
->y
, p
->y
);
209 z
= F_INV(f
, MP_NEW
, p
->z
);
210 zz
= F_SQR(f
, MP_NEW
, z
);
211 z
= F_MUL(f
, z
, zz
, z
);
212 x
= F_MUL(f
, d
->x
, p
->x
, zz
);
213 y
= F_MUL(f
, d
->y
, p
->y
, z
);
216 d
->x
= F_OUT(f
, x
, x
);
217 d
->y
= F_OUT(f
, y
, y
);
225 ec
*ec_projfix(ec_curve
*c
, ec
*d
, const ec
*p
)
229 else if (p
->z
== c
->f
->one
)
234 z
= F_INV(f
, MP_NEW
, p
->z
);
235 zz
= F_SQR(f
, MP_NEW
, z
);
236 z
= F_MUL(f
, z
, zz
, z
);
237 d
->x
= F_MUL(f
, d
->x
, p
->x
, zz
);
238 d
->y
= F_MUL(f
, d
->y
, p
->y
, z
);
242 d
->z
= MP_COPY(f
->one
);
247 /* --- @ec_stdsub@ --- *
249 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
250 * @ec *d@ = pointer to the destination
251 * @const ec *p, *q@ = the operand points
253 * Returns: The destination @d@.
255 * Use: Standard point subtraction operation, in terms of negation
256 * and addition. This isn't as efficient as a ready-made
257 * subtraction operator.
260 ec
*ec_stdsub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
270 /*----- Creating curves ---------------------------------------------------*/
272 /* --- @ec_destroycurve@ --- *
274 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
278 * Use: Destroys a description of an elliptic curve.
281 void ec_destroycurve(ec_curve
*c
) { c
->ops
->destroy(c
); }
283 /*----- Real arithmetic ---------------------------------------------------*/
285 /* --- @ec_find@ --- *
287 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
288 * @ec *d@ = pointer to the destination point
289 * @mp *x@ = a possible x-coordinate
291 * Returns: Zero if OK, nonzero if there isn't a point there.
293 * Use: Finds a point on an elliptic curve with a given x-coordinate.
296 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
298 x
= F_IN(c
->f
, MP_NEW
, x
);
299 if ((d
= EC_FIND(c
, d
, x
)) != 0)
305 /* --- @ec_neg@ --- *
307 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
308 * @ec *d@ = pointer to the destination point
309 * @const ec *p@ = pointer to the operand point
311 * Returns: The destination point.
313 * Use: Computes the negation of the given point.
316 ec
*ec_neg(ec_curve
*c
, ec
*d
, const ec
*p
)
317 { EC_IN(c
, d
, p
); EC_NEG(c
, d
, d
); return (EC_OUT(c
, d
, d
)); }
319 /* --- @ec_add@ --- *
321 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
322 * @ec *d@ = pointer to the destination point
323 * @const ec *p, *q@ = pointers to the operand points
327 * Use: Adds two points on an elliptic curve.
330 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
332 ec pp
= EC_INIT
, qq
= EC_INIT
;
335 EC_ADD(c
, d
, &pp
, &qq
);
342 /* --- @ec_sub@ --- *
344 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
345 * @ec *d@ = pointer to the destination point
346 * @const ec *p, *q@ = pointers to the operand points
348 * Returns: The destination @d@.
350 * Use: Subtracts one point from another on an elliptic curve.
353 ec
*ec_sub(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
355 ec pp
= EC_INIT
, qq
= EC_INIT
;
358 EC_SUB(c
, d
, &pp
, &qq
);
365 /* --- @ec_dbl@ --- *
367 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
368 * @ec *d@ = pointer to the destination point
369 * @const ec *p@ = pointer to the operand point
373 * Use: Doubles a point on an elliptic curve.
376 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
377 { EC_IN(c
, d
, p
); EC_DBL(c
, d
, d
); return (EC_OUT(c
, d
, d
)); }
379 /* --- @ec_check@ --- *
381 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
382 * @const ec *p@ = pointer to the point
384 * Returns: Zero if OK, nonzero if this is an invalid point.
386 * Use: Checks that a point is actually on an elliptic curve.
389 int ec_check(ec_curve
*c
, const ec
*p
)
397 rc
= EC_CHECK(c
, &t
);
402 /* --- @ec_rand@ --- *
404 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
405 * @ec *d@ = pointer to the destination point
406 * @grand *r@ = random number source
408 * Returns: The destination @d@.
410 * Use: Finds a random point on the given curve.
413 ec
*ec_rand(ec_curve
*c
, ec
*d
, grand
*r
)
416 do x
= F_RAND(c
->f
, x
, r
); while (!EC_FIND(c
, d
, x
));
418 if (grand_range(r
, 2)) EC_NEG(c
, d
, d
);
419 return (EC_OUT(c
, d
, d
));
422 /*----- That's all, folks -------------------------------------------------*/