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