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 static const char *const 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")) ty
= primefield_pytype
;
78 else if (STRCMP(F_NAME(f
), ==, "niceprime")) ty
= niceprimefield_pytype
;
79 else if (STRCMP(F_NAME(f
), ==, "binpoly")) ty
= binpolyfield_pytype
;
80 else if (STRCMP(F_NAME(f
), ==, "binnorm")) ty
= binnormfield_pytype
;
82 return (field_dopywrap(ty
, f
));
85 field
*field_copy(field
*f
)
87 if (STRCMP(F_NAME(f
), ==, "prime"))
88 f
= field_prime(f
->m
);
89 else if (STRCMP(F_NAME(f
), ==, "niceprime"))
90 f
= field_niceprime(f
->m
);
91 else if (STRCMP(F_NAME(f
), ==, "binpoly"))
92 f
= field_binpoly(f
->m
);
93 else if (STRCMP(F_NAME(f
), ==, "binnorm")) {
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 /*----- Field elements ----------------------------------------------------*/
131 static int febinop(PyObject
*x
, PyObject
*y
,
132 field
**f
, PyObject
**fobj
, mp
**xx
, mp
**yy
)
134 if (FE_PYCHECK(x
)) *fobj
= FE_FOBJ(x
);
135 else if (FE_PYCHECK(y
)) *fobj
= FE_FOBJ(y
);
138 if ((*xx
= tofe(*f
, x
)) == 0)
140 if ((*yy
= tofe(*f
, y
)) == 0) {
147 #define BINOP(name) \
148 static PyObject *fe_py##name(PyObject *x, PyObject *y) { \
152 if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL; \
153 zz = ff->ops->name(ff, MP_NEW, xx, yy); \
154 MP_DROP(xx); MP_DROP(yy); \
155 return (fe_pywrap(fobj, zz)); \
162 static PyObject
*fe_pydiv(PyObject
*x
, PyObject
*y
)
168 if (febinop(x
, y
, &ff
, &fobj
, &xx
, &yy
)) RETURN_NOTIMPL
;
169 if (F_ZEROP(ff
, yy
)) ZDIVERR("division by zero");
170 yy
= F_INV(ff
, yy
, yy
);
171 z
= fe_pywrap(fobj
, F_MUL(ff
, MP_NEW
, xx
, yy
));
173 MP_DROP(xx
); MP_DROP(yy
);
177 static PyObject
*fe_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
182 if (z
!= Py_None
|| !FE_PYCHECK(x
) || (yy
= tomp(y
)) == 0)
184 ff
= FE_F(x
); xx
= FE_X(x
); MP_COPY(xx
);
185 if (MP_NEGP(yy
) && F_ZEROP(ff
, xx
)) ZDIVERR("division by zero");
186 z
= fe_pywrap(FE_FOBJ(x
), field_exp(ff
, MP_NEW
, xx
, yy
));
188 MP_DROP(xx
); MP_DROP(yy
);
192 static PyObject
*fe_pyneg(PyObject
*x
)
194 return fe_pywrap(FE_FOBJ(x
), FE_F(x
)->ops
->neg(FE_F(x
), MP_NEW
, FE_X(x
)));
197 static PyObject
*fe_pyid(PyObject
*x
) { RETURN_OBJ(x
); }
199 static int fe_pynonzerop(PyObject
*x
) { return !F_ZEROP(FE_F(x
), FE_X(x
)); }
201 static PyObject
*fe_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
209 if (febinop(x
, y
, &ff
, &fobj
, &xx
, &yy
)) RETURN_NOTIMPL
;
211 case Py_EQ
: b
= MP_EQ(xx
, yy
); break;
212 case Py_NE
: b
= !MP_EQ(xx
, yy
); break;
213 default: TYERR("field elements are unordered");
217 MP_DROP(xx
); MP_DROP(yy
);
221 static Py_hash_t
fe_pyhash(PyObject
*me
)
222 { return (mphash(FE_X(me
))); }
225 static int fe_pycoerce(PyObject
**x
, PyObject
**y
)
229 if (FE_PYCHECK(*y
)) {
230 if (FE_F(*x
) != FE_F(*y
) && !field_samep(FE_F(*x
), FE_F(*y
)))
231 TYERR("field mismatch");
232 Py_INCREF(*x
); Py_INCREF(*y
);
235 if ((z
= tofe(FE_F(*x
), *y
)) != 0) {
237 *y
= fe_pywrap(FE_FOBJ(*x
), z
);
247 static PyObject
*fe_pyint(PyObject
*x
)
251 mp
*xx
= F_OUT(FE_F(x
), MP_NEW
, FE_X(x
));
252 if (!mp_tolong_checked(xx
, &l
, 0)) rc
= PyInt_FromLong(l
);
253 else rc
= mp_topylong(xx
);
259 static PyObject
*fe_pylong(PyObject
*x
)
261 mp
*xx
= F_OUT(FE_F(x
), MP_NEW
, FE_X(x
));
262 PyObject
*rc
= mp_topylong(xx
);
268 #define BASEOP(name, radix, pre) \
269 static PyObject *fe_py##name(PyObject *x) { \
270 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x)); \
271 PyObject *rc = mp_topystring(xx, radix, 0, pre, 0); \
278 BASEOP(hex
, 16, "0x");
281 static void fe_pydealloc(PyObject
*me
)
283 Py_DECREF(FE_FOBJ(me
));
288 #define UNOP(name, check) \
289 static PyObject *femeth_##name(PyObject *me) { \
290 field *f = FE_F(me); \
292 if (!f->ops->name) TYERR(#name " not supported for this field"); \
294 x = f->ops->name(f, MP_NEW, x); \
295 if (!x) RETURN_NONE; \
296 return (fe_pywrap(FE_FOBJ(me), x)); \
300 UNOP(inv
, if (F_ZEROP(f
, x
)) ZDIVERR("division by zero"); )
310 static PyObject
*feget_value(PyObject
*me
, void *hunoz
)
312 mp
*x
= F_OUT(FE_F(me
), MP_NEW
, FE_X(me
));
313 if (F_TYPE(FE_F(me
)) == FTY_BINARY
)
314 return (gf_pywrap(x
));
316 return (mp_pywrap(x
));
319 static PyObject
*feget__value(PyObject
*me
, void *hunoz
)
323 if (F_TYPE(FE_F(me
)) == FTY_BINARY
)
324 return (gf_pywrap(x
));
326 return (mp_pywrap(x
));
329 static const PyMemberDef fe_pymembers
[] = {
330 #define MEMBERSTRUCT fe_pyobj
331 MEMRNM(field
, T_OBJECT
, PY23(ob_type
, ob_base
.ob_type
), READONLY
,
332 "X.field -> field containing X")
337 static const PyGetSetDef fe_pygetset
[] = {
338 #define GETSETNAME(op, name) fe##op##_##name
339 GET (value
, "X.value -> `natural' MP/GF representation of X")
340 GET (_value
, "X._value -> internal MP/GF representation of X")
345 static const PyMethodDef fe_pymethods
[] = {
346 #define METHNAME(func) femeth_##func
347 NAMETH(inv
, "X.inv() -> X^{-1}")
348 NAMETH(sqr
, "X.sqr() -> X^2")
349 NAMETH(sqrt
, "X.sqrt() -> sqrt(X)")
350 NAMETH(quadsolve
, "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
351 NAMETH(dbl
, "X.dbl() -> 2 * X (prime only)")
352 NAMETH(tpl
, "X.tpl() -> 3 * X (prime only)")
353 NAMETH(qdl
, "X.qdl() -> 4 * X (prime only)")
354 NAMETH(hlv
, "X.hlv() -> X/2 (prime only)")
359 static const PyNumberMethods fe_pynumber
= {
360 fe_pyadd
, /* @nb_add@ */
361 fe_pysub
, /* @nb_subtract@ */
362 fe_pymul
, /* @nb_multiply@ */
364 fe_pydiv
, /* @nb_divide@ */
366 0, /* @nb_remainder@ */
368 fe_pyexp
, /* @nb_power@ */
369 fe_pyneg
, /* @nb_negative@ */
370 fe_pyid
, /* @nb_positive@ */
371 0, /* @nb_absolute@ */
372 fe_pynonzerop
, /* @nb_nonzero@ */
380 fe_pycoerce
, /* @nb_coerce@ */
382 fe_pyint
, /* @nb_int@ */
383 PY23(fe_pylong
, 0), /* @nb_long@ */
384 0 /* meaningless */, /* @nb_float@ */
386 fe_pyoct
, /* @nb_oct@ */
387 fe_pyhex
, /* @nb_hex@ */
390 0, /* @nb_inplace_add@ */
391 0, /* @nb_inplace_subtract@ */
392 0, /* @nb_inplace_multiply@ */
394 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@ */
409 fe_pyint
, /* @nb_index@ */
412 static const PyTypeObject fe_pytype_skel
= {
413 PyVarObject_HEAD_INIT(0, 0) /* Header */
414 "FE", /* @tp_name@ */
415 sizeof(fe_pyobj
), /* @tp_basicsize@ */
416 0, /* @tp_itemsize@ */
418 fe_pydealloc
, /* @tp_dealloc@ */
420 0, /* @tp_getattr@ */
421 0, /* @tp_setattr@ */
422 0, /* @tp_compare@ */
424 PYNUMBER(fe
), /* @tp_as_number@ */
425 0, /* @tp_as_sequence@ */
426 0, /* @tp_as_mapping@ */
427 fe_pyhash
, /* @tp_hash@ */
429 fe_pyhex
, /* @tp_str@ */
430 0, /* @tp_getattro@ */
431 0, /* @tp_setattro@ */
432 0, /* @tp_as_buffer@ */
433 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
434 Py_TPFLAGS_CHECKTYPES
|
438 "Finite field elements, abstract base class.",
440 0, /* @tp_traverse@ */
442 fe_pyrichcompare
, /* @tp_richcompare@ */
443 0, /* @tp_weaklistoffset@ */
445 0, /* @tp_iternext@ */
446 PYMETHODS(fe
), /* @tp_methods@ */
447 PYMEMBERS(fe
), /* @tp_members@ */
448 PYGETSET(fe
), /* @tp_getset@ */
451 0, /* @tp_descr_get@ */
452 0, /* @tp_descr_set@ */
453 0, /* @tp_dictoffset@ */
455 PyType_GenericAlloc
, /* @tp_alloc@ */
456 abstract_pynew
, /* @tp_new@ */
461 /*----- Fields ------------------------------------------------------------*/
463 static PyObject
*field_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
465 int b
= field_samep(FIELD_F(x
), FIELD_F(y
));
469 default: TYERR("can't order fields");
476 static PyObject
*fmeth_rand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
478 static const char *const kwlist
[] = { "rng", 0 };
479 grand
*r
= &rand_global
;
481 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:rand", KWLIST
,
484 return (fe_pywrap(me
, F_RAND(FIELD_F(me
), MP_NEW
, r
)));
487 static PyObject
*fmeth__adopt(PyObject
*me
, PyObject
*arg
)
490 if (!PyArg_ParseTuple(arg
, "O&:_adopt", convmp
, &xx
)) return (0);
491 return (fe_pywrap(me
, xx
));
494 static PyObject
*fmeth_parse(PyObject
*me
, PyObject
*arg
)
501 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
503 if ((f
= field_parse(&qd
)) == 0) VALERR(qd
.e
);
504 rc
= Py_BuildValue("(Ns)", field_pywrap(f
), qd
.p
);
509 static void field_pydealloc(PyObject
*me
)
511 F_DESTROY(FIELD_F(me
));
512 PyType_Type
.tp_dealloc(me
);
515 static PyObject
*fget_zero(PyObject
*me
, void *hunoz
)
516 { return (fe_pywrap(me
, MP_COPY(FIELD_F(me
)->zero
))); }
518 static PyObject
*fget_one(PyObject
*me
, void *hunoz
)
519 { return (fe_pywrap(me
, MP_COPY(FIELD_F(me
)->one
))); }
521 static PyObject
*fget_q(PyObject
*me
, void *hunoz
)
522 { return (mp_pywrap(MP_COPY(FIELD_F(me
)->q
))); }
524 static PyObject
*fget_nbits(PyObject
*me
, void *hunoz
)
525 { return (PyInt_FromLong(FIELD_F(me
)->nbits
)); }
527 static PyObject
*fget_noctets(PyObject
*me
, void *hunoz
)
528 { return (PyInt_FromLong(FIELD_F(me
)->noctets
)); }
530 static PyObject
*fget_name(PyObject
*me
, void *hunoz
)
531 { return (TEXT_FROMSTR(F_NAME(FIELD_F(me
)))); }
533 static PyObject
*fget_type(PyObject
*me
, void *hunoz
)
534 { return (PyInt_FromLong(F_TYPE(FIELD_F(me
)))); }
536 static const PyGetSetDef field_pygetset
[] = {
537 #define GETSETNAME(op, name) f##op##_##name
538 GET (zero
, "F.zero -> field additive identity")
539 GET (one
, "F.one -> field multiplicative identity")
540 GET (q
, "F.q -> number of elements in field")
541 GET (nbits
, "F.nbits -> bits needed to represent element")
542 GET (noctets
, "F.noctets -> octetss needed to represent element")
543 GET (name
, "F.name -> name of this kind of field")
544 GET (type
, "F.type -> type code of this kind of field")
549 static const PyMethodDef field_pymethods
[] = {
550 #define METHNAME(name) fmeth_##name
551 METH (_adopt
, "F._adopt(X) -> FE")
552 KWMETH(rand
, "F.rand([rng = rand]) -> FE, uniformly distributed")
553 SMTH (parse
, "parse(STR) -> F, REST")
558 static const PyTypeObject field_pytype_skel
= {
559 PyVarObject_HEAD_INIT(0, 0) /* Header */
560 "Field", /* @tp_name@ */
561 sizeof(field_pyobj
), /* @tp_basicsize@ */
562 0, /* @tp_itemsize@ */
564 field_pydealloc
, /* @tp_dealloc@ */
566 0, /* @tp_getattr@ */
567 0, /* @tp_setattr@ */
568 0, /* @tp_compare@ */
570 0, /* @tp_as_number@ */
571 0, /* @tp_as_sequence@ */
572 0, /* @tp_as_mapping@ */
576 0, /* @tp_getattro@ */
577 0, /* @tp_setattro@ */
578 0, /* @tp_as_buffer@ */
579 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
583 "An abstract field. This is an abstract type.",
585 0, /* @tp_traverse@ */
587 field_pyrichcompare
, /* @tp_richcompare@ */
588 0, /* @tp_weaklistoffset@ */
590 0, /* @tp_iternext@ */
591 PYMETHODS(field
), /* @tp_methods@ */
592 0, /* @tp_members@ */
593 PYGETSET(field
), /* @tp_getset@ */
596 0, /* @tp_descr_get@ */
597 0, /* @tp_descr_set@ */
598 0, /* @tp_dictoffset@ */
600 PyType_GenericAlloc
, /* @tp_alloc@ */
601 abstract_pynew
, /* @tp_new@ */
606 /*----- Prime fields ------------------------------------------------------*/
608 static PyObject
*primefield_pynew(PyTypeObject
*ty
,
609 PyObject
*arg
, PyObject
*kw
)
613 static const char *const kwlist
[] = { "p", 0 };
615 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:primefield", KWLIST
,
618 if ((f
= field_prime(xx
)) == 0)
619 VALERR("bad prime for primefield");
621 return (field_dopywrap(ty
, f
));
627 static PyObject
*pfget_p(PyObject
*me
, void *hunoz
)
628 { return (mp_pywrap(MP_COPY(FIELD_F(me
)->m
))); }
630 static const PyGetSetDef primefield_pygetset
[] = {
631 #define GETSETNAME(op, name) pf##op##_##name
632 GET (p
, "F.p -> prime field characteristic")
636 static const PyTypeObject primefield_pytype_skel
= {
637 PyVarObject_HEAD_INIT(0, 0) /* Header */
638 "PrimeField", /* @tp_name@ */
639 sizeof(field_pyobj
), /* @tp_basicsize@ */
640 0, /* @tp_itemsize@ */
642 field_pydealloc
, /* @tp_dealloc@ */
644 0, /* @tp_getattr@ */
645 0, /* @tp_setattr@ */
646 0, /* @tp_compare@ */
648 0, /* @tp_as_number@ */
649 0, /* @tp_as_sequence@ */
650 0, /* @tp_as_mapping@ */
654 0, /* @tp_getattro@ */
655 0, /* @tp_setattro@ */
656 0, /* @tp_as_buffer@ */
657 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
661 "PrimeField(P): prime fields.",
663 0, /* @tp_traverse@ */
665 0, /* @tp_richcompare@ */
666 0, /* @tp_weaklistoffset@ */
668 0, /* @tp_iternext@ */
669 0, /* @tp_methods@ */
670 0, /* @tp_members@ */
671 PYGETSET(primefield
), /* @tp_getset@ */
674 0, /* @tp_descr_get@ */
675 0, /* @tp_descr_set@ */
676 0, /* @tp_dictoffset@ */
678 PyType_GenericAlloc
, /* @tp_alloc@ */
679 primefield_pynew
, /* @tp_new@ */
684 static PyObject
*niceprimefield_pynew(PyTypeObject
*ty
,
685 PyObject
*arg
, PyObject
*kw
)
689 static const char *const kwlist
[] = { "p", 0 };
691 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:niceprimefield",
692 KWLIST
, convmp
, &xx
))
694 if ((f
= field_niceprime(xx
)) == 0)
695 VALERR("bad prime for niceprimefield");
697 return (field_dopywrap(ty
, f
));
703 static const PyTypeObject niceprimefield_pytype_skel
= {
704 PyVarObject_HEAD_INIT(0, 0) /* Header */
705 "NicePrimeField", /* @tp_name@ */
706 sizeof(field_pyobj
), /* @tp_basicsize@ */
707 0, /* @tp_itemsize@ */
709 field_pydealloc
, /* @tp_dealloc@ */
711 0, /* @tp_getattr@ */
712 0, /* @tp_setattr@ */
713 0, /* @tp_compare@ */
715 0, /* @tp_as_number@ */
716 0, /* @tp_as_sequence@ */
717 0, /* @tp_as_mapping@ */
721 0, /* @tp_getattro@ */
722 0, /* @tp_setattro@ */
723 0, /* @tp_as_buffer@ */
724 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
728 "NicePrimeField(P): prime field using Solinas reduction.",
730 0, /* @tp_traverse@ */
732 0, /* @tp_richcompare@ */
733 0, /* @tp_weaklistoffset@ */
735 0, /* @tp_iternext@ */
736 0, /* @tp_methods@ */
737 0, /* @tp_members@ */
741 0, /* @tp_descr_get@ */
742 0, /* @tp_descr_set@ */
743 0, /* @tp_dictoffset@ */
745 PyType_GenericAlloc
, /* @tp_alloc@ */
746 niceprimefield_pynew
, /* @tp_new@ */
751 /*----- Binary fields -----------------------------------------------------*/
753 static PyObject
*bfget_m(PyObject
*me
, void *hunoz
)
754 { return (PyInt_FromLong(FIELD_F(me
)->nbits
)); }
756 static PyObject
*bfget_p(PyObject
*me
, void *hunoz
)
757 { return (gf_pywrap(MP_COPY(FIELD_F(me
)->m
))); }
759 static const PyGetSetDef binfield_pygetset
[] = {
760 #define GETSETNAME(op, name) bf##op##_##name
761 GET (m
, "F.m -> field polynomial degree")
762 GET (p
, "F.p -> field polynomial")
767 static const PyTypeObject binfield_pytype_skel
= {
768 PyVarObject_HEAD_INIT(0, 0) /* Header */
769 "BinField", /* @tp_name@ */
770 sizeof(field_pyobj
), /* @tp_basicsize@ */
771 0, /* @tp_itemsize@ */
773 field_pydealloc
, /* @tp_dealloc@ */
775 0, /* @tp_getattr@ */
776 0, /* @tp_setattr@ */
777 0, /* @tp_compare@ */
779 0, /* @tp_as_number@ */
780 0, /* @tp_as_sequence@ */
781 0, /* @tp_as_mapping@ */
785 0, /* @tp_getattro@ */
786 0, /* @tp_setattro@ */
787 0, /* @tp_as_buffer@ */
788 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
792 "Binary fields. Abstract class.",
794 0, /* @tp_traverse@ */
796 0, /* @tp_richcompare@ */
797 0, /* @tp_weaklistoffset@ */
799 0, /* @tp_iternext@ */
800 0, /* @tp_methods@ */
801 0, /* @tp_members@ */
802 PYGETSET(binfield
), /* @tp_getset@ */
805 0, /* @tp_descr_get@ */
806 0, /* @tp_descr_set@ */
807 0, /* @tp_dictoffset@ */
809 PyType_GenericAlloc
, /* @tp_alloc@ */
810 abstract_pynew
, /* @tp_new@ */
815 static PyObject
*binpolyfield_pynew(PyTypeObject
*ty
,
816 PyObject
*arg
, PyObject
*kw
)
820 static const char *const kwlist
[] = { "p", 0 };
822 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:binpolyfield", KWLIST
,
825 if ((f
= field_binpoly(xx
)) == 0) VALERR("bad poly for binpolyfield");
827 return (field_dopywrap(ty
, f
));
833 static const PyTypeObject binpolyfield_pytype_skel
= {
834 PyVarObject_HEAD_INIT(0, 0) /* Header */
835 "BinPolyField", /* @tp_name@ */
836 sizeof(field_pyobj
), /* @tp_basicsize@ */
837 0, /* @tp_itemsize@ */
839 field_pydealloc
, /* @tp_dealloc@ */
841 0, /* @tp_getattr@ */
842 0, /* @tp_setattr@ */
843 0, /* @tp_compare@ */
845 0, /* @tp_as_number@ */
846 0, /* @tp_as_sequence@ */
847 0, /* @tp_as_mapping@ */
851 0, /* @tp_getattro@ */
852 0, /* @tp_setattro@ */
853 0, /* @tp_as_buffer@ */
854 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
858 "BinPolyField(P): binary fields with polynomial basis representation.",
860 0, /* @tp_traverse@ */
862 0, /* @tp_richcompare@ */
863 0, /* @tp_weaklistoffset@ */
865 0, /* @tp_iternext@ */
866 0, /* @tp_methods@ */
867 0, /* @tp_members@ */
871 0, /* @tp_descr_get@ */
872 0, /* @tp_descr_set@ */
873 0, /* @tp_dictoffset@ */
875 PyType_GenericAlloc
, /* @tp_alloc@ */
876 binpolyfield_pynew
, /* @tp_new@ */
881 static PyObject
*binnormfield_pynew(PyTypeObject
*ty
,
882 PyObject
*arg
, PyObject
*kw
)
886 static const char *const kwlist
[] = { "p", "beta", 0 };
888 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:binnormfield",
889 KWLIST
, convgf
, &xx
, convgf
, &yy
))
891 if ((f
= field_binnorm(xx
, yy
)) == 0) VALERR("bad args for binnormfield");
892 MP_DROP(xx
); MP_DROP(yy
);
893 return (field_dopywrap(ty
, f
));
895 mp_drop(xx
); mp_drop(yy
);
899 static PyObject
*bnfget_beta(PyObject
*me
, void *hunoz
)
901 fctx_binnorm
*fc
= (fctx_binnorm
*)FIELD_F(me
);
902 return (gf_pywrap(MP_COPY(fc
->ntop
.r
[fc
->ntop
.n
- 1])));
905 static const PyGetSetDef binnormfield_pygetset
[] = {
906 #define GETSETNAME(op, name) bnf##op##_##name
907 GET (beta
, "F.beta -> conversion factor")
912 static const PyTypeObject binnormfield_pytype_skel
= {
913 PyVarObject_HEAD_INIT(0, 0) /* Header */
914 "BinNormField", /* @tp_name@ */
915 sizeof(field_pyobj
), /* @tp_basicsize@ */
916 0, /* @tp_itemsize@ */
918 field_pydealloc
, /* @tp_dealloc@ */
920 0, /* @tp_getattr@ */
921 0, /* @tp_setattr@ */
922 0, /* @tp_compare@ */
924 0, /* @tp_as_number@ */
925 0, /* @tp_as_sequence@ */
926 0, /* @tp_as_mapping@ */
930 0, /* @tp_getattro@ */
931 0, /* @tp_setattro@ */
932 0, /* @tp_as_buffer@ */
933 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
937 "BinNormField(P, BETA): binary fields with normal basis representation.",
939 0, /* @tp_traverse@ */
941 0, /* @tp_richcompare@ */
942 0, /* @tp_weaklistoffset@ */
944 0, /* @tp_iternext@ */
945 0, /* @tp_methods@ */
946 0, /* @tp_members@ */
947 PYGETSET(binnormfield
), /* @tp_getset@ */
950 0, /* @tp_descr_get@ */
951 0, /* @tp_descr_set@ */
952 0, /* @tp_dictoffset@ */
954 PyType_GenericAlloc
, /* @tp_alloc@ */
955 binnormfield_pynew
, /* @tp_new@ */
960 /*----- Setup -------------------------------------------------------------*/
962 static const struct nameval consts
[] = {
963 CONST(FTY_PRIME
), CONST(FTY_BINARY
),
967 void field_pyinit(void)
970 INITTYPE(field
, type
);
971 INITTYPE(primefield
, field
);
972 INITTYPE(niceprimefield
, primefield
);
973 INITTYPE(binfield
, field
);
974 INITTYPE(binpolyfield
, binfield
);
975 INITTYPE(binnormfield
, binfield
);
978 void field_pyinsert(PyObject
*mod
)
980 INSERT("FE", fe_pytype
);
981 INSERT("Field", field_pytype
);
982 INSERT("PrimeField", primefield_pytype
);
983 INSERT("NicePrimeField", niceprimefield_pytype
);
984 INSERT("BinField", binfield_pytype
);
985 INSERT("BinPolyField", binpolyfield_pytype
);
986 INSERT("BinNormField", binnormfield_pytype
);
987 setconstants(mod
, consts
);
990 /*----- That's all, folks -------------------------------------------------*/