chiark / gitweb /
catacomb/__init__.py: Settle on SHAKE256 for X448 box-key generation.
[catacomb-python] / field.c
diff --git a/field.c b/field.c
index 9f51f5f6f4f01852178a737f0bdea309a198cd05..0a5d908f805dcff2ffe04b2f8d29899bcc14fcec 100644 (file)
--- a/field.c
+++ b/field.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
- *
- * $Id$
  *
  * Abstract fields
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python 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 General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -68,7 +66,7 @@ static PyObject *field_dopywrap(PyTypeObject *ty, field *f)
   fobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   fobj->ty.ht_type.tp_free = 0;
   fobj->ty.ht_type.tp_new = fe_pynew;
-  PyType_Ready(&fobj->ty.ht_type);
+  typeready(&fobj->ty.ht_type);
   return ((PyObject *)fobj);
 }
 
@@ -232,11 +230,13 @@ end:
 
 static long fe_pyhash(PyObject *me)
 {
-  long i = mp_tolong(FE_X(me));
-  i ^= 0xdcf62d6c; /* random perturbance */
-  if (i == -1)
-    i = -2;
-  return (i);
+  size_t sz = FE_F(me)->noctets;
+  uint32 h = 0xe0c127ca + FE_F(me)->ops->ty;
+  octet *p = xmalloc(sz);
+  mp_storeb(FE_X(me), p, sz);
+  h = unihash_hash(&unihash_global, h, p, sz);
+  xfree(p);
+  return (h % LONG_MAX);
 }
 
 static int fe_pycoerce(PyObject **x, PyObject **y)
@@ -263,10 +263,12 @@ end:
 static PyObject *fe_pyint(PyObject *x)
 {
   long l;
+  PyObject *rc;
   mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
-  if (mp_tolong_checked(xx, &l)) { MP_DROP(xx); return (0); }
+  if (!mp_tolong_checked(xx, &l, 0)) rc = PyInt_FromLong(l);
+  else rc = mp_topylong(xx);
   MP_DROP(xx);
-  return (PyInt_FromLong(l));
+  return (rc);
 }
 
 static PyObject *fe_pylong(PyObject *x)
@@ -408,7 +410,7 @@ static PyNumberMethods fe_pynumber = {
 
 static PyTypeObject fe_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.FE",                       /* @tp_name@ */
+  "FE",                                        /* @tp_name@ */
   sizeof(fe_pyobj),                    /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -538,7 +540,7 @@ static PyMethodDef field_pymethods[] = {
 
 static PyTypeObject field_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.Field",                    /* @tp_name@ */
+  "Field",                             /* @tp_name@ */
   sizeof(field_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -610,13 +612,13 @@ static PyObject *pfget_p(PyObject *me, void *hunoz)
 
 static PyGetSetDef primefield_pygetset[] = {
 #define GETSETNAME(op, name) pf##op##_##name
-  GET  (p,             "F.p -> prime field characteristic")
+  GET  (p,             "F.p -> prime field characteristic")
 #undef GETSETNAME
 };
 
 static PyTypeObject primefield_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeField",               /* @tp_name@ */
+  "PrimeField",                                /* @tp_name@ */
   sizeof(field_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -683,7 +685,7 @@ end:
 
 static PyTypeObject niceprimefield_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.NicePrimeField",           /* @tp_name@ */
+  "NicePrimeField",                    /* @tp_name@ */
   sizeof(field_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -736,14 +738,14 @@ static PyObject *bfget_m(PyObject *me, void *hunoz)
 
 static PyGetSetDef binfield_pygetset[] = {
 #define GETSETNAME(op, name) bf##op##_##name
-  GET  (m,             "F.m -> field polynomial degree")
+  GET  (m,             "F.m -> field polynomial degree")
 #undef GETSETNAME
   { 0 }
 };
 
 static PyTypeObject binfield_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.BinField",                 /* @tp_name@ */
+  "BinField",                          /* @tp_name@ */
   sizeof(field_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -809,14 +811,14 @@ end:
 
 static PyGetSetDef binpolyfield_pygetset[] = {
 #define GETSETNAME(op, name) pf##op##_##name
-  GET  (p,             "F.p -> field polynomial")
+  GET  (p,             "F.p -> field polynomial")
 #undef GETSETNAME
   { 0 }
 };
 
 static PyTypeObject binpolyfield_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.BinPolyField",             /* @tp_name@ */
+  "BinPolyField",                      /* @tp_name@ */
   sizeof(field_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -888,17 +890,17 @@ static PyObject *bnfget_beta(PyObject *me, void *hunoz)
 
 static PyGetSetDef binnormfield_pygetset[] = {
 #define GETSETNAME(op, name) pf##op##_##name
-  GET  (p,             "F.p -> field polynomial")
+  GET  (p,             "F.p -> field polynomial")
 #undef GETSETNAME
 #define GETSETNAME(op, name) bnf##op##_##name
-  GET  (beta,          "F.beta -> conversion factor")
+  GET  (beta,          "F.beta -> conversion factor")
 #undef GETSETNAME
   { 0 }
 };
 
 static PyTypeObject binnormfield_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.BinNormField",             /* @tp_name@ */
+  "BinNormField",                      /* @tp_name@ */
   sizeof(field_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -958,7 +960,7 @@ static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
   qd.p = p;
   qd.e = 0;
   if ((f = field_parse(&qd)) == 0)
-    SYNERR(qd.e);
+    VALERR(qd.e);
   rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
 end:
   return (rc);
@@ -966,7 +968,7 @@ end:
 
 static PyMethodDef methods[] = {
 #define METHNAME(func) meth_##func
-  METH (_Field_parse,          "parse(STR) -> F, REST")
+  METH (_Field_parse,          "parse(STR) -> F, REST")
 #undef METHNAME
   { 0 }
 };