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
))); }
224 static int fe_pycoerce(PyObject
**x
, PyObject
**y
)
228 if (FE_PYCHECK(*y
)) {
229 if (FE_F(*x
) != FE_F(*y
) && !field_samep(FE_F(*x
), FE_F(*y
)))
230 TYERR("field mismatch");
231 Py_INCREF(*x
); Py_INCREF(*y
);
234 if ((z
= tofe(FE_F(*x
), *y
)) != 0) {
236 *y
= fe_pywrap(FE_FOBJ(*x
), z
);
245 static PyObject
*fe_pyint(PyObject
*x
)
249 mp
*xx
= F_OUT(FE_F(x
), MP_NEW
, FE_X(x
));
250 if (!mp_tolong_checked(xx
, &l
, 0)) rc
= PyInt_FromLong(l
);
251 else rc
= mp_topylong(xx
);
256 static PyObject
*fe_pylong(PyObject
*x
)
258 mp
*xx
= F_OUT(FE_F(x
), MP_NEW
, FE_X(x
));
259 PyObject
*rc
= mp_topylong(xx
);
264 #define BASEOP(name, radix, pre) \
265 static PyObject *fe_py##name(PyObject *x) { \
266 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x)); \
267 PyObject *rc = mp_topystring(xx, radix, 0, pre, 0); \
272 BASEOP(hex
, 16, "0x");
275 static void fe_pydealloc(PyObject
*me
)
277 Py_DECREF(FE_FOBJ(me
));
282 #define UNOP(name, check) \
283 static PyObject *femeth_##name(PyObject *me) { \
284 field *f = FE_F(me); \
286 if (!f->ops->name) TYERR(#name " not supported for this field"); \
288 x = f->ops->name(f, MP_NEW, x); \
289 if (!x) RETURN_NONE; \
290 return (fe_pywrap(FE_FOBJ(me), x)); \
294 UNOP(inv
, if (F_ZEROP(f
, x
)) ZDIVERR("division by zero"); )
304 static PyObject
*feget_value(PyObject
*me
, void *hunoz
)
306 mp
*x
= F_OUT(FE_F(me
), MP_NEW
, FE_X(me
));
307 if (F_TYPE(FE_F(me
)) == FTY_BINARY
)
308 return (gf_pywrap(x
));
310 return (mp_pywrap(x
));
313 static PyObject
*feget__value(PyObject
*me
, void *hunoz
)
317 if (F_TYPE(FE_F(me
)) == FTY_BINARY
)
318 return (gf_pywrap(x
));
320 return (mp_pywrap(x
));
323 static const PyMemberDef fe_pymembers
[] = {
324 #define MEMBERSTRUCT fe_pyobj
325 MEMRNM(field
, T_OBJECT
, ob_type
, READONLY
,
326 "X.field -> field containing X")
331 static const PyGetSetDef fe_pygetset
[] = {
332 #define GETSETNAME(op, name) fe##op##_##name
333 GET (value
, "X.value -> `natural' MP/GF representation of X")
334 GET (_value
, "X._value -> internal MP/GF representation of X")
339 static const PyMethodDef fe_pymethods
[] = {
340 #define METHNAME(func) femeth_##func
341 NAMETH(inv
, "X.inv() -> X^{-1}")
342 NAMETH(sqr
, "X.sqr() -> X^2")
343 NAMETH(sqrt
, "X.sqrt() -> sqrt(X)")
344 NAMETH(quadsolve
, "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
345 NAMETH(dbl
, "X.dbl() -> 2 * X (prime only)")
346 NAMETH(tpl
, "X.tpl() -> 3 * X (prime only)")
347 NAMETH(qdl
, "X.qdl() -> 4 * X (prime only)")
348 NAMETH(hlv
, "X.hlv() -> X/2 (prime only)")
353 static const PyNumberMethods fe_pynumber
= {
354 fe_pyadd
, /* @nb_add@ */
355 fe_pysub
, /* @nb_subtract@ */
356 fe_pymul
, /* @nb_multiply@ */
357 fe_pydiv
, /* @nb_divide@ */
358 0, /* @nb_remainder@ */
360 fe_pyexp
, /* @nb_power@ */
361 fe_pyneg
, /* @nb_negative@ */
362 fe_pyid
, /* @nb_positive@ */
363 0, /* @nb_absolute@ */
364 fe_pynonzerop
, /* @nb_nonzero@ */
371 fe_pycoerce
, /* @nb_coerce@ */
372 fe_pyint
, /* @nb_int@ */
373 fe_pylong
, /* @nb_long@ */
374 0 /* meaningless */, /* @nb_float@ */
375 fe_pyoct
, /* @nb_oct@ */
376 fe_pyhex
, /* @nb_hex@ */
378 0, /* @nb_inplace_add@ */
379 0, /* @nb_inplace_subtract@ */
380 0, /* @nb_inplace_multiply@ */
381 0, /* @nb_inplace_divide@ */
382 0, /* @nb_inplace_remainder@ */
383 0, /* @nb_inplace_power@ */
384 0, /* @nb_inplace_lshift@ */
385 0, /* @nb_inplace_rshift@ */
386 0, /* @nb_inplace_and@ */
387 0, /* @nb_inplace_xor@ */
388 0, /* @nb_inplace_or@ */
390 0, /* @nb_floor_divide@ */
391 fe_pydiv
, /* @nb_true_divide@ */
392 0, /* @nb_inplace_floor_divide@ */
393 0, /* @nb_inplace_true_divide@ */
395 fe_pyint
, /* @nb_index@ */
398 static const PyTypeObject fe_pytype_skel
= {
399 PyVarObject_HEAD_INIT(0, 0) /* Header */
400 "FE", /* @tp_name@ */
401 sizeof(fe_pyobj
), /* @tp_basicsize@ */
402 0, /* @tp_itemsize@ */
404 fe_pydealloc
, /* @tp_dealloc@ */
406 0, /* @tp_getattr@ */
407 0, /* @tp_setattr@ */
408 0, /* @tp_compare@ */
410 PYNUMBER(fe
), /* @tp_as_number@ */
411 0, /* @tp_as_sequence@ */
412 0, /* @tp_as_mapping@ */
413 fe_pyhash
, /* @tp_hash@ */
415 fe_pyhex
, /* @tp_str@ */
416 0, /* @tp_getattro@ */
417 0, /* @tp_setattro@ */
418 0, /* @tp_as_buffer@ */
419 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
420 Py_TPFLAGS_CHECKTYPES
|
424 "Finite field elements, abstract base class.",
426 0, /* @tp_traverse@ */
428 fe_pyrichcompare
, /* @tp_richcompare@ */
429 0, /* @tp_weaklistoffset@ */
431 0, /* @tp_iternext@ */
432 PYMETHODS(fe
), /* @tp_methods@ */
433 PYMEMBERS(fe
), /* @tp_members@ */
434 PYGETSET(fe
), /* @tp_getset@ */
437 0, /* @tp_descr_get@ */
438 0, /* @tp_descr_set@ */
439 0, /* @tp_dictoffset@ */
441 PyType_GenericAlloc
, /* @tp_alloc@ */
442 abstract_pynew
, /* @tp_new@ */
447 /*----- Fields ------------------------------------------------------------*/
449 static PyObject
*field_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
451 int b
= field_samep(FIELD_F(x
), FIELD_F(y
));
455 default: TYERR("can't order fields");
462 static PyObject
*fmeth_rand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
464 static const char *const kwlist
[] = { "rng", 0 };
465 grand
*r
= &rand_global
;
467 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:rand", KWLIST
,
470 return (fe_pywrap(me
, F_RAND(FIELD_F(me
), MP_NEW
, r
)));
473 static PyObject
*fmeth__adopt(PyObject
*me
, PyObject
*arg
)
476 if (!PyArg_ParseTuple(arg
, "O&:_adopt", convmp
, &xx
)) return (0);
477 return (fe_pywrap(me
, xx
));
480 static PyObject
*fmeth_parse(PyObject
*me
, PyObject
*arg
)
487 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
489 if ((f
= field_parse(&qd
)) == 0) VALERR(qd
.e
);
490 rc
= Py_BuildValue("(Ns)", field_pywrap(f
), qd
.p
);
495 static void field_pydealloc(PyObject
*me
)
497 F_DESTROY(FIELD_F(me
));
498 PyType_Type
.tp_dealloc(me
);
501 static PyObject
*fget_zero(PyObject
*me
, void *hunoz
)
502 { return (fe_pywrap(me
, MP_COPY(FIELD_F(me
)->zero
))); }
504 static PyObject
*fget_one(PyObject
*me
, void *hunoz
)
505 { return (fe_pywrap(me
, MP_COPY(FIELD_F(me
)->one
))); }
507 static PyObject
*fget_q(PyObject
*me
, void *hunoz
)
508 { return (mp_pywrap(MP_COPY(FIELD_F(me
)->q
))); }
510 static PyObject
*fget_nbits(PyObject
*me
, void *hunoz
)
511 { return (PyInt_FromLong(FIELD_F(me
)->nbits
)); }
513 static PyObject
*fget_noctets(PyObject
*me
, void *hunoz
)
514 { return (PyInt_FromLong(FIELD_F(me
)->noctets
)); }
516 static PyObject
*fget_name(PyObject
*me
, void *hunoz
)
517 { return (TEXT_FROMSTR(F_NAME(FIELD_F(me
)))); }
519 static PyObject
*fget_type(PyObject
*me
, void *hunoz
)
520 { return (PyInt_FromLong(F_TYPE(FIELD_F(me
)))); }
522 static const PyGetSetDef field_pygetset
[] = {
523 #define GETSETNAME(op, name) f##op##_##name
524 GET (zero
, "F.zero -> field additive identity")
525 GET (one
, "F.one -> field multiplicative identity")
526 GET (q
, "F.q -> number of elements in field")
527 GET (nbits
, "F.nbits -> bits needed to represent element")
528 GET (noctets
, "F.noctets -> octetss needed to represent element")
529 GET (name
, "F.name -> name of this kind of field")
530 GET (type
, "F.type -> type code of this kind of field")
535 static const PyMethodDef field_pymethods
[] = {
536 #define METHNAME(name) fmeth_##name
537 METH (_adopt
, "F._adopt(X) -> FE")
538 KWMETH(rand
, "F.rand([rng = rand]) -> FE, uniformly distributed")
539 SMTH (parse
, "parse(STR) -> F, REST")
544 static const PyTypeObject field_pytype_skel
= {
545 PyVarObject_HEAD_INIT(0, 0) /* Header */
546 "Field", /* @tp_name@ */
547 sizeof(field_pyobj
), /* @tp_basicsize@ */
548 0, /* @tp_itemsize@ */
550 field_pydealloc
, /* @tp_dealloc@ */
552 0, /* @tp_getattr@ */
553 0, /* @tp_setattr@ */
554 0, /* @tp_compare@ */
556 0, /* @tp_as_number@ */
557 0, /* @tp_as_sequence@ */
558 0, /* @tp_as_mapping@ */
562 0, /* @tp_getattro@ */
563 0, /* @tp_setattro@ */
564 0, /* @tp_as_buffer@ */
565 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
569 "An abstract field. This is an abstract type.",
571 0, /* @tp_traverse@ */
573 field_pyrichcompare
, /* @tp_richcompare@ */
574 0, /* @tp_weaklistoffset@ */
576 0, /* @tp_iternext@ */
577 PYMETHODS(field
), /* @tp_methods@ */
578 0, /* @tp_members@ */
579 PYGETSET(field
), /* @tp_getset@ */
582 0, /* @tp_descr_get@ */
583 0, /* @tp_descr_set@ */
584 0, /* @tp_dictoffset@ */
586 PyType_GenericAlloc
, /* @tp_alloc@ */
587 abstract_pynew
, /* @tp_new@ */
592 /*----- Prime fields ------------------------------------------------------*/
594 static PyObject
*primefield_pynew(PyTypeObject
*ty
,
595 PyObject
*arg
, PyObject
*kw
)
599 static const char *const kwlist
[] = { "p", 0 };
601 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:primefield", KWLIST
,
604 if ((f
= field_prime(xx
)) == 0)
605 VALERR("bad prime for primefield");
607 return (field_dopywrap(ty
, f
));
613 static PyObject
*pfget_p(PyObject
*me
, void *hunoz
)
614 { return (mp_pywrap(MP_COPY(FIELD_F(me
)->m
))); }
616 static const PyGetSetDef primefield_pygetset
[] = {
617 #define GETSETNAME(op, name) pf##op##_##name
618 GET (p
, "F.p -> prime field characteristic")
622 static const PyTypeObject primefield_pytype_skel
= {
623 PyVarObject_HEAD_INIT(0, 0) /* Header */
624 "PrimeField", /* @tp_name@ */
625 sizeof(field_pyobj
), /* @tp_basicsize@ */
626 0, /* @tp_itemsize@ */
628 field_pydealloc
, /* @tp_dealloc@ */
630 0, /* @tp_getattr@ */
631 0, /* @tp_setattr@ */
632 0, /* @tp_compare@ */
634 0, /* @tp_as_number@ */
635 0, /* @tp_as_sequence@ */
636 0, /* @tp_as_mapping@ */
640 0, /* @tp_getattro@ */
641 0, /* @tp_setattro@ */
642 0, /* @tp_as_buffer@ */
643 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
647 "PrimeField(P): prime fields.",
649 0, /* @tp_traverse@ */
651 0, /* @tp_richcompare@ */
652 0, /* @tp_weaklistoffset@ */
654 0, /* @tp_iternext@ */
655 0, /* @tp_methods@ */
656 0, /* @tp_members@ */
657 PYGETSET(primefield
), /* @tp_getset@ */
660 0, /* @tp_descr_get@ */
661 0, /* @tp_descr_set@ */
662 0, /* @tp_dictoffset@ */
664 PyType_GenericAlloc
, /* @tp_alloc@ */
665 primefield_pynew
, /* @tp_new@ */
670 static PyObject
*niceprimefield_pynew(PyTypeObject
*ty
,
671 PyObject
*arg
, PyObject
*kw
)
675 static const char *const kwlist
[] = { "p", 0 };
677 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:niceprimefield",
678 KWLIST
, convmp
, &xx
))
680 if ((f
= field_niceprime(xx
)) == 0)
681 VALERR("bad prime for niceprimefield");
683 return (field_dopywrap(ty
, f
));
689 static const PyTypeObject niceprimefield_pytype_skel
= {
690 PyVarObject_HEAD_INIT(0, 0) /* Header */
691 "NicePrimeField", /* @tp_name@ */
692 sizeof(field_pyobj
), /* @tp_basicsize@ */
693 0, /* @tp_itemsize@ */
695 field_pydealloc
, /* @tp_dealloc@ */
697 0, /* @tp_getattr@ */
698 0, /* @tp_setattr@ */
699 0, /* @tp_compare@ */
701 0, /* @tp_as_number@ */
702 0, /* @tp_as_sequence@ */
703 0, /* @tp_as_mapping@ */
707 0, /* @tp_getattro@ */
708 0, /* @tp_setattro@ */
709 0, /* @tp_as_buffer@ */
710 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
714 "NicePrimeField(P): prime field using Solinas reduction.",
716 0, /* @tp_traverse@ */
718 0, /* @tp_richcompare@ */
719 0, /* @tp_weaklistoffset@ */
721 0, /* @tp_iternext@ */
722 0, /* @tp_methods@ */
723 0, /* @tp_members@ */
727 0, /* @tp_descr_get@ */
728 0, /* @tp_descr_set@ */
729 0, /* @tp_dictoffset@ */
731 PyType_GenericAlloc
, /* @tp_alloc@ */
732 niceprimefield_pynew
, /* @tp_new@ */
737 /*----- Binary fields -----------------------------------------------------*/
739 static PyObject
*bfget_m(PyObject
*me
, void *hunoz
)
740 { return (PyInt_FromLong(FIELD_F(me
)->nbits
)); }
742 static PyObject
*bfget_p(PyObject
*me
, void *hunoz
)
743 { return (gf_pywrap(MP_COPY(FIELD_F(me
)->m
))); }
745 static const PyGetSetDef binfield_pygetset
[] = {
746 #define GETSETNAME(op, name) bf##op##_##name
747 GET (m
, "F.m -> field polynomial degree")
748 GET (p
, "F.p -> field polynomial")
753 static const PyTypeObject binfield_pytype_skel
= {
754 PyVarObject_HEAD_INIT(0, 0) /* Header */
755 "BinField", /* @tp_name@ */
756 sizeof(field_pyobj
), /* @tp_basicsize@ */
757 0, /* @tp_itemsize@ */
759 field_pydealloc
, /* @tp_dealloc@ */
761 0, /* @tp_getattr@ */
762 0, /* @tp_setattr@ */
763 0, /* @tp_compare@ */
765 0, /* @tp_as_number@ */
766 0, /* @tp_as_sequence@ */
767 0, /* @tp_as_mapping@ */
771 0, /* @tp_getattro@ */
772 0, /* @tp_setattro@ */
773 0, /* @tp_as_buffer@ */
774 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
778 "Binary fields. Abstract class.",
780 0, /* @tp_traverse@ */
782 0, /* @tp_richcompare@ */
783 0, /* @tp_weaklistoffset@ */
785 0, /* @tp_iternext@ */
786 0, /* @tp_methods@ */
787 0, /* @tp_members@ */
788 PYGETSET(binfield
), /* @tp_getset@ */
791 0, /* @tp_descr_get@ */
792 0, /* @tp_descr_set@ */
793 0, /* @tp_dictoffset@ */
795 PyType_GenericAlloc
, /* @tp_alloc@ */
796 abstract_pynew
, /* @tp_new@ */
801 static PyObject
*binpolyfield_pynew(PyTypeObject
*ty
,
802 PyObject
*arg
, PyObject
*kw
)
806 static const char *const kwlist
[] = { "p", 0 };
808 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:binpolyfield", KWLIST
,
811 if ((f
= field_binpoly(xx
)) == 0) VALERR("bad poly for binpolyfield");
813 return (field_dopywrap(ty
, f
));
819 static const PyTypeObject binpolyfield_pytype_skel
= {
820 PyVarObject_HEAD_INIT(0, 0) /* Header */
821 "BinPolyField", /* @tp_name@ */
822 sizeof(field_pyobj
), /* @tp_basicsize@ */
823 0, /* @tp_itemsize@ */
825 field_pydealloc
, /* @tp_dealloc@ */
827 0, /* @tp_getattr@ */
828 0, /* @tp_setattr@ */
829 0, /* @tp_compare@ */
831 0, /* @tp_as_number@ */
832 0, /* @tp_as_sequence@ */
833 0, /* @tp_as_mapping@ */
837 0, /* @tp_getattro@ */
838 0, /* @tp_setattro@ */
839 0, /* @tp_as_buffer@ */
840 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
844 "BinPolyField(P): binary fields with polynomial basis representation.",
846 0, /* @tp_traverse@ */
848 0, /* @tp_richcompare@ */
849 0, /* @tp_weaklistoffset@ */
851 0, /* @tp_iternext@ */
852 0, /* @tp_methods@ */
853 0, /* @tp_members@ */
857 0, /* @tp_descr_get@ */
858 0, /* @tp_descr_set@ */
859 0, /* @tp_dictoffset@ */
861 PyType_GenericAlloc
, /* @tp_alloc@ */
862 binpolyfield_pynew
, /* @tp_new@ */
867 static PyObject
*binnormfield_pynew(PyTypeObject
*ty
,
868 PyObject
*arg
, PyObject
*kw
)
872 static const char *const kwlist
[] = { "p", "beta", 0 };
874 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:binnormfield",
875 KWLIST
, convgf
, &xx
, convgf
, &yy
))
877 if ((f
= field_binnorm(xx
, yy
)) == 0) VALERR("bad args for binnormfield");
878 MP_DROP(xx
); MP_DROP(yy
);
879 return (field_dopywrap(ty
, f
));
881 mp_drop(xx
); mp_drop(yy
);
885 static PyObject
*bnfget_beta(PyObject
*me
, void *hunoz
)
887 fctx_binnorm
*fc
= (fctx_binnorm
*)FIELD_F(me
);
888 return (gf_pywrap(MP_COPY(fc
->ntop
.r
[fc
->ntop
.n
- 1])));
891 static const PyGetSetDef binnormfield_pygetset
[] = {
892 #define GETSETNAME(op, name) bnf##op##_##name
893 GET (beta
, "F.beta -> conversion factor")
898 static const PyTypeObject binnormfield_pytype_skel
= {
899 PyVarObject_HEAD_INIT(0, 0) /* Header */
900 "BinNormField", /* @tp_name@ */
901 sizeof(field_pyobj
), /* @tp_basicsize@ */
902 0, /* @tp_itemsize@ */
904 field_pydealloc
, /* @tp_dealloc@ */
906 0, /* @tp_getattr@ */
907 0, /* @tp_setattr@ */
908 0, /* @tp_compare@ */
910 0, /* @tp_as_number@ */
911 0, /* @tp_as_sequence@ */
912 0, /* @tp_as_mapping@ */
916 0, /* @tp_getattro@ */
917 0, /* @tp_setattro@ */
918 0, /* @tp_as_buffer@ */
919 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
923 "BinNormField(P, BETA): binary fields with normal basis representation.",
925 0, /* @tp_traverse@ */
927 0, /* @tp_richcompare@ */
928 0, /* @tp_weaklistoffset@ */
930 0, /* @tp_iternext@ */
931 0, /* @tp_methods@ */
932 0, /* @tp_members@ */
933 PYGETSET(binnormfield
), /* @tp_getset@ */
936 0, /* @tp_descr_get@ */
937 0, /* @tp_descr_set@ */
938 0, /* @tp_dictoffset@ */
940 PyType_GenericAlloc
, /* @tp_alloc@ */
941 binnormfield_pynew
, /* @tp_new@ */
946 /*----- Setup -------------------------------------------------------------*/
948 static const struct nameval consts
[] = {
949 CONST(FTY_PRIME
), CONST(FTY_BINARY
),
953 void field_pyinit(void)
956 INITTYPE(field
, type
);
957 INITTYPE(primefield
, field
);
958 INITTYPE(niceprimefield
, primefield
);
959 INITTYPE(binfield
, field
);
960 INITTYPE(binpolyfield
, binfield
);
961 INITTYPE(binnormfield
, binfield
);
964 void field_pyinsert(PyObject
*mod
)
966 INSERT("FE", fe_pytype
);
967 INSERT("Field", field_pytype
);
968 INSERT("PrimeField", primefield_pytype
);
969 INSERT("NicePrimeField", niceprimefield_pytype
);
970 INSERT("BinField", binfield_pytype
);
971 INSERT("BinPolyField", binpolyfield_pytype
);
972 INSERT("BinNormField", binnormfield_pytype
);
973 setconstants(mod
, consts
);
976 /*----- That's all, folks -------------------------------------------------*/