chiark / gitweb /
*.c: Use Python's facilities for defining class and static methods.
[catacomb-python] / field.c
1 /* -*-c-*-
2  *
3  * Abstract fields
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the Python interface to Catacomb.
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30
31 /*----- Various utilities -------------------------------------------------*/
32
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;
40
41 static PyObject *fe_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
42 {
43   PyObject *x;
44   mp *z;
45   static const char *const kwlist[] = { "x", 0 };
46
47   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:fe", KWLIST, &x))
48     return (0);
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));
53 }
54
55 static PyObject *field_dopywrap(PyTypeObject *ty, field *f)
56 {
57   field_pyobj *fobj = newtype(ty, 0, f->ops->name);
58   fobj->f = f;
59   fobj->ty.ht_type.tp_basicsize = sizeof(fe_pyobj);
60   fobj->ty.ht_type.tp_base = fe_pytype;
61   Py_INCREF(fe_pytype);
62   fobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
63                                Py_TPFLAGS_BASETYPE |
64                                Py_TPFLAGS_CHECKTYPES |
65                                Py_TPFLAGS_HEAPTYPE);
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);
71 }
72
73 PyObject *field_pywrap(field *f)
74 {
75   PyTypeObject *ty;
76
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;
81   else abort();
82   return (field_dopywrap(ty, f));
83 }
84
85 field *field_copy(field *f)
86 {
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]);
96   } else
97     abort();
98   return (f);
99 }
100
101 PyObject *fe_pywrap(PyObject *fobj, mp *x)
102 {
103   fe_pyobj *z = PyObject_New(fe_pyobj, (PyTypeObject *)fobj);
104   z->f = FIELD_F(fobj);
105   Py_INCREF(fobj);
106   z->x = x;
107   return ((PyObject *)z);
108 }
109
110 static mp *tofe(field *f, PyObject *o)
111 {
112   mp *x = 0, *y = 0;
113
114   if (FE_PYCHECK(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) {
118     if (MP_ZEROP(x))
119       y = MP_COPY(f->zero);
120     else if (MP_EQ(x, MP_ONE))
121       y = MP_COPY(f->one);
122     else
123       y = F_IN(f, MP_NEW, x);
124     MP_DROP(x);
125   }
126   return (y);
127 }
128
129 /*----- Field elements ----------------------------------------------------*/
130
131 static int febinop(PyObject *x, PyObject *y,
132                    field **f, PyObject **fobj, mp **xx, mp **yy)
133 {
134   if (FE_PYCHECK(x)) *fobj = FE_FOBJ(x);
135   else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y);
136   else return (-1);
137   *f = FIELD_F(*fobj);
138   if ((*xx = tofe(*f, x)) == 0)
139     return (-1);
140   if ((*yy = tofe(*f, y)) == 0) {
141     MP_DROP(*xx);
142     return (-1);
143   }
144   return (0);
145 }
146
147 #define BINOP(name)                                                     \
148   static PyObject *fe_py##name(PyObject *x, PyObject *y) {              \
149     PyObject *fobj;                                                     \
150     field *ff;                                                          \
151     mp *xx, *yy, *zz;                                                   \
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));                                       \
156   }
157 BINOP(add)
158 BINOP(sub)
159 BINOP(mul)
160 #undef BINOP
161
162 static PyObject *fe_pydiv(PyObject *x, PyObject *y)
163 {
164   PyObject *fobj;
165   field *ff;
166   mp *xx, *yy;
167   PyObject *z = 0;
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));
172 end:
173   MP_DROP(xx); MP_DROP(yy);
174   return (z);
175 }
176
177 static PyObject *fe_pyexp(PyObject *x, PyObject *y, PyObject *z)
178 {
179   field *ff;
180   mp *xx, *yy;
181
182   if (z != Py_None || !FE_PYCHECK(x) || (yy = tomp(y)) == 0)
183     RETURN_NOTIMPL;
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));
187 end:
188   MP_DROP(xx); MP_DROP(yy);
189   return (z);
190 }
191
192 static PyObject *fe_pyneg(PyObject *x)
193 {
194   return fe_pywrap(FE_FOBJ(x), FE_F(x)->ops->neg(FE_F(x), MP_NEW, FE_X(x)));
195 }
196
197 static PyObject *fe_pyid(PyObject *x) { RETURN_OBJ(x); }
198
199 static int fe_pynonzerop(PyObject *x) { return !F_ZEROP(FE_F(x), FE_X(x)); }
200
201 static PyObject *fe_pyrichcompare(PyObject *x, PyObject *y, int op)
202 {
203   PyObject *fobj;
204   field *ff;
205   mp *xx, *yy;
206   int b;
207   PyObject *rc = 0;
208
209   if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL;
210   switch (op) {
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");
214   }
215   rc = getbool(b);
216 end:
217   MP_DROP(xx); MP_DROP(yy);
218   return (rc);
219 }
220
221 static long fe_pyhash(PyObject *me)
222   { return (mphash(FE_X(me))); }
223
224 static int fe_pycoerce(PyObject **x, PyObject **y)
225 {
226   mp *z;
227
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);
232     return (0);
233   }
234   if ((z = tofe(FE_F(*x), *y)) != 0) {
235     Py_INCREF(*x);
236     *y = fe_pywrap(FE_FOBJ(*x), z);
237     return (0);
238   }
239   return (1);
240
241 end:
242   return (-1);
243 }
244
245 static PyObject *fe_pyint(PyObject *x)
246 {
247   long l;
248   PyObject *rc;
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);
252   MP_DROP(xx);
253   return (rc);
254 }
255
256 static PyObject *fe_pylong(PyObject *x)
257 {
258   mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
259   PyObject *rc = mp_topylong(xx);
260   MP_DROP(xx);
261   return (rc);
262 }
263
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);                 \
268     MP_DROP(xx);                                                        \
269     return (rc);                                                        \
270   }
271 BASEOP(oct, 8, "0");
272 BASEOP(hex, 16, "0x");
273 #undef BASEOP
274
275 static void fe_pydealloc(PyObject *me)
276 {
277   Py_DECREF(FE_FOBJ(me));
278   MP_DROP(FE_X(me));
279   FREEOBJ(me);
280 }
281
282 #define UNOP(name, check)                                               \
283   static PyObject *femeth_##name(PyObject *me, PyObject *arg) {         \
284     field *f = FE_F(me);                                                \
285     mp *x = FE_X(me);                                                   \
286     if (!PyArg_ParseTuple(arg, ":" #name)) return (0);                  \
287     if (!f->ops->name) TYERR(#name " not supported for this field");    \
288     check                                                               \
289     x = f->ops->name(f, MP_NEW, x);                                     \
290     if (!x) RETURN_NONE;                                                \
291     return (fe_pywrap(FE_FOBJ(me), x));                                 \
292   end:                                                                  \
293     return (0);                                                         \
294   }
295 UNOP(inv, if (F_ZEROP(f, x)) ZDIVERR("division by zero"); )
296 UNOP(sqr, ; )
297 UNOP(sqrt, ; )
298 UNOP(quadsolve, ; )
299 UNOP(dbl, ; )
300 UNOP(tpl, ; )
301 UNOP(qdl, ; )
302 UNOP(hlv, ; )
303 #undef UNOP
304
305 static PyObject *feget_field(PyObject *me, void *hunoz)
306   { RETURN_OBJ(FE_FOBJ(me)); }
307
308 static PyObject *feget_value(PyObject *me, void *hunoz)
309 {
310   mp *x = F_OUT(FE_F(me), MP_NEW, FE_X(me));
311   if (F_TYPE(FE_F(me)) == FTY_BINARY)
312     return (gf_pywrap(x));
313   else
314     return (mp_pywrap(x));
315 }
316
317 static PyObject *feget__value(PyObject *me, void *hunoz)
318 {
319   mp *x = FE_X(me);
320   MP_COPY(x);
321   if (F_TYPE(FE_F(me)) == FTY_BINARY)
322     return (gf_pywrap(x));
323   else
324     return (mp_pywrap(x));
325 }
326
327 static const PyGetSetDef fe_pygetset[] = {
328 #define GETSETNAME(op, name) fe##op##_##name
329   GET   (field,         "X.field -> field containing X")
330   GET   (value,         "X.value -> `natural' MP/GF representation of X")
331   GET   (_value,        "X._value -> internal MP/GF representation of X")
332 #undef GETSETNAME
333   { 0 }
334 };
335
336 static const PyMethodDef fe_pymethods[] = {
337 #define METHNAME(func) femeth_##func
338   METH  (inv,           "X.inv() -> X^{-1}")
339   METH  (sqr,           "X.sqr() -> X^2")
340   METH  (sqrt,          "X.sqrt() -> sqrt(X)")
341   METH  (quadsolve,     "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
342   METH  (dbl,           "X.dbl() -> 2 * X (prime only)")
343   METH  (tpl,           "X.tpl() -> 3 * X (prime only)")
344   METH  (qdl,           "X.qdl() -> 4 * X (prime only)")
345   METH  (hlv,           "X.hlv() -> X/2 (prime only)")
346 #undef METHNAME
347   { 0 }
348 };
349
350 static const PyNumberMethods fe_pynumber = {
351   fe_pyadd,                             /* @nb_add@ */
352   fe_pysub,                             /* @nb_subtract@ */
353   fe_pymul,                             /* @nb_multiply@ */
354   fe_pydiv,                             /* @nb_divide@ */
355   0,                                    /* @nb_remainder@ */
356   0,                                    /* @nb_divmod@ */
357   fe_pyexp,                             /* @nb_power@ */
358   fe_pyneg,                             /* @nb_negative@ */
359   fe_pyid,                              /* @nb_positive@ */
360   0,                                    /* @nb_absolute@ */
361   fe_pynonzerop,                        /* @nb_nonzero@ */
362   0,                                    /* @nb_invert@ */
363   0,                                    /* @nb_lshift@ */
364   0,                                    /* @nb_rshift@ */
365   0,                                    /* @nb_and@ */
366   0,                                    /* @nb_xor@ */
367   0,                                    /* @nb_or@ */
368   fe_pycoerce,                          /* @nb_coerce@ */
369   fe_pyint,                             /* @nb_int@ */
370   fe_pylong,                            /* @nb_long@ */
371   0 /* meaningless */,                  /* @nb_float@ */
372   fe_pyoct,                             /* @nb_oct@ */
373   fe_pyhex,                             /* @nb_hex@ */
374
375   0,                                    /* @nb_inplace_add@ */
376   0,                                    /* @nb_inplace_subtract@ */
377   0,                                    /* @nb_inplace_multiply@ */
378   0,                                    /* @nb_inplace_divide@ */
379   0,                                    /* @nb_inplace_remainder@ */
380   0,                                    /* @nb_inplace_power@ */
381   0,                                    /* @nb_inplace_lshift@ */
382   0,                                    /* @nb_inplace_rshift@ */
383   0,                                    /* @nb_inplace_and@ */
384   0,                                    /* @nb_inplace_xor@ */
385   0,                                    /* @nb_inplace_or@ */
386
387   0,                                    /* @nb_floor_divide@ */
388   fe_pydiv,                             /* @nb_true_divide@ */
389   0,                                    /* @nb_inplace_floor_divide@ */
390   0,                                    /* @nb_inplace_true_divide@ */
391 };
392
393 static PyTypeObject fe_pytype_skel = {
394   PyObject_HEAD_INIT(0) 0,              /* Header */
395   "FE",                                 /* @tp_name@ */
396   sizeof(fe_pyobj),                     /* @tp_basicsize@ */
397   0,                                    /* @tp_itemsize@ */
398
399   fe_pydealloc,                         /* @tp_dealloc@ */
400   0,                                    /* @tp_print@ */
401   0,                                    /* @tp_getattr@ */
402   0,                                    /* @tp_setattr@ */
403   0,                                    /* @tp_compare@ */
404   0,                                    /* @tp_repr@ */
405   PYNUMBER(fe),                         /* @tp_as_number@ */
406   0,                                    /* @tp_as_sequence@ */
407   0,                                    /* @tp_as_mapping@ */
408   fe_pyhash,                            /* @tp_hash@ */
409   0,                                    /* @tp_call@ */
410   fe_pyhex,                             /* @tp_str@ */
411   0,                                    /* @tp_getattro@ */
412   0,                                    /* @tp_setattro@ */
413   0,                                    /* @tp_as_buffer@ */
414   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
415     Py_TPFLAGS_CHECKTYPES |
416     Py_TPFLAGS_BASETYPE,
417
418   /* @tp_doc@ */
419   "Finite field elements, abstract base class.",
420
421   0,                                    /* @tp_traverse@ */
422   0,                                    /* @tp_clear@ */
423   fe_pyrichcompare,                     /* @tp_richcompare@ */
424   0,                                    /* @tp_weaklistoffset@ */
425   0,                                    /* @tp_iter@ */
426   0,                                    /* @tp_iternext@ */
427   PYMETHODS(fe),                        /* @tp_methods@ */
428   0,                                    /* @tp_members@ */
429   PYGETSET(fe),                         /* @tp_getset@ */
430   0,                                    /* @tp_base@ */
431   0,                                    /* @tp_dict@ */
432   0,                                    /* @tp_descr_get@ */
433   0,                                    /* @tp_descr_set@ */
434   0,                                    /* @tp_dictoffset@ */
435   0,                                    /* @tp_init@ */
436   PyType_GenericAlloc,                  /* @tp_alloc@ */
437   abstract_pynew,                       /* @tp_new@ */
438   0,                                    /* @tp_free@ */
439   0                                     /* @tp_is_gc@ */
440 };
441
442 /*----- Fields ------------------------------------------------------------*/
443
444 static PyObject *field_pyrichcompare(PyObject *x, PyObject *y, int op)
445 {
446   int b = field_samep(FIELD_F(x), FIELD_F(y));
447   switch (op) {
448     case Py_EQ: break;
449     case Py_NE: b = !b;
450     default: TYERR("can't order fields");
451   }
452   return (getbool(b));
453 end:
454   return (0);
455 }
456
457 static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
458 {
459   static const char *const kwlist[] = { "rng", 0 };
460   grand *r = &rand_global;
461
462   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", KWLIST,
463                                    convgrand, &r))
464     return (0);
465   return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
466 }
467
468 static PyObject *fmeth__adopt(PyObject *me, PyObject *arg)
469 {
470   mp *xx;
471   if (!PyArg_ParseTuple(arg, "O&:_adopt", convmp, &xx)) return (0);
472   return (fe_pywrap(me, xx));
473 }
474
475 static PyObject *fmeth_parse(PyObject *me, PyObject *arg)
476 {
477   field *f;
478   char *p;
479   PyObject *rc = 0;
480   qd_parse qd;
481
482   if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
483   qd.p = p; qd.e = 0;
484   if ((f = field_parse(&qd)) == 0) VALERR(qd.e);
485   rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
486 end:
487   return (rc);
488 }
489
490 static void field_pydealloc(PyObject *me)
491 {
492   F_DESTROY(FIELD_F(me));
493   PyType_Type.tp_dealloc(me);
494 }
495
496 static PyObject *fget_zero(PyObject *me, void *hunoz)
497   { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->zero))); }
498
499 static PyObject *fget_one(PyObject *me, void *hunoz)
500   { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->one))); }
501
502 static PyObject *fget_q(PyObject *me, void *hunoz)
503   { return (mp_pywrap(MP_COPY(FIELD_F(me)->q))); }
504
505 static PyObject *fget_nbits(PyObject *me, void *hunoz)
506   { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
507
508 static PyObject *fget_noctets(PyObject *me, void *hunoz)
509   { return (PyInt_FromLong(FIELD_F(me)->noctets)); }
510
511 static PyObject *fget_name(PyObject *me, void *hunoz)
512   { return (PyString_FromString(F_NAME(FIELD_F(me)))); }
513
514 static PyObject *fget_type(PyObject *me, void *hunoz)
515   { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
516
517 static const PyGetSetDef field_pygetset[] = {
518 #define GETSETNAME(op, name) f##op##_##name
519   GET   (zero,          "F.zero -> field additive identity")
520   GET   (one,           "F.one -> field multiplicative identity")
521   GET   (q,             "F.q -> number of elements in field")
522   GET   (nbits,         "F.nbits -> bits needed to represent element")
523   GET   (noctets,       "F.noctets -> octetss needed to represent element")
524   GET   (name,          "F.name -> name of this kind of field")
525   GET   (type,          "F.type -> type code of this kind of field")
526 #undef GETSETNAME
527   { 0 }
528 };
529
530 static const PyMethodDef field_pymethods[] = {
531 #define METHNAME(name) fmeth_##name
532   METH  (_adopt,        "F._adopt(X) -> FE")
533   KWMETH(rand,          "F.rand([rng = rand]) -> FE, uniformly distributed")
534   SMTH  (parse,         "parse(STR) -> F, REST")
535 #undef METHNAME
536   { 0 }
537 };
538
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@ */
544
545   field_pydealloc,                      /* @tp_dealloc@ */
546   0,                                    /* @tp_print@ */
547   0,                                    /* @tp_getattr@ */
548   0,                                    /* @tp_setattr@ */
549   0,                                    /* @tp_compare@ */
550   0,                                    /* @tp_repr@ */
551   0,                                    /* @tp_as_number@ */
552   0,                                    /* @tp_as_sequence@ */
553   0,                                    /* @tp_as_mapping@ */
554   0,                                    /* @tp_hash@ */
555   0,                                    /* @tp_call@ */
556   0,                                    /* @tp_str@ */
557   0,                                    /* @tp_getattro@ */
558   0,                                    /* @tp_setattro@ */
559   0,                                    /* @tp_as_buffer@ */
560   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
561     Py_TPFLAGS_BASETYPE,
562
563   /* @tp_doc@ */
564   "An abstract field.  This is an abstract type.",
565
566   0,                                    /* @tp_traverse@ */
567   0,                                    /* @tp_clear@ */
568   field_pyrichcompare,                  /* @tp_richcompare@ */
569   0,                                    /* @tp_weaklistoffset@ */
570   0,                                    /* @tp_iter@ */
571   0,                                    /* @tp_iternext@ */
572   PYMETHODS(field),                     /* @tp_methods@ */
573   0,                                    /* @tp_members@ */
574   PYGETSET(field),                      /* @tp_getset@ */
575   0,                                    /* @tp_base@ */
576   0,                                    /* @tp_dict@ */
577   0,                                    /* @tp_descr_get@ */
578   0,                                    /* @tp_descr_set@ */
579   0,                                    /* @tp_dictoffset@ */
580   0,                                    /* @tp_init@ */
581   PyType_GenericAlloc,                  /* @tp_alloc@ */
582   abstract_pynew,                       /* @tp_new@ */
583   0,                                    /* @tp_free@ */
584   0                                     /* @tp_is_gc@ */
585 };
586
587 /*----- Prime fields ------------------------------------------------------*/
588
589 static PyObject *primefield_pynew(PyTypeObject *ty,
590                                   PyObject *arg, PyObject *kw)
591 {
592   mp *xx = 0;
593   field *f;
594   static const char *const kwlist[] = { "p", 0 };
595
596   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:primefield", KWLIST,
597                                    convmp, &xx))
598     goto end;
599   if ((f = field_prime(xx)) == 0)
600     VALERR("bad prime for primefield");
601   MP_DROP(xx);
602   return (field_dopywrap(ty, f));
603 end:
604   mp_drop(xx);
605   return (0);
606 }
607
608 static PyObject *pfget_p(PyObject *me, void *hunoz)
609   { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
610
611 static const PyGetSetDef primefield_pygetset[] = {
612 #define GETSETNAME(op, name) pf##op##_##name
613   GET   (p,             "F.p -> prime field characteristic")
614 #undef GETSETNAME
615 };
616
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@ */
622
623   field_pydealloc,                      /* @tp_dealloc@ */
624   0,                                    /* @tp_print@ */
625   0,                                    /* @tp_getattr@ */
626   0,                                    /* @tp_setattr@ */
627   0,                                    /* @tp_compare@ */
628   0,                                    /* @tp_repr@ */
629   0,                                    /* @tp_as_number@ */
630   0,                                    /* @tp_as_sequence@ */
631   0,                                    /* @tp_as_mapping@ */
632   0,                                    /* @tp_hash@ */
633   0,                                    /* @tp_call@ */
634   0,                                    /* @tp_str@ */
635   0,                                    /* @tp_getattro@ */
636   0,                                    /* @tp_setattro@ */
637   0,                                    /* @tp_as_buffer@ */
638   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
639     Py_TPFLAGS_BASETYPE,
640
641   /* @tp_doc@ */
642   "PrimeField(P): prime fields.",
643
644   0,                                    /* @tp_traverse@ */
645   0,                                    /* @tp_clear@ */
646   field_pyrichcompare,                  /* @tp_richcompare@ */
647   0,                                    /* @tp_weaklistoffset@ */
648   0,                                    /* @tp_iter@ */
649   0,                                    /* @tp_iternext@ */
650   0,                                    /* @tp_methods@ */
651   0,                                    /* @tp_members@ */
652   PYGETSET(primefield),                 /* @tp_getset@ */
653   0,                                    /* @tp_base@ */
654   0,                                    /* @tp_dict@ */
655   0,                                    /* @tp_descr_get@ */
656   0,                                    /* @tp_descr_set@ */
657   0,                                    /* @tp_dictoffset@ */
658   0,                                    /* @tp_init@ */
659   PyType_GenericAlloc,                  /* @tp_alloc@ */
660   primefield_pynew,                     /* @tp_new@ */
661   0,                                    /* @tp_free@ */
662   0                                     /* @tp_is_gc@ */
663 };
664
665 static PyObject *niceprimefield_pynew(PyTypeObject *ty,
666                                       PyObject *arg, PyObject *kw)
667 {
668   mp *xx = 0;
669   field *f;
670   static const char *const kwlist[] = { "p", 0 };
671
672   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
673                                    KWLIST, convmp, &xx))
674     goto end;
675   if ((f = field_niceprime(xx)) == 0)
676     VALERR("bad prime for niceprimefield");
677   MP_DROP(xx);
678   return (field_dopywrap(ty, f));
679 end:
680   mp_drop(xx);
681   return (0);
682 }
683
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@ */
689
690   field_pydealloc,                      /* @tp_dealloc@ */
691   0,                                    /* @tp_print@ */
692   0,                                    /* @tp_getattr@ */
693   0,                                    /* @tp_setattr@ */
694   0,                                    /* @tp_compare@ */
695   0,                                    /* @tp_repr@ */
696   0,                                    /* @tp_as_number@ */
697   0,                                    /* @tp_as_sequence@ */
698   0,                                    /* @tp_as_mapping@ */
699   0,                                    /* @tp_hash@ */
700   0,                                    /* @tp_call@ */
701   0,                                    /* @tp_str@ */
702   0,                                    /* @tp_getattro@ */
703   0,                                    /* @tp_setattro@ */
704   0,                                    /* @tp_as_buffer@ */
705   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
706     Py_TPFLAGS_BASETYPE,
707
708   /* @tp_doc@ */
709   "NicePrimeField(P): prime field using Solinas reduction.",
710
711   0,                                    /* @tp_traverse@ */
712   0,                                    /* @tp_clear@ */
713   field_pyrichcompare,                  /* @tp_richcompare@ */
714   0,                                    /* @tp_weaklistoffset@ */
715   0,                                    /* @tp_iter@ */
716   0,                                    /* @tp_iternext@ */
717   0,                                    /* @tp_methods@ */
718   0,                                    /* @tp_members@ */
719   0,                                    /* @tp_getset@ */
720   0,                                    /* @tp_base@ */
721   0,                                    /* @tp_dict@ */
722   0,                                    /* @tp_descr_get@ */
723   0,                                    /* @tp_descr_set@ */
724   0,                                    /* @tp_dictoffset@ */
725   0,                                    /* @tp_init@ */
726   PyType_GenericAlloc,                  /* @tp_alloc@ */
727   niceprimefield_pynew,                 /* @tp_new@ */
728   0,                                    /* @tp_free@ */
729   0                                     /* @tp_is_gc@ */
730 };
731
732 /*----- Binary fields -----------------------------------------------------*/
733
734 static PyObject *bfget_m(PyObject *me, void *hunoz)
735   { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
736
737 static PyObject *bfget_p(PyObject *me, void *hunoz)
738   { return (gf_pywrap(MP_COPY(FIELD_F(me)->m))); }
739
740 static const PyGetSetDef binfield_pygetset[] = {
741 #define GETSETNAME(op, name) bf##op##_##name
742   GET   (m,             "F.m -> field polynomial degree")
743   GET   (p,             "F.p -> field polynomial")
744 #undef GETSETNAME
745   { 0 }
746 };
747
748 static PyTypeObject binfield_pytype_skel = {
749   PyObject_HEAD_INIT(0) 0,              /* Header */
750   "BinField",                           /* @tp_name@ */
751   sizeof(field_pyobj),                  /* @tp_basicsize@ */
752   0,                                    /* @tp_itemsize@ */
753
754   field_pydealloc,                      /* @tp_dealloc@ */
755   0,                                    /* @tp_print@ */
756   0,                                    /* @tp_getattr@ */
757   0,                                    /* @tp_setattr@ */
758   0,                                    /* @tp_compare@ */
759   0,                                    /* @tp_repr@ */
760   0,                                    /* @tp_as_number@ */
761   0,                                    /* @tp_as_sequence@ */
762   0,                                    /* @tp_as_mapping@ */
763   0,                                    /* @tp_hash@ */
764   0,                                    /* @tp_call@ */
765   0,                                    /* @tp_str@ */
766   0,                                    /* @tp_getattro@ */
767   0,                                    /* @tp_setattro@ */
768   0,                                    /* @tp_as_buffer@ */
769   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
770     Py_TPFLAGS_BASETYPE,
771
772   /* @tp_doc@ */
773   "Binary fields.  Abstract class.",
774
775   0,                                    /* @tp_traverse@ */
776   0,                                    /* @tp_clear@ */
777   field_pyrichcompare,                  /* @tp_richcompare@ */
778   0,                                    /* @tp_weaklistoffset@ */
779   0,                                    /* @tp_iter@ */
780   0,                                    /* @tp_iternext@ */
781   0,                                    /* @tp_methods@ */
782   0,                                    /* @tp_members@ */
783   PYGETSET(binfield),                   /* @tp_getset@ */
784   0,                                    /* @tp_base@ */
785   0,                                    /* @tp_dict@ */
786   0,                                    /* @tp_descr_get@ */
787   0,                                    /* @tp_descr_set@ */
788   0,                                    /* @tp_dictoffset@ */
789   0,                                    /* @tp_init@ */
790   PyType_GenericAlloc,                  /* @tp_alloc@ */
791   abstract_pynew,                       /* @tp_new@ */
792   0,                                    /* @tp_free@ */
793   0                                     /* @tp_is_gc@ */
794 };
795
796 static PyObject *binpolyfield_pynew(PyTypeObject *ty,
797                                     PyObject *arg, PyObject *kw)
798 {
799   mp *xx = 0;
800   field *f;
801   static const char *const kwlist[] = { "p", 0 };
802
803   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield", KWLIST,
804                                    convgf, &xx))
805     goto end;
806   if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
807   MP_DROP(xx);
808   return (field_dopywrap(ty, f));
809 end:
810   mp_drop(xx);
811   return (0);
812 }
813
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@ */
819
820   field_pydealloc,                      /* @tp_dealloc@ */
821   0,                                    /* @tp_print@ */
822   0,                                    /* @tp_getattr@ */
823   0,                                    /* @tp_setattr@ */
824   0,                                    /* @tp_compare@ */
825   0,                                    /* @tp_repr@ */
826   0,                                    /* @tp_as_number@ */
827   0,                                    /* @tp_as_sequence@ */
828   0,                                    /* @tp_as_mapping@ */
829   0,                                    /* @tp_hash@ */
830   0,                                    /* @tp_call@ */
831   0,                                    /* @tp_str@ */
832   0,                                    /* @tp_getattro@ */
833   0,                                    /* @tp_setattro@ */
834   0,                                    /* @tp_as_buffer@ */
835   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
836     Py_TPFLAGS_BASETYPE,
837
838   /* @tp_doc@ */
839   "BinPolyField(P): binary fields with polynomial basis representation.",
840
841   0,                                    /* @tp_traverse@ */
842   0,                                    /* @tp_clear@ */
843   field_pyrichcompare,                  /* @tp_richcompare@ */
844   0,                                    /* @tp_weaklistoffset@ */
845   0,                                    /* @tp_iter@ */
846   0,                                    /* @tp_iternext@ */
847   0,                                    /* @tp_methods@ */
848   0,                                    /* @tp_members@ */
849   0,                                    /* @tp_getset@ */
850   0,                                    /* @tp_base@ */
851   0,                                    /* @tp_dict@ */
852   0,                                    /* @tp_descr_get@ */
853   0,                                    /* @tp_descr_set@ */
854   0,                                    /* @tp_dictoffset@ */
855   0,                                    /* @tp_init@ */
856   PyType_GenericAlloc,                  /* @tp_alloc@ */
857   binpolyfield_pynew,                   /* @tp_new@ */
858   0,                                    /* @tp_free@ */
859   0                                     /* @tp_is_gc@ */
860 };
861
862 static PyObject *binnormfield_pynew(PyTypeObject *ty,
863                                     PyObject *arg, PyObject *kw)
864 {
865   mp *xx = 0, *yy = 0;
866   field *f;
867   static const char *const kwlist[] = { "p", "beta", 0 };
868
869   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
870                                    KWLIST, convgf, &xx, convgf, &yy))
871     goto end;
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));
875 end:
876   mp_drop(xx); mp_drop(yy);
877   return (0);
878 }
879
880 static PyObject *bnfget_beta(PyObject *me, void *hunoz)
881 {
882   fctx_binnorm *fc = (fctx_binnorm *)FIELD_F(me);
883   return (gf_pywrap(MP_COPY(fc->ntop.r[fc->ntop.n - 1])));
884 }
885
886 static const PyGetSetDef binnormfield_pygetset[] = {
887 #define GETSETNAME(op, name) bnf##op##_##name
888   GET   (beta,          "F.beta -> conversion factor")
889 #undef GETSETNAME
890   { 0 }
891 };
892
893 static PyTypeObject binnormfield_pytype_skel = {
894   PyObject_HEAD_INIT(0) 0,              /* Header */
895   "BinNormField",                       /* @tp_name@ */
896   sizeof(field_pyobj),                  /* @tp_basicsize@ */
897   0,                                    /* @tp_itemsize@ */
898
899   field_pydealloc,                      /* @tp_dealloc@ */
900   0,                                    /* @tp_print@ */
901   0,                                    /* @tp_getattr@ */
902   0,                                    /* @tp_setattr@ */
903   0,                                    /* @tp_compare@ */
904   0,                                    /* @tp_repr@ */
905   0,                                    /* @tp_as_number@ */
906   0,                                    /* @tp_as_sequence@ */
907   0,                                    /* @tp_as_mapping@ */
908   0,                                    /* @tp_hash@ */
909   0,                                    /* @tp_call@ */
910   0,                                    /* @tp_str@ */
911   0,                                    /* @tp_getattro@ */
912   0,                                    /* @tp_setattro@ */
913   0,                                    /* @tp_as_buffer@ */
914   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
915     Py_TPFLAGS_BASETYPE,
916
917   /* @tp_doc@ */
918   "BinNormField(P, BETA): binary fields with normal basis representation.",
919
920   0,                                    /* @tp_traverse@ */
921   0,                                    /* @tp_clear@ */
922   field_pyrichcompare,                  /* @tp_richcompare@ */
923   0,                                    /* @tp_weaklistoffset@ */
924   0,                                    /* @tp_iter@ */
925   0,                                    /* @tp_iternext@ */
926   0,                                    /* @tp_methods@ */
927   0,                                    /* @tp_members@ */
928   PYGETSET(binnormfield),               /* @tp_getset@ */
929   0,                                    /* @tp_base@ */
930   0,                                    /* @tp_dict@ */
931   0,                                    /* @tp_descr_get@ */
932   0,                                    /* @tp_descr_set@ */
933   0,                                    /* @tp_dictoffset@ */
934   0,                                    /* @tp_init@ */
935   PyType_GenericAlloc,                  /* @tp_alloc@ */
936   binnormfield_pynew,                   /* @tp_new@ */
937   0,                                    /* @tp_free@ */
938   0                                     /* @tp_is_gc@ */
939 };
940
941 /*----- Setup -------------------------------------------------------------*/
942
943 void field_pyinit(void)
944 {
945   INITTYPE(fe, root);
946   INITTYPE(field, type);
947   INITTYPE(primefield, field);
948   INITTYPE(niceprimefield, primefield);
949   INITTYPE(binfield, field);
950   INITTYPE(binpolyfield, binfield);
951   INITTYPE(binnormfield, binfield);
952 }
953
954 void field_pyinsert(PyObject *mod)
955 {
956   INSERT("FE", fe_pytype);
957   INSERT("Field", field_pytype);
958   INSERT("PrimeField", primefield_pytype);
959   INSERT("NicePrimeField", niceprimefield_pytype);
960   INSERT("BinField", binfield_pytype);
961   INSERT("BinPolyField", binpolyfield_pytype);
962   INSERT("BinNormField", binnormfield_pytype);
963 }
964
965 /*----- That's all, folks -------------------------------------------------*/