chiark / gitweb /
rand.c, pgen.c: Invalidate random generators from pgen events.
[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   char *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") == 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;
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") == 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]);
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 mp *getfe(field *f, PyObject *o)
130 {
131   mp *x = 0;
132   if ((x = tofe(f, o)) == 0) {
133     PyErr_Format(PyExc_TypeError, "can't convert %.100s to fe",
134                  o->ob_type->tp_name);
135   }
136   return (x);
137 }
138
139 /*----- Field elements ----------------------------------------------------*/
140
141 static int febinop(PyObject *x, PyObject *y,
142                    field **f, PyObject **fobj, mp **xx, mp **yy)
143 {
144   if (FE_PYCHECK(x)) *fobj = FE_FOBJ(x);
145   else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y);
146   else return (-1);
147   *f = FIELD_F(*fobj);
148   if ((*xx = tofe(*f, x)) == 0)
149     return (-1);
150   if ((*yy = tofe(*f, y)) == 0) {
151     MP_DROP(*xx);
152     return (-1);
153   }
154   return (0);
155 }
156
157 #define BINOP(name)                                                     \
158   static PyObject *fe_py##name(PyObject *x, PyObject *y) {              \
159     PyObject *fobj;                                                     \
160     field *ff;                                                          \
161     mp *xx, *yy, *zz;                                                   \
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));                                       \
166   }
167 BINOP(add)
168 BINOP(sub)
169 BINOP(mul)
170 #undef BINOP
171
172 static PyObject *fe_pydiv(PyObject *x, PyObject *y)
173 {
174   PyObject *fobj;
175   field *ff;
176   mp *xx, *yy;
177   PyObject *z = 0;
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));
182 end:
183   MP_DROP(xx); MP_DROP(yy);
184   return (z);
185 }
186
187 static PyObject *fe_pyexp(PyObject *x, PyObject *y, PyObject *z)
188 {
189   field *ff;
190   mp *xx, *yy;
191
192   if (z != Py_None || !FE_PYCHECK(x) || (yy = tomp(y)) == 0)
193     RETURN_NOTIMPL;
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));
197 end:
198   MP_DROP(xx); MP_DROP(yy);
199   return (z);
200 }
201
202 static PyObject *fe_pyneg(PyObject *x)
203 {
204   return fe_pywrap(FE_FOBJ(x), FE_F(x)->ops->neg(FE_F(x), MP_NEW, FE_X(x)));
205 }
206
207 static PyObject *fe_pyid(PyObject *x) { RETURN_OBJ(x); }
208
209 static int fe_pynonzerop(PyObject *x) { return !F_ZEROP(FE_F(x), FE_X(x)); }
210
211 static PyObject *fe_pyrichcompare(PyObject *x, PyObject *y, int op)
212 {
213   PyObject *fobj;
214   field *ff;
215   mp *xx, *yy;
216   int b;
217   PyObject *rc = 0;
218
219   if (febinop(x, y, &ff, &fobj, &xx, &yy)) RETURN_NOTIMPL;
220   switch (op) {
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");
224   }
225   rc = getbool(b);
226 end:
227   MP_DROP(xx); MP_DROP(yy);
228   return (rc);
229 }
230
231 static long fe_pyhash(PyObject *me)
232   { return (mphash(FE_X(me))); }
233
234 static int fe_pycoerce(PyObject **x, PyObject **y)
235 {
236   mp *z;
237
238   if (FE_PYCHECK(*y)) {
239     if (FE_F(*x) != FE_F(*y) && !field_samep(FE_F(*x), FE_F(*y)))
240       TYERR("field mismatch");
241     Py_INCREF(*x); Py_INCREF(*y);
242     return (0);
243   }
244   if ((z = tofe(FE_F(*x), *y)) != 0) {
245     Py_INCREF(*x);
246     *y = fe_pywrap(FE_FOBJ(*x), z);
247     return (0);
248   }
249   return (1);
250
251 end:
252   return (-1);
253 }
254
255 static PyObject *fe_pyint(PyObject *x)
256 {
257   long l;
258   PyObject *rc;
259   mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
260   if (!mp_tolong_checked(xx, &l, 0)) rc = PyInt_FromLong(l);
261   else rc = mp_topylong(xx);
262   MP_DROP(xx);
263   return (rc);
264 }
265
266 static PyObject *fe_pylong(PyObject *x)
267 {
268   mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
269   PyObject *rc = mp_topylong(xx);
270   MP_DROP(xx);
271   return (rc);
272 }
273
274 #define BASEOP(name, radix, pre)                                        \
275   static PyObject *fe_py##name(PyObject *x) {                           \
276     mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));                           \
277     PyObject *rc = mp_topystring(xx, radix, 0, pre, 0);                 \
278     MP_DROP(xx);                                                        \
279     return (rc);                                                        \
280   }
281 BASEOP(oct, 8, "0");
282 BASEOP(hex, 16, "0x");
283 #undef BASEOP
284
285 static void fe_pydealloc(PyObject *me)
286 {
287   Py_DECREF(FE_FOBJ(me));
288   MP_DROP(FE_X(me));
289   FREEOBJ(me);
290 }
291
292 #define UNOP(name, check)                                               \
293   static PyObject *femeth_##name(PyObject *me, PyObject *arg) {         \
294     field *f = FE_F(me);                                                \
295     mp *x = FE_X(me);                                                   \
296     if (!PyArg_ParseTuple(arg, ":" #name)) return (0);                  \
297     if (!f->ops->name) TYERR(#name " not supported for this field");    \
298     check                                                               \
299     x = f->ops->name(f, MP_NEW, x);                                     \
300     if (!x) RETURN_NONE;                                                \
301     return (fe_pywrap(FE_FOBJ(me), x));                                 \
302   end:                                                                  \
303     return (0);                                                         \
304   }
305 UNOP(inv, if (F_ZEROP(f, x)) ZDIVERR("division by zero"); )
306 UNOP(sqr, ; )
307 UNOP(sqrt, ; )
308 UNOP(quadsolve, ; )
309 UNOP(dbl, ; )
310 UNOP(tpl, ; )
311 UNOP(qdl, ; )
312 UNOP(hlv, ; )
313 #undef UNOP
314
315 static PyObject *feget_field(PyObject *me, void *hunoz)
316   { RETURN_OBJ(FE_FOBJ(me)); }
317
318 static PyObject *feget_value(PyObject *me, void *hunoz)
319 {
320   mp *x = F_OUT(FE_F(me), MP_NEW, FE_X(me));
321   if (F_TYPE(FE_F(me)) == FTY_BINARY)
322     return (gf_pywrap(x));
323   else
324     return (mp_pywrap(x));
325 }
326
327 static PyObject *feget__value(PyObject *me, void *hunoz)
328 {
329   mp *x = FE_X(me);
330   MP_COPY(x);
331   if (F_TYPE(FE_F(me)) == FTY_BINARY)
332     return (gf_pywrap(x));
333   else
334     return (mp_pywrap(x));
335 }
336
337 static PyGetSetDef fe_pygetset[] = {
338 #define GETSETNAME(op, name) fe##op##_##name
339   GET   (field,         "X.field -> field containing X")
340   GET   (value,         "X.value -> `natural' MP/GF representation of X")
341   GET   (_value,        "X._value -> internal MP/GF representation of X")
342 #undef GETSETNAME
343   { 0 }
344 };
345
346 static PyMethodDef fe_pymethods[] = {
347 #define METHNAME(func) femeth_##func
348   METH  (inv,           "X.inv() -> X^{-1}")
349   METH  (sqr,           "X.sqr() -> X^2")
350   METH  (sqrt,          "X.sqrt() -> sqrt(X)")
351   METH  (quadsolve,     "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
352   METH  (dbl,           "X.dbl() -> 2 * X (prime only)")
353   METH  (tpl,           "X.tpl() -> 3 * X (prime only)")
354   METH  (qdl,           "X.qdl() -> 4 * X (prime only)")
355   METH  (hlv,           "X.hlv() -> X/2 (prime only)")
356 #undef METHNAME
357   { 0 }
358 };
359
360 static PyNumberMethods fe_pynumber = {
361   fe_pyadd,                             /* @nb_add@ */
362   fe_pysub,                             /* @nb_subtract@ */
363   fe_pymul,                             /* @nb_multiply@ */
364   fe_pydiv,                             /* @nb_divide@ */
365   0,                                    /* @nb_remainder@ */
366   0,                                    /* @nb_divmod@ */
367   fe_pyexp,                             /* @nb_power@ */
368   fe_pyneg,                             /* @nb_negative@ */
369   fe_pyid,                              /* @nb_positive@ */
370   0,                                    /* @nb_absolute@ */
371   fe_pynonzerop,                        /* @nb_nonzero@ */
372   0,                                    /* @nb_invert@ */
373   0,                                    /* @nb_lshift@ */
374   0,                                    /* @nb_rshift@ */
375   0,                                    /* @nb_and@ */
376   0,                                    /* @nb_xor@ */
377   0,                                    /* @nb_or@ */
378   fe_pycoerce,                          /* @nb_coerce@ */
379   fe_pyint,                             /* @nb_int@ */
380   fe_pylong,                            /* @nb_long@ */
381   0 /* meaningless */,                  /* @nb_float@ */
382   fe_pyoct,                             /* @nb_oct@ */
383   fe_pyhex,                             /* @nb_hex@ */
384
385   0,                                    /* @nb_inplace_add@ */
386   0,                                    /* @nb_inplace_subtract@ */
387   0,                                    /* @nb_inplace_multiply@ */
388   0,                                    /* @nb_inplace_divide@ */
389   0,                                    /* @nb_inplace_remainder@ */
390   0,                                    /* @nb_inplace_power@ */
391   0,                                    /* @nb_inplace_lshift@ */
392   0,                                    /* @nb_inplace_rshift@ */
393   0,                                    /* @nb_inplace_and@ */
394   0,                                    /* @nb_inplace_xor@ */
395   0,                                    /* @nb_inplace_or@ */
396
397   0,                                    /* @nb_floor_divide@ */
398   fe_pydiv,                             /* @nb_true_divide@ */
399   0,                                    /* @nb_inplace_floor_divide@ */
400   0,                                    /* @nb_inplace_true_divide@ */
401 };
402
403 static PyTypeObject fe_pytype_skel = {
404   PyObject_HEAD_INIT(0) 0,              /* Header */
405   "FE",                                 /* @tp_name@ */
406   sizeof(fe_pyobj),                     /* @tp_basicsize@ */
407   0,                                    /* @tp_itemsize@ */
408
409   fe_pydealloc,                         /* @tp_dealloc@ */
410   0,                                    /* @tp_print@ */
411   0,                                    /* @tp_getattr@ */
412   0,                                    /* @tp_setattr@ */
413   0,                                    /* @tp_compare@ */
414   0,                                    /* @tp_repr@ */
415   &fe_pynumber,                         /* @tp_as_number@ */
416   0,                                    /* @tp_as_sequence@ */
417   0,                                    /* @tp_as_mapping@ */
418   fe_pyhash,                            /* @tp_hash@ */
419   0,                                    /* @tp_call@ */
420   fe_pyhex,                             /* @tp_str@ */
421   0,                                    /* @tp_getattro@ */
422   0,                                    /* @tp_setattro@ */
423   0,                                    /* @tp_as_buffer@ */
424   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
425     Py_TPFLAGS_CHECKTYPES |
426     Py_TPFLAGS_BASETYPE,
427
428   /* @tp_doc@ */
429 "Finite field elements, abstract base class.",
430
431   0,                                    /* @tp_traverse@ */
432   0,                                    /* @tp_clear@ */
433   fe_pyrichcompare,                     /* @tp_richcompare@ */
434   0,                                    /* @tp_weaklistoffset@ */
435   0,                                    /* @tp_iter@ */
436   0,                                    /* @tp_iternext@ */
437   fe_pymethods,                         /* @tp_methods@ */
438   0,                                    /* @tp_members@ */
439   fe_pygetset,                          /* @tp_getset@ */
440   0,                                    /* @tp_base@ */
441   0,                                    /* @tp_dict@ */
442   0,                                    /* @tp_descr_get@ */
443   0,                                    /* @tp_descr_set@ */
444   0,                                    /* @tp_dictoffset@ */
445   0,                                    /* @tp_init@ */
446   PyType_GenericAlloc,                  /* @tp_alloc@ */
447   abstract_pynew,                       /* @tp_new@ */
448   0,                                    /* @tp_free@ */
449   0                                     /* @tp_is_gc@ */
450 };
451
452 /*----- Fields ------------------------------------------------------------*/
453
454 static PyObject *field_pyrichcompare(PyObject *x, PyObject *y, int op)
455 {
456   int b = field_samep(FIELD_F(x), FIELD_F(y));
457   switch (op) {
458     case Py_EQ: break;
459     case Py_NE: b = !b;
460     default: TYERR("can't order fields");
461   }
462   return (getbool(b));
463 end:
464   return (0);
465 }
466
467 static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
468 {
469   char *kwlist[] = { "rng", 0 };
470   grand *r = &rand_global;
471
472   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
473                                    convgrand, &r))
474     return (0);
475   return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
476 }
477
478 static PyObject *fmeth__adopt(PyObject *me, PyObject *arg)
479 {
480   mp *xx;
481   if (!PyArg_ParseTuple(arg, "O&:_adopt", convmp, &xx)) return (0);
482   return (fe_pywrap(me, xx));
483 }
484
485 static void field_pydealloc(PyObject *me)
486 {
487   F_DESTROY(FIELD_F(me));
488   PyType_Type.tp_dealloc(me);
489 }
490
491 static PyObject *fget_zero(PyObject *me, void *hunoz)
492   { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->zero))); }
493
494 static PyObject *fget_one(PyObject *me, void *hunoz)
495   { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->one))); }
496
497 static PyObject *fget_q(PyObject *me, void *hunoz)
498   { return (mp_pywrap(MP_COPY(FIELD_F(me)->q))); }
499
500 static PyObject *fget_nbits(PyObject *me, void *hunoz)
501   { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
502
503 static PyObject *fget_noctets(PyObject *me, void *hunoz)
504   { return (PyInt_FromLong(FIELD_F(me)->noctets)); }
505
506 static PyObject *fget_name(PyObject *me, void *hunoz)
507   { return (PyString_FromString(F_NAME(FIELD_F(me)))); }
508
509 static PyObject *fget_type(PyObject *me, void *hunoz)
510   { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
511
512 static PyGetSetDef field_pygetset[] = {
513 #define GETSETNAME(op, name) f##op##_##name
514   GET   (zero,          "F.zero -> field additive identity")
515   GET   (one,           "F.one -> field multiplicative identity")
516   GET   (q,             "F.q -> number of elements in field")
517   GET   (nbits,         "F.nbits -> bits needed to represent element")
518   GET   (noctets,       "F.noctets -> octetss needed to represent element")
519   GET   (name,          "F.name -> name of this kind of field")
520   GET   (type,          "F.type -> type code of this kind of field")
521 #undef GETSETNAME
522   { 0 }
523 };
524
525 static PyMethodDef field_pymethods[] = {
526 #define METHNAME(name) fmeth_##name
527   METH  (_adopt,        "F._adopt(X) -> FE")
528   KWMETH(rand,          "F.rand([rng = rand]) -> FE, uniformly distributed")
529 #undef METHNAME
530   { 0 }
531 };
532
533 static PyTypeObject field_pytype_skel = {
534   PyObject_HEAD_INIT(0) 0,              /* Header */
535   "Field",                              /* @tp_name@ */
536   sizeof(field_pyobj),                  /* @tp_basicsize@ */
537   0,                                    /* @tp_itemsize@ */
538
539   field_pydealloc,                      /* @tp_dealloc@ */
540   0,                                    /* @tp_print@ */
541   0,                                    /* @tp_getattr@ */
542   0,                                    /* @tp_setattr@ */
543   0,                                    /* @tp_compare@ */
544   0,                                    /* @tp_repr@ */
545   0,                                    /* @tp_as_number@ */
546   0,                                    /* @tp_as_sequence@ */
547   0,                                    /* @tp_as_mapping@ */
548   0,                                    /* @tp_hash@ */
549   0,                                    /* @tp_call@ */
550   0,                                    /* @tp_str@ */
551   0,                                    /* @tp_getattro@ */
552   0,                                    /* @tp_setattro@ */
553   0,                                    /* @tp_as_buffer@ */
554   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
555     Py_TPFLAGS_BASETYPE,
556
557   /* @tp_doc@ */
558 "An abstract field.  This is an abstract type.",
559
560   0,                                    /* @tp_traverse@ */
561   0,                                    /* @tp_clear@ */
562   field_pyrichcompare,                  /* @tp_richcompare@ */
563   0,                                    /* @tp_weaklistoffset@ */
564   0,                                    /* @tp_iter@ */
565   0,                                    /* @tp_iternext@ */
566   field_pymethods,                      /* @tp_methods@ */
567   0,                                    /* @tp_members@ */
568   field_pygetset,                       /* @tp_getset@ */
569   0,                                    /* @tp_base@ */
570   0,                                    /* @tp_dict@ */
571   0,                                    /* @tp_descr_get@ */
572   0,                                    /* @tp_descr_set@ */
573   0,                                    /* @tp_dictoffset@ */
574   0,                                    /* @tp_init@ */
575   PyType_GenericAlloc,                  /* @tp_alloc@ */
576   abstract_pynew,                       /* @tp_new@ */
577   0,                                    /* @tp_free@ */
578   0                                     /* @tp_is_gc@ */
579 };
580
581 /*----- Prime fields ------------------------------------------------------*/
582
583 static PyObject *primefield_pynew(PyTypeObject *ty,
584                                   PyObject *arg, PyObject *kw)
585 {
586   mp *xx = 0;
587   field *f;
588   char *kwlist[] = { "p", 0 };
589
590   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:primefield", kwlist,
591                                    convmp, &xx))
592     goto end;
593   if ((f = field_prime(xx)) == 0)
594     VALERR("bad prime for primefield");
595   MP_DROP(xx);
596   return (field_dopywrap(ty, f));
597 end:
598   mp_drop(xx);
599   return (0);
600 }
601
602 static PyObject *pfget_p(PyObject *me, void *hunoz)
603   { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
604
605 static PyGetSetDef primefield_pygetset[] = {
606 #define GETSETNAME(op, name) pf##op##_##name
607   GET   (p,             "F.p -> prime field characteristic")
608 #undef GETSETNAME
609 };
610
611 static PyTypeObject primefield_pytype_skel = {
612   PyObject_HEAD_INIT(0) 0,              /* Header */
613   "PrimeField",                         /* @tp_name@ */
614   sizeof(field_pyobj),                  /* @tp_basicsize@ */
615   0,                                    /* @tp_itemsize@ */
616
617   field_pydealloc,                      /* @tp_dealloc@ */
618   0,                                    /* @tp_print@ */
619   0,                                    /* @tp_getattr@ */
620   0,                                    /* @tp_setattr@ */
621   0,                                    /* @tp_compare@ */
622   0,                                    /* @tp_repr@ */
623   0,                                    /* @tp_as_number@ */
624   0,                                    /* @tp_as_sequence@ */
625   0,                                    /* @tp_as_mapping@ */
626   0,                                    /* @tp_hash@ */
627   0,                                    /* @tp_call@ */
628   0,                                    /* @tp_str@ */
629   0,                                    /* @tp_getattro@ */
630   0,                                    /* @tp_setattro@ */
631   0,                                    /* @tp_as_buffer@ */
632   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
633     Py_TPFLAGS_BASETYPE,
634
635   /* @tp_doc@ */
636   "Prime fields.",
637
638   0,                                    /* @tp_traverse@ */
639   0,                                    /* @tp_clear@ */
640   field_pyrichcompare,                  /* @tp_richcompare@ */
641   0,                                    /* @tp_weaklistoffset@ */
642   0,                                    /* @tp_iter@ */
643   0,                                    /* @tp_iternext@ */
644   0,                                    /* @tp_methods@ */
645   0,                                    /* @tp_members@ */
646   primefield_pygetset,                  /* @tp_getset@ */
647   0,                                    /* @tp_base@ */
648   0,                                    /* @tp_dict@ */
649   0,                                    /* @tp_descr_get@ */
650   0,                                    /* @tp_descr_set@ */
651   0,                                    /* @tp_dictoffset@ */
652   0,                                    /* @tp_init@ */
653   PyType_GenericAlloc,                  /* @tp_alloc@ */
654   primefield_pynew,                     /* @tp_new@ */
655   0,                                    /* @tp_free@ */
656   0                                     /* @tp_is_gc@ */
657 };
658
659 static PyObject *niceprimefield_pynew(PyTypeObject *ty,
660                                       PyObject *arg, PyObject *kw)
661 {
662   mp *xx = 0;
663   field *f;
664   char *kwlist[] = { "p", 0 };
665
666   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
667                                    kwlist, convmp, &xx))
668     goto end;
669   if ((f = field_niceprime(xx)) == 0)
670     VALERR("bad prime for niceprimefield");
671   MP_DROP(xx);
672   return (field_dopywrap(ty, f));
673 end:
674   mp_drop(xx);
675   return (0);
676 }
677
678 static PyTypeObject niceprimefield_pytype_skel = {
679   PyObject_HEAD_INIT(0) 0,              /* Header */
680   "NicePrimeField",                     /* @tp_name@ */
681   sizeof(field_pyobj),                  /* @tp_basicsize@ */
682   0,                                    /* @tp_itemsize@ */
683
684   field_pydealloc,                      /* @tp_dealloc@ */
685   0,                                    /* @tp_print@ */
686   0,                                    /* @tp_getattr@ */
687   0,                                    /* @tp_setattr@ */
688   0,                                    /* @tp_compare@ */
689   0,                                    /* @tp_repr@ */
690   0,                                    /* @tp_as_number@ */
691   0,                                    /* @tp_as_sequence@ */
692   0,                                    /* @tp_as_mapping@ */
693   0,                                    /* @tp_hash@ */
694   0,                                    /* @tp_call@ */
695   0,                                    /* @tp_str@ */
696   0,                                    /* @tp_getattro@ */
697   0,                                    /* @tp_setattro@ */
698   0,                                    /* @tp_as_buffer@ */
699   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
700     Py_TPFLAGS_BASETYPE,
701
702   /* @tp_doc@ */
703   "Nice prime fields.",
704
705   0,                                    /* @tp_traverse@ */
706   0,                                    /* @tp_clear@ */
707   field_pyrichcompare,                  /* @tp_richcompare@ */
708   0,                                    /* @tp_weaklistoffset@ */
709   0,                                    /* @tp_iter@ */
710   0,                                    /* @tp_iternext@ */
711   0,                                    /* @tp_methods@ */
712   0,                                    /* @tp_members@ */
713   0,                                    /* @tp_getset@ */
714   0,                                    /* @tp_base@ */
715   0,                                    /* @tp_dict@ */
716   0,                                    /* @tp_descr_get@ */
717   0,                                    /* @tp_descr_set@ */
718   0,                                    /* @tp_dictoffset@ */
719   0,                                    /* @tp_init@ */
720   PyType_GenericAlloc,                  /* @tp_alloc@ */
721   niceprimefield_pynew,                 /* @tp_new@ */
722   0,                                    /* @tp_free@ */
723   0                                     /* @tp_is_gc@ */
724 };
725
726 /*----- Binary fields -----------------------------------------------------*/
727
728 static PyObject *bfget_m(PyObject *me, void *hunoz)
729   { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
730
731 static PyGetSetDef binfield_pygetset[] = {
732 #define GETSETNAME(op, name) bf##op##_##name
733   GET   (m,             "F.m -> field polynomial degree")
734 #undef GETSETNAME
735   { 0 }
736 };
737
738 static PyTypeObject binfield_pytype_skel = {
739   PyObject_HEAD_INIT(0) 0,              /* Header */
740   "BinField",                           /* @tp_name@ */
741   sizeof(field_pyobj),                  /* @tp_basicsize@ */
742   0,                                    /* @tp_itemsize@ */
743
744   field_pydealloc,                      /* @tp_dealloc@ */
745   0,                                    /* @tp_print@ */
746   0,                                    /* @tp_getattr@ */
747   0,                                    /* @tp_setattr@ */
748   0,                                    /* @tp_compare@ */
749   0,                                    /* @tp_repr@ */
750   0,                                    /* @tp_as_number@ */
751   0,                                    /* @tp_as_sequence@ */
752   0,                                    /* @tp_as_mapping@ */
753   0,                                    /* @tp_hash@ */
754   0,                                    /* @tp_call@ */
755   0,                                    /* @tp_str@ */
756   0,                                    /* @tp_getattro@ */
757   0,                                    /* @tp_setattro@ */
758   0,                                    /* @tp_as_buffer@ */
759   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
760     Py_TPFLAGS_BASETYPE,
761
762   /* @tp_doc@ */
763   "Binary fields.  Abstract class.",
764
765   0,                                    /* @tp_traverse@ */
766   0,                                    /* @tp_clear@ */
767   field_pyrichcompare,                  /* @tp_richcompare@ */
768   0,                                    /* @tp_weaklistoffset@ */
769   0,                                    /* @tp_iter@ */
770   0,                                    /* @tp_iternext@ */
771   0,                                    /* @tp_methods@ */
772   0,                                    /* @tp_members@ */
773   binfield_pygetset,                    /* @tp_getset@ */
774   0,                                    /* @tp_base@ */
775   0,                                    /* @tp_dict@ */
776   0,                                    /* @tp_descr_get@ */
777   0,                                    /* @tp_descr_set@ */
778   0,                                    /* @tp_dictoffset@ */
779   0,                                    /* @tp_init@ */
780   PyType_GenericAlloc,                  /* @tp_alloc@ */
781   abstract_pynew,                       /* @tp_new@ */
782   0,                                    /* @tp_free@ */
783   0                                     /* @tp_is_gc@ */
784 };
785
786 static PyObject *binpolyfield_pynew(PyTypeObject *ty,
787                                     PyObject *arg, PyObject *kw)
788 {
789   mp *xx = 0;
790   field *f;
791   char *kwlist[] = { "p", 0 };
792
793   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield", kwlist,
794                                    convgf, &xx))
795     goto end;
796   if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
797   MP_DROP(xx);
798   return (field_dopywrap(ty, f));
799 end:
800   mp_drop(xx);
801   return (0);
802 }
803
804 static PyObject *bfget_p(PyObject *me, void *hunoz)
805   { return (gf_pywrap(MP_COPY(FIELD_F(me)->m))); }
806
807 static PyGetSetDef binpolyfield_pygetset[] = {
808 #define GETSETNAME(op, name) bf##op##_##name
809   GET   (p,             "F.p -> field polynomial")
810 #undef GETSETNAME
811   { 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   "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   binpolyfield_pygetset,                /* @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   char *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 PyGetSetDef binnormfield_pygetset[] = {
887 #define GETSETNAME(op, name) bf##op##_##name
888   GET   (p,             "F.p -> field polynomial")
889 #undef GETSETNAME
890 #define GETSETNAME(op, name) bnf##op##_##name
891   GET   (beta,          "F.beta -> conversion factor")
892 #undef GETSETNAME
893   { 0 }
894 };
895
896 static PyTypeObject binnormfield_pytype_skel = {
897   PyObject_HEAD_INIT(0) 0,              /* Header */
898   "BinNormField",                       /* @tp_name@ */
899   sizeof(field_pyobj),                  /* @tp_basicsize@ */
900   0,                                    /* @tp_itemsize@ */
901
902   field_pydealloc,                      /* @tp_dealloc@ */
903   0,                                    /* @tp_print@ */
904   0,                                    /* @tp_getattr@ */
905   0,                                    /* @tp_setattr@ */
906   0,                                    /* @tp_compare@ */
907   0,                                    /* @tp_repr@ */
908   0,                                    /* @tp_as_number@ */
909   0,                                    /* @tp_as_sequence@ */
910   0,                                    /* @tp_as_mapping@ */
911   0,                                    /* @tp_hash@ */
912   0,                                    /* @tp_call@ */
913   0,                                    /* @tp_str@ */
914   0,                                    /* @tp_getattro@ */
915   0,                                    /* @tp_setattro@ */
916   0,                                    /* @tp_as_buffer@ */
917   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
918     Py_TPFLAGS_BASETYPE,
919
920   /* @tp_doc@ */
921   "Binary fields with normal basis representation.",
922
923   0,                                    /* @tp_traverse@ */
924   0,                                    /* @tp_clear@ */
925   field_pyrichcompare,                  /* @tp_richcompare@ */
926   0,                                    /* @tp_weaklistoffset@ */
927   0,                                    /* @tp_iter@ */
928   0,                                    /* @tp_iternext@ */
929   0,                                    /* @tp_methods@ */
930   0,                                    /* @tp_members@ */
931   binnormfield_pygetset,                /* @tp_getset@ */
932   0,                                    /* @tp_base@ */
933   0,                                    /* @tp_dict@ */
934   0,                                    /* @tp_descr_get@ */
935   0,                                    /* @tp_descr_set@ */
936   0,                                    /* @tp_dictoffset@ */
937   0,                                    /* @tp_init@ */
938   PyType_GenericAlloc,                  /* @tp_alloc@ */
939   binnormfield_pynew,                   /* @tp_new@ */
940   0,                                    /* @tp_free@ */
941   0                                     /* @tp_is_gc@ */
942 };
943
944 /*----- Setup -------------------------------------------------------------*/
945
946 static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
947 {
948   field *f;
949   char *p;
950   PyObject *rc = 0;
951   qd_parse qd;
952
953   if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
954     goto end;
955   qd.p = p;
956   qd.e = 0;
957   if ((f = field_parse(&qd)) == 0)
958     VALERR(qd.e);
959   rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
960 end:
961   return (rc);
962 }
963
964 static PyMethodDef methods[] = {
965 #define METHNAME(func) meth_##func
966   METH  (_Field_parse,          "parse(STR) -> F, REST")
967 #undef METHNAME
968   { 0 }
969 };
970
971 void field_pyinit(void)
972 {
973   INITTYPE(fe, root);
974   INITTYPE(field, type);
975   INITTYPE(primefield, field);
976   INITTYPE(niceprimefield, primefield);
977   INITTYPE(binfield, field);
978   INITTYPE(binpolyfield, binfield);
979   INITTYPE(binnormfield, binfield);
980   addmethods(methods);
981 }
982
983 void field_pyinsert(PyObject *mod)
984 {
985   INSERT("FE", fe_pytype);
986   INSERT("Field", field_pytype);
987   INSERT("PrimeField", primefield_pytype);
988   INSERT("NicePrimeField", niceprimefield_pytype);
989   INSERT("BinField", binfield_pytype);
990   INSERT("BinPolyField", binpolyfield_pytype);
991   INSERT("BinNormField", binnormfield_pytype);
992 }
993
994 /*----- That's all, folks -------------------------------------------------*/