X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/8823192f6413bed15cfa884ed3a3cbbb97885657..8ef2f81dfbf105ae89082a340ac56f46368387f0:/ec.c diff --git a/ec.c b/ec.c index a2b229f..d4dfafb 100644 --- a/ec.c +++ b/ec.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec.c,v 1.4.4.2 2004/03/20 00:13:31 mdw Exp $ + * $Id: ec.c,v 1.7 2004/03/27 17:54:11 mdw Exp $ * * Elliptic curve definitions * @@ -30,6 +30,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: ec.c,v $ + * Revision 1.7 2004/03/27 17:54:11 mdw + * Standard curves and curve checking. + * + * Revision 1.6 2004/03/23 15:19:32 mdw + * Test elliptic curves more thoroughly. + * + * Revision 1.5 2004/03/21 22:52:06 mdw + * Merge and close elliptic curve branch. + * * Revision 1.4.4.2 2004/03/20 00:13:31 mdw * Projective coordinates for prime curves * @@ -114,6 +123,16 @@ ec *ec_setinf(ec *p) { EC_SETINF(p); return (p); } 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_idfix@ --- * @@ -342,10 +361,10 @@ ec *ec_add(ec_curve *c, ec *d, const ec *p, const ec *q) 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); @@ -393,6 +412,26 @@ int ec_check(ec_curve *c, const ec *p) return (rc); } +/* --- @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) +{ + 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)); +} + /* --- @ec_imul@, @ec_mul@ --- * * * Arguments: @ec_curve *c@ = pointer to an elliptic curve