X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/b085fd9162fa3ff5b16788ced6d11a71ffe5a5fe..8823192f6413bed15cfa884ed3a3cbbb97885657:/ec.c diff --git a/ec.c b/ec.c index ce4e428..a2b229f 100644 --- a/ec.c +++ b/ec.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec.c,v 1.3 2002/01/13 13:48:44 mdw Exp $ + * $Id: ec.c,v 1.4.4.2 2004/03/20 00:13:31 mdw Exp $ * * Elliptic curve definitions * @@ -30,6 +30,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: ec.c,v $ + * Revision 1.4.4.2 2004/03/20 00:13:31 mdw + * Projective coordinates for prime curves + * + * 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. * @@ -53,13 +62,13 @@ * * Arguments: @ec *p@ = pointer to an elliptic-curve point * - * Returns: --- + * Returns: The argument @p@. * * Use: Initializes a new point. The initial value is the additive * identity (which is universal for all curves). */ -void ec_create(ec *p) { EC_CREATE(p); } +ec *ec_create(ec *p) { EC_CREATE(p); return (p); } /* --- @ec_destroy@ --- * * @@ -86,28 +95,28 @@ int ec_atinf(const ec *p) { return (EC_ATINF(p)); } * * Arguments: @ec *p@ = pointer to a point * - * Returns: --- + * Returns: The argument @p@. * * Use: Sets the given point to be the point %$O$% at infinity. */ -void ec_setinf(ec *p) { EC_SETINF(p); } +ec *ec_setinf(ec *p) { EC_SETINF(p); return (p); } /* --- @ec_copy@ --- * * * Arguments: @ec *d@ = pointer to destination point * @const ec *p@ = pointer to source point * - * Returns: --- + * Returns: The destination @d@. * * Use: Creates a copy of an elliptic curve point. */ -void ec_copy(ec *d, const ec *p) { EC_COPY(d, p); } +ec *ec_copy(ec *d, const ec *p) { EC_COPY(d, p); return (d); } /*----- Standard curve operations -----------------------------------------*/ -/* --- @ec_idin@, @ec_idout@ --- * +/* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve * @ec *d@ = pointer to the destination @@ -146,6 +155,12 @@ ec *ec_idout(ec_curve *c, ec *d, const ec *p) return (d); } +ec *ec_idfix(ec_curve *c, ec *d, const ec *p) +{ + EC_COPY(d, p); + return (d); +} + /* --- @ec_projin@, @ec_projout@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve @@ -176,12 +191,15 @@ ec *ec_projout(ec_curve *c, ec *d, const ec *p) if (EC_ATINF(p)) EC_SETINF(d); else { - mp *x, *y, *z; + mp *x, *y, *z, *zz; field *f = c->f; z = F_INV(f, MP_NEW, p->z); - x = F_MUL(f, d->x, p->x, 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); mp_drop(d->z); d->x = F_OUT(f, x, x); d->y = F_OUT(f, y, y); @@ -190,11 +208,33 @@ ec *ec_projout(ec_curve *c, ec *d, const ec *p) return (d); } +ec *ec_projfix(ec_curve *c, ec *d, const ec *p) +{ + if (EC_ATINF(p)) + EC_SETINF(d); + else if (d->z == c->f->one) + EC_COPY(d, p); + else { + mp *z, *zz; + field *f = c->f; + z = F_INV(f, MP_NEW, p->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->z = MP_COPY(f->one); + } + return (d); +} + /* --- @ec_stdsub@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve * @ec *d@ = pointer to the destination - * @const ec *a, *b@ = the operand points + * @const ec *p, *q@ = the operand points * * Returns: The destination @d@. * @@ -203,15 +243,29 @@ ec *ec_projout(ec_curve *c, ec *d, const ec *p) * subtraction operator. */ -ec *ec_stdsub(ec_curve *c, ec *d, const ec *a, const ec *b) +ec *ec_stdsub(ec_curve *c, ec *d, const ec *p, const ec *q) { ec t = EC_INIT; - EC_NEG(c, &t, b); - EC_SUB(c, d, a, &t); + EC_NEG(c, &t, q); + EC_FIX(c, &t, &t); + EC_ADD(c, d, p, &t); EC_DESTROY(&t); return (d); } +/*----- Creating curves ---------------------------------------------------*/ + +/* --- @ec_destroycurve@ --- * + * + * Arguments: @ec_curve *c@ = pointer to an ellptic curve + * + * Returns: --- + * + * Use: Destroys a description of an elliptic curve. + */ + +void ec_destroycurve(ec_curve *c) { c->ops->destroy(c); } + /*----- Real arithmetic ---------------------------------------------------*/ /* --- @ec_find@ --- * @@ -230,10 +284,28 @@ ec *ec_find(ec_curve *c, ec *d, mp *x) 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_neg@ --- * + * + * Arguments: @ec_curve *c@ = pointer to an elliptic curve + * @ec *d@ = pointer to the destination point + * @const ec *p@ = pointer to the operand point + * + * Returns: The destination point. + * + * Use: Computes the negation of the given point. + */ + +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_add@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve @@ -257,6 +329,29 @@ ec *ec_add(ec_curve *c, ec *d, const ec *p, const ec *q) return (d); } +/* --- @ec_sub@ --- * + * + * Arguments: @ec_curve *c@ = pointer to an elliptic curve + * @ec *d@ = pointer to the destination point + * @const ec *p, *q@ = pointers to the operand points + * + * Returns: The destination @d@. + * + * Use: Subtracts one point from another on an elliptic curve. + */ + +ec *ec_sub(ec_curve *c, ec *d, const ec *p, const ec *q) +{ + ec pp, qq; + EC_IN(c, &pp, p); + EC_IN(c, &qq, q); + EC_SUB(c, d, &qq, &qq); + EC_OUT(c, d, d); + EC_DESTROY(&pp); + EC_DESTROY(&qq); + return (d); +} + /* --- @ec_dbl@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve @@ -275,6 +370,29 @@ ec *ec_dbl(ec_curve *c, ec *d, const ec *p) return (EC_OUT(c, d, d)); } +/* --- @ec_check@ --- * + * + * Arguments: @ec_curve *c@ = pointer to an elliptic curve + * @const ec *p@ = pointer to the point + * + * Returns: Zero if OK, nonzero if this is an invalid point. + * + * Use: Checks that a point is actually on an elliptic curve. + */ + +int ec_check(ec_curve *c, const ec *p) +{ + ec t = EC_INIT; + int rc; + + if (EC_ATINF(p)) + return (0); + EC_IN(c, &t, p); + rc = EC_CHECK(c, &t); + EC_DESTROY(&t); + return (rc); +} + /* --- @ec_imul@, @ec_mul@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve @@ -300,10 +418,15 @@ ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *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); + else { + if (n->f & MP_NEG) + EC_NEG(c, &t, &t); + if (MP_LEN(n) < EXP_THRESH) + EXP_SIMPLE(*d, t, n); + else + EXP_WINDOW(*d, t, n); + } + EC_DESTROY(&t); return (d); }