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