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