mpmul.[ch]: Move internal `HWM' and `LWM' constants to implementation.
[u/mdw/catacomb] / ec.c
diff --git a/ec.c b/ec.c
index 9a929ca..cad7a56 100644 (file)
--- a/ec.c
+++ b/ec.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: ec.c,v 1.9 2004/04/01 21:28:41 mdw Exp $
+ * $Id$
  *
  * Elliptic curve definitions
  *
  * (c) 2001 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- 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.
- *
- * 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.
- *
- * 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.
- *
- * Revision 1.1  2001/04/29 18:12:33  mdw
- * Prototype version.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "ec.h"
@@ -91,7 +46,8 @@
 
 int ec_samep(ec_curve *c, ec_curve *d)
 {
-  return (field_samep(c->f, d->f) && c->ops == d->ops && EC_SAMEP(c, d));
+  return (c == d || (field_samep(c->f, d->f) &&
+                    c->ops == d->ops && EC_SAMEP(c, d)));
 }
 
 /* --- @ec_create@ --- *
@@ -172,9 +128,7 @@ int ec_eq(const ec *p, const ec *q) { return (EC_EQ(p, q)); }
  */
 
 int ec_stdsamep(ec_curve *c, ec_curve *d)
-{
-  return (MP_EQ(c->a, d->a) && MP_EQ(c->b, d->b));
-}
+  { return (MP_EQ(c->a, d->a) && MP_EQ(c->b, d->b)); }
 
 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
  *
@@ -216,10 +170,7 @@ ec *ec_idout(ec_curve *c, ec *d, const ec *p)
 }
 
 ec *ec_idfix(ec_curve *c, ec *d, const ec *p)
-{
-  EC_COPY(d, p);
-  return (d);
-}
+  { EC_COPY(d, p); return (d); }
 
 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
  *
@@ -253,16 +204,21 @@ ec *ec_projout(ec_curve *c, ec *d, const ec *p)
   else {
     mp *x, *y, *z, *zz;
     field *f = c->f;
-    z = F_INV(f, MP_NEW, p->z);
-    zz = F_SQR(f, MP_NEW, z);
-    z = F_MUL(f, z, zz, z);
-    x = F_MUL(f, d->x, p->x, zz);
-    y = F_MUL(f, d->y, p->y, z);
-    mp_drop(z);
-    mp_drop(zz);
+    if (p->z == f->one) {
+      d->x = F_OUT(f, d->x, p->x);
+      d->y = F_OUT(f, d->y, p->y);
+    } else {
+      z = F_INV(f, MP_NEW, p->z);
+      zz = F_SQR(f, MP_NEW, z);
+      z = F_MUL(f, z, zz, z);
+      x = F_MUL(f, d->x, p->x, zz);
+      y = F_MUL(f, d->y, p->y, z);
+      mp_drop(z);
+      mp_drop(zz);
+      d->x = F_OUT(f, x, x);
+      d->y = F_OUT(f, y, y);
+    }
     mp_drop(d->z);
-    d->x = F_OUT(f, x, x);
-    d->y = F_OUT(f, y, y);
     d->z = 0;
   }
   return (d);
@@ -272,7 +228,7 @@ ec *ec_projfix(ec_curve *c, ec *d, const ec *p)
 {
   if (EC_ATINF(p))
     EC_SETINF(d);
-  else if (d->z == c->f->one)
+  else if (p->z == c->f->one)
     EC_COPY(d, p);
   else {
     mp *z, *zz;
@@ -360,11 +316,7 @@ ec *ec_find(ec_curve *c, ec *d, mp *x)
  */
 
 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_IN(c, d, p); EC_NEG(c, d, d); return (EC_OUT(c, d, d)); }
 
 /* --- @ec_add@ --- *
  *
@@ -424,11 +376,7 @@ ec *ec_sub(ec_curve *c, ec *d, const ec *p, const ec *q)
  */
 
 ec *ec_dbl(ec_curve *c, ec *d, const ec *p)
-{
-  EC_IN(c, d, p);
-  EC_DBL(c, d, d);
-  return (EC_OUT(c, d, d));
-}
+  { EC_IN(c, d, p); EC_DBL(c, d, d); return (EC_OUT(c, d, d)); }
 
 /* --- @ec_check@ --- *
  *
@@ -470,7 +418,7 @@ ec *ec_rand(ec_curve *c, ec *d, grand *r)
   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));    
+  return (EC_OUT(c, d, d));
 }
 
 /*----- That's all, folks -------------------------------------------------*/