General robustification.
[u/mdw/catacomb] / ec.h
diff --git a/ec.h b/ec.h
index 105838c..24bd6b2 100644 (file)
--- a/ec.h
+++ b/ec.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: ec.h,v 1.4 2003/05/15 23:25:59 mdw Exp $
+ * $Id: ec.h,v 1.10 2004/04/03 03:32:05 mdw Exp $
  *
  * Elliptic curve definitions
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: ec.h,v $
+ * Revision 1.10  2004/04/03 03:32:05  mdw
+ * General robustification.
+ *
+ * Revision 1.9  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.8  2004/03/27 17:54:11  mdw
+ * Standard curves and curve checking.
+ *
+ * Revision 1.7  2004/03/23 15:19:32  mdw
+ * Test elliptic curves more thoroughly.
+ *
+ * Revision 1.6  2004/03/22 02:19:10  mdw
+ * Rationalise the sliding-window threshold.  Drop guarantee that right
+ * arguments to EC @add@ are canonical, and fix up projective implementations
+ * to cope.
+ *
+ * Revision 1.5  2004/03/21 22:52:06  mdw
+ * Merge and close elliptic curve branch.
+ *
+ * Revision 1.4.4.3  2004/03/21 22:39:46  mdw
+ * Elliptic curves on binary fields work.
+ *
+ * Revision 1.4.4.2  2004/03/20 00:13:31  mdw
+ * Projective coordinates for prime curves
+ *
+ * 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.
  *
 
 /*----- Header files ------------------------------------------------------*/
 
-#include "field.h"
-#include "mp.h"
+#ifndef CATACOMB_FIELD_H
+#  include "field.h"
+#endif
+
+#ifndef CATACOMB_MP_H
+#  include "mp.h"
+#endif
+
+#ifndef CATACOMB_QDPARSE_H
+#  include "qdparse.h"
+#endif
 
 /*----- Data structures ---------------------------------------------------*/
 
 typedef struct ec_curve {
   const struct ec_ops *ops;            /* Curve operations */
   field *f;                            /* Underlying field structure */
+  mp *a, *b;                           /* Standard params (internal form) */
 } ec_curve;
 
 /* --- An elliptic curve point --- */
@@ -80,27 +123,51 @@ typedef struct ec_mulfactor {
   mp *exp;                             /* The exponent */
 } ec_mulfactor;
 
-/* --- Elliptic curve operations --- */
+/* --- Elliptic curve operations --- *
+ *
+ * All operations (apart from @destroy@ and @in@) are guaranteed to be
+ * performed on internal representations of points.
+ *
+ * (Historical note.  We used to guarantee that the second to @add@ and @mul@
+ * was the output of @in@ or @fix@, but this canonification turned out to
+ * make the precomputation in @ec_exp@ too slow.  Projective implementations
+ * must therefore cope with a pair of arbitrary points.)
+ */
 
 typedef struct ec_ops {
   void (*destroy)(ec_curve */*c*/);
+  int (*samep)(ec_curve */*c*/, ec_curve */*d*/);
   ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
   ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
+  ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
   ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/);
   ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
   ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
   ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
   ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
+  int (*check)(ec_curve */*c*/, const ec */*p*/);
 } ec_ops;
 
+#define EC_SAMEP(c, d)         (c)->ops->samep((c), (d))
 #define EC_IN(c, d, p)         (c)->ops->in((c), (d), (p))
-#define EC_OUT(c, d, p)                (c)->ops->in((c), (d), (p))
+#define EC_OUT(c, d, p)                (c)->ops->out((c), (d), (p))
+#define EC_FIX(c, d, p)                (c)->ops->fix((c), (d), (p))
 
 #define EC_FIND(c, d, x)       (c)->ops->find((c), (d), (x))
 #define EC_NEG(c, d, x)                (c)->ops->neg((c), (d), (x))
 #define EC_ADD(c, d, p, q)     (c)->ops->add((c), (d), (p), (q))
 #define EC_SUB(c, d, p, q)     (c)->ops->sub((c), (d), (p), (q))
 #define EC_DBL(c, d, p)                (c)->ops->dbl((c), (d), (p))
+#define EC_CHECK(c, p)         (c)->ops->check((c), (p))
+
+/* --- Elliptic curve parameters --- */
+
+typedef struct ec_info {
+  ec_curve *c;                         /* The actual curve */
+  ec g;                                        /* The common point */
+  mp *r;                               /* Order of %$g$% */
+  mp *h;                               /* Cofactor %$h = \#E/r$% */
+} ec_info;
 
 /*----- Simple memory management things -----------------------------------*/
 
@@ -205,33 +272,36 @@ extern ec *ec_setinf(ec */*p*/);
 
 extern ec *ec_copy(ec */*d*/, const ec */*p*/);
 
-/*----- Interesting arithmetic --------------------------------------------*/
-
-/* --- @ec_in@ --- *
- *
- * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
- *             @ec *d@ = pointer to the destination point
- *             @const ec *p@ = pointer to the source point
+/* --- @ec_eq@ --- *
  *
- * Returns:    The destination point.
+ * Arguments:  @const ec *p, *q@ = two points
  *
- * Use:                Converts a point to internal representation.
+ * Returns:    Nonzero if the points are equal.  Compares external-format
+ *             points.
  */
 
-extern ec *ec_in(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
+#define EC_EQ(p, q)                                                    \
+    ((EC_ATINF(p) && EC_ATINF(q)) ||                                   \
+     (!EC_ATINF(p) && !EC_ATINF(q) &&                                  \
+      MP_EQ((p)->x, (q)->x) &&                                         \
+      MP_EQ((p)->y, (q)->y)))
+
+extern int ec_eq(const ec *p, const ec *q);
 
-/* --- @ec_out@ --- *
+/*----- Interesting arithmetic --------------------------------------------*/
+
+/* --- @ec_samep@ --- *
  *
- * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
- *             @ec *d@ = pointer to the destination point
- *             @const ec *p@ = pointer to the source point
+ * Arguments:  @ec_curve *c, *d@ = two elliptic curves
  *
- * Returns:    The destination point.
+ * Returns:    Nonzero if the curves are identical (not just isomorphic).
  *
- * Use:                Converts a point to external representation.
+ * 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.
  */
 
-extern ec *ec_out(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
+extern int ec_samep(ec_curve */*c*/, ec_curve */*d*/);
 
 /* --- @ec_find@ --- *
  *
@@ -249,6 +319,19 @@ extern ec *ec_out(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 
 extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
 
+/* --- @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.
+ */
+
+extern ec *ec_rand(ec_curve */*c*/, ec */*d*/, grand */*r*/);
+
 /* --- @ec_neg@ --- *
  *
  * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
@@ -303,6 +386,18 @@ extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
 
 extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 
+/* --- @ec_check@ --- *
+ *
+ * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
+ *             @const ec *p@ = pointer to the point
+ *
+ * Returns:    Zero if OK, nonzero if this is an invalid point.
+ *
+ * Use:                Checks that a point is actually on an elliptic curve.
+ */
+
+extern int ec_check(ec_curve */*c*/, const ec */*p*/);
+
 /* --- @ec_mul@, @ec_imul@ --- *
  *
  * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
@@ -340,7 +435,18 @@ extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
 
 /*----- Standard curve operations -----------------------------------------*/
 
-/* --- @ec_idin@, @ec_idout@ --- *
+/* --- @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.
+ */
+
+extern int ec_stdsamep(ec_curve */*c*/, ec_curve */*d*/);
+
+/* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
  *
  * Arguments:  @ec_curve *c@ = pointer to an elliptic curve
  *             @ec *d@ = pointer to the destination
@@ -355,8 +461,9 @@ extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
 
 extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
+extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 
-/* --- @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
@@ -370,6 +477,7 @@ extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 
 extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
+extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
 
 /* --- @ec_stdsub@ --- *
  *
@@ -402,10 +510,10 @@ extern void ec_destroycurve(ec_curve */*c*/);
 
 /* --- @ec_prime@, @ec_primeproj@ --- *
  *
- * Arguments:  @field *f@ = the underyling field for this elliptic curve
+ * Arguments:  @field *f@ = the underlying field for this elliptic curve
  *             @mp *a, *b@ = the coefficients for this curve
  *
- * Returns:    A pointer to the curve.
+ * Returns:    A pointer to the curve, or null.
  *
  * Use:                Creates a curve structure for an elliptic curve defined over
  *             a prime field.  The @primeproj@ variant uses projective
@@ -415,18 +523,125 @@ extern void ec_destroycurve(ec_curve */*c*/);
 extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
 extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
 
-/* --- @ec_bin@ --- *
+/* --- @ec_bin@, @ec_binproj@ --- *
  *
  * Arguments:  @field *f@ = the underlying field for this elliptic curve
  *             @mp *a, *b@ = the coefficients for this curve
  *
- * Returns:    A pointer to the curve.
+ * Returns:    A pointer to the curve, or null.
  *
- * Use:                Creates a curve structure for a non-supersingular elliptic
- *             curve defined over a binary field.
+ * Use:                Creates a curve structure for an elliptic curve defined over
+ *             a binary field.  The @binproj@ variant uses projective
+ *             coordinates, which can be a win.
  */
 
 extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
+extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
+
+/*----- Curve parameter sets ----------------------------------------------*/
+
+/* --- @ec_curveparse@ --- *
+ *
+ * Arguments:  @qd_parse *qd@ = parser context
+ *
+ * Returns:    Elliptic curve pointer if OK, or null.
+ *
+ * Use:                Parses an elliptic curve description, which has the form
+ *
+ *               * a field description
+ *               * an optional `/'
+ *               * `prime', `primeproj', `bin', or `binproj'
+ *               * an optional `:'
+ *               * the %$a$% parameter
+ *               * an optional `,'
+ *               * the %$b$% parameter
+ */
+
+extern ec_curve *ec_curveparse(qd_parse */*qd*/);
+
+/* --- @ec_ptparse@ --- *
+ *
+ * Arguments:  @qd_parse *qd@ = parser context
+ *             @ec *p@ = where to put the point
+ *
+ * Returns:    The point address, or null.
+ *
+ * Use:                Parses an elliptic curve point.  This has the form
+ *
+ *               * %$x$%-coordinate
+ *               * optional `,'
+ *               * %$y$%-coordinate
+ */
+
+extern ec *ec_ptparse(qd_parse */*qd*/, ec */*p*/);
+
+/* --- @ec_infoparse@ --- *
+ *
+ * Arguments:  @qd_parse *qd@ = parser context
+ *             @ec_info *ei@ = curve information block, currently
+ *                     uninitialized
+ *
+ * Returns:    Zero on success, nonzero on failure.
+ *
+ * Use:                Parses an elliptic curve information string, and stores the
+ *             information in @ei@.  This has the form
+ *
+ *               * elliptic curve description
+ *               * optional `/'
+ *               * common point
+ *               * optional `:'
+ *               * group order
+ *               * optional `*'
+ *               * cofactor
+ */
+
+extern int ec_infoparse(qd_parse */*qd*/, ec_info */*ei*/);
+
+/* --- @ec_getinfo@ --- *
+ *
+ * Arguments:  @ec_info *ei@ = where to write the information
+ *             @const char *p@ = string describing a curve
+ *
+ * Returns:    Null on success, or a pointer to an error message.
+ *
+ * Use:                Parses out information about a curve.  The string is either a
+ *             standard curve name, or a curve info string.
+ */
+
+extern const char *ec_getinfo(ec_info */*ei*/, const char */*p*/);
+
+/* --- @ec_sameinfop@ --- *
+ *
+ * Arguments:  @ec_info *ei, *ej@ = two elliptic curve parameter sets
+ *
+ * Returns:    Nonzero if the curves are identical (not just isomorphic).
+ *
+ * Use:                Checks for sameness of curve parameters.
+ */
+
+extern int ec_sameinfop(ec_info */*ei*/, ec_info */*ej*/);
+
+/* --- @ec_freeinfo@ --- *
+ *
+ * Arguments:  @ec_info *ei@ = elliptic curve information block to free
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees the information block.
+ */
+
+extern void ec_freeinfo(ec_info */*ei*/);
+
+/* --- @ec_checkinfo@ --- *
+ *
+ * Arguments:  @const ec_info *ei@ = elliptic curve information block
+ *
+ * Returns:    Null if OK, or pointer to error message.
+ *
+ * Use:                Checks an elliptic curve according to the rules in SEC1.
+ */
+
+extern const char *ec_checkinfo(const ec_info */*ei*/, grand */*gr*/);
 
 /*----- That's all, folks -------------------------------------------------*/