Simple (non-projective) curves over prime fields now seem to work.
[u/mdw/catacomb] / ec.c
diff --git a/ec.c b/ec.c
index 37d8ad3..4111928 100644 (file)
--- a/ec.c
+++ b/ec.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: ec.c,v 1.2 2001/05/07 17:29:44 mdw Exp $
+ * $Id: ec.c,v 1.4.4.1 2003/06/10 13:43:53 mdw Exp $
  *
  * Elliptic curve definitions
  *
 /*----- 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.
@@ -42,6 +51,7 @@
 /*----- Header files ------------------------------------------------------*/
 
 #include "ec.h"
+#include "ec-exp.h"
 
 /*----- Trivial wrappers --------------------------------------------------*/
 
  *
  * 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@ --- *
  *
@@ -82,24 +92,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 -----------------------------------------*/
 
@@ -186,6 +196,41 @@ ec *ec_projout(ec_curve *c, ec *d, const ec *p)
   return (d);
 }
 
+/* --- @ec_stdsub@ --- *
+ *
+ * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
+ *             @ec *d@ = pointer to the destination
+ *             @const ec *p, *q@ = the operand points
+ *
+ * Returns:    The destination @d@.
+ *
+ * Use:                Standard point subtraction operation, in terms of negation
+ *             and addition.  This isn't as efficient as a ready-made
+ *             subtraction operator.
+ */
+
+ec *ec_stdsub(ec_curve *c, ec *d, const ec *p, const ec *q)
+{
+  ec t = EC_INIT;
+  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@ --- *
@@ -208,6 +253,24 @@ ec *ec_find(ec_curve *c, ec *d, mp *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
@@ -231,6 +294,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
@@ -249,65 +335,43 @@ ec *ec_dbl(ec_curve *c, ec *d, const ec *p)
   return (EC_OUT(c, d, d));
 }
 
-/* --- @ec_mul@ --- *
+/* --- @ec_imul@, @ec_mul@ --- *
  *
  * 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
  *
- * Returns:    ---
+ * Returns:    The destination @d@.
  *
- * Use:                Multiplies a point by a scalar, returning %$n p$%.
+ * Use:                Multiplies a point by a scalar, returning %$n p$%.  The
+ *             @imul@ variant uses internal representations for argument
+ *             and result.
  */
 
-ec *ec_mul(ec_curve *c, ec *d, const ec *p, mp *n)
+ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n)
 {
-  mpscan sc;
-  ec g = EC_INIT;
-  unsigned sq = 0;
+  ec t = EC_INIT;
 
+  EC_COPY(&t, p);
+  if (t.x && (n->f & MP_BURN))
+    t.x->f |= MP_BURN;
+  MP_SHRINK(n);
   EC_SETINF(d);
-  if (EC_ATINF(p))
-    return;
-
-  mp_rscan(&sc, n);
-  if (!MP_RSTEP(&sc))
-    goto exit;
-  while (!MP_RBIT(&sc))
-    MP_RSTEP(&sc);
-
-  EC_IN(c, &g, p);
-  if ((n->f & MP_BURN) && !(g.x->f & MP_BURN))
-    MP_DEST(g.x, 0, MP_BURN);
-  if ((n->f & MP_BURN) && !(g.y->f & MP_BURN))
-    MP_DEST(g.y, 0, MP_BURN);
-
-  for (;;) {
-    EC_ADD(c, d, d, &g);
-    sq = 0;
-    for (;;) {
-      if (!MP_RSTEP(&sc))
-       goto done;
-      if (MP_RBIT(&sc))
-       break;
-      sq++;
-    }
-    sq++;
-    while (sq) {
-      EC_DBL(c, d, d);
-      sq--;
-    }
-  }
-
-done:
-  while (sq) {
-    EC_DBL(c, d, d);
-    sq--;
-  }
+  if (MP_LEN(n) == 0)
+    ;
+  else if (MP_LEN(n) < EXP_THRESH)
+    EXP_SIMPLE(*d, t, n);
+  else
+    EXP_WINDOW(*d, t, n);
+  EC_DESTROY(&t);
+  return (d);
+}
 
-  EC_DESTROY(&g);
-exit:
+ec *ec_mul(ec_curve *c, ec *d, const ec *p, mp *n)
+{
+  EC_IN(c, d, p);
+  ec_imul(c, d, d, n);
   return (EC_OUT(c, d, d));
 }