Merge and close elliptic curve branch.
[u/mdw/catacomb] / calc / ec2.cal
diff --git a/calc/ec2.cal b/calc/ec2.cal
new file mode 100644 (file)
index 0000000..3e89034
--- /dev/null
@@ -0,0 +1,183 @@
+/* -*-apcalc-*-
+ *
+ * $Id: ec2.cal,v 1.2 2004/03/21 22:52:06 mdw Exp $
+ *
+ * Testbed for elliptic curve arithmetic over binary fields
+ *
+ * (c) 2004 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * 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: ec2.cal,v $
+ * Revision 1.2  2004/03/21 22:52:06  mdw
+ * Merge and close elliptic curve branch.
+ *
+ * Revision 1.1.2.1  2004/03/21 22:39:46  mdw
+ * Elliptic curves on binary fields work.
+ *
+ * Revision 1.1.4.2  2004/03/20 00:13:31  mdw
+ * Projective coordinates for prime curves
+ *
+ * Revision 1.1.4.1  2003/06/10 13:43:53  mdw
+ * Simple (non-projective) curves over prime fields now seem to work.
+ *
+ * Revision 1.1  2000/10/08 16:01:37  mdw
+ * Prototypes of various bits of code.
+ *
+ */
+
+/*----- Object types ------------------------------------------------------*/
+
+obj ec2_curve { a, b, p };
+obj ec2_pt { x, y, e };
+obj ecpp_pt { x, y, z, e };
+
+/*----- Main code ---------------------------------------------------------*/
+
+define ec2_curve(a, b, p)
+{
+  local obj ec2_curve e;
+  e.a = a;
+  e.b = b;
+  e.p = p;
+  return (e);
+}
+
+define ec2_pt(x, y, e)
+{
+  local obj ec2_pt p;
+  p.x = x % e.p;
+  p.y = y % e.p;
+  p.e = e;
+  return (p);
+}
+
+define ec2_pt_print(a)
+{
+  print "(" : a.x : ", " : a.y : ")" :;
+}
+
+define ec2_pt_add(a, b)
+{
+  local e, alpha;
+  local obj ec2_pt d;
+
+  print "> ecadd: ", a, b;
+  if (a == 0)
+    d = b;
+  else if (b == 0)
+    d = a;
+  else if (!istype(a, b))
+    quit "bad type arguments to ec2_pt_add";
+  else if (a.e != b.e)
+    quit "points from different curves in ec2_pt_add";
+  else {
+    e = a.e;
+    if (a.x != b.x) {
+      alpha = ((a.y + b.y) * gf_inv(a.x + b.x, e.p)) % e.p;
+      d.x = (e.a + alpha^2 + alpha + a.x + b.x) % e.p;
+    } else if (a.y != b.y || a.x == gf(0))
+      return 0;
+    else {
+      alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p;
+      d.x = (e.a + alpha^2 + alpha) % e.p;
+    }
+    d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p;
+    d.e = e;
+  }
+
+  print "< ecadd: ", d;
+  return (d);
+}
+
+define ec2_pt_dbl(a)
+{
+  local e, alpha;
+  local obj ec2_pt d;
+  print "> ecdbl: ", a;
+  if (istype(a, 1))
+    return (0);
+  e = a.e;
+  alpha = a.x + a.y * gf_inv(a.x, e.p) % e.p;
+  d.x = (e.a + alpha^2 + alpha) % e.p;
+  d.y = ((a.x + d.x) * alpha + d.x + a.y) % e.p;
+  d.e = e;
+  print "< ecdbl: ", d;
+  return (d);
+}
+
+define ec2_pt_sub(a, b) { return ec2_pt_add(a, ec2_pt_neg(b)); }
+
+define ec2_pt_neg(a)
+{
+  local obj ec2_pt d;
+  d.x = a.x;
+  d.y = a.x + a.y;
+  d.e = a.e;
+  return (d);
+}
+
+define ec2_pt_check(a)
+{
+  local e;
+
+  e = a.e;
+  if ((a.y^2 + a.x * a.y) % e.p != (a.x^3 + e.a * a.x^2 + e.b) % e.p)
+    quit "bad curve point";
+}
+
+define ec2_pt_mul(a, b)
+{
+  local p, n;
+  local d;
+
+  if (istype(a, 1)) {
+    n = a;
+    p = b;
+  } else if (istype(b, 1)) {
+    n = b;
+    p = a;
+  } else
+    return (newerror("bad arguments to ec2_pt_mul"));
+
+  d = 0;
+  while (n) {
+    if (n & 1)
+      d += p;
+    n >>= 1;
+    p = ec2_pt_dbl(p);
+  }
+  return (d);
+}
+
+/*----- FIPS186-2 standard curves -----------------------------------------*/
+
+b163 = ec2_curve(gf(1),gf(0x20a601907b8c953ca1481eb10512f78744a3205fd),
+                gf(0x800000000000000000000000000000000000000c9));
+b163_r = 5846006549323611672814742442876390689256843201587;
+b163_g = ec2_pt(0x3f0eba16286a2d57ea0991168d4994637e8343e36,
+               0x0d51fbc6c71a0094fa2cdd545b11c5c0c797324f1, b163);
+
+/*----- That's all, folks -------------------------------------------------*/
+