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