field.c, mp.c: Hash `GF' and `FE' objects the same as `MP'.
[catacomb-python] / field.c
CommitLineData
d7ab1bab 1/* -*-c-*-
2 *
d7ab1bab 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)
ab723d73 232 { return (mphash(FE_X(me))); }
d7ab1bab 233
234static 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
251end:
252 return (-1);
253}
254
255static PyObject *fe_pyint(PyObject *x)
256{
257 long l;
bc243788 258 PyObject *rc;
d7ab1bab 259 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
bc243788
MW
260 if (!mp_tolong_checked(xx, &l, 0)) rc = PyInt_FromLong(l);
261 else rc = mp_topylong(xx);
d7ab1bab 262 MP_DROP(xx);
bc243788 263 return (rc);
d7ab1bab 264}
265
266static PyObject *fe_pylong(PyObject *x)
267{
268 mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
f368b46e 269 PyObject *rc = mp_topylong(xx);
d7ab1bab 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(FE_X(x), radix, 0, pre, 0); \
278 MP_DROP(xx); \
279 return (rc); \
280 }
281BASEOP(oct, 8, "0");
282BASEOP(hex, 16, "0x");
283#undef BASEOP
284
285static void fe_pydealloc(PyObject *me)
286{
287 Py_DECREF(FE_FOBJ(me));
288 MP_DROP(FE_X(me));
3aa33042 289 FREEOBJ(me);
d7ab1bab 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 }
305UNOP(inv, if (F_ZEROP(f, x)) ZDIVERR("division by zero"); )
306UNOP(sqr, ; )
307UNOP(sqrt, ; )
308UNOP(quadsolve, ; )
309UNOP(dbl, ; )
310UNOP(tpl, ; )
311UNOP(qdl, ; )
312UNOP(hlv, ; )
313#undef UNOP
314
315static PyObject *feget_field(PyObject *me, void *hunoz)
316 { RETURN_OBJ(FE_FOBJ(me)); }
317
318static 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
327static 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
337static 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' integer representation of X")
341 GET (_value, "X._value -> internal integer representation of X")
342#undef GETSETNAME
343 { 0 }
344};
345
346static 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
360static 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
403static PyTypeObject fe_pytype_skel = {
6d4db0bf 404 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 405 "FE", /* @tp_name@ */
d7ab1bab 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@ */
963a6148 436 0, /* @tp_iternext@ */
d7ab1bab 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@ */
3aa33042 448 0, /* @tp_free@ */
d7ab1bab 449 0 /* @tp_is_gc@ */
450};
451
452/*----- Fields ------------------------------------------------------------*/
453
454static 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));
463end:
464 return (0);
465}
466
467static 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
478static 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
485static void field_pydealloc(PyObject *me)
486{
487 F_DESTROY(FIELD_F(me));
488 PyType_Type.tp_dealloc(me);
489}
490
491static PyObject *fget_zero(PyObject *me, void *hunoz)
492 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->zero))); }
493
494static PyObject *fget_one(PyObject *me, void *hunoz)
495 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->one))); }
496
497static PyObject *fget_q(PyObject *me, void *hunoz)
498 { return (mp_pywrap(MP_COPY(FIELD_F(me)->q))); }
499
500static PyObject *fget_nbits(PyObject *me, void *hunoz)
501 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
502
503static PyObject *fget_noctets(PyObject *me, void *hunoz)
504 { return (PyInt_FromLong(FIELD_F(me)->noctets)); }
505
506static PyObject *fget_name(PyObject *me, void *hunoz)
507 { return (PyString_FromString(F_NAME(FIELD_F(me)))); }
508
509static PyObject *fget_type(PyObject *me, void *hunoz)
510 { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
511
512static 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
525static 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
533static PyTypeObject field_pytype_skel = {
6d4db0bf 534 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 535 "Field", /* @tp_name@ */
d7ab1bab 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@ */
963a6148 565 0, /* @tp_iternext@ */
d7ab1bab 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@ */
3aa33042 577 0, /* @tp_free@ */
d7ab1bab 578 0 /* @tp_is_gc@ */
579};
580
581/*----- Prime fields ------------------------------------------------------*/
582
583static PyObject *primefield_pynew(PyTypeObject *ty,
584 PyObject *arg, PyObject *kw)
585{
586 mp *xx = 0;
587 field *f;
588 char *kwlist[] = { "p", 0 };
589
f368b46e 590 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:primefield", kwlist,
d7ab1bab 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));
597end:
598 mp_drop(xx);
599 return (0);
600}
601
602static PyObject *pfget_p(PyObject *me, void *hunoz)
603 { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
604
605static PyGetSetDef primefield_pygetset[] = {
606#define GETSETNAME(op, name) pf##op##_##name
b2687a0a 607 GET (p, "F.p -> prime field characteristic")
d7ab1bab 608#undef GETSETNAME
609};
610
611static PyTypeObject primefield_pytype_skel = {
6d4db0bf 612 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 613 "PrimeField", /* @tp_name@ */
d7ab1bab 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@ */
963a6148 643 0, /* @tp_iternext@ */
d7ab1bab 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@ */
3aa33042 655 0, /* @tp_free@ */
d7ab1bab 656 0 /* @tp_is_gc@ */
657};
658
659static 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));
673end:
674 mp_drop(xx);
675 return (0);
676}
677
678static PyTypeObject niceprimefield_pytype_skel = {
6d4db0bf 679 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 680 "NicePrimeField", /* @tp_name@ */
d7ab1bab 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@ */
963a6148 710 0, /* @tp_iternext@ */
d7ab1bab 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@ */
3aa33042 722 0, /* @tp_free@ */
d7ab1bab 723 0 /* @tp_is_gc@ */
724};
725
726/*----- Binary fields -----------------------------------------------------*/
727
728static PyObject *bfget_m(PyObject *me, void *hunoz)
729 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
730
731static PyGetSetDef binfield_pygetset[] = {
732#define GETSETNAME(op, name) bf##op##_##name
b2687a0a 733 GET (m, "F.m -> field polynomial degree")
d7ab1bab 734#undef GETSETNAME
735 { 0 }
736};
737
738static PyTypeObject binfield_pytype_skel = {
6d4db0bf 739 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 740 "BinField", /* @tp_name@ */
d7ab1bab 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@ */
963a6148 770 0, /* @tp_iternext@ */
d7ab1bab 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@ */
3aa33042 782 0, /* @tp_free@ */
d7ab1bab 783 0 /* @tp_is_gc@ */
784};
785
786static 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));
799end:
800 mp_drop(xx);
801 return (0);
802}
803
804static PyGetSetDef binpolyfield_pygetset[] = {
805#define GETSETNAME(op, name) pf##op##_##name
b2687a0a 806 GET (p, "F.p -> field polynomial")
d7ab1bab 807#undef GETSETNAME
808 { 0 }
809};
810
811static PyTypeObject binpolyfield_pytype_skel = {
6d4db0bf 812 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 813 "BinPolyField", /* @tp_name@ */
d7ab1bab 814 sizeof(field_pyobj), /* @tp_basicsize@ */
815 0, /* @tp_itemsize@ */
816
817 field_pydealloc, /* @tp_dealloc@ */
818 0, /* @tp_print@ */
819 0, /* @tp_getattr@ */
820 0, /* @tp_setattr@ */
821 0, /* @tp_compare@ */
822 0, /* @tp_repr@ */
823 0, /* @tp_as_number@ */
824 0, /* @tp_as_sequence@ */
825 0, /* @tp_as_mapping@ */
826 0, /* @tp_hash@ */
827 0, /* @tp_call@ */
828 0, /* @tp_str@ */
829 0, /* @tp_getattro@ */
830 0, /* @tp_setattro@ */
831 0, /* @tp_as_buffer@ */
832 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
833 Py_TPFLAGS_BASETYPE,
834
835 /* @tp_doc@ */
836 "Binary fields with polynomial basis representation.",
837
838 0, /* @tp_traverse@ */
839 0, /* @tp_clear@ */
840 field_pyrichcompare, /* @tp_richcompare@ */
841 0, /* @tp_weaklistoffset@ */
842 0, /* @tp_iter@ */
963a6148 843 0, /* @tp_iternext@ */
d7ab1bab 844 0, /* @tp_methods@ */
845 0, /* @tp_members@ */
846 binpolyfield_pygetset, /* @tp_getset@ */
847 0, /* @tp_base@ */
848 0, /* @tp_dict@ */
849 0, /* @tp_descr_get@ */
850 0, /* @tp_descr_set@ */
851 0, /* @tp_dictoffset@ */
852 0, /* @tp_init@ */
853 PyType_GenericAlloc, /* @tp_alloc@ */
854 binpolyfield_pynew, /* @tp_new@ */
3aa33042 855 0, /* @tp_free@ */
d7ab1bab 856 0 /* @tp_is_gc@ */
857};
858
859static PyObject *binnormfield_pynew(PyTypeObject *ty,
860 PyObject *arg, PyObject *kw)
861{
862 mp *xx = 0, *yy = 0;
863 field *f;
864 char *kwlist[] = { "p", "beta", 0 };
865
866 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
867 kwlist, convgf, &xx, convgf, &yy))
868 goto end;
869 if ((f = field_binnorm(xx, yy)) == 0) VALERR("bad args for binnormfield");
870 MP_DROP(xx); MP_DROP(yy);
871 return (field_dopywrap(ty, f));
872end:
873 mp_drop(xx); mp_drop(yy);
874 return (0);
875}
876
877static PyObject *bnfget_beta(PyObject *me, void *hunoz)
878{
879 fctx_binnorm *fc = (fctx_binnorm *)FIELD_F(me);
880 return (gf_pywrap(MP_COPY(fc->ntop.r[fc->ntop.n - 1])));
881}
882
883static PyGetSetDef binnormfield_pygetset[] = {
884#define GETSETNAME(op, name) pf##op##_##name
b2687a0a 885 GET (p, "F.p -> field polynomial")
d7ab1bab 886#undef GETSETNAME
887#define GETSETNAME(op, name) bnf##op##_##name
b2687a0a 888 GET (beta, "F.beta -> conversion factor")
d7ab1bab 889#undef GETSETNAME
890 { 0 }
891};
892
893static PyTypeObject binnormfield_pytype_skel = {
6d4db0bf 894 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 895 "BinNormField", /* @tp_name@ */
d7ab1bab 896 sizeof(field_pyobj), /* @tp_basicsize@ */
897 0, /* @tp_itemsize@ */
898
899 field_pydealloc, /* @tp_dealloc@ */
900 0, /* @tp_print@ */
901 0, /* @tp_getattr@ */
902 0, /* @tp_setattr@ */
903 0, /* @tp_compare@ */
904 0, /* @tp_repr@ */
905 0, /* @tp_as_number@ */
906 0, /* @tp_as_sequence@ */
907 0, /* @tp_as_mapping@ */
908 0, /* @tp_hash@ */
909 0, /* @tp_call@ */
910 0, /* @tp_str@ */
911 0, /* @tp_getattro@ */
912 0, /* @tp_setattro@ */
913 0, /* @tp_as_buffer@ */
914 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
915 Py_TPFLAGS_BASETYPE,
916
917 /* @tp_doc@ */
918 "Binary fields with normal basis representation.",
919
920 0, /* @tp_traverse@ */
921 0, /* @tp_clear@ */
922 field_pyrichcompare, /* @tp_richcompare@ */
923 0, /* @tp_weaklistoffset@ */
924 0, /* @tp_iter@ */
963a6148 925 0, /* @tp_iternext@ */
d7ab1bab 926 0, /* @tp_methods@ */
927 0, /* @tp_members@ */
928 binnormfield_pygetset, /* @tp_getset@ */
929 0, /* @tp_base@ */
930 0, /* @tp_dict@ */
931 0, /* @tp_descr_get@ */
932 0, /* @tp_descr_set@ */
933 0, /* @tp_dictoffset@ */
934 0, /* @tp_init@ */
935 PyType_GenericAlloc, /* @tp_alloc@ */
936 binnormfield_pynew, /* @tp_new@ */
3aa33042 937 0, /* @tp_free@ */
d7ab1bab 938 0 /* @tp_is_gc@ */
939};
940
941/*----- Setup -------------------------------------------------------------*/
942
943static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
944{
945 field *f;
946 char *p;
947 PyObject *rc = 0;
948 qd_parse qd;
949
950 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
951 goto end;
952 qd.p = p;
953 qd.e = 0;
954 if ((f = field_parse(&qd)) == 0)
80f7cd89 955 VALERR(qd.e);
d7ab1bab 956 rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
957end:
958 return (rc);
959}
960
961static PyMethodDef methods[] = {
962#define METHNAME(func) meth_##func
b2687a0a 963 METH (_Field_parse, "parse(STR) -> F, REST")
d7ab1bab 964#undef METHNAME
965 { 0 }
966};
967
968void field_pyinit(void)
969{
970 INITTYPE(fe, root);
971 INITTYPE(field, type);
972 INITTYPE(primefield, field);
973 INITTYPE(niceprimefield, primefield);
974 INITTYPE(binfield, field);
975 INITTYPE(binpolyfield, binfield);
976 INITTYPE(binnormfield, binfield);
977 addmethods(methods);
978}
979
980void field_pyinsert(PyObject *mod)
981{
982 INSERT("FE", fe_pytype);
983 INSERT("Field", field_pytype);
984 INSERT("PrimeField", primefield_pytype);
985 INSERT("NicePrimeField", niceprimefield_pytype);
986 INSERT("BinField", binfield_pytype);
987 INSERT("BinPolyField", binpolyfield_pytype);
988 INSERT("BinNormField", binnormfield_pytype);
989}
990
991/*----- That's all, folks -------------------------------------------------*/