3 * $Id: ec.c,v 1.2 2001/05/07 17:29:44 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.2 2001/05/07 17:29:44 mdw
34 * Treat projective coordinates as an internal representation. Various
35 * minor interface changes.
37 * Revision 1.1 2001/04/29 18:12:33 mdw
42 /*----- Header files ------------------------------------------------------*/
46 /*----- Trivial wrappers --------------------------------------------------*/
48 /* --- @ec_create@ --- *
50 * Arguments: @ec *p@ = pointer to an elliptic-curve point
54 * Use: Initializes a new point. The initial value is the additive
55 * identity (which is universal for all curves).
58 void ec_create(ec
*p
) { EC_CREATE(p
); }
60 /* --- @ec_destroy@ --- *
62 * Arguments: @ec *p@ = pointer to an elliptic-curve point
66 * Use: Destroys a point, making it invalid.
69 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
71 /* --- @ec_atinf@ --- *
73 * Arguments: @const ec *p@ = pointer to a point
75 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
79 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
81 /* --- @ec_setinf@ --- *
83 * Arguments: @ec *p@ = pointer to a point
87 * Use: Sets the given point to be the point %$O$% at infinity.
90 void ec_setinf(ec
*p
) { EC_SETINF(p
); }
92 /* --- @ec_copy@ --- *
94 * Arguments: @ec *d@ = pointer to destination point
95 * @const ec *p@ = pointer to source point
99 * Use: Creates a copy of an elliptic curve point.
102 void ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); }
104 /*----- Standard curve operations -----------------------------------------*/
106 /* --- @ec_idin@, @ec_idout@ --- *
108 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
109 * @ec *d@ = pointer to the destination
110 * @const ec *p@ = pointer to a source point
112 * Returns: The destination @d@.
114 * Use: An identity operation if your curve has no internal
115 * representation. (The field internal representation is still
119 ec
*ec_idin(ec_curve
*c
, ec
*d
, const ec
*p
)
125 d
->x
= F_IN(f
, d
->x
, p
->x
);
126 d
->y
= F_IN(f
, d
->y
, p
->y
);
127 mp_drop(d
->z
); d
->z
= 0;
132 ec
*ec_idout(ec_curve
*c
, ec
*d
, const ec
*p
)
138 d
->x
= F_OUT(f
, d
->x
, p
->x
);
139 d
->y
= F_OUT(f
, d
->y
, p
->y
);
140 mp_drop(d
->z
); d
->z
= 0;
145 /* --- @ec_projin@, @ec_projout@ --- *
147 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
148 * @ec *d@ = pointer to the destination
149 * @const ec *p@ = pointer to a source point
151 * Returns: The destination @d@.
153 * Use: Conversion functions if your curve operations use a
154 * projective representation.
157 ec
*ec_projin(ec_curve
*c
, ec
*d
, const ec
*p
)
163 d
->x
= F_IN(f
, d
->x
, p
->x
);
164 d
->y
= F_IN(f
, d
->y
, p
->y
);
165 mp_drop(d
->z
); d
->z
= MP_COPY(f
->one
);
170 ec
*ec_projout(ec_curve
*c
, ec
*d
, const ec
*p
)
177 z
= F_INV(f
, MP_NEW
, p
->z
);
178 x
= F_MUL(f
, d
->x
, p
->x
, z
);
179 y
= F_MUL(f
, d
->y
, p
->y
, z
);
182 d
->x
= F_OUT(f
, x
, x
);
183 d
->y
= F_OUT(f
, y
, y
);
189 /*----- Real arithmetic ---------------------------------------------------*/
191 /* --- @ec_find@ --- *
193 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
194 * @ec *d@ = pointer to the destination point
195 * @mp *x@ = a possible x-coordinate
197 * Returns: Zero if OK, nonzero if there isn't a point there.
199 * Use: Finds a point on an elliptic curve with a given x-coordinate.
202 ec
*ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
204 x
= F_IN(c
->f
, MP_NEW
, x
);
205 if ((d
= EC_FIND(c
, d
, x
)) != 0)
211 /* --- @ec_add@ --- *
213 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
214 * @ec *d@ = pointer to the destination point
215 * @const ec *p, *q@ = pointers to the operand points
219 * Use: Adds two points on an elliptic curve.
222 ec
*ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
224 ec pp
= EC_INIT
, qq
= EC_INIT
;
227 EC_ADD(c
, d
, &pp
, &qq
);
234 /* --- @ec_dbl@ --- *
236 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
237 * @ec *d@ = pointer to the destination point
238 * @const ec *p@ = pointer to the operand point
242 * Use: Doubles a point on an elliptic curve.
245 ec
*ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
249 return (EC_OUT(c
, d
, d
));
252 /* --- @ec_mul@ --- *
254 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
255 * @ec *d@ = pointer to the destination point
256 * @const ec *p@ = pointer to the generator point
257 * @mp *n@ = integer multiplier
261 * Use: Multiplies a point by a scalar, returning %$n p$%.
264 ec
*ec_mul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
277 while (!MP_RBIT(&sc
))
281 if ((n
->f
& MP_BURN
) && !(g
.x
->f
& MP_BURN
))
282 MP_DEST(g
.x
, 0, MP_BURN
);
283 if ((n
->f
& MP_BURN
) && !(g
.y
->f
& MP_BURN
))
284 MP_DEST(g
.y
, 0, MP_BURN
);
311 return (EC_OUT(c
, d
, d
));
314 /*----- That's all, folks -------------------------------------------------*/