Release 2.1.2.
[u/mdw/catacomb] / ec-bin.c
index 1a70fe2..8ba7354 100644 (file)
--- a/ec-bin.c
+++ b/ec-bin.c
@@ -7,7 +7,7 @@
  * (c) 2004 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,
@@ -61,7 +61,7 @@ static ec *ecfind(ec_curve *c, ec *d, mp *x)
 {
   field *f = c->f;
   mp *y, *u, *v;
-  
+
   if (F_ZEROP(f, x))
     y = F_SQRT(f, MP_NEW, c->b);
   else {
@@ -180,7 +180,7 @@ static ec *ecadd(ec_curve *c, ec *d, const ec *a, const ec *b)
       dx = F_ADD(f, dx, dx, c->a);     /* %$a + \lambda^2 + \lambda$% */
       dx = F_ADD(f, dx, dx, a->x);    /* %$a + \lambda^2 + \lambda + x_0$% */
       dx = F_ADD(f, dx, dx, b->x);
-                           /* %$x' = a + \lambda^2 + \lambda + x_0 + x_1$% */
+                          /* %$x' = a + \lambda^2 + \lambda + x_0 + x_1$% */
     } else if (!MP_EQ(a->y, b->y) || F_ZEROP(f, a->x)) {
       EC_SETINF(d);
       return (d);
@@ -194,7 +194,7 @@ static ec *ecadd(ec_curve *c, ec *d, const ec *a, const ec *b)
       dx = F_ADD(f, dx, dx, c->a);    /* %$x' = a + \lambda^2 + \lambda$% */
       dy = MP_NEW;
     }
-      
+
     dy = F_ADD(f, dy, a->x, dx);       /* %$ x + x' $% */
     dy = F_MUL(f, dy, dy, lambda);     /* %$ (x + x') \lambda$% */
     dy = F_ADD(f, dy, dy, a->y);       /* %$ (x + x') \lambda + y$% */
@@ -311,7 +311,7 @@ static int ecprojcheck(ec_curve *c, const ec *p)
 {
   ec t = EC_INIT;
   int rc;
-  
+
   c->ops->fix(c, &t, p);
   rc = eccheck(c, &t);
   EC_DESTROY(&t);
@@ -353,19 +353,28 @@ ec_curve *ec_bin(field *f, mp *a, mp *b)
 ec_curve *ec_binproj(field *f, mp *a, mp *b)
 {
   ecctx_bin *cc = CREATE(ecctx_bin);
+  int i;
+  mp *c, *d;
+
   cc->c.ops = &ec_binprojops;
   cc->c.f = f;
   cc->c.a = F_IN(f, MP_NEW, a);
   cc->c.b = F_IN(f, MP_NEW, b);
-  cc->bb = F_SQRT(f, MP_NEW, cc->c.b);
-  if (cc->bb)
-    cc->bb = F_SQRT(f, cc->bb, cc->bb);
-  if (!cc->bb) {
+
+  c = MP_COPY(cc->c.b);
+  for (i = 0; i < f->nbits - 2; i++)
+    c = F_SQR(f, c, c);
+  d = F_SQR(f, MP_NEW, c); d = F_SQR(f, d, d);
+  if (!MP_EQ(d, cc->c.b)) {
+    MP_DROP(c);
+    MP_DROP(d);
     MP_DROP(cc->c.a);
     MP_DROP(cc->c.b);
     DESTROY(cc);
     return (0);
   }
+  cc->bb = c;
+  MP_DROP(d);
   return (&cc->c);
 }
 
@@ -408,7 +417,7 @@ int main(int argc, char *argv[])
   g.x = MP(0x0311103c17167564ace77ccb09c681f886ba54ee8);
   g.y = MP(0x333ac13c6447f2e67613bf7009daf98c87bb50c7f);
 
-  for (i = 0; i < n; i++) { 
+  for (i = 0; i < n; i++) {
     ec_mul(c, &d, &g, r);
     if (EC_ATINF(&d)) {
       fprintf(stderr, "zero too early\n");