3 * $Id: ec.c,v 1.1 2001/04/29 18:12:33 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.1 2001/04/29 18:12:33 mdw
38 /*----- Header files ------------------------------------------------------*/
42 /*----- Trivial wrappers --------------------------------------------------*/
44 /* --- @ec_create@ --- *
46 * Arguments: @ec *p@ = pointer to an elliptic-curve point
50 * Use: Initializes a new point. The initial value is the additive
51 * identity (which is universal for all curves).
54 void ec_create(ec
*p
) { EC_CREATE(p
); }
56 /* --- @ec_destroy@ --- *
58 * Arguments: @ec *p@ = pointer to an elliptic-curve point
62 * Use: Destroys a point, making it invalid.
65 void ec_destroy(ec
*p
) { EC_DESTROY(p
); }
67 /* --- @ec_atinf@ --- *
69 * Arguments: @const ec *p@ = pointer to a point
71 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
75 int ec_atinf(const ec
*p
) { return (EC_ATINF(p
)); }
77 /* --- @ec_setinf@ --- *
79 * Arguments: @ec *p@ = pointer to a point
83 * Use: Sets the given point to be the point %$O$% at infinity.
86 void ec_setinf(ec
*p
) { EC_SETINF(p
); }
88 /* --- @ec_copy@ --- *
90 * Arguments: @ec *d@ = pointer to destination point
91 * @const ec *p@ = pointer to source point
95 * Use: Creates a copy of an elliptic curve point.
98 void ec_copy(ec
*d
, const ec
*p
) { EC_COPY(d
, p
); }
100 /*----- Real arithmetic ---------------------------------------------------*/
102 /* --- @ec_denorm@ --- *
104 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
105 * @ec *d@ = pointer to the destination point
106 * @const ec *p@ = pointer to the source point
110 * Use: Denormalizes the given point, converting to internal
111 * representations and setting the denominator to 1.
114 void ec_denorm(ec_curve
*c
, ec
*d
, const ec
*p
)
120 d
->x
= F_IN(f
, d
->x
, p
->x
);
121 d
->y
= F_IN(f
, d
->y
, p
->y
);
123 d
->z
= MP_COPY(f
->one
);
127 /* --- @ec_norm@ --- *
129 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
130 * @ec *d@ = pointer to the destination point
131 * @const ec *p@ = pointer to the source point
135 * Use: Normalizes the given point, by dividing through by the
136 * denominator and returning to external representation.
139 void ec_norm(ec_curve
*c
, ec
*d
, const ec
*p
)
146 z
= F_INV(f
, MP_NEW
, p
->z
);
147 x
= F_MUL(f
, d
->x
, p
->x
, z
);
148 y
= F_MUL(f
, d
->y
, p
->y
, z
);
151 d
->x
= F_OUT(f
, x
, x
);
152 d
->y
= F_OUT(f
, y
, y
);
157 /* --- @ec_find@ --- *
159 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
160 * @ec *d@ = pointer to the destination point
161 * @mp *x@ = a possible x-coordinate
163 * Returns: Zero if OK, nonzero if there isn't a point there.
165 * Use: Finds a point on an elliptic curve with a given x-coordinate.
168 void ec_find(ec_curve
*c
, ec
*d
, mp
*x
)
171 x
= F_IN(c
->f
, MP_NEW
, x
);
172 if ((rc
= EC_FIND(c
, d
, x
)) == 0)
178 /* --- @ec_add@ --- *
180 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
181 * @ec *d@ = pointer to the destination point
182 * @const ec *p, *q@ = pointers to the operand points
186 * Use: Adds two points on an elliptic curve.
189 void ec_add(ec_curve
*c
, ec
*d
, const ec
*p
, const ec
*q
)
191 ec pp
= EC_INIT
, qq
= EC_INIT
;
192 ec_denorm(c
, &pp
, p
);
193 ec_denorm(c
, &qq
, q
);
194 EC_ADD(c
, d
, &pp
, &qq
);
200 /* --- @ec_dbl@ --- *
202 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
203 * @ec *d@ = pointer to the destination point
204 * @const ec *p@ = pointer to the operand point
208 * Use: Doubles a point on an elliptic curve.
211 void ec_dbl(ec_curve
*c
, ec
*d
, const ec
*p
)
218 /* --- @ec_mul@ --- *
220 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
221 * @ec *d@ = pointer to the destination point
222 * @const ec *p@ = pointer to the generator point
223 * @mp *n@ = integer multiplier
227 * Use: Multiplies a point by a scalar, returning %$n p$%.
230 void ec_mul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
243 while (!MP_RBIT(&sc
))
247 if ((n
->f
& MP_BURN
) && !(g
.x
->f
& MP_BURN
))
248 MP_DEST(g
.x
, 0, MP_BURN
);
249 if ((n
->f
& MP_BURN
) && !(g
.y
->f
& MP_BURN
))
250 MP_DEST(g
.y
, 0, MP_BURN
);
280 /*----- That's all, folks -------------------------------------------------*/