/* -*-c-*-
*
- * $Id: ec.c,v 1.4.4.1 2003/06/10 13:43:53 mdw Exp $
+ * $Id$
*
* Elliptic curve definitions
*
* (c) 2001 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of Catacomb.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* Catacomb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with Catacomb; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: ec.c,v $
- * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
- * Simple (non-projective) curves over prime fields now seem to work.
- *
- * Revision 1.4 2003/05/15 23:25:59 mdw
- * Make elliptic curve stuff build.
- *
- * Revision 1.3 2002/01/13 13:48:44 mdw
- * Further progress.
- *
- * Revision 1.2 2001/05/07 17:29:44 mdw
- * Treat projective coordinates as an internal representation. Various
- * minor interface changes.
- *
- * Revision 1.1 2001/04/29 18:12:33 mdw
- * Prototype version.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include "ec.h"
-#include "ec-exp.h"
/*----- Trivial wrappers --------------------------------------------------*/
+/* --- @ec_samep@ --- *
+ *
+ * Arguments: @ec_curve *c, *d@ = two elliptic curves
+ *
+ * Returns: Nonzero if the curves are identical (not just isomorphic).
+ *
+ * Use: Checks for sameness of curves. This function does the full
+ * check, not just the curve-type-specific check done by the
+ * @sampep@ field operation.
+ */
+
+int ec_samep(ec_curve *c, ec_curve *d)
+{
+ return (c == d || (field_samep(c->f, d->f) &&
+ c->ops == d->ops && EC_SAMEP(c, d)));
+}
+
/* --- @ec_create@ --- *
*
* Arguments: @ec *p@ = pointer to an elliptic-curve point
ec *ec_copy(ec *d, const ec *p) { EC_COPY(d, p); return (d); }
+/* --- @ec_eq@ --- *
+ *
+ * Arguments: @const ec *p, *q@ = two points
+ *
+ * Returns: Nonzero if the points are equal. Compares external-format
+ * points.
+ */
+
+int ec_eq(const ec *p, const ec *q) { return (EC_EQ(p, q)); }
+
/*----- Standard curve operations -----------------------------------------*/
-/* --- @ec_idin@, @ec_idout@ --- *
+/* --- @ec_stdsamep@ --- *
+ *
+ * Arguments: @ec_curve *c, *d@ = two elliptic curves
+ *
+ * Returns: Nonzero if the curves are identical (not just isomorphic).
+ *
+ * Use: Simple sameness check on @a@ and @b@ curve members.
+ */
+
+int ec_stdsamep(ec_curve *c, ec_curve *d)
+ { return (MP_EQ(c->a, d->a) && MP_EQ(c->b, d->b)); }
+
+/* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
*
* Arguments: @ec_curve *c@ = pointer to an elliptic curve
* @ec *d@ = pointer to the destination
return (d);
}
-/* --- @ec_projin@, @ec_projout@ --- *
+ec *ec_idfix(ec_curve *c, ec *d, const ec *p)
+ { EC_COPY(d, p); return (d); }
+
+/* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
*
* Arguments: @ec_curve *c@ = pointer to an elliptic curve
* @ec *d@ = pointer to the destination
if (EC_ATINF(p))
EC_SETINF(d);
else {
- mp *x, *y, *z;
+ mp *x, *y, *z, *zz;
+ field *f = c->f;
+ if (p->z == f->one) {
+ d->x = F_OUT(f, d->x, p->x);
+ d->y = F_OUT(f, d->y, p->y);
+ } else {
+ z = F_INV(f, MP_NEW, p->z);
+ zz = F_SQR(f, MP_NEW, z);
+ z = F_MUL(f, z, zz, z);
+ x = F_MUL(f, d->x, p->x, zz);
+ y = F_MUL(f, d->y, p->y, z);
+ mp_drop(z);
+ mp_drop(zz);
+ d->x = F_OUT(f, x, x);
+ d->y = F_OUT(f, y, y);
+ }
+ mp_drop(d->z);
+ d->z = 0;
+ }
+ return (d);
+}
+
+ec *ec_projfix(ec_curve *c, ec *d, const ec *p)
+{
+ if (EC_ATINF(p))
+ EC_SETINF(d);
+ else if (p->z == c->f->one)
+ EC_COPY(d, p);
+ else {
+ mp *z, *zz;
field *f = c->f;
z = F_INV(f, MP_NEW, p->z);
- x = F_MUL(f, d->x, p->x, z);
- y = F_MUL(f, d->y, p->y, z);
+ zz = F_SQR(f, MP_NEW, z);
+ z = F_MUL(f, z, zz, z);
+ d->x = F_MUL(f, d->x, p->x, zz);
+ d->y = F_MUL(f, d->y, p->y, z);
mp_drop(z);
+ mp_drop(zz);
mp_drop(d->z);
- d->x = F_OUT(f, x, x);
- d->y = F_OUT(f, y, y);
- d->z = 0;
+ d->z = MP_COPY(f->one);
}
return (d);
}
{
ec t = EC_INIT;
EC_NEG(c, &t, q);
+ EC_FIX(c, &t, &t);
EC_ADD(c, d, p, &t);
EC_DESTROY(&t);
return (d);
x = F_IN(c->f, MP_NEW, x);
if ((d = EC_FIND(c, d, x)) != 0)
EC_OUT(c, d, d);
- mp_drop(x);
+ MP_DROP(x);
return (d);
}
*/
ec *ec_neg(ec_curve *c, ec *d, const ec *p)
-{
- EC_IN(c, d, p);
- EC_NEG(c, d, d);
- return (EC_OUT(c, d, d));
-}
+ { EC_IN(c, d, p); EC_NEG(c, d, d); return (EC_OUT(c, d, d)); }
/* --- @ec_add@ --- *
*
ec *ec_sub(ec_curve *c, ec *d, const ec *p, const ec *q)
{
- ec pp, qq;
+ ec pp = EC_INIT, qq = EC_INIT;
EC_IN(c, &pp, p);
EC_IN(c, &qq, q);
- EC_SUB(c, d, &qq, &qq);
+ EC_SUB(c, d, &pp, &qq);
EC_OUT(c, d, d);
EC_DESTROY(&pp);
EC_DESTROY(&qq);
*/
ec *ec_dbl(ec_curve *c, ec *d, const ec *p)
-{
- EC_IN(c, d, p);
- EC_DBL(c, d, d);
- return (EC_OUT(c, d, d));
-}
+ { EC_IN(c, d, p); EC_DBL(c, d, d); return (EC_OUT(c, d, d)); }
-/* --- @ec_imul@, @ec_mul@ --- *
+/* --- @ec_check@ --- *
*
* Arguments: @ec_curve *c@ = pointer to an elliptic curve
- * @ec *d@ = pointer to the destination point
- * @const ec *p@ = pointer to the generator point
- * @mp *n@ = integer multiplier
+ * @const ec *p@ = pointer to the point
*
- * Returns: The destination @d@.
+ * Returns: Zero if OK, nonzero if this is an invalid point.
*
- * Use: Multiplies a point by a scalar, returning %$n p$%. The
- * @imul@ variant uses internal representations for argument
- * and result.
+ * Use: Checks that a point is actually on an elliptic curve.
*/
-ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n)
+int ec_check(ec_curve *c, const ec *p)
{
ec t = EC_INIT;
+ int rc;
- EC_COPY(&t, p);
- if (t.x && (n->f & MP_BURN))
- t.x->f |= MP_BURN;
- MP_SHRINK(n);
- EC_SETINF(d);
- if (MP_LEN(n) == 0)
- ;
- else if (MP_LEN(n) < EXP_THRESH)
- EXP_SIMPLE(*d, t, n);
- else
- EXP_WINDOW(*d, t, n);
+ if (EC_ATINF(p))
+ return (0);
+ EC_IN(c, &t, p);
+ rc = EC_CHECK(c, &t);
EC_DESTROY(&t);
- return (d);
+ return (rc);
}
-ec *ec_mul(ec_curve *c, ec *d, const ec *p, mp *n)
+/* --- @ec_rand@ --- *
+ *
+ * Arguments: @ec_curve *c@ = pointer to an elliptic curve
+ * @ec *d@ = pointer to the destination point
+ * @grand *r@ = random number source
+ *
+ * Returns: The destination @d@.
+ *
+ * Use: Finds a random point on the given curve.
+ */
+
+ec *ec_rand(ec_curve *c, ec *d, grand *r)
{
- EC_IN(c, d, p);
- ec_imul(c, d, d, n);
+ mp *x = MP_NEW;
+ do x = F_RAND(c->f, x, r); while (!EC_FIND(c, d, x));
+ mp_drop(x);
+ if (grand_range(r, 2)) EC_NEG(c, d, d);
return (EC_OUT(c, d, d));
}