~mdw
/
catacomb-python
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
bytestring.c: Cache empty and singleton strings.
[catacomb-python]
/
field.c
diff --git
a/field.c
b/field.c
index
df6d4d4
..
f2a0d2f
100644
(file)
--- a/
field.c
+++ b/
field.c
@@
-1,13
+1,11
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id$
- *
* Abstract fields
*
* (c) 2004 Straylight/Edgeware
*/
* Abstract fields
*
* (c) 2004 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of the Python interface to Catacomb.
*
*
* This file is part of the Python interface to Catacomb.
*
@@
-15,12
+13,12
@@
* 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.
* 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.
* 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.
* 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.
@@
-44,9
+42,9
@@
static PyObject *fe_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
{
PyObject *x;
mp *z;
{
PyObject *x;
mp *z;
-
char *
kwlist[] = { "x", 0 };
+
static const char *const
kwlist[] = { "x", 0 };
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:fe",
kwlist
, &x))
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:fe",
KWLIST
, &x))
return (0);
if (FE_PYCHECK(x) && FE_F(x) == FIELD_F(ty)) RETURN_OBJ(x);
if ((z = getmp(x)) == 0) return (0);
return (0);
if (FE_PYCHECK(x) && FE_F(x) == FIELD_F(ty)) RETURN_OBJ(x);
if ((z = getmp(x)) == 0) return (0);
@@
-58,17
+56,17
@@
static PyObject *field_dopywrap(PyTypeObject *ty, field *f)
{
field_pyobj *fobj = newtype(ty, 0, f->ops->name);
fobj->f = f;
{
field_pyobj *fobj = newtype(ty, 0, f->ops->name);
fobj->f = f;
- fobj->ty.type.tp_basicsize = sizeof(fe_pyobj);
- fobj->ty.type.tp_base = fe_pytype;
+ fobj->ty.
ht_
type.tp_basicsize = sizeof(fe_pyobj);
+ fobj->ty.
ht_
type.tp_base = fe_pytype;
Py_INCREF(fe_pytype);
Py_INCREF(fe_pytype);
- fobj->ty.type.tp_flags = (Py_TPFLAGS_DEFAULT |
- Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_CHECKTYPES |
- Py_TPFLAGS_HEAPTYPE);
- fobj->ty.type.tp_alloc = PyType_GenericAlloc;
- fobj->ty.type.tp_free = 0;
- fobj->ty.type.tp_new = fe_pynew;
-
PyType_Ready(&fobj->ty.
type);
+ fobj->ty.
ht_
type.tp_flags = (Py_TPFLAGS_DEFAULT |
+
Py_TPFLAGS_BASETYPE |
+
Py_TPFLAGS_CHECKTYPES |
+
Py_TPFLAGS_HEAPTYPE);
+ fobj->ty.
ht_
type.tp_alloc = PyType_GenericAlloc;
+ fobj->ty.
ht_
type.tp_free = 0;
+ fobj->ty.
ht_
type.tp_new = fe_pynew;
+
typeready(&fobj->ty.ht_
type);
return ((PyObject *)fobj);
}
return ((PyObject *)fobj);
}
@@
-115,16
+113,16
@@
static mp *tofe(field *f, PyObject *o)
if (FE_PYCHECK(o)) {
if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
if (FE_PYCHECK(o)) {
if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
- y = FE_X(o);
- }
- if ((x = tomp(o)) != 0) {
+ y = MP_COPY(FE_X(o));
+ } else if ((x = tomp(o)) != 0) {
if (MP_ZEROP(x))
if (MP_ZEROP(x))
- y =
f->zero
;
+ y =
MP_COPY(f->zero)
;
else if (MP_EQ(x, MP_ONE))
else if (MP_EQ(x, MP_ONE))
- y = f->one;
+ y = MP_COPY(f->one);
+ else
+ y = F_IN(f, MP_NEW, x);
+ MP_DROP(x);
}
}
- if (x) MP_DROP(x);
- if (y) MP_COPY(y);
return (y);
}
return (y);
}
@@
-232,11
+230,13
@@
end:
static long fe_pyhash(PyObject *me)
{
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)
}
static int fe_pycoerce(PyObject **x, PyObject **y)
@@
-263,16
+263,18
@@
end:
static PyObject *fe_pyint(PyObject *x)
{
long l;
static PyObject *fe_pyint(PyObject *x)
{
long l;
+ PyObject *rc;
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
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);
MP_DROP(xx);
- return (
PyInt_FromLong(l)
);
+ return (
rc
);
}
static PyObject *fe_pylong(PyObject *x)
{
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
}
static PyObject *fe_pylong(PyObject *x)
{
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
- PyObject *rc =
(PyObject *)
mp_topylong(xx);
+ PyObject *rc = mp_topylong(xx);
MP_DROP(xx);
return (rc);
}
MP_DROP(xx);
return (rc);
}
@@
-408,7
+410,7
@@
static PyNumberMethods fe_pynumber = {
static PyTypeObject fe_pytype_skel = {
PyObject_HEAD_INIT(0) 0, /* Header */
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@ */
sizeof(fe_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-472,10
+474,10
@@
end:
static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
{
static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
{
-
char *
kwlist[] = { "rng", 0 };
+
static const char *const
kwlist[] = { "rng", 0 };
grand *r = &rand_global;
grand *r = &rand_global;
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand",
kwlist
,
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand",
KWLIST
,
convgrand, &r))
return (0);
return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
convgrand, &r))
return (0);
return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
@@
-538,7
+540,7
@@
static PyMethodDef field_pymethods[] = {
static PyTypeObject field_pytype_skel = {
PyObject_HEAD_INIT(0) 0, /* Header */
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@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-591,9
+593,9
@@
static PyObject *primefield_pynew(PyTypeObject *ty,
{
mp *xx = 0;
field *f;
{
mp *xx = 0;
field *f;
-
char *
kwlist[] = { "p", 0 };
+
static const char *const
kwlist[] = { "p", 0 };
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "O
:primefield", kwlist
,
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O
&:primefield", KWLIST
,
convmp, &xx))
goto end;
if ((f = field_prime(xx)) == 0)
convmp, &xx))
goto end;
if ((f = field_prime(xx)) == 0)
@@
-610,13
+612,13
@@
static PyObject *pfget_p(PyObject *me, void *hunoz)
static PyGetSetDef primefield_pygetset[] = {
#define GETSETNAME(op, name) pf##op##_##name
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 */
#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@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-639,7
+641,7
@@
static PyTypeObject primefield_pytype_skel = {
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
-
"P
rime fields.",
+
"PrimeField(P): p
rime fields.",
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
@@
-667,10
+669,10
@@
static PyObject *niceprimefield_pynew(PyTypeObject *ty,
{
mp *xx = 0;
field *f;
{
mp *xx = 0;
field *f;
-
char *
kwlist[] = { "p", 0 };
+
static const char *const
kwlist[] = { "p", 0 };
if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
-
kwlist
, convmp, &xx))
+
KWLIST
, convmp, &xx))
goto end;
if ((f = field_niceprime(xx)) == 0)
VALERR("bad prime for niceprimefield");
goto end;
if ((f = field_niceprime(xx)) == 0)
VALERR("bad prime for niceprimefield");
@@
-683,7
+685,7
@@
end:
static PyTypeObject niceprimefield_pytype_skel = {
PyObject_HEAD_INIT(0) 0, /* Header */
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@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-706,7
+708,7
@@
static PyTypeObject niceprimefield_pytype_skel = {
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
-
"Nice prime fields
.",
+
"NicePrimeField(P): prime field using Solinas reduction
.",
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
@@
-736,14
+738,14
@@
static PyObject *bfget_m(PyObject *me, void *hunoz)
static PyGetSetDef binfield_pygetset[] = {
#define GETSETNAME(op, name) bf##op##_##name
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 */
#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@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-766,7
+768,7
@@
static PyTypeObject binfield_pytype_skel = {
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
-
"Binary fields. Abstract class.",
+"Binary fields. Abstract class.",
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
@@
-794,9
+796,9
@@
static PyObject *binpolyfield_pynew(PyTypeObject *ty,
{
mp *xx = 0;
field *f;
{
mp *xx = 0;
field *f;
-
char *
kwlist[] = { "p", 0 };
+
static const char *const
kwlist[] = { "p", 0 };
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield",
kwlist
,
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield",
KWLIST
,
convgf, &xx))
goto end;
if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
convgf, &xx))
goto end;
if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
@@
-809,14
+811,14
@@
end:
static PyGetSetDef binpolyfield_pygetset[] = {
#define GETSETNAME(op, name) pf##op##_##name
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 */
#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@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-839,7
+841,7
@@
static PyTypeObject binpolyfield_pytype_skel = {
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
-
"B
inary fields with polynomial basis representation.",
+
"BinPolyField(P): b
inary fields with polynomial basis representation.",
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
@@
-867,10
+869,10
@@
static PyObject *binnormfield_pynew(PyTypeObject *ty,
{
mp *xx = 0, *yy = 0;
field *f;
{
mp *xx = 0, *yy = 0;
field *f;
-
char *
kwlist[] = { "p", "beta", 0 };
+
static const char *const
kwlist[] = { "p", "beta", 0 };
if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
-
kwlist
, convgf, &xx, convgf, &yy))
+
KWLIST
, convgf, &xx, convgf, &yy))
goto end;
if ((f = field_binnorm(xx, yy)) == 0) VALERR("bad args for binnormfield");
MP_DROP(xx); MP_DROP(yy);
goto end;
if ((f = field_binnorm(xx, yy)) == 0) VALERR("bad args for binnormfield");
MP_DROP(xx); MP_DROP(yy);
@@
-888,17
+890,17
@@
static PyObject *bnfget_beta(PyObject *me, void *hunoz)
static PyGetSetDef binnormfield_pygetset[] = {
#define GETSETNAME(op, name) pf##op##_##name
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
#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 */
#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@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-921,7
+923,7
@@
static PyTypeObject binnormfield_pytype_skel = {
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
-
"B
inary fields with normal basis representation.",
+
"BinNormField(P, BETA): b
inary fields with normal basis representation.",
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
@@
-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)
qd.p = p;
qd.e = 0;
if ((f = field_parse(&qd)) == 0)
-
SYN
ERR(qd.e);
+
VAL
ERR(qd.e);
rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
end:
return (rc);
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
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 }
};
#undef METHNAME
{ 0 }
};