5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Various utilities -------------------------------------------------*/
33 PyTypeObject *field_pytype;
34 PyTypeObject *primefield_pytype;
35 PyTypeObject *niceprimefield_pytype;
36 PyTypeObject *binfield_pytype;
37 PyTypeObject *binpolyfield_pytype;
38 PyTypeObject *binnormfield_pytype;
39 PyTypeObject *fe_pytype;
41 static PyObject *fe_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
45 char *kwlist[] = { "x", 0 };
47 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:fe", kwlist, &x))
49 if (FE_PYCHECK(x) && FE_F(x) == FIELD_F(ty)) RETURN_OBJ(x);
50 if ((z = getmp(x)) == 0) return (0);
51 z = F_IN(FIELD_F(ty), z, z);
52 return (fe_pywrap((PyObject *)ty, z));
55 static PyObject *field_dopywrap(PyTypeObject *ty, field *f)
57 field_pyobj *fobj = newtype(ty, 0, f->ops->name);
59 fobj->ty.ht_type.tp_basicsize = sizeof(fe_pyobj);
60 fobj->ty.ht_type.tp_base = fe_pytype;
62 fobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
64 Py_TPFLAGS_CHECKTYPES |
66 fobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
67 fobj->ty.ht_type.tp_free = 0;
68 fobj->ty.ht_type.tp_new = fe_pynew;
69 typeready(&fobj->ty.ht_type);
70 return ((PyObject *)fobj);
73 PyObject *field_pywrap(field *f)
77 if (strcmp(F_NAME(f), "prime") == 0) ty = primefield_pytype;
78 else if (strcmp(F_NAME(f), "niceprime") == 0) ty = niceprimefield_pytype;
79 else if (strcmp(F_NAME(f), "binpoly") == 0) ty = binpolyfield_pytype;
80 else if (strcmp(F_NAME(f), "binnorm") == 0) ty = binnormfield_pytype;
82 return (field_dopywrap(ty, f));
85 field *field_copy(field *f)
87 if (strcmp(F_NAME(f), "prime") == 0)
88 f = field_prime(f->m);
89 else if (strcmp(F_NAME(f), "niceprime") == 0)
90 f = field_niceprime(f->m);
91 else if (strcmp(F_NAME(f), "binpoly") == 0)
92 f = field_binpoly(f->m);
93 else if (strcmp(F_NAME(f), "binnorm") == 0) {
94 fctx_binnorm *fc = (fctx_binnorm *)f;
95 f = field_binnorm(f->m, fc->ntop.r[fc->ntop.n - 1]);
101 PyObject *fe_pywrap(PyObject *fobj, mp *x)
103 fe_pyobj *z = PyObject_New(fe_pyobj, (PyTypeObject *)fobj);
104 z->f = FIELD_F(fobj);
107 return ((PyObject *)z);
110 static mp *tofe(field *f, PyObject *o)
115 if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
116 y = MP_COPY(FE_X(o));
117 } else if ((x = tomp(o)) != 0) {
119 y = MP_COPY(f->zero);
120 else if (MP_EQ(x, MP_ONE))
123 y = F_IN(f, MP_NEW, x);
129 mp *getfe(field *f, PyObject *o)
132 if ((x = tofe(f, o)) == 0) {
133 PyErr_Format(PyExc_TypeError, "can't convert %.100s to fe",
134 o->ob_type->tp_name);
139 /*----- Field elements ----------------------------------------------------*/
141 static int febinop(PyObject *x, PyObject *y,
142 field **f, PyObject **fobj, mp **xx, mp **yy)
144 if (FE_PYCHECK(x)) *fobj = FE_FOBJ(x);
145 else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y);
148 if ((*xx = tofe(*f, x)) == 0)
150 if ((*yy = tofe(*f, y)) == 0) {
157 #define BINOP(name) \
158 static PyObject *fe_py##name(PyObject *x, PyObject *y) { \
162 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL; \
163 zz = ff->ops->name(ff, MP_NEW, xx, yy); \
164 MP_DROP(xx); MP_DROP(yy); \
165 return (fe_pywrap(fobj, zz)); \
172 static PyObject *fe_pydiv(PyObject *x, PyObject *y)
178 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL;
179 if (F_ZEROP(ff, yy)) ZDIVERR("division by zero");
180 yy = F_INV(ff, yy, yy);
181 z = fe_pywrap(fobj, F_MUL(ff, MP_NEW, xx, yy));
183 MP_DROP(xx); MP_DROP(yy);
187 static PyObject *fe_pyexp(PyObject *x, PyObject *y, PyObject *z)
192 if (z != Py_None || !FE_PYCHECK(x) || (yy = tomp(y)) == 0)
194 ff = FE_F(x); xx = FE_X(x); MP_COPY(xx);
195 if (MP_NEGP(yy) && F_ZEROP(ff, xx)) ZDIVERR("division by zero");
196 z = fe_pywrap(FE_FOBJ(x), field_exp(ff, MP_NEW, xx, yy));
198 MP_DROP(xx); MP_DROP(yy);
202 static PyObject *fe_pyneg(PyObject *x)
204 return fe_pywrap(FE_FOBJ(x), FE_F(x)->ops->neg(FE_F(x), MP_NEW, FE_X(x)));
207 static PyObject *fe_pyid(PyObject *x) { RETURN_OBJ(x); }
209 static int fe_pynonzerop(PyObject *x) { return !F_ZEROP(FE_F(x), FE_X(x)); }
211 static PyObject *fe_pyrichcompare(PyObject *x, PyObject *y, int op)
219 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL;
221 case Py_EQ: b = MP_EQ(xx, yy); break;
222 case Py_NE: b = !MP_EQ(xx, yy); break;
223 default: TYERR("field elements are unordered");
227 MP_DROP(xx); MP_DROP(yy);
231 static long fe_pyhash(PyObject *me)
232 { return (mphash(FE_X(me))); }
234 static int fe_pycoerce(PyObject **x, PyObject **y)
238 if (FE_PYCHECK(*y)) {
239 if (FE_F(*x) != FE_F(*y) && !field_samep(FE_F(*x), FE_F(*y)))
240 TYERR("field mismatch");
241 Py_INCREF(*x); Py_INCREF(*y);
244 if ((z = tofe(FE_F(*x), *y)) != 0) {
246 *y = fe_pywrap(FE_FOBJ(*x), z);
255 static PyObject *fe_pyint(PyObject *x)
259 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
260 if (!mp_tolong_checked(xx, &l, 0)) rc = PyInt_FromLong(l);
261 else rc = mp_topylong(xx);
266 static PyObject *fe_pylong(PyObject *x)
268 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
269 PyObject *rc = mp_topylong(xx);
274 #define BASEOP(name, radix, pre) \
275 static PyObject *fe_py##name(PyObject *x) { \
276 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x)); \
277 PyObject *rc = mp_topystring(FE_X(x), radix, 0, pre, 0); \
282 BASEOP(hex, 16, "0x");
285 static void fe_pydealloc(PyObject *me)
287 Py_DECREF(FE_FOBJ(me));
292 #define UNOP(name, check) \
293 static PyObject *femeth_##name(PyObject *me, PyObject *arg) { \
294 field *f = FE_F(me); \
296 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
297 if (!f->ops->name) TYERR(#name " not supported for this field"); \
299 x = f->ops->name(f, MP_NEW, x); \
300 if (!x) RETURN_NONE; \
301 return (fe_pywrap(FE_FOBJ(me), x)); \
305 UNOP(inv, if (F_ZEROP(f, x)) ZDIVERR("division by zero"); )
315 static PyObject *feget_field(PyObject *me, void *hunoz)
316 { RETURN_OBJ(FE_FOBJ(me)); }
318 static PyObject *feget_value(PyObject *me, void *hunoz)
320 mp *x = F_OUT(FE_F(me), MP_NEW, FE_X(me));
321 if (F_TYPE(FE_F(me)) == FTY_BINARY)
322 return (gf_pywrap(x));
324 return (mp_pywrap(x));
327 static PyObject *feget__value(PyObject *me, void *hunoz)
331 if (F_TYPE(FE_F(me)) == FTY_BINARY)
332 return (gf_pywrap(x));
334 return (mp_pywrap(x));
337 static PyGetSetDef fe_pygetset[] = {
338 #define GETSETNAME(op, name) fe##op##_##name
339 GET (field, "X.field -> field containing X")
340 GET (value, "X.value -> `natural' MP/GF representation of X")
341 GET (_value, "X._value -> internal MP/GF representation of X")
346 static PyMethodDef fe_pymethods[] = {
347 #define METHNAME(func) femeth_##func
348 METH (inv, "X.inv() -> X^{-1}")
349 METH (sqr, "X.sqr() -> X^2")
350 METH (sqrt, "X.sqrt() -> sqrt(X)")
351 METH (quadsolve, "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
352 METH (dbl, "X.dbl() -> 2 * X (prime only)")
353 METH (tpl, "X.tpl() -> 3 * X (prime only)")
354 METH (qdl, "X.qdl() -> 4 * X (prime only)")
355 METH (hlv, "X.hlv() -> X/2 (prime only)")
360 static PyNumberMethods fe_pynumber = {
361 fe_pyadd, /* @nb_add@ */
362 fe_pysub, /* @nb_subtract@ */
363 fe_pymul, /* @nb_multiply@ */
364 fe_pydiv, /* @nb_divide@ */
365 0, /* @nb_remainder@ */
367 fe_pyexp, /* @nb_power@ */
368 fe_pyneg, /* @nb_negative@ */
369 fe_pyid, /* @nb_positive@ */
370 0, /* @nb_absolute@ */
371 fe_pynonzerop, /* @nb_nonzero@ */
378 fe_pycoerce, /* @nb_coerce@ */
379 fe_pyint, /* @nb_int@ */
380 fe_pylong, /* @nb_long@ */
381 0 /* meaningless */, /* @nb_float@ */
382 fe_pyoct, /* @nb_oct@ */
383 fe_pyhex, /* @nb_hex@ */
385 0, /* @nb_inplace_add@ */
386 0, /* @nb_inplace_subtract@ */
387 0, /* @nb_inplace_multiply@ */
388 0, /* @nb_inplace_divide@ */
389 0, /* @nb_inplace_remainder@ */
390 0, /* @nb_inplace_power@ */
391 0, /* @nb_inplace_lshift@ */
392 0, /* @nb_inplace_rshift@ */
393 0, /* @nb_inplace_and@ */
394 0, /* @nb_inplace_xor@ */
395 0, /* @nb_inplace_or@ */
397 0, /* @nb_floor_divide@ */
398 fe_pydiv, /* @nb_true_divide@ */
399 0, /* @nb_inplace_floor_divide@ */
400 0, /* @nb_inplace_true_divide@ */
403 static PyTypeObject fe_pytype_skel = {
404 PyObject_HEAD_INIT(0) 0, /* Header */
405 "FE", /* @tp_name@ */
406 sizeof(fe_pyobj), /* @tp_basicsize@ */
407 0, /* @tp_itemsize@ */
409 fe_pydealloc, /* @tp_dealloc@ */
411 0, /* @tp_getattr@ */
412 0, /* @tp_setattr@ */
413 0, /* @tp_compare@ */
415 &fe_pynumber, /* @tp_as_number@ */
416 0, /* @tp_as_sequence@ */
417 0, /* @tp_as_mapping@ */
418 fe_pyhash, /* @tp_hash@ */
420 fe_pyhex, /* @tp_str@ */
421 0, /* @tp_getattro@ */
422 0, /* @tp_setattro@ */
423 0, /* @tp_as_buffer@ */
424 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
425 Py_TPFLAGS_CHECKTYPES |
429 "Finite field elements, abstract base class.",
431 0, /* @tp_traverse@ */
433 fe_pyrichcompare, /* @tp_richcompare@ */
434 0, /* @tp_weaklistoffset@ */
436 0, /* @tp_iternext@ */
437 fe_pymethods, /* @tp_methods@ */
438 0, /* @tp_members@ */
439 fe_pygetset, /* @tp_getset@ */
442 0, /* @tp_descr_get@ */
443 0, /* @tp_descr_set@ */
444 0, /* @tp_dictoffset@ */
446 PyType_GenericAlloc, /* @tp_alloc@ */
447 abstract_pynew, /* @tp_new@ */
452 /*----- Fields ------------------------------------------------------------*/
454 static PyObject *field_pyrichcompare(PyObject *x, PyObject *y, int op)
456 int b = field_samep(FIELD_F(x), FIELD_F(y));
460 default: TYERR("can't order fields");
467 static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
469 char *kwlist[] = { "rng", 0 };
470 grand *r = &rand_global;
472 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
475 return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
478 static PyObject *fmeth__adopt(PyObject *me, PyObject *arg)
481 if (!PyArg_ParseTuple(arg, "O&:_adopt", convmp, &xx)) return (0);
482 return (fe_pywrap(me, xx));
485 static void field_pydealloc(PyObject *me)
487 F_DESTROY(FIELD_F(me));
488 PyType_Type.tp_dealloc(me);
491 static PyObject *fget_zero(PyObject *me, void *hunoz)
492 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->zero))); }
494 static PyObject *fget_one(PyObject *me, void *hunoz)
495 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->one))); }
497 static PyObject *fget_q(PyObject *me, void *hunoz)
498 { return (mp_pywrap(MP_COPY(FIELD_F(me)->q))); }
500 static PyObject *fget_nbits(PyObject *me, void *hunoz)
501 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
503 static PyObject *fget_noctets(PyObject *me, void *hunoz)
504 { return (PyInt_FromLong(FIELD_F(me)->noctets)); }
506 static PyObject *fget_name(PyObject *me, void *hunoz)
507 { return (PyString_FromString(F_NAME(FIELD_F(me)))); }
509 static PyObject *fget_type(PyObject *me, void *hunoz)
510 { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
512 static PyGetSetDef field_pygetset[] = {
513 #define GETSETNAME(op, name) f##op##_##name
514 GET (zero, "F.zero -> field additive identity")
515 GET (one, "F.one -> field multiplicative identity")
516 GET (q, "F.q -> number of elements in field")
517 GET (nbits, "F.nbits -> bits needed to represent element")
518 GET (noctets, "F.noctets -> octetss needed to represent element")
519 GET (name, "F.name -> name of this kind of field")
520 GET (type, "F.type -> type code of this kind of field")
525 static PyMethodDef field_pymethods[] = {
526 #define METHNAME(name) fmeth_##name
527 METH (_adopt, "F._adopt(X) -> FE")
528 KWMETH(rand, "F.rand([rng = rand]) -> FE, uniformly distributed")
533 static PyTypeObject field_pytype_skel = {
534 PyObject_HEAD_INIT(0) 0, /* Header */
535 "Field", /* @tp_name@ */
536 sizeof(field_pyobj), /* @tp_basicsize@ */
537 0, /* @tp_itemsize@ */
539 field_pydealloc, /* @tp_dealloc@ */
541 0, /* @tp_getattr@ */
542 0, /* @tp_setattr@ */
543 0, /* @tp_compare@ */
545 0, /* @tp_as_number@ */
546 0, /* @tp_as_sequence@ */
547 0, /* @tp_as_mapping@ */
551 0, /* @tp_getattro@ */
552 0, /* @tp_setattro@ */
553 0, /* @tp_as_buffer@ */
554 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
558 "An abstract field. This is an abstract type.",
560 0, /* @tp_traverse@ */
562 field_pyrichcompare, /* @tp_richcompare@ */
563 0, /* @tp_weaklistoffset@ */
565 0, /* @tp_iternext@ */
566 field_pymethods, /* @tp_methods@ */
567 0, /* @tp_members@ */
568 field_pygetset, /* @tp_getset@ */
571 0, /* @tp_descr_get@ */
572 0, /* @tp_descr_set@ */
573 0, /* @tp_dictoffset@ */
575 PyType_GenericAlloc, /* @tp_alloc@ */
576 abstract_pynew, /* @tp_new@ */
581 /*----- Prime fields ------------------------------------------------------*/
583 static PyObject *primefield_pynew(PyTypeObject *ty,
584 PyObject *arg, PyObject *kw)
588 char *kwlist[] = { "p", 0 };
590 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:primefield", kwlist,
593 if ((f = field_prime(xx)) == 0)
594 VALERR("bad prime for primefield");
596 return (field_dopywrap(ty, f));
602 static PyObject *pfget_p(PyObject *me, void *hunoz)
603 { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
605 static PyGetSetDef primefield_pygetset[] = {
606 #define GETSETNAME(op, name) pf##op##_##name
607 GET (p, "F.p -> prime field characteristic")
611 static PyTypeObject primefield_pytype_skel = {
612 PyObject_HEAD_INIT(0) 0, /* Header */
613 "PrimeField", /* @tp_name@ */
614 sizeof(field_pyobj), /* @tp_basicsize@ */
615 0, /* @tp_itemsize@ */
617 field_pydealloc, /* @tp_dealloc@ */
619 0, /* @tp_getattr@ */
620 0, /* @tp_setattr@ */
621 0, /* @tp_compare@ */
623 0, /* @tp_as_number@ */
624 0, /* @tp_as_sequence@ */
625 0, /* @tp_as_mapping@ */
629 0, /* @tp_getattro@ */
630 0, /* @tp_setattro@ */
631 0, /* @tp_as_buffer@ */
632 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
638 0, /* @tp_traverse@ */
640 field_pyrichcompare, /* @tp_richcompare@ */
641 0, /* @tp_weaklistoffset@ */
643 0, /* @tp_iternext@ */
644 0, /* @tp_methods@ */
645 0, /* @tp_members@ */
646 primefield_pygetset, /* @tp_getset@ */
649 0, /* @tp_descr_get@ */
650 0, /* @tp_descr_set@ */
651 0, /* @tp_dictoffset@ */
653 PyType_GenericAlloc, /* @tp_alloc@ */
654 primefield_pynew, /* @tp_new@ */
659 static PyObject *niceprimefield_pynew(PyTypeObject *ty,
660 PyObject *arg, PyObject *kw)
664 char *kwlist[] = { "p", 0 };
666 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
667 kwlist, convmp, &xx))
669 if ((f = field_niceprime(xx)) == 0)
670 VALERR("bad prime for niceprimefield");
672 return (field_dopywrap(ty, f));
678 static PyTypeObject niceprimefield_pytype_skel = {
679 PyObject_HEAD_INIT(0) 0, /* Header */
680 "NicePrimeField", /* @tp_name@ */
681 sizeof(field_pyobj), /* @tp_basicsize@ */
682 0, /* @tp_itemsize@ */
684 field_pydealloc, /* @tp_dealloc@ */
686 0, /* @tp_getattr@ */
687 0, /* @tp_setattr@ */
688 0, /* @tp_compare@ */
690 0, /* @tp_as_number@ */
691 0, /* @tp_as_sequence@ */
692 0, /* @tp_as_mapping@ */
696 0, /* @tp_getattro@ */
697 0, /* @tp_setattro@ */
698 0, /* @tp_as_buffer@ */
699 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
703 "Nice prime fields.",
705 0, /* @tp_traverse@ */
707 field_pyrichcompare, /* @tp_richcompare@ */
708 0, /* @tp_weaklistoffset@ */
710 0, /* @tp_iternext@ */
711 0, /* @tp_methods@ */
712 0, /* @tp_members@ */
716 0, /* @tp_descr_get@ */
717 0, /* @tp_descr_set@ */
718 0, /* @tp_dictoffset@ */
720 PyType_GenericAlloc, /* @tp_alloc@ */
721 niceprimefield_pynew, /* @tp_new@ */
726 /*----- Binary fields -----------------------------------------------------*/
728 static PyObject *bfget_m(PyObject *me, void *hunoz)
729 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
731 static PyGetSetDef binfield_pygetset[] = {
732 #define GETSETNAME(op, name) bf##op##_##name
733 GET (m, "F.m -> field polynomial degree")
738 static PyTypeObject binfield_pytype_skel = {
739 PyObject_HEAD_INIT(0) 0, /* Header */
740 "BinField", /* @tp_name@ */
741 sizeof(field_pyobj), /* @tp_basicsize@ */
742 0, /* @tp_itemsize@ */
744 field_pydealloc, /* @tp_dealloc@ */
746 0, /* @tp_getattr@ */
747 0, /* @tp_setattr@ */
748 0, /* @tp_compare@ */
750 0, /* @tp_as_number@ */
751 0, /* @tp_as_sequence@ */
752 0, /* @tp_as_mapping@ */
756 0, /* @tp_getattro@ */
757 0, /* @tp_setattro@ */
758 0, /* @tp_as_buffer@ */
759 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
763 "Binary fields. Abstract class.",
765 0, /* @tp_traverse@ */
767 field_pyrichcompare, /* @tp_richcompare@ */
768 0, /* @tp_weaklistoffset@ */
770 0, /* @tp_iternext@ */
771 0, /* @tp_methods@ */
772 0, /* @tp_members@ */
773 binfield_pygetset, /* @tp_getset@ */
776 0, /* @tp_descr_get@ */
777 0, /* @tp_descr_set@ */
778 0, /* @tp_dictoffset@ */
780 PyType_GenericAlloc, /* @tp_alloc@ */
781 abstract_pynew, /* @tp_new@ */
786 static PyObject *binpolyfield_pynew(PyTypeObject *ty,
787 PyObject *arg, PyObject *kw)
791 char *kwlist[] = { "p", 0 };
793 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield", kwlist,
796 if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
798 return (field_dopywrap(ty, f));
804 static PyObject *bfget_p(PyObject *me, void *hunoz)
805 { return (gf_pywrap(MP_COPY(FIELD_F(me)->m))); }
807 static PyGetSetDef binpolyfield_pygetset[] = {
808 #define GETSETNAME(op, name) bf##op##_##name
809 GET (p, "F.p -> field polynomial")
814 static PyTypeObject binpolyfield_pytype_skel = {
815 PyObject_HEAD_INIT(0) 0, /* Header */
816 "BinPolyField", /* @tp_name@ */
817 sizeof(field_pyobj), /* @tp_basicsize@ */
818 0, /* @tp_itemsize@ */
820 field_pydealloc, /* @tp_dealloc@ */
822 0, /* @tp_getattr@ */
823 0, /* @tp_setattr@ */
824 0, /* @tp_compare@ */
826 0, /* @tp_as_number@ */
827 0, /* @tp_as_sequence@ */
828 0, /* @tp_as_mapping@ */
832 0, /* @tp_getattro@ */
833 0, /* @tp_setattro@ */
834 0, /* @tp_as_buffer@ */
835 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
839 "Binary fields with polynomial basis representation.",
841 0, /* @tp_traverse@ */
843 field_pyrichcompare, /* @tp_richcompare@ */
844 0, /* @tp_weaklistoffset@ */
846 0, /* @tp_iternext@ */
847 0, /* @tp_methods@ */
848 0, /* @tp_members@ */
849 binpolyfield_pygetset, /* @tp_getset@ */
852 0, /* @tp_descr_get@ */
853 0, /* @tp_descr_set@ */
854 0, /* @tp_dictoffset@ */
856 PyType_GenericAlloc, /* @tp_alloc@ */
857 binpolyfield_pynew, /* @tp_new@ */
862 static PyObject *binnormfield_pynew(PyTypeObject *ty,
863 PyObject *arg, PyObject *kw)
867 char *kwlist[] = { "p", "beta", 0 };
869 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
870 kwlist, convgf, &xx, convgf, &yy))
872 if ((f = field_binnorm(xx, yy)) == 0) VALERR("bad args for binnormfield");
873 MP_DROP(xx); MP_DROP(yy);
874 return (field_dopywrap(ty, f));
876 mp_drop(xx); mp_drop(yy);
880 static PyObject *bnfget_beta(PyObject *me, void *hunoz)
882 fctx_binnorm *fc = (fctx_binnorm *)FIELD_F(me);
883 return (gf_pywrap(MP_COPY(fc->ntop.r[fc->ntop.n - 1])));
886 static PyGetSetDef binnormfield_pygetset[] = {
887 #define GETSETNAME(op, name) bf##op##_##name
888 GET (p, "F.p -> field polynomial")
890 #define GETSETNAME(op, name) bnf##op##_##name
891 GET (beta, "F.beta -> conversion factor")
896 static PyTypeObject binnormfield_pytype_skel = {
897 PyObject_HEAD_INIT(0) 0, /* Header */
898 "BinNormField", /* @tp_name@ */
899 sizeof(field_pyobj), /* @tp_basicsize@ */
900 0, /* @tp_itemsize@ */
902 field_pydealloc, /* @tp_dealloc@ */
904 0, /* @tp_getattr@ */
905 0, /* @tp_setattr@ */
906 0, /* @tp_compare@ */
908 0, /* @tp_as_number@ */
909 0, /* @tp_as_sequence@ */
910 0, /* @tp_as_mapping@ */
914 0, /* @tp_getattro@ */
915 0, /* @tp_setattro@ */
916 0, /* @tp_as_buffer@ */
917 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
921 "Binary fields with normal basis representation.",
923 0, /* @tp_traverse@ */
925 field_pyrichcompare, /* @tp_richcompare@ */
926 0, /* @tp_weaklistoffset@ */
928 0, /* @tp_iternext@ */
929 0, /* @tp_methods@ */
930 0, /* @tp_members@ */
931 binnormfield_pygetset, /* @tp_getset@ */
934 0, /* @tp_descr_get@ */
935 0, /* @tp_descr_set@ */
936 0, /* @tp_dictoffset@ */
938 PyType_GenericAlloc, /* @tp_alloc@ */
939 binnormfield_pynew, /* @tp_new@ */
944 /*----- Setup -------------------------------------------------------------*/
946 static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
953 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
957 if ((f = field_parse(&qd)) == 0)
959 rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
964 static PyMethodDef methods[] = {
965 #define METHNAME(func) meth_##func
966 METH (_Field_parse, "parse(STR) -> F, REST")
971 void field_pyinit(void)
974 INITTYPE(field, type);
975 INITTYPE(primefield, field);
976 INITTYPE(niceprimefield, primefield);
977 INITTYPE(binfield, field);
978 INITTYPE(binpolyfield, binfield);
979 INITTYPE(binnormfield, binfield);
983 void field_pyinsert(PyObject *mod)
985 INSERT("FE", fe_pytype);
986 INSERT("Field", field_pytype);
987 INSERT("PrimeField", primefield_pytype);
988 INSERT("NicePrimeField", niceprimefield_pytype);
989 INSERT("BinField", binfield_pytype);
990 INSERT("BinPolyField", binpolyfield_pytype);
991 INSERT("BinNormField", binnormfield_pytype);
994 /*----- That's all, folks -------------------------------------------------*/