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
)
233 size_t sz
= FE_F(me
)->noctets
;
234 uint32 h
= 0xe0c127ca + FE_F(me
)->ops
->ty
;
235 octet
*p
= xmalloc(sz
);
236 mp_storeb(FE_X(me
), p
, sz
);
237 h
= unihash_hash(&unihash_global
, h
, p
, sz
);
239 return (h
% LONG_MAX
);
242 static int fe_pycoerce(PyObject
**x
, PyObject
**y
)
246 if (FE_PYCHECK(*y
)) {
247 if (FE_F(*x
) != FE_F(*y
) && !field_samep(FE_F(*x
), FE_F(*y
)))
248 TYERR("field mismatch");
249 Py_INCREF(*x
); Py_INCREF(*y
);
252 if ((z
= tofe(FE_F(*x
), *y
)) != 0) {
254 *y
= fe_pywrap(FE_FOBJ(*x
), z
);
263 static PyObject
*fe_pyint(PyObject
*x
)
266 mp
*xx
= F_OUT(FE_F(x
), MP_NEW
, FE_X(x
));
267 if (mp_tolong_checked(xx
, &l
)) { MP_DROP(xx
); return (0); }
269 return (PyInt_FromLong(l
));
272 static PyObject
*fe_pylong(PyObject
*x
)
274 mp
*xx
= F_OUT(FE_F(x
), MP_NEW
, FE_X(x
));
275 PyObject
*rc
= mp_topylong(xx
);
280 #define BASEOP(name, radix, pre) \
281 static PyObject *fe_py##name(PyObject *x) { \
282 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x)); \
283 PyObject *rc = mp_topystring(FE_X(x), radix, 0, pre, 0); \
288 BASEOP(hex
, 16, "0x");
291 static void fe_pydealloc(PyObject
*me
)
293 Py_DECREF(FE_FOBJ(me
));
298 #define UNOP(name, check) \
299 static PyObject *femeth_##name(PyObject *me, PyObject *arg) { \
300 field *f = FE_F(me); \
302 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
303 if (!f->ops->name) TYERR(#name " not supported for this field"); \
305 x = f->ops->name(f, MP_NEW, x); \
306 if (!x) RETURN_NONE; \
307 return (fe_pywrap(FE_FOBJ(me), x)); \
311 UNOP(inv
, if (F_ZEROP(f
, x
)) ZDIVERR("division by zero"); )
321 static PyObject
*feget_field(PyObject
*me
, void *hunoz
)
322 { RETURN_OBJ(FE_FOBJ(me
)); }
324 static PyObject
*feget_value(PyObject
*me
, void *hunoz
)
326 mp
*x
= F_OUT(FE_F(me
), MP_NEW
, FE_X(me
));
327 if (F_TYPE(FE_F(me
)) == FTY_BINARY
)
328 return (gf_pywrap(x
));
330 return (mp_pywrap(x
));
333 static PyObject
*feget__value(PyObject
*me
, void *hunoz
)
337 if (F_TYPE(FE_F(me
)) == FTY_BINARY
)
338 return (gf_pywrap(x
));
340 return (mp_pywrap(x
));
343 static PyGetSetDef fe_pygetset
[] = {
344 #define GETSETNAME(op, name) fe##op##_##name
345 GET (field
, "X.field -> field containing X")
346 GET (value
, "X.value -> `natural' integer representation of X")
347 GET (_value
, "X._value -> internal integer representation of X")
352 static PyMethodDef fe_pymethods
[] = {
353 #define METHNAME(func) femeth_##func
354 METH (inv
, "X.inv() -> X^{-1}")
355 METH (sqr
, "X.sqr() -> X^2")
356 METH (sqrt
, "X.sqrt() -> sqrt(X)")
357 METH (quadsolve
, "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
358 METH (dbl
, "X.dbl() -> 2 * X (prime only)")
359 METH (tpl
, "X.tpl() -> 3 * X (prime only)")
360 METH (qdl
, "X.qdl() -> 4 * X (prime only)")
361 METH (hlv
, "X.hlv() -> X/2 (prime only)")
366 static PyNumberMethods fe_pynumber
= {
367 fe_pyadd
, /* @nb_add@ */
368 fe_pysub
, /* @nb_subtract@ */
369 fe_pymul
, /* @nb_multiply@ */
370 fe_pydiv
, /* @nb_divide@ */
371 0, /* @nb_remainder@ */
373 fe_pyexp
, /* @nb_power@ */
374 fe_pyneg
, /* @nb_negative@ */
375 fe_pyid
, /* @nb_positive@ */
376 0, /* @nb_absolute@ */
377 fe_pynonzerop
, /* @nb_nonzero@ */
384 fe_pycoerce
, /* @nb_coerce@ */
385 fe_pyint
, /* @nb_int@ */
386 fe_pylong
, /* @nb_long@ */
387 0 /* meaningless */, /* @nb_float@ */
388 fe_pyoct
, /* @nb_oct@ */
389 fe_pyhex
, /* @nb_hex@ */
391 0, /* @nb_inplace_add@ */
392 0, /* @nb_inplace_subtract@ */
393 0, /* @nb_inplace_multiply@ */
394 0, /* @nb_inplace_divide@ */
395 0, /* @nb_inplace_remainder@ */
396 0, /* @nb_inplace_power@ */
397 0, /* @nb_inplace_lshift@ */
398 0, /* @nb_inplace_rshift@ */
399 0, /* @nb_inplace_and@ */
400 0, /* @nb_inplace_xor@ */
401 0, /* @nb_inplace_or@ */
403 0, /* @nb_floor_divide@ */
404 fe_pydiv
, /* @nb_true_divide@ */
405 0, /* @nb_inplace_floor_divide@ */
406 0, /* @nb_inplace_true_divide@ */
409 static PyTypeObject fe_pytype_skel
= {
410 PyObject_HEAD_INIT(0) 0, /* Header */
411 "FE", /* @tp_name@ */
412 sizeof(fe_pyobj
), /* @tp_basicsize@ */
413 0, /* @tp_itemsize@ */
415 fe_pydealloc
, /* @tp_dealloc@ */
417 0, /* @tp_getattr@ */
418 0, /* @tp_setattr@ */
419 0, /* @tp_compare@ */
421 &fe_pynumber
, /* @tp_as_number@ */
422 0, /* @tp_as_sequence@ */
423 0, /* @tp_as_mapping@ */
424 fe_pyhash
, /* @tp_hash@ */
426 fe_pyhex
, /* @tp_str@ */
427 0, /* @tp_getattro@ */
428 0, /* @tp_setattro@ */
429 0, /* @tp_as_buffer@ */
430 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
431 Py_TPFLAGS_CHECKTYPES
|
435 "Finite field elements, abstract base class.",
437 0, /* @tp_traverse@ */
439 fe_pyrichcompare
, /* @tp_richcompare@ */
440 0, /* @tp_weaklistoffset@ */
442 0, /* @tp_iternext@ */
443 fe_pymethods
, /* @tp_methods@ */
444 0, /* @tp_members@ */
445 fe_pygetset
, /* @tp_getset@ */
448 0, /* @tp_descr_get@ */
449 0, /* @tp_descr_set@ */
450 0, /* @tp_dictoffset@ */
452 PyType_GenericAlloc
, /* @tp_alloc@ */
453 abstract_pynew
, /* @tp_new@ */
458 /*----- Fields ------------------------------------------------------------*/
460 static PyObject
*field_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
462 int b
= field_samep(FIELD_F(x
), FIELD_F(y
));
466 default: TYERR("can't order fields");
473 static PyObject
*fmeth_rand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
475 char *kwlist
[] = { "rng", 0 };
476 grand
*r
= &rand_global
;
478 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:rand", kwlist
,
481 return (fe_pywrap(me
, F_RAND(FIELD_F(me
), MP_NEW
, r
)));
484 static PyObject
*fmeth__adopt(PyObject
*me
, PyObject
*arg
)
487 if (!PyArg_ParseTuple(arg
, "O&:_adopt", convmp
, &xx
)) return (0);
488 return (fe_pywrap(me
, xx
));
491 static void field_pydealloc(PyObject
*me
)
493 F_DESTROY(FIELD_F(me
));
494 PyType_Type
.tp_dealloc(me
);
497 static PyObject
*fget_zero(PyObject
*me
, void *hunoz
)
498 { return (fe_pywrap(me
, MP_COPY(FIELD_F(me
)->zero
))); }
500 static PyObject
*fget_one(PyObject
*me
, void *hunoz
)
501 { return (fe_pywrap(me
, MP_COPY(FIELD_F(me
)->one
))); }
503 static PyObject
*fget_q(PyObject
*me
, void *hunoz
)
504 { return (mp_pywrap(MP_COPY(FIELD_F(me
)->q
))); }
506 static PyObject
*fget_nbits(PyObject
*me
, void *hunoz
)
507 { return (PyInt_FromLong(FIELD_F(me
)->nbits
)); }
509 static PyObject
*fget_noctets(PyObject
*me
, void *hunoz
)
510 { return (PyInt_FromLong(FIELD_F(me
)->noctets
)); }
512 static PyObject
*fget_name(PyObject
*me
, void *hunoz
)
513 { return (PyString_FromString(F_NAME(FIELD_F(me
)))); }
515 static PyObject
*fget_type(PyObject
*me
, void *hunoz
)
516 { return (PyInt_FromLong(F_TYPE(FIELD_F(me
)))); }
518 static PyGetSetDef field_pygetset
[] = {
519 #define GETSETNAME(op, name) f##op##_##name
520 GET (zero
, "F.zero -> field additive identity")
521 GET (one
, "F.one -> field multiplicative identity")
522 GET (q
, "F.q -> number of elements in field")
523 GET (nbits
, "F.nbits -> bits needed to represent element")
524 GET (noctets
, "F.noctets -> octetss needed to represent element")
525 GET (name
, "F.name -> name of this kind of field")
526 GET (type
, "F.type -> type code of this kind of field")
531 static PyMethodDef field_pymethods
[] = {
532 #define METHNAME(name) fmeth_##name
533 METH (_adopt
, "F._adopt(X) -> FE")
534 KWMETH(rand
, "F.rand(rng = rand) -> FE, uniformly distributed")
539 static PyTypeObject field_pytype_skel
= {
540 PyObject_HEAD_INIT(0) 0, /* Header */
541 "Field", /* @tp_name@ */
542 sizeof(field_pyobj
), /* @tp_basicsize@ */
543 0, /* @tp_itemsize@ */
545 field_pydealloc
, /* @tp_dealloc@ */
547 0, /* @tp_getattr@ */
548 0, /* @tp_setattr@ */
549 0, /* @tp_compare@ */
551 0, /* @tp_as_number@ */
552 0, /* @tp_as_sequence@ */
553 0, /* @tp_as_mapping@ */
557 0, /* @tp_getattro@ */
558 0, /* @tp_setattro@ */
559 0, /* @tp_as_buffer@ */
560 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
564 "An abstract field. This is an abstract type.",
566 0, /* @tp_traverse@ */
568 field_pyrichcompare
, /* @tp_richcompare@ */
569 0, /* @tp_weaklistoffset@ */
571 0, /* @tp_iternext@ */
572 field_pymethods
, /* @tp_methods@ */
573 0, /* @tp_members@ */
574 field_pygetset
, /* @tp_getset@ */
577 0, /* @tp_descr_get@ */
578 0, /* @tp_descr_set@ */
579 0, /* @tp_dictoffset@ */
581 PyType_GenericAlloc
, /* @tp_alloc@ */
582 abstract_pynew
, /* @tp_new@ */
587 /*----- Prime fields ------------------------------------------------------*/
589 static PyObject
*primefield_pynew(PyTypeObject
*ty
,
590 PyObject
*arg
, PyObject
*kw
)
594 char *kwlist
[] = { "p", 0 };
596 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:primefield", kwlist
,
599 if ((f
= field_prime(xx
)) == 0)
600 VALERR("bad prime for primefield");
602 return (field_dopywrap(ty
, f
));
608 static PyObject
*pfget_p(PyObject
*me
, void *hunoz
)
609 { return (mp_pywrap(MP_COPY(FIELD_F(me
)->m
))); }
611 static PyGetSetDef primefield_pygetset
[] = {
612 #define GETSETNAME(op, name) pf##op##_##name
613 GET (p
, "F.p -> prime field characteristic")
617 static PyTypeObject primefield_pytype_skel
= {
618 PyObject_HEAD_INIT(0) 0, /* Header */
619 "PrimeField", /* @tp_name@ */
620 sizeof(field_pyobj
), /* @tp_basicsize@ */
621 0, /* @tp_itemsize@ */
623 field_pydealloc
, /* @tp_dealloc@ */
625 0, /* @tp_getattr@ */
626 0, /* @tp_setattr@ */
627 0, /* @tp_compare@ */
629 0, /* @tp_as_number@ */
630 0, /* @tp_as_sequence@ */
631 0, /* @tp_as_mapping@ */
635 0, /* @tp_getattro@ */
636 0, /* @tp_setattro@ */
637 0, /* @tp_as_buffer@ */
638 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
644 0, /* @tp_traverse@ */
646 field_pyrichcompare
, /* @tp_richcompare@ */
647 0, /* @tp_weaklistoffset@ */
649 0, /* @tp_iternext@ */
650 0, /* @tp_methods@ */
651 0, /* @tp_members@ */
652 primefield_pygetset
, /* @tp_getset@ */
655 0, /* @tp_descr_get@ */
656 0, /* @tp_descr_set@ */
657 0, /* @tp_dictoffset@ */
659 PyType_GenericAlloc
, /* @tp_alloc@ */
660 primefield_pynew
, /* @tp_new@ */
665 static PyObject
*niceprimefield_pynew(PyTypeObject
*ty
,
666 PyObject
*arg
, PyObject
*kw
)
670 char *kwlist
[] = { "p", 0 };
672 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:niceprimefield",
673 kwlist
, convmp
, &xx
))
675 if ((f
= field_niceprime(xx
)) == 0)
676 VALERR("bad prime for niceprimefield");
678 return (field_dopywrap(ty
, f
));
684 static PyTypeObject niceprimefield_pytype_skel
= {
685 PyObject_HEAD_INIT(0) 0, /* Header */
686 "NicePrimeField", /* @tp_name@ */
687 sizeof(field_pyobj
), /* @tp_basicsize@ */
688 0, /* @tp_itemsize@ */
690 field_pydealloc
, /* @tp_dealloc@ */
692 0, /* @tp_getattr@ */
693 0, /* @tp_setattr@ */
694 0, /* @tp_compare@ */
696 0, /* @tp_as_number@ */
697 0, /* @tp_as_sequence@ */
698 0, /* @tp_as_mapping@ */
702 0, /* @tp_getattro@ */
703 0, /* @tp_setattro@ */
704 0, /* @tp_as_buffer@ */
705 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
709 "Nice prime fields.",
711 0, /* @tp_traverse@ */
713 field_pyrichcompare
, /* @tp_richcompare@ */
714 0, /* @tp_weaklistoffset@ */
716 0, /* @tp_iternext@ */
717 0, /* @tp_methods@ */
718 0, /* @tp_members@ */
722 0, /* @tp_descr_get@ */
723 0, /* @tp_descr_set@ */
724 0, /* @tp_dictoffset@ */
726 PyType_GenericAlloc
, /* @tp_alloc@ */
727 niceprimefield_pynew
, /* @tp_new@ */
732 /*----- Binary fields -----------------------------------------------------*/
734 static PyObject
*bfget_m(PyObject
*me
, void *hunoz
)
735 { return (PyInt_FromLong(FIELD_F(me
)->nbits
)); }
737 static PyGetSetDef binfield_pygetset
[] = {
738 #define GETSETNAME(op, name) bf##op##_##name
739 GET (m
, "F.m -> field polynomial degree")
744 static PyTypeObject binfield_pytype_skel
= {
745 PyObject_HEAD_INIT(0) 0, /* Header */
746 "BinField", /* @tp_name@ */
747 sizeof(field_pyobj
), /* @tp_basicsize@ */
748 0, /* @tp_itemsize@ */
750 field_pydealloc
, /* @tp_dealloc@ */
752 0, /* @tp_getattr@ */
753 0, /* @tp_setattr@ */
754 0, /* @tp_compare@ */
756 0, /* @tp_as_number@ */
757 0, /* @tp_as_sequence@ */
758 0, /* @tp_as_mapping@ */
762 0, /* @tp_getattro@ */
763 0, /* @tp_setattro@ */
764 0, /* @tp_as_buffer@ */
765 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
769 "Binary fields. Abstract class.",
771 0, /* @tp_traverse@ */
773 field_pyrichcompare
, /* @tp_richcompare@ */
774 0, /* @tp_weaklistoffset@ */
776 0, /* @tp_iternext@ */
777 0, /* @tp_methods@ */
778 0, /* @tp_members@ */
779 binfield_pygetset
, /* @tp_getset@ */
782 0, /* @tp_descr_get@ */
783 0, /* @tp_descr_set@ */
784 0, /* @tp_dictoffset@ */
786 PyType_GenericAlloc
, /* @tp_alloc@ */
787 abstract_pynew
, /* @tp_new@ */
792 static PyObject
*binpolyfield_pynew(PyTypeObject
*ty
,
793 PyObject
*arg
, PyObject
*kw
)
797 char *kwlist
[] = { "p", 0 };
799 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:binpolyfield", kwlist
,
802 if ((f
= field_binpoly(xx
)) == 0) VALERR("bad poly for binpolyfield");
804 return (field_dopywrap(ty
, f
));
810 static PyGetSetDef binpolyfield_pygetset
[] = {
811 #define GETSETNAME(op, name) pf##op##_##name
812 GET (p
, "F.p -> field polynomial")
817 static PyTypeObject binpolyfield_pytype_skel
= {
818 PyObject_HEAD_INIT(0) 0, /* Header */
819 "BinPolyField", /* @tp_name@ */
820 sizeof(field_pyobj
), /* @tp_basicsize@ */
821 0, /* @tp_itemsize@ */
823 field_pydealloc
, /* @tp_dealloc@ */
825 0, /* @tp_getattr@ */
826 0, /* @tp_setattr@ */
827 0, /* @tp_compare@ */
829 0, /* @tp_as_number@ */
830 0, /* @tp_as_sequence@ */
831 0, /* @tp_as_mapping@ */
835 0, /* @tp_getattro@ */
836 0, /* @tp_setattro@ */
837 0, /* @tp_as_buffer@ */
838 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
842 "Binary fields with polynomial basis representation.",
844 0, /* @tp_traverse@ */
846 field_pyrichcompare
, /* @tp_richcompare@ */
847 0, /* @tp_weaklistoffset@ */
849 0, /* @tp_iternext@ */
850 0, /* @tp_methods@ */
851 0, /* @tp_members@ */
852 binpolyfield_pygetset
, /* @tp_getset@ */
855 0, /* @tp_descr_get@ */
856 0, /* @tp_descr_set@ */
857 0, /* @tp_dictoffset@ */
859 PyType_GenericAlloc
, /* @tp_alloc@ */
860 binpolyfield_pynew
, /* @tp_new@ */
865 static PyObject
*binnormfield_pynew(PyTypeObject
*ty
,
866 PyObject
*arg
, PyObject
*kw
)
870 char *kwlist
[] = { "p", "beta", 0 };
872 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:binnormfield",
873 kwlist
, convgf
, &xx
, convgf
, &yy
))
875 if ((f
= field_binnorm(xx
, yy
)) == 0) VALERR("bad args for binnormfield");
876 MP_DROP(xx
); MP_DROP(yy
);
877 return (field_dopywrap(ty
, f
));
879 mp_drop(xx
); mp_drop(yy
);
883 static PyObject
*bnfget_beta(PyObject
*me
, void *hunoz
)
885 fctx_binnorm
*fc
= (fctx_binnorm
*)FIELD_F(me
);
886 return (gf_pywrap(MP_COPY(fc
->ntop
.r
[fc
->ntop
.n
- 1])));
889 static PyGetSetDef binnormfield_pygetset
[] = {
890 #define GETSETNAME(op, name) pf##op##_##name
891 GET (p
, "F.p -> field polynomial")
893 #define GETSETNAME(op, name) bnf##op##_##name
894 GET (beta
, "F.beta -> conversion factor")
899 static PyTypeObject binnormfield_pytype_skel
= {
900 PyObject_HEAD_INIT(0) 0, /* Header */
901 "BinNormField", /* @tp_name@ */
902 sizeof(field_pyobj
), /* @tp_basicsize@ */
903 0, /* @tp_itemsize@ */
905 field_pydealloc
, /* @tp_dealloc@ */
907 0, /* @tp_getattr@ */
908 0, /* @tp_setattr@ */
909 0, /* @tp_compare@ */
911 0, /* @tp_as_number@ */
912 0, /* @tp_as_sequence@ */
913 0, /* @tp_as_mapping@ */
917 0, /* @tp_getattro@ */
918 0, /* @tp_setattro@ */
919 0, /* @tp_as_buffer@ */
920 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
924 "Binary fields with normal basis representation.",
926 0, /* @tp_traverse@ */
928 field_pyrichcompare
, /* @tp_richcompare@ */
929 0, /* @tp_weaklistoffset@ */
931 0, /* @tp_iternext@ */
932 0, /* @tp_methods@ */
933 0, /* @tp_members@ */
934 binnormfield_pygetset
, /* @tp_getset@ */
937 0, /* @tp_descr_get@ */
938 0, /* @tp_descr_set@ */
939 0, /* @tp_dictoffset@ */
941 PyType_GenericAlloc
, /* @tp_alloc@ */
942 binnormfield_pynew
, /* @tp_new@ */
947 /*----- Setup -------------------------------------------------------------*/
949 static PyObject
*meth__Field_parse(PyObject
*me
, PyObject
*arg
)
956 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
960 if ((f
= field_parse(&qd
)) == 0)
962 rc
= Py_BuildValue("(Ns)", field_pywrap(f
), qd
.p
);
967 static PyMethodDef methods
[] = {
968 #define METHNAME(func) meth_##func
969 METH (_Field_parse
, "parse(STR) -> F, REST")
974 void field_pyinit(void)
977 INITTYPE(field
, type
);
978 INITTYPE(primefield
, field
);
979 INITTYPE(niceprimefield
, primefield
);
980 INITTYPE(binfield
, field
);
981 INITTYPE(binpolyfield
, binfield
);
982 INITTYPE(binnormfield
, binfield
);
986 void field_pyinsert(PyObject
*mod
)
988 INSERT("FE", fe_pytype
);
989 INSERT("Field", field_pytype
);
990 INSERT("PrimeField", primefield_pytype
);
991 INSERT("NicePrimeField", niceprimefield_pytype
);
992 INSERT("BinField", binfield_pytype
);
993 INSERT("BinPolyField", binpolyfield_pytype
);
994 INSERT("BinNormField", binnormfield_pytype
);
997 /*----- That's all, folks -------------------------------------------------*/