7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- Various utilities -------------------------------------------------*/
35 PyTypeObject *field_pytype;
36 PyTypeObject *primefield_pytype;
37 PyTypeObject *niceprimefield_pytype;
38 PyTypeObject *binfield_pytype;
39 PyTypeObject *binpolyfield_pytype;
40 PyTypeObject *binnormfield_pytype;
41 PyTypeObject *fe_pytype;
43 static PyObject *fe_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
47 char *kwlist[] = { "x", 0 };
49 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:fe", kwlist, &x))
51 if (FE_PYCHECK(x) && FE_F(x) == FIELD_F(ty)) RETURN_OBJ(x);
52 if ((z = getmp(x)) == 0) return (0);
53 z = F_IN(FIELD_F(ty), z, z);
54 return (fe_pywrap((PyObject *)ty, z));
57 static PyObject *field_dopywrap(PyTypeObject *ty, field *f)
59 field_pyobj *fobj = newtype(ty, 0);
61 fobj->ty.tp_name = (/*unconst*/ char *)f->ops->name;
62 fobj->ty.tp_basicsize = sizeof(fe_pyobj);
63 fobj->ty.tp_base = fe_pytype;
65 fobj->ty.tp_flags = (Py_TPFLAGS_DEFAULT |
67 Py_TPFLAGS_CHECKTYPES |
69 fobj->ty.tp_alloc = PyType_GenericAlloc;
70 fobj->ty.tp_free = _PyObject_Del;
71 fobj->ty.tp_new = fe_pynew;
72 PyType_Ready(&fobj->ty);
73 return ((PyObject *)fobj);
76 PyObject *field_pywrap(field *f)
80 if (strcmp(F_NAME(f), "prime") == 0) ty = primefield_pytype;
81 else if (strcmp(F_NAME(f), "niceprime") == 0) ty = niceprimefield_pytype;
82 else if (strcmp(F_NAME(f), "binpoly") == 0) ty = binpolyfield_pytype;
83 else if (strcmp(F_NAME(f), "binnorm") == 0) ty = binnormfield_pytype;
85 return (field_dopywrap(ty, f));
88 field *field_copy(field *f)
90 if (strcmp(F_NAME(f), "prime") == 0)
91 f = field_prime(f->m);
92 else if (strcmp(F_NAME(f), "niceprime") == 0)
93 f = field_niceprime(f->m);
94 else if (strcmp(F_NAME(f), "binpoly") == 0)
95 f = field_binpoly(f->m);
96 else if (strcmp(F_NAME(f), "binnorm") == 0) {
97 fctx_binnorm *fc = (fctx_binnorm *)f;
98 f = field_binnorm(f->m, fc->ntop.r[fc->ntop.n - 1]);
104 PyObject *fe_pywrap(PyObject *fobj, mp *x)
106 fe_pyobj *z = PyObject_New(fe_pyobj, (PyTypeObject *)fobj);
107 z->f = FIELD_F(fobj);
110 return ((PyObject *)z);
113 static mp *tofe(field *f, PyObject *o)
118 if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
121 if ((x = tomp(o)) != 0) {
124 else if (MP_EQ(x, MP_ONE))
132 mp *getfe(field *f, PyObject *o)
135 if ((x = tofe(f, o)) == 0) {
136 PyErr_Format(PyExc_TypeError, "can't convert %.100s to fe",
137 o->ob_type->tp_name);
142 /*----- Field elements ----------------------------------------------------*/
144 static int febinop(PyObject *x, PyObject *y,
145 field **f, PyObject **fobj, mp **xx, mp **yy)
147 if (FE_PYCHECK(x)) *fobj = FE_FOBJ(x);
148 else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y);
151 if ((*xx = tofe(*f, x)) == 0)
153 if ((*yy = tofe(*f, y)) == 0) {
160 #define BINOP(name) \
161 static PyObject *fe_py##name(PyObject *x, PyObject *y) { \
165 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL; \
166 zz = ff->ops->name(ff, MP_NEW, xx, yy); \
167 MP_DROP(xx); MP_DROP(yy); \
168 return (fe_pywrap(fobj, zz)); \
175 static PyObject *fe_pydiv(PyObject *x, PyObject *y)
181 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL;
182 if (F_ZEROP(ff, yy)) ZDIVERR("division by zero");
183 yy = F_INV(ff, yy, yy);
184 z = fe_pywrap(fobj, F_MUL(ff, MP_NEW, xx, yy));
186 MP_DROP(xx); MP_DROP(yy);
190 static PyObject *fe_pyexp(PyObject *x, PyObject *y, PyObject *z)
195 if (z != Py_None || !FE_PYCHECK(x) || (yy = tomp(y)) == 0)
197 ff = FE_F(x); xx = FE_X(x); MP_COPY(xx);
198 if (MP_NEGP(yy) && F_ZEROP(ff, xx)) ZDIVERR("division by zero");
199 z = fe_pywrap(FE_FOBJ(x), field_exp(ff, MP_NEW, xx, yy));
201 MP_DROP(xx); MP_DROP(yy);
205 static PyObject *fe_pyneg(PyObject *x)
207 return fe_pywrap(FE_FOBJ(x), FE_F(x)->ops->neg(FE_F(x), MP_NEW, FE_X(x)));
210 static PyObject *fe_pyid(PyObject *x) { RETURN_OBJ(x); }
212 static int fe_pynonzerop(PyObject *x) { return !F_ZEROP(FE_F(x), FE_X(x)); }
214 static PyObject *fe_pyrichcompare(PyObject *x, PyObject *y, int op)
222 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL;
224 case Py_EQ: b = MP_EQ(xx, yy); break;
225 case Py_NE: b = !MP_EQ(xx, yy); break;
226 default: TYERR("field elements are unordered");
230 MP_DROP(xx); MP_DROP(yy);
234 static long fe_pyhash(PyObject *me)
236 long i = mp_tolong(FE_X(me));
237 i ^= 0xdcf62d6c; /* random perturbance */
243 static int fe_pycoerce(PyObject **x, PyObject **y)
247 if (FE_PYCHECK(*y)) {
248 if (FE_F(*x) != FE_F(*y) && !field_samep(FE_F(*x), FE_F(*y)))
249 TYERR("field mismatch");
250 Py_INCREF(*x); Py_INCREF(*y);
253 if ((z = tofe(FE_F(*x), *y)) != 0) {
255 *y = fe_pywrap(FE_FOBJ(*x), z);
264 static PyObject *fe_pyint(PyObject *x)
267 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
268 if (mp_tolong_checked(xx, &l)) { MP_DROP(xx); return (0); }
270 return (PyInt_FromLong(l));
273 static PyObject *fe_pylong(PyObject *x)
275 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
276 PyObject *rc = (PyObject *)mp_topylong(xx);
281 #define BASEOP(name, radix, pre) \
282 static PyObject *fe_py##name(PyObject *x) { \
283 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x)); \
284 PyObject *rc = mp_topystring(FE_X(x), radix, 0, pre, 0); \
289 BASEOP(hex, 16, "0x");
292 static void fe_pydealloc(PyObject *me)
294 Py_DECREF(FE_FOBJ(me));
299 #define UNOP(name, check) \
300 static PyObject *femeth_##name(PyObject *me, PyObject *arg) { \
301 field *f = FE_F(me); \
303 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
304 if (!f->ops->name) TYERR(#name " not supported for this field"); \
306 x = f->ops->name(f, MP_NEW, x); \
307 if (!x) RETURN_NONE; \
308 return (fe_pywrap(FE_FOBJ(me), x)); \
312 UNOP(inv, if (F_ZEROP(f, x)) ZDIVERR("division by zero"); )
322 static PyObject *feget_field(PyObject *me, void *hunoz)
323 { RETURN_OBJ(FE_FOBJ(me)); }
325 static PyObject *feget_value(PyObject *me, void *hunoz)
327 mp *x = F_OUT(FE_F(me), MP_NEW, FE_X(me));
328 if (F_TYPE(FE_F(me)) == FTY_BINARY)
329 return (gf_pywrap(x));
331 return (mp_pywrap(x));
334 static PyObject *feget__value(PyObject *me, void *hunoz)
338 if (F_TYPE(FE_F(me)) == FTY_BINARY)
339 return (gf_pywrap(x));
341 return (mp_pywrap(x));
344 static PyGetSetDef fe_pygetset[] = {
345 #define GETSETNAME(op, name) fe##op##_##name
346 GET (field, "X.field -> field containing X")
347 GET (value, "X.value -> `natural' integer representation of X")
348 GET (_value, "X._value -> internal integer representation of X")
353 static PyMethodDef fe_pymethods[] = {
354 #define METHNAME(func) femeth_##func
355 METH (inv, "X.inv() -> X^{-1}")
356 METH (sqr, "X.sqr() -> X^2")
357 METH (sqrt, "X.sqrt() -> sqrt(X)")
358 METH (quadsolve, "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
359 METH (dbl, "X.dbl() -> 2 * X (prime only)")
360 METH (tpl, "X.tpl() -> 3 * X (prime only)")
361 METH (qdl, "X.qdl() -> 4 * X (prime only)")
362 METH (hlv, "X.hlv() -> X/2 (prime only)")
367 static PyNumberMethods fe_pynumber = {
368 fe_pyadd, /* @nb_add@ */
369 fe_pysub, /* @nb_subtract@ */
370 fe_pymul, /* @nb_multiply@ */
371 fe_pydiv, /* @nb_divide@ */
372 0, /* @nb_remainder@ */
374 fe_pyexp, /* @nb_power@ */
375 fe_pyneg, /* @nb_negative@ */
376 fe_pyid, /* @nb_positive@ */
377 0, /* @nb_absolute@ */
378 fe_pynonzerop, /* @nb_nonzero@ */
385 fe_pycoerce, /* @nb_coerce@ */
386 fe_pyint, /* @nb_int@ */
387 fe_pylong, /* @nb_long@ */
388 0 /* meaningless */, /* @nb_float@ */
389 fe_pyoct, /* @nb_oct@ */
390 fe_pyhex, /* @nb_hex@ */
392 0, /* @nb_inplace_add@ */
393 0, /* @nb_inplace_subtract@ */
394 0, /* @nb_inplace_multiply@ */
395 0, /* @nb_inplace_divide@ */
396 0, /* @nb_inplace_remainder@ */
397 0, /* @nb_inplace_power@ */
398 0, /* @nb_inplace_lshift@ */
399 0, /* @nb_inplace_rshift@ */
400 0, /* @nb_inplace_and@ */
401 0, /* @nb_inplace_xor@ */
402 0, /* @nb_inplace_or@ */
404 0, /* @nb_floor_divide@ */
405 fe_pydiv, /* @nb_true_divide@ */
406 0, /* @nb_inplace_floor_divide@ */
407 0, /* @nb_inplace_true_divide@ */
410 static PyTypeObject fe_pytype_skel = {
411 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
412 "catacomb.FE", /* @tp_name@ */
413 sizeof(fe_pyobj), /* @tp_basicsize@ */
414 0, /* @tp_itemsize@ */
416 fe_pydealloc, /* @tp_dealloc@ */
418 0, /* @tp_getattr@ */
419 0, /* @tp_setattr@ */
420 0, /* @tp_compare@ */
422 &fe_pynumber, /* @tp_as_number@ */
423 0, /* @tp_as_sequence@ */
424 0, /* @tp_as_mapping@ */
425 fe_pyhash, /* @tp_hash@ */
427 fe_pyhex, /* @tp_str@ */
428 0, /* @tp_getattro@ */
429 0, /* @tp_setattro@ */
430 0, /* @tp_as_buffer@ */
431 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
432 Py_TPFLAGS_CHECKTYPES |
436 "Finite field elements, abstract base class.",
438 0, /* @tp_traverse@ */
440 fe_pyrichcompare, /* @tp_richcompare@ */
441 0, /* @tp_weaklistoffset@ */
443 0, /* @tp_iternexr@ */
444 fe_pymethods, /* @tp_methods@ */
445 0, /* @tp_members@ */
446 fe_pygetset, /* @tp_getset@ */
449 0, /* @tp_descr_get@ */
450 0, /* @tp_descr_set@ */
451 0, /* @tp_dictoffset@ */
453 PyType_GenericAlloc, /* @tp_alloc@ */
454 abstract_pynew, /* @tp_new@ */
455 _PyObject_Del, /* @tp_free@ */
459 /*----- Fields ------------------------------------------------------------*/
461 static PyObject *field_pyrichcompare(PyObject *x, PyObject *y, int op)
463 int b = field_samep(FIELD_F(x), FIELD_F(y));
467 default: TYERR("can't order fields");
474 static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
476 char *kwlist[] = { "rng", 0 };
477 grand *r = &rand_global;
479 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
482 return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
485 static PyObject *fmeth__adopt(PyObject *me, PyObject *arg)
488 if (!PyArg_ParseTuple(arg, "O&:_adopt", convmp, &xx)) return (0);
489 return (fe_pywrap(me, xx));
492 static void field_pydealloc(PyObject *me)
494 F_DESTROY(FIELD_F(me));
495 PyType_Type.tp_dealloc(me);
498 static PyObject *fget_zero(PyObject *me, void *hunoz)
499 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->zero))); }
501 static PyObject *fget_one(PyObject *me, void *hunoz)
502 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->one))); }
504 static PyObject *fget_q(PyObject *me, void *hunoz)
505 { return (mp_pywrap(MP_COPY(FIELD_F(me)->q))); }
507 static PyObject *fget_nbits(PyObject *me, void *hunoz)
508 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
510 static PyObject *fget_noctets(PyObject *me, void *hunoz)
511 { return (PyInt_FromLong(FIELD_F(me)->noctets)); }
513 static PyObject *fget_name(PyObject *me, void *hunoz)
514 { return (PyString_FromString(F_NAME(FIELD_F(me)))); }
516 static PyObject *fget_type(PyObject *me, void *hunoz)
517 { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
519 static PyGetSetDef field_pygetset[] = {
520 #define GETSETNAME(op, name) f##op##_##name
521 GET (zero, "F.zero -> field additive identity")
522 GET (one, "F.one -> field multiplicative identity")
523 GET (q, "F.q -> number of elements in field")
524 GET (nbits, "F.nbits -> bits needed to represent element")
525 GET (noctets, "F.noctets -> octetss needed to represent element")
526 GET (name, "F.name -> name of this kind of field")
527 GET (type, "F.type -> type code of this kind of field")
532 static PyMethodDef field_pymethods[] = {
533 #define METHNAME(name) fmeth_##name
534 METH (_adopt, "F._adopt(X) -> FE")
535 KWMETH(rand, "F.rand(rng = rand) -> FE, uniformly distributed")
540 static PyTypeObject field_pytype_skel = {
541 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
542 "catacomb.Field", /* @tp_name@ */
543 sizeof(field_pyobj), /* @tp_basicsize@ */
544 0, /* @tp_itemsize@ */
546 field_pydealloc, /* @tp_dealloc@ */
548 0, /* @tp_getattr@ */
549 0, /* @tp_setattr@ */
550 0, /* @tp_compare@ */
552 0, /* @tp_as_number@ */
553 0, /* @tp_as_sequence@ */
554 0, /* @tp_as_mapping@ */
558 0, /* @tp_getattro@ */
559 0, /* @tp_setattro@ */
560 0, /* @tp_as_buffer@ */
561 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
565 "An abstract field. This is an abstract type.",
567 0, /* @tp_traverse@ */
569 field_pyrichcompare, /* @tp_richcompare@ */
570 0, /* @tp_weaklistoffset@ */
572 0, /* @tp_iternexr@ */
573 field_pymethods, /* @tp_methods@ */
574 0, /* @tp_members@ */
575 field_pygetset, /* @tp_getset@ */
578 0, /* @tp_descr_get@ */
579 0, /* @tp_descr_set@ */
580 0, /* @tp_dictoffset@ */
582 PyType_GenericAlloc, /* @tp_alloc@ */
583 abstract_pynew, /* @tp_new@ */
584 _PyObject_Del, /* @tp_free@ */
588 /*----- Prime fields ------------------------------------------------------*/
590 static PyObject *primefield_pynew(PyTypeObject *ty,
591 PyObject *arg, PyObject *kw)
595 char *kwlist[] = { "p", 0 };
597 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:primefield", kwlist,
600 if ((f = field_prime(xx)) == 0)
601 VALERR("bad prime for primefield");
603 return (field_dopywrap(ty, f));
609 static PyObject *pfget_p(PyObject *me, void *hunoz)
610 { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
612 static PyGetSetDef primefield_pygetset[] = {
613 #define GETSETNAME(op, name) pf##op##_##name
614 GET (p, "F.p -> prime field characteristic")
618 static PyTypeObject primefield_pytype_skel = {
619 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
620 "catacomb.PrimeField", /* @tp_name@ */
621 sizeof(field_pyobj), /* @tp_basicsize@ */
622 0, /* @tp_itemsize@ */
624 field_pydealloc, /* @tp_dealloc@ */
626 0, /* @tp_getattr@ */
627 0, /* @tp_setattr@ */
628 0, /* @tp_compare@ */
630 0, /* @tp_as_number@ */
631 0, /* @tp_as_sequence@ */
632 0, /* @tp_as_mapping@ */
636 0, /* @tp_getattro@ */
637 0, /* @tp_setattro@ */
638 0, /* @tp_as_buffer@ */
639 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
645 0, /* @tp_traverse@ */
647 field_pyrichcompare, /* @tp_richcompare@ */
648 0, /* @tp_weaklistoffset@ */
650 0, /* @tp_iternexr@ */
651 0, /* @tp_methods@ */
652 0, /* @tp_members@ */
653 primefield_pygetset, /* @tp_getset@ */
656 0, /* @tp_descr_get@ */
657 0, /* @tp_descr_set@ */
658 0, /* @tp_dictoffset@ */
660 PyType_GenericAlloc, /* @tp_alloc@ */
661 primefield_pynew, /* @tp_new@ */
662 _PyObject_Del, /* @tp_free@ */
666 static PyObject *niceprimefield_pynew(PyTypeObject *ty,
667 PyObject *arg, PyObject *kw)
671 char *kwlist[] = { "p", 0 };
673 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
674 kwlist, convmp, &xx))
676 if ((f = field_niceprime(xx)) == 0)
677 VALERR("bad prime for niceprimefield");
679 return (field_dopywrap(ty, f));
685 static PyTypeObject niceprimefield_pytype_skel = {
686 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
687 "catacomb.NicePrimeField", /* @tp_name@ */
688 sizeof(field_pyobj), /* @tp_basicsize@ */
689 0, /* @tp_itemsize@ */
691 field_pydealloc, /* @tp_dealloc@ */
693 0, /* @tp_getattr@ */
694 0, /* @tp_setattr@ */
695 0, /* @tp_compare@ */
697 0, /* @tp_as_number@ */
698 0, /* @tp_as_sequence@ */
699 0, /* @tp_as_mapping@ */
703 0, /* @tp_getattro@ */
704 0, /* @tp_setattro@ */
705 0, /* @tp_as_buffer@ */
706 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
710 "Nice prime fields.",
712 0, /* @tp_traverse@ */
714 field_pyrichcompare, /* @tp_richcompare@ */
715 0, /* @tp_weaklistoffset@ */
717 0, /* @tp_iternexr@ */
718 0, /* @tp_methods@ */
719 0, /* @tp_members@ */
723 0, /* @tp_descr_get@ */
724 0, /* @tp_descr_set@ */
725 0, /* @tp_dictoffset@ */
727 PyType_GenericAlloc, /* @tp_alloc@ */
728 niceprimefield_pynew, /* @tp_new@ */
729 _PyObject_Del, /* @tp_free@ */
733 /*----- Binary fields -----------------------------------------------------*/
735 static PyObject *bfget_m(PyObject *me, void *hunoz)
736 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
738 static PyGetSetDef binfield_pygetset[] = {
739 #define GETSETNAME(op, name) bf##op##_##name
740 GET (m, "F.m -> field polynomial degree")
745 static PyTypeObject binfield_pytype_skel = {
746 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
747 "catacomb.BinField", /* @tp_name@ */
748 sizeof(field_pyobj), /* @tp_basicsize@ */
749 0, /* @tp_itemsize@ */
751 field_pydealloc, /* @tp_dealloc@ */
753 0, /* @tp_getattr@ */
754 0, /* @tp_setattr@ */
755 0, /* @tp_compare@ */
757 0, /* @tp_as_number@ */
758 0, /* @tp_as_sequence@ */
759 0, /* @tp_as_mapping@ */
763 0, /* @tp_getattro@ */
764 0, /* @tp_setattro@ */
765 0, /* @tp_as_buffer@ */
766 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
770 "Binary fields. Abstract class.",
772 0, /* @tp_traverse@ */
774 field_pyrichcompare, /* @tp_richcompare@ */
775 0, /* @tp_weaklistoffset@ */
777 0, /* @tp_iternexr@ */
778 0, /* @tp_methods@ */
779 0, /* @tp_members@ */
780 binfield_pygetset, /* @tp_getset@ */
783 0, /* @tp_descr_get@ */
784 0, /* @tp_descr_set@ */
785 0, /* @tp_dictoffset@ */
787 PyType_GenericAlloc, /* @tp_alloc@ */
788 abstract_pynew, /* @tp_new@ */
789 _PyObject_Del, /* @tp_free@ */
793 static PyObject *binpolyfield_pynew(PyTypeObject *ty,
794 PyObject *arg, PyObject *kw)
798 char *kwlist[] = { "p", 0 };
800 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield", kwlist,
803 if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
805 return (field_dopywrap(ty, f));
811 static PyGetSetDef binpolyfield_pygetset[] = {
812 #define GETSETNAME(op, name) pf##op##_##name
813 GET (p, "F.p -> field polynomial")
818 static PyTypeObject binpolyfield_pytype_skel = {
819 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
820 "catacomb.BinPolyField", /* @tp_name@ */
821 sizeof(field_pyobj), /* @tp_basicsize@ */
822 0, /* @tp_itemsize@ */
824 field_pydealloc, /* @tp_dealloc@ */
826 0, /* @tp_getattr@ */
827 0, /* @tp_setattr@ */
828 0, /* @tp_compare@ */
830 0, /* @tp_as_number@ */
831 0, /* @tp_as_sequence@ */
832 0, /* @tp_as_mapping@ */
836 0, /* @tp_getattro@ */
837 0, /* @tp_setattro@ */
838 0, /* @tp_as_buffer@ */
839 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
843 "Binary fields with polynomial basis representation.",
845 0, /* @tp_traverse@ */
847 field_pyrichcompare, /* @tp_richcompare@ */
848 0, /* @tp_weaklistoffset@ */
850 0, /* @tp_iternexr@ */
851 0, /* @tp_methods@ */
852 0, /* @tp_members@ */
853 binpolyfield_pygetset, /* @tp_getset@ */
856 0, /* @tp_descr_get@ */
857 0, /* @tp_descr_set@ */
858 0, /* @tp_dictoffset@ */
860 PyType_GenericAlloc, /* @tp_alloc@ */
861 binpolyfield_pynew, /* @tp_new@ */
862 _PyObject_Del, /* @tp_free@ */
866 static PyObject *binnormfield_pynew(PyTypeObject *ty,
867 PyObject *arg, PyObject *kw)
871 char *kwlist[] = { "p", "beta", 0 };
873 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
874 kwlist, convgf, &xx, convgf, &yy))
876 if ((f = field_binnorm(xx, yy)) == 0) VALERR("bad args for binnormfield");
877 MP_DROP(xx); MP_DROP(yy);
878 return (field_dopywrap(ty, f));
880 mp_drop(xx); mp_drop(yy);
884 static PyObject *bnfget_beta(PyObject *me, void *hunoz)
886 fctx_binnorm *fc = (fctx_binnorm *)FIELD_F(me);
887 return (gf_pywrap(MP_COPY(fc->ntop.r[fc->ntop.n - 1])));
890 static PyGetSetDef binnormfield_pygetset[] = {
891 #define GETSETNAME(op, name) pf##op##_##name
892 GET (p, "F.p -> field polynomial")
894 #define GETSETNAME(op, name) bnf##op##_##name
895 GET (beta, "F.beta -> conversion factor")
900 static PyTypeObject binnormfield_pytype_skel = {
901 PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */
902 "catacomb.BinNormField", /* @tp_name@ */
903 sizeof(field_pyobj), /* @tp_basicsize@ */
904 0, /* @tp_itemsize@ */
906 field_pydealloc, /* @tp_dealloc@ */
908 0, /* @tp_getattr@ */
909 0, /* @tp_setattr@ */
910 0, /* @tp_compare@ */
912 0, /* @tp_as_number@ */
913 0, /* @tp_as_sequence@ */
914 0, /* @tp_as_mapping@ */
918 0, /* @tp_getattro@ */
919 0, /* @tp_setattro@ */
920 0, /* @tp_as_buffer@ */
921 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
925 "Binary fields with normal basis representation.",
927 0, /* @tp_traverse@ */
929 field_pyrichcompare, /* @tp_richcompare@ */
930 0, /* @tp_weaklistoffset@ */
932 0, /* @tp_iternexr@ */
933 0, /* @tp_methods@ */
934 0, /* @tp_members@ */
935 binnormfield_pygetset, /* @tp_getset@ */
938 0, /* @tp_descr_get@ */
939 0, /* @tp_descr_set@ */
940 0, /* @tp_dictoffset@ */
942 PyType_GenericAlloc, /* @tp_alloc@ */
943 binnormfield_pynew, /* @tp_new@ */
944 _PyObject_Del, /* @tp_free@ */
948 /*----- Setup -------------------------------------------------------------*/
950 static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
957 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
961 if ((f = field_parse(&qd)) == 0)
963 rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
968 static PyMethodDef methods[] = {
969 #define METHNAME(func) meth_##func
970 METH (_Field_parse, "parse(STR) -> F, REST")
975 void field_pyinit(void)
978 INITTYPE(field, type);
979 INITTYPE(primefield, field);
980 INITTYPE(niceprimefield, primefield);
981 INITTYPE(binfield, field);
982 INITTYPE(binpolyfield, binfield);
983 INITTYPE(binnormfield, binfield);
987 void field_pyinsert(PyObject *mod)
989 INSERT("FE", fe_pytype);
990 INSERT("Field", field_pytype);
991 INSERT("PrimeField", primefield_pytype);
992 INSERT("NicePrimeField", niceprimefield_pytype);
993 INSERT("BinField", binfield_pytype);
994 INSERT("BinPolyField", binpolyfield_pytype);
995 INSERT("BinNormField", binnormfield_pytype);
998 /*----- That's all, folks -------------------------------------------------*/