Normal basis support (translates to poly basis internally). Rewrite
[u/mdw/catacomb] / ec.c
diff --git a/ec.c b/ec.c
index c95333f..9a929ca 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.9 2004/04/01 21:28:41 mdw Exp $
  *
  * Elliptic curve definitions
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: ec.c,v $
+ * Revision 1.9  2004/04/01 21:28:41  mdw
+ * Normal basis support (translates to poly basis internally).  Rewrite
+ * EC and prime group table generators in awk, so that they can reuse data
+ * for repeated constants.
+ *
+ * Revision 1.8  2004/04/01 12:50:09  mdw
+ * Add cyclic group abstraction, with test code.  Separate off exponentation
+ * functions for better static linking.  Fix a buttload of bugs on the way.
+ * Generally ensure that negative exponents do inversion correctly.  Add
+ * table of standard prime-field subgroups.  (Binary field subgroups are
+ * currently unimplemented but easy to add if anyone ever finds a good one.)
+ *
+ * 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.
  *
 /*----- Header files ------------------------------------------------------*/
 
 #include "ec.h"
-#include "ec-exp.h"
 
 /*----- Trivial wrappers --------------------------------------------------*/
 
+/* --- @ec_samep@ --- *
+ *
+ * Arguments:  @ec_curve *c, *d@ = two elliptic curves
+ *
+ * Returns:    Nonzero if the curves are identical (not just isomorphic).
+ *
+ * Use:                Checks for sameness of curves.  This function does the full
+ *             check, not just the curve-type-specific check done by the
+ *             @sampep@ field operation.
+ */
+
+int ec_samep(ec_curve *c, ec_curve *d)
+{
+  return (field_samep(c->f, d->f) && c->ops == d->ops && EC_SAMEP(c, d));
+}
+
 /* --- @ec_create@ --- *
  *
  * Arguments:  @ec *p@ = pointer to an elliptic-curve point
@@ -117,8 +150,32 @@ 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_stdsamep@ --- *
+ *
+ * Arguments:  @ec_curve *c, *d@ = two elliptic curves
+ *
+ * Returns:    Nonzero if the curves are identical (not just isomorphic).
+ *
+ * Use:                Simple sameness check on @a@ and @b@ curve members.
+ */
+
+int ec_stdsamep(ec_curve *c, ec_curve *d)
+{
+  return (MP_EQ(c->a, d->a) && MP_EQ(c->b, d->b));
+}
+
 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
  *
  * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
@@ -164,7 +221,7 @@ ec *ec_idfix(ec_curve *c, ec *d, const ec *p)
   return (d);
 }
 
-/* --- @ec_projin@, @ec_projout@ --- *
+/* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
  *
  * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
  *             @ec *d@ = pointer to the destination
@@ -230,7 +287,7 @@ ec *ec_projfix(ec_curve *c, ec *d, const ec *p)
     mp_drop(d->z);
     d->z = MP_COPY(f->one);
   }
-  return (d);  
+  return (d);
 }
 
 /* --- @ec_stdsub@ --- *
@@ -345,10 +402,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);
@@ -396,48 +453,24 @@ int ec_check(ec_curve *c, const ec *p)
   return (rc);
 }
 
-/* --- @ec_imul@, @ec_mul@ --- *
+/* --- @ec_rand@ --- *
  *
  * 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
+ *             @grand *r@ = random number source
  *
  * Returns:    The destination @d@.
  *
- * Use:                Multiplies a point by a scalar, returning %$n p$%.  The
- *             @imul@ variant uses internal representations for argument
- *             and result.
+ * Use:                Finds a random point on the given curve.
  */
 
-ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n)
+ec *ec_rand(ec_curve *c, ec *d, grand *r)
 {
-  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 (MP_LEN(n) == 0)
-    ;
-  else {
-    if (n->f & MP_NEG)
-      EC_NEG(c, &t, &t);
-    if (MP_LEN(n) < EXP_THRESH)
-      EXP_SIMPLE(*d, t, n);
-    else
-      EXP_WINDOW(*d, t, n);
-  }
-  EC_DESTROY(&t);
-  return (d);
-}
-
-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));
+  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));    
 }
 
 /*----- That's all, folks -------------------------------------------------*/