From: mdw Date: Thu, 15 May 2003 23:25:59 +0000 (+0000) Subject: Make elliptic curve stuff build. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/41cb1beba35c8a69ee7ae1298f51711995011b5c Make elliptic curve stuff build. --- diff --git a/ec-exp.h b/ec-exp.h index c760c43..9ba7bc5 100644 --- a/ec-exp.h +++ b/ec-exp.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec-exp.h,v 1.1 2002/01/13 13:48:44 mdw Exp $ + * $Id: ec-exp.h,v 1.2 2003/05/15 23:25:59 mdw Exp $ * * Exponentiation operations for elliptic curves * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: ec-exp.h,v $ + * Revision 1.2 2003/05/15 23:25:59 mdw + * Make elliptic curve stuff build. + * * Revision 1.1 2002/01/13 13:48:44 mdw * Further progress. * @@ -46,23 +49,23 @@ #define EXP_TYPE ec -#define EXP_COPY(d, x) do { \ - d.x = MP_COPY(x.x); \ - d.y = MP_COPY(x.y); \ - d.z = x.z ? MP_COPY(x.x) : MP_NEW; \ +#define EXP_COPY(d, p) do { \ + (d).x = MP_COPY((p).x); \ + (d).y = MP_COPY((p).y); \ + (d).z = (p).z ? MP_COPY((p).z) : MP_NEW; \ } while (0) -#define EXP_DROP(x) EC_DESTROY(c, &x) +#define EXP_DROP(x) EC_DESTROY(&(x)) -#define EXP_MUL(a, x) EC_ADD(c, &a, &a, &x) -#define EXP_SQR(a) EC_DBL(c, &a, &a); +#define EXP_MUL(a, x) EC_ADD(c, &(a), &(a), &(x)) +#define EXP_SQR(a) EC_DBL(c, &(a), &(a)); #define EXP_SETMUL(d, x, y) do { \ - EC_CREATE(&d); \ - EC_ADD(c, &d, &x, &y); \ + EC_CREATE(&(d)); \ + EC_ADD(c, &(d), &(x), &(y)); \ } while (0) #define EXP_SETSQR(d, x) do { \ - EC_CREATE(&d); \ - EC_DBL(c, &d, &x); \ + EC_CREATE(&(d)); \ + EC_DBL(c, &(d), &(x)); \ } while (0) #include "exp.h" diff --git a/ec-prime.c b/ec-prime.c index 09733ce..4611855 100644 --- a/ec-prime.c +++ b/ec-prime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec-prime.c,v 1.2 2002/01/13 13:48:44 mdw Exp $ + * $Id: ec-prime.c,v 1.3 2003/05/15 23:25:59 mdw Exp $ * * Elliptic curves over prime fields * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: ec-prime.c,v $ + * Revision 1.3 2003/05/15 23:25:59 mdw + * Make elliptic curve stuff build. + * * Revision 1.2 2002/01/13 13:48:44 mdw * Further progress. * @@ -40,6 +43,8 @@ /*----- Header files ------------------------------------------------------*/ +#include + #include "ec.h" /*----- Data structures ---------------------------------------------------*/ @@ -51,7 +56,9 @@ typedef struct ecctx { /*----- Main code ---------------------------------------------------------*/ -static ec *ecneg(ec_cuvrve *c, ec *d, const ec *p) +static const ec_ops ec_primeops; + +static ec *ecneg(ec_curve *c, ec *d, const ec *p) { EC_COPY(d, p); d->y = F_NEG(c->f, d->y, d->y); @@ -75,10 +82,10 @@ static ec *ecdbl(ec_curve *c, ec *d, const ec *a) dx = F_TPL(f, dx, dx); dx = F_ADD(f, dx, dx, cc->a); dy = F_INV(f, dy, dy); - lambda = F_MUL(d, MP_NEW, dx, dy); + lambda = F_MUL(f, MP_NEW, dx, dy); dx = F_SQR(f, dx, lambda); - dy = F_DBL(d, dy, a->x); + dy = F_DBL(f, dy, a->x); dx = F_SUB(f, dx, dx, dy); dy = F_SUB(f, dy, a->x, dx); dy = F_MUL(f, dy, lambda, dy); @@ -121,7 +128,7 @@ static ec *ecadd(ec_curve *c, ec *d, const ec *a, const ec *b) dx = F_ADD(f, dx, dx, cc->a); dy = F_DBL(f, MP_NEW, a->y); dy = F_INV(f, dy, dy); - lambda = F_MUL(d, MP_NEW, dx, dy); + lambda = F_MUL(f, MP_NEW, dx, dy); } dx = F_SQR(f, dx, lambda); @@ -140,4 +147,76 @@ static ec *ecadd(ec_curve *c, ec *d, const ec *a, const ec *b) return (d); } +static void ecdestroy(ec_curve *c) +{ + ecctx *cc = (ecctx *)c; + MP_DROP(cc->a); + MP_DROP(cc->b); + DESTROY(cc); +} + +/* --- @ec_prime@, @ec_primeproj@ --- * + * + * Arguments: @field *f@ = the underyling field for this elliptic curve + * @mp *a, *b@ = the coefficients for this curve + * + * Returns: A pointer to the curve. + * + * Use: Creates a curve structure for an elliptic curve defined over + * a prime field. The @primeproj@ variant uses projective + * coordinates, which can be a win. + */ + +extern ec_curve *ec_prime(field *f, mp *a, mp *b) +{ + ecctx *cc = CREATE(ecctx); + cc->c.ops = &ec_primeops; + cc->c.f = f; + cc->a = MP_COPY(a); + cc->b = MP_COPY(b); + return (&cc->c); +} + +static const ec_ops ec_primeops = { + ecdestroy, ec_idin, ec_idout, 0, ecneg, ecadd, ec_stdsub, ecdbl +}; + +/*----- Test rig ----------------------------------------------------------*/ + +#ifdef TEST_RIG + +#define MP(x) mp_readstring(MP_NEW, #x, 0, 0) + +int main(void) +{ + field *f; + ec_curve *c; + ec g = EC_INIT, d = EC_INIT; + mp *p, *a, *b, *r; + + a = MP(-3); + b = MP(0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1); + p = MP(6277101735386680763835789423207666416083908700390324961279); + r = MP(6277101735386680763835789423176059013767194773182842284081); + + f = field_prime(p); + c = ec_prime(f, a, b); + + g.x = MP(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012); + g.y = MP(0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811); + + ec_mul(c, &d, &g, r); + MP_PRINT("d.x", d.x); + MP_PRINT("d.y", d.y); + + ec_destroy(&d); + ec_destroy(&g); + ec_destroycurve(c); + F_DESTROY(f); + + return (0); +} + +#endif + /*----- That's all, folks -------------------------------------------------*/ diff --git a/ec.c b/ec.c index ce4e428..47c01a9 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 2003/05/15 23:25:59 mdw Exp $ * * Elliptic curve definitions * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: ec.c,v $ + * 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 +56,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,24 +89,24 @@ 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 -----------------------------------------*/ @@ -194,7 +197,7 @@ ec *ec_projout(ec_curve *c, ec *d, const ec *p) * * 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 +206,28 @@ 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_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@ --- * @@ -301,9 +317,9 @@ ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n) if (MP_LEN(n) == 0) ; else if (MP_LEN(n) < EXP_THRESH) - EXP_SIMPLE(&d, t, n); + EXP_SIMPLE(*d, t, n); else - EXP_WINDOW(&d, t, n); + EXP_WINDOW(*d, t, n); return (d); } diff --git a/ec.h b/ec.h index 2efe939..105838c 100644 --- a/ec.h +++ b/ec.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: ec.h,v 1.3 2002/01/13 13:48:44 mdw Exp $ + * $Id: ec.h,v 1.4 2003/05/15 23:25:59 mdw Exp $ * * Elliptic curve definitions * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: ec.h,v $ + * 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. * @@ -74,7 +77,7 @@ typedef struct ec { typedef struct ec_mulfactor { ec base; /* The point */ - ec *exp; /* The exponent */ + mp *exp; /* The exponent */ } ec_mulfactor; /* --- Elliptic curve operations --- */ @@ -372,7 +375,7 @@ extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/); * * 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@. * @@ -381,7 +384,8 @@ extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/); * subtraction operator. */ -extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/, const ec */*p*/); +extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/, + const ec */*p*/, const ec */*q*/); /*----- Creating curves ---------------------------------------------------*/ diff --git a/f-prime.c b/f-prime.c index a9673b8..f4bf3a7 100644 --- a/f-prime.c +++ b/f-prime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: f-prime.c,v 1.2 2002/01/13 13:48:44 mdw Exp $ + * $Id: f-prime.c,v 1.3 2003/05/15 23:25:59 mdw Exp $ * * Prime fields with Montgomery arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: f-prime.c,v $ + * Revision 1.3 2003/05/15 23:25:59 mdw + * Make elliptic curve stuff build. + * * Revision 1.2 2002/01/13 13:48:44 mdw * Further progress. * @@ -107,7 +110,7 @@ static mp *fsqr(field *ff, mp *d, mp *x) static mp *finv(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; - d = mpmont_reduce(&f->mm, x); + d = mpmont_reduce(&f->mm, d, x); mp_gcd(0, 0, &d, f->mm.m, d); return (mpmont_mul(&f->mm, d, d, f->mm.r2)); } @@ -121,13 +124,13 @@ static mp *freduce(field *ff, mp *d, mp *x) static mp *fdbl(field *ff, mp *d, mp *x) { - fctx *f = (fctx *)ff; +/* fctx *f = (fctx *)ff; */ return (mp_lsl(d, x, 1)); } static mp *ftpl(field *ff, mp *d, mp *x) { - fctx *f = (fctx *)ff; +/* fctx *f = (fctx *)ff; */ MP_DEST(d, MP_LEN(x) + 1, x->f); MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); return (d); @@ -136,7 +139,7 @@ static mp *ftpl(field *ff, mp *d, mp *x) static mp *fsqrt(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; - d = mpmont_reduce(&f->mm, x); + d = mpmont_reduce(&f->mm, d, x); d = mp_modsqrt(d, d, f->mm.m); return (mpmont_mul(&f->mm, d, d, f->mm.r2)); } @@ -162,11 +165,11 @@ static field_ops fops = { field *field_prime(mp *p) { - ftcx *f = CREATE(fctx); + fctx *f = CREATE(fctx); f->f.ops = &fops; mpmont_create(&f->mm, p); - f->zero = MP_ZERO; - f->one = &f->mm.r; + f->f.zero = MP_ZERO; + f->f.one = f->mm.r; return (&f->f); }