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