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