/* -*-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
*
/*----- 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.
*
#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"
/* -*-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
*
/*----- 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.
*
/*----- Header files ------------------------------------------------------*/
+#include <mLib/sub.h>
+
#include "ec.h"
/*----- Data structures ---------------------------------------------------*/
/*----- 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);
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);
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);
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 -------------------------------------------------*/
/* -*-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
*
/*----- 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.
*
*
* 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@ --- *
*
*
* 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 -----------------------------------------*/
*
* 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@.
*
* 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@ --- *
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);
}
/* -*-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
*
/*----- 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.
*
typedef struct ec_mulfactor {
ec base; /* The point */
- ec *exp; /* The exponent */
+ mp *exp; /* The exponent */
} ec_mulfactor;
/* --- Elliptic curve operations --- */
*
* 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@.
*
* 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 ---------------------------------------------------*/
/* -*-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
*
/*----- 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.
*
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));
}
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);
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));
}
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);
}