Test elliptic curves more thoroughly.
[u/mdw/catacomb] / ec.c
diff --git a/ec.c b/ec.c
index c95333f..ac00c92 100644 (file)
--- a/ec.c
+++ b/ec.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: ec.c,v 1.5 2004/03/21 22:52:06 mdw Exp $
+ * $Id: ec.c,v 1.6 2004/03/23 15:19:32 mdw Exp $
  *
  * Elliptic curve definitions
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: ec.c,v $
+ * 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.
  *
@@ -117,6 +120,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@ --- *
@@ -348,7 +361,7 @@ 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_SUB(c, d, &pp, &qq);
   EC_OUT(c, d, d);
   EC_DESTROY(&pp);
   EC_DESTROY(&qq);
@@ -396,6 +409,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