chiark / gitweb /
t/t-algorithms.py: Add tests for other HSalsa20 and HChaCha key sizes.
[catacomb-python] / field.c
CommitLineData
d7ab1bab 1/* -*-c-*-
d7ab1bab 2 *
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
77 if (strcmp(F_NAME(f), "prime") == 0) ty = primefield_pytype;
78 else if (strcmp(F_NAME(f), "niceprime") == 0) ty = niceprimefield_pytype;
79 else if (strcmp(F_NAME(f), "binpoly") == 0) ty = binpolyfield_pytype;
80 else if (strcmp(F_NAME(f), "binnorm") == 0) ty = binnormfield_pytype;
81 else abort();
82 return (field_dopywrap(ty, f));
83}
84
85field *field_copy(field *f)
86{
87 if (strcmp(F_NAME(f), "prime") == 0)
88 f = field_prime(f->m);
89 else if (strcmp(F_NAME(f), "niceprime") == 0)
90 f = field_niceprime(f->m);
91 else if (strcmp(F_NAME(f), "binpoly") == 0)
92 f = field_binpoly(f->m);
93 else if (strcmp(F_NAME(f), "binnorm") == 0) {
94 fctx_binnorm *fc = (fctx_binnorm *)f;
95 f = field_binnorm(f->m, fc->ntop.r[fc->ntop.n - 1]);
96 } else
97 abort();
98 return (f);
99}
100
101PyObject *fe_pywrap(PyObject *fobj, mp *x)
102{
103 fe_pyobj *z = PyObject_New(fe_pyobj, (PyTypeObject *)fobj);
104 z->f = FIELD_F(fobj);
105 Py_INCREF(fobj);
106 z->x = x;
107 return ((PyObject *)z);
108}
109
110static mp *tofe(field *f, PyObject *o)
111{
112 mp *x = 0, *y = 0;
113
114 if (FE_PYCHECK(o)) {
115 if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
c62f9ebf
MW
116 y = MP_COPY(FE_X(o));
117 } else if ((x = tomp(o)) != 0) {
d7ab1bab 118 if (MP_ZEROP(x))
c62f9ebf 119 y = MP_COPY(f->zero);
d7ab1bab 120 else if (MP_EQ(x, MP_ONE))
c62f9ebf
MW
121 y = MP_COPY(f->one);
122 else
123 y = F_IN(f, MP_NEW, x);
124 MP_DROP(x);
d7ab1bab 125 }
d7ab1bab 126 return (y);
127}
128
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) \
283 static PyObject *femeth_##name(PyObject *me, PyObject *arg) { \
284 field *f = FE_F(me); \
285 mp *x = FE_X(me); \
286 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
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
305static PyObject *feget_field(PyObject *me, void *hunoz)
306 { RETURN_OBJ(FE_FOBJ(me)); }
307
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
327static PyGetSetDef fe_pygetset[] = {
328#define GETSETNAME(op, name) fe##op##_##name
329 GET (field, "X.field -> field containing X")
45c4c120
MW
330 GET (value, "X.value -> `natural' MP/GF representation of X")
331 GET (_value, "X._value -> internal MP/GF representation of X")
d7ab1bab 332#undef GETSETNAME
333 { 0 }
334};
335
336static PyMethodDef fe_pymethods[] = {
337#define METHNAME(func) femeth_##func
338 METH (inv, "X.inv() -> X^{-1}")
339 METH (sqr, "X.sqr() -> X^2")
340 METH (sqrt, "X.sqrt() -> sqrt(X)")
341 METH (quadsolve, "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
342 METH (dbl, "X.dbl() -> 2 * X (prime only)")
343 METH (tpl, "X.tpl() -> 3 * X (prime only)")
344 METH (qdl, "X.qdl() -> 4 * X (prime only)")
345 METH (hlv, "X.hlv() -> X/2 (prime only)")
346#undef METHNAME
347 { 0 }
348};
349
350static PyNumberMethods fe_pynumber = {
351 fe_pyadd, /* @nb_add@ */
352 fe_pysub, /* @nb_subtract@ */
353 fe_pymul, /* @nb_multiply@ */
354 fe_pydiv, /* @nb_divide@ */
355 0, /* @nb_remainder@ */
356 0, /* @nb_divmod@ */
357 fe_pyexp, /* @nb_power@ */
358 fe_pyneg, /* @nb_negative@ */
359 fe_pyid, /* @nb_positive@ */
360 0, /* @nb_absolute@ */
361 fe_pynonzerop, /* @nb_nonzero@ */
362 0, /* @nb_invert@ */
363 0, /* @nb_lshift@ */
364 0, /* @nb_rshift@ */
365 0, /* @nb_and@ */
366 0, /* @nb_xor@ */
367 0, /* @nb_or@ */
368 fe_pycoerce, /* @nb_coerce@ */
369 fe_pyint, /* @nb_int@ */
370 fe_pylong, /* @nb_long@ */
371 0 /* meaningless */, /* @nb_float@ */
372 fe_pyoct, /* @nb_oct@ */
373 fe_pyhex, /* @nb_hex@ */
374
375 0, /* @nb_inplace_add@ */
376 0, /* @nb_inplace_subtract@ */
377 0, /* @nb_inplace_multiply@ */
378 0, /* @nb_inplace_divide@ */
379 0, /* @nb_inplace_remainder@ */
380 0, /* @nb_inplace_power@ */
381 0, /* @nb_inplace_lshift@ */
382 0, /* @nb_inplace_rshift@ */
383 0, /* @nb_inplace_and@ */
384 0, /* @nb_inplace_xor@ */
385 0, /* @nb_inplace_or@ */
386
387 0, /* @nb_floor_divide@ */
388 fe_pydiv, /* @nb_true_divide@ */
389 0, /* @nb_inplace_floor_divide@ */
390 0, /* @nb_inplace_true_divide@ */
391};
392
393static PyTypeObject fe_pytype_skel = {
6d4db0bf 394 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 395 "FE", /* @tp_name@ */
d7ab1bab 396 sizeof(fe_pyobj), /* @tp_basicsize@ */
397 0, /* @tp_itemsize@ */
398
399 fe_pydealloc, /* @tp_dealloc@ */
400 0, /* @tp_print@ */
401 0, /* @tp_getattr@ */
402 0, /* @tp_setattr@ */
403 0, /* @tp_compare@ */
404 0, /* @tp_repr@ */
405 &fe_pynumber, /* @tp_as_number@ */
406 0, /* @tp_as_sequence@ */
407 0, /* @tp_as_mapping@ */
408 fe_pyhash, /* @tp_hash@ */
409 0, /* @tp_call@ */
410 fe_pyhex, /* @tp_str@ */
411 0, /* @tp_getattro@ */
412 0, /* @tp_setattro@ */
413 0, /* @tp_as_buffer@ */
414 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
415 Py_TPFLAGS_CHECKTYPES |
416 Py_TPFLAGS_BASETYPE,
417
418 /* @tp_doc@ */
419"Finite field elements, abstract base class.",
420
421 0, /* @tp_traverse@ */
422 0, /* @tp_clear@ */
423 fe_pyrichcompare, /* @tp_richcompare@ */
424 0, /* @tp_weaklistoffset@ */
425 0, /* @tp_iter@ */
963a6148 426 0, /* @tp_iternext@ */
d7ab1bab 427 fe_pymethods, /* @tp_methods@ */
428 0, /* @tp_members@ */
429 fe_pygetset, /* @tp_getset@ */
430 0, /* @tp_base@ */
431 0, /* @tp_dict@ */
432 0, /* @tp_descr_get@ */
433 0, /* @tp_descr_set@ */
434 0, /* @tp_dictoffset@ */
435 0, /* @tp_init@ */
436 PyType_GenericAlloc, /* @tp_alloc@ */
437 abstract_pynew, /* @tp_new@ */
3aa33042 438 0, /* @tp_free@ */
d7ab1bab 439 0 /* @tp_is_gc@ */
440};
441
442/*----- Fields ------------------------------------------------------------*/
443
444static PyObject *field_pyrichcompare(PyObject *x, PyObject *y, int op)
445{
446 int b = field_samep(FIELD_F(x), FIELD_F(y));
447 switch (op) {
448 case Py_EQ: break;
449 case Py_NE: b = !b;
450 default: TYERR("can't order fields");
451 }
452 return (getbool(b));
453end:
454 return (0);
455}
456
457static PyObject *fmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
458{
827f89d7 459 static const char *const kwlist[] = { "rng", 0 };
d7ab1bab 460 grand *r = &rand_global;
461
827f89d7 462 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", KWLIST,
d7ab1bab 463 convgrand, &r))
464 return (0);
465 return (fe_pywrap(me, F_RAND(FIELD_F(me), MP_NEW, r)));
466}
467
468static PyObject *fmeth__adopt(PyObject *me, PyObject *arg)
469{
470 mp *xx;
471 if (!PyArg_ParseTuple(arg, "O&:_adopt", convmp, &xx)) return (0);
472 return (fe_pywrap(me, xx));
473}
474
475static void field_pydealloc(PyObject *me)
476{
477 F_DESTROY(FIELD_F(me));
478 PyType_Type.tp_dealloc(me);
479}
480
481static PyObject *fget_zero(PyObject *me, void *hunoz)
482 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->zero))); }
483
484static PyObject *fget_one(PyObject *me, void *hunoz)
485 { return (fe_pywrap(me, MP_COPY(FIELD_F(me)->one))); }
486
487static PyObject *fget_q(PyObject *me, void *hunoz)
488 { return (mp_pywrap(MP_COPY(FIELD_F(me)->q))); }
489
490static PyObject *fget_nbits(PyObject *me, void *hunoz)
491 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
492
493static PyObject *fget_noctets(PyObject *me, void *hunoz)
494 { return (PyInt_FromLong(FIELD_F(me)->noctets)); }
495
496static PyObject *fget_name(PyObject *me, void *hunoz)
497 { return (PyString_FromString(F_NAME(FIELD_F(me)))); }
498
499static PyObject *fget_type(PyObject *me, void *hunoz)
500 { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
501
502static PyGetSetDef field_pygetset[] = {
503#define GETSETNAME(op, name) f##op##_##name
504 GET (zero, "F.zero -> field additive identity")
505 GET (one, "F.one -> field multiplicative identity")
506 GET (q, "F.q -> number of elements in field")
507 GET (nbits, "F.nbits -> bits needed to represent element")
508 GET (noctets, "F.noctets -> octetss needed to represent element")
509 GET (name, "F.name -> name of this kind of field")
510 GET (type, "F.type -> type code of this kind of field")
511#undef GETSETNAME
512 { 0 }
513};
514
515static PyMethodDef field_pymethods[] = {
516#define METHNAME(name) fmeth_##name
517 METH (_adopt, "F._adopt(X) -> FE")
1df8d5fe 518 KWMETH(rand, "F.rand([rng = rand]) -> FE, uniformly distributed")
d7ab1bab 519#undef METHNAME
520 { 0 }
521};
522
523static PyTypeObject field_pytype_skel = {
6d4db0bf 524 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 525 "Field", /* @tp_name@ */
d7ab1bab 526 sizeof(field_pyobj), /* @tp_basicsize@ */
527 0, /* @tp_itemsize@ */
528
529 field_pydealloc, /* @tp_dealloc@ */
530 0, /* @tp_print@ */
531 0, /* @tp_getattr@ */
532 0, /* @tp_setattr@ */
533 0, /* @tp_compare@ */
534 0, /* @tp_repr@ */
535 0, /* @tp_as_number@ */
536 0, /* @tp_as_sequence@ */
537 0, /* @tp_as_mapping@ */
538 0, /* @tp_hash@ */
539 0, /* @tp_call@ */
540 0, /* @tp_str@ */
541 0, /* @tp_getattro@ */
542 0, /* @tp_setattro@ */
543 0, /* @tp_as_buffer@ */
544 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
545 Py_TPFLAGS_BASETYPE,
546
547 /* @tp_doc@ */
548"An abstract field. This is an abstract type.",
549
550 0, /* @tp_traverse@ */
551 0, /* @tp_clear@ */
552 field_pyrichcompare, /* @tp_richcompare@ */
553 0, /* @tp_weaklistoffset@ */
554 0, /* @tp_iter@ */
963a6148 555 0, /* @tp_iternext@ */
d7ab1bab 556 field_pymethods, /* @tp_methods@ */
557 0, /* @tp_members@ */
558 field_pygetset, /* @tp_getset@ */
559 0, /* @tp_base@ */
560 0, /* @tp_dict@ */
561 0, /* @tp_descr_get@ */
562 0, /* @tp_descr_set@ */
563 0, /* @tp_dictoffset@ */
564 0, /* @tp_init@ */
565 PyType_GenericAlloc, /* @tp_alloc@ */
566 abstract_pynew, /* @tp_new@ */
3aa33042 567 0, /* @tp_free@ */
d7ab1bab 568 0 /* @tp_is_gc@ */
569};
570
571/*----- Prime fields ------------------------------------------------------*/
572
573static PyObject *primefield_pynew(PyTypeObject *ty,
574 PyObject *arg, PyObject *kw)
575{
576 mp *xx = 0;
577 field *f;
827f89d7 578 static const char *const kwlist[] = { "p", 0 };
d7ab1bab 579
827f89d7 580 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:primefield", KWLIST,
d7ab1bab 581 convmp, &xx))
582 goto end;
583 if ((f = field_prime(xx)) == 0)
584 VALERR("bad prime for primefield");
585 MP_DROP(xx);
586 return (field_dopywrap(ty, f));
587end:
588 mp_drop(xx);
589 return (0);
590}
591
592static PyObject *pfget_p(PyObject *me, void *hunoz)
593 { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
594
595static PyGetSetDef primefield_pygetset[] = {
596#define GETSETNAME(op, name) pf##op##_##name
b2687a0a 597 GET (p, "F.p -> prime field characteristic")
d7ab1bab 598#undef GETSETNAME
599};
600
601static PyTypeObject primefield_pytype_skel = {
6d4db0bf 602 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 603 "PrimeField", /* @tp_name@ */
d7ab1bab 604 sizeof(field_pyobj), /* @tp_basicsize@ */
605 0, /* @tp_itemsize@ */
606
607 field_pydealloc, /* @tp_dealloc@ */
608 0, /* @tp_print@ */
609 0, /* @tp_getattr@ */
610 0, /* @tp_setattr@ */
611 0, /* @tp_compare@ */
612 0, /* @tp_repr@ */
613 0, /* @tp_as_number@ */
614 0, /* @tp_as_sequence@ */
615 0, /* @tp_as_mapping@ */
616 0, /* @tp_hash@ */
617 0, /* @tp_call@ */
618 0, /* @tp_str@ */
619 0, /* @tp_getattro@ */
620 0, /* @tp_setattro@ */
621 0, /* @tp_as_buffer@ */
622 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
623 Py_TPFLAGS_BASETYPE,
624
625 /* @tp_doc@ */
06cd26e8 626"PrimeField(P): prime fields.",
d7ab1bab 627
628 0, /* @tp_traverse@ */
629 0, /* @tp_clear@ */
630 field_pyrichcompare, /* @tp_richcompare@ */
631 0, /* @tp_weaklistoffset@ */
632 0, /* @tp_iter@ */
963a6148 633 0, /* @tp_iternext@ */
d7ab1bab 634 0, /* @tp_methods@ */
635 0, /* @tp_members@ */
636 primefield_pygetset, /* @tp_getset@ */
637 0, /* @tp_base@ */
638 0, /* @tp_dict@ */
639 0, /* @tp_descr_get@ */
640 0, /* @tp_descr_set@ */
641 0, /* @tp_dictoffset@ */
642 0, /* @tp_init@ */
643 PyType_GenericAlloc, /* @tp_alloc@ */
644 primefield_pynew, /* @tp_new@ */
3aa33042 645 0, /* @tp_free@ */
d7ab1bab 646 0 /* @tp_is_gc@ */
647};
648
649static PyObject *niceprimefield_pynew(PyTypeObject *ty,
650 PyObject *arg, PyObject *kw)
651{
652 mp *xx = 0;
653 field *f;
827f89d7 654 static const char *const kwlist[] = { "p", 0 };
d7ab1bab 655
656 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:niceprimefield",
827f89d7 657 KWLIST, convmp, &xx))
d7ab1bab 658 goto end;
659 if ((f = field_niceprime(xx)) == 0)
660 VALERR("bad prime for niceprimefield");
661 MP_DROP(xx);
662 return (field_dopywrap(ty, f));
663end:
664 mp_drop(xx);
665 return (0);
666}
667
668static PyTypeObject niceprimefield_pytype_skel = {
6d4db0bf 669 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 670 "NicePrimeField", /* @tp_name@ */
d7ab1bab 671 sizeof(field_pyobj), /* @tp_basicsize@ */
672 0, /* @tp_itemsize@ */
673
674 field_pydealloc, /* @tp_dealloc@ */
675 0, /* @tp_print@ */
676 0, /* @tp_getattr@ */
677 0, /* @tp_setattr@ */
678 0, /* @tp_compare@ */
679 0, /* @tp_repr@ */
680 0, /* @tp_as_number@ */
681 0, /* @tp_as_sequence@ */
682 0, /* @tp_as_mapping@ */
683 0, /* @tp_hash@ */
684 0, /* @tp_call@ */
685 0, /* @tp_str@ */
686 0, /* @tp_getattro@ */
687 0, /* @tp_setattro@ */
688 0, /* @tp_as_buffer@ */
689 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
690 Py_TPFLAGS_BASETYPE,
691
692 /* @tp_doc@ */
06cd26e8 693"NicePrimeField(P): prime field using Solinas reduction.",
d7ab1bab 694
695 0, /* @tp_traverse@ */
696 0, /* @tp_clear@ */
697 field_pyrichcompare, /* @tp_richcompare@ */
698 0, /* @tp_weaklistoffset@ */
699 0, /* @tp_iter@ */
963a6148 700 0, /* @tp_iternext@ */
d7ab1bab 701 0, /* @tp_methods@ */
702 0, /* @tp_members@ */
703 0, /* @tp_getset@ */
704 0, /* @tp_base@ */
705 0, /* @tp_dict@ */
706 0, /* @tp_descr_get@ */
707 0, /* @tp_descr_set@ */
708 0, /* @tp_dictoffset@ */
709 0, /* @tp_init@ */
710 PyType_GenericAlloc, /* @tp_alloc@ */
711 niceprimefield_pynew, /* @tp_new@ */
3aa33042 712 0, /* @tp_free@ */
d7ab1bab 713 0 /* @tp_is_gc@ */
714};
715
716/*----- Binary fields -----------------------------------------------------*/
717
718static PyObject *bfget_m(PyObject *me, void *hunoz)
719 { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
720
721static PyGetSetDef binfield_pygetset[] = {
722#define GETSETNAME(op, name) bf##op##_##name
b2687a0a 723 GET (m, "F.m -> field polynomial degree")
d7ab1bab 724#undef GETSETNAME
725 { 0 }
726};
727
728static PyTypeObject binfield_pytype_skel = {
6d4db0bf 729 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 730 "BinField", /* @tp_name@ */
d7ab1bab 731 sizeof(field_pyobj), /* @tp_basicsize@ */
732 0, /* @tp_itemsize@ */
733
734 field_pydealloc, /* @tp_dealloc@ */
735 0, /* @tp_print@ */
736 0, /* @tp_getattr@ */
737 0, /* @tp_setattr@ */
738 0, /* @tp_compare@ */
739 0, /* @tp_repr@ */
740 0, /* @tp_as_number@ */
741 0, /* @tp_as_sequence@ */
742 0, /* @tp_as_mapping@ */
743 0, /* @tp_hash@ */
744 0, /* @tp_call@ */
745 0, /* @tp_str@ */
746 0, /* @tp_getattro@ */
747 0, /* @tp_setattro@ */
748 0, /* @tp_as_buffer@ */
749 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
750 Py_TPFLAGS_BASETYPE,
751
752 /* @tp_doc@ */
06cd26e8 753"Binary fields. Abstract class.",
d7ab1bab 754
755 0, /* @tp_traverse@ */
756 0, /* @tp_clear@ */
757 field_pyrichcompare, /* @tp_richcompare@ */
758 0, /* @tp_weaklistoffset@ */
759 0, /* @tp_iter@ */
963a6148 760 0, /* @tp_iternext@ */
d7ab1bab 761 0, /* @tp_methods@ */
762 0, /* @tp_members@ */
763 binfield_pygetset, /* @tp_getset@ */
764 0, /* @tp_base@ */
765 0, /* @tp_dict@ */
766 0, /* @tp_descr_get@ */
767 0, /* @tp_descr_set@ */
768 0, /* @tp_dictoffset@ */
769 0, /* @tp_init@ */
770 PyType_GenericAlloc, /* @tp_alloc@ */
771 abstract_pynew, /* @tp_new@ */
3aa33042 772 0, /* @tp_free@ */
d7ab1bab 773 0 /* @tp_is_gc@ */
774};
775
776static PyObject *binpolyfield_pynew(PyTypeObject *ty,
777 PyObject *arg, PyObject *kw)
778{
779 mp *xx = 0;
780 field *f;
827f89d7 781 static const char *const kwlist[] = { "p", 0 };
d7ab1bab 782
827f89d7 783 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:binpolyfield", KWLIST,
d7ab1bab 784 convgf, &xx))
785 goto end;
786 if ((f = field_binpoly(xx)) == 0) VALERR("bad poly for binpolyfield");
787 MP_DROP(xx);
788 return (field_dopywrap(ty, f));
789end:
790 mp_drop(xx);
791 return (0);
792}
793
56e8606c
MW
794static PyObject *bfget_p(PyObject *me, void *hunoz)
795 { return (gf_pywrap(MP_COPY(FIELD_F(me)->m))); }
796
d7ab1bab 797static PyGetSetDef binpolyfield_pygetset[] = {
56e8606c 798#define GETSETNAME(op, name) bf##op##_##name
b2687a0a 799 GET (p, "F.p -> field polynomial")
d7ab1bab 800#undef GETSETNAME
801 { 0 }
802};
803
804static PyTypeObject binpolyfield_pytype_skel = {
6d4db0bf 805 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 806 "BinPolyField", /* @tp_name@ */
d7ab1bab 807 sizeof(field_pyobj), /* @tp_basicsize@ */
808 0, /* @tp_itemsize@ */
809
810 field_pydealloc, /* @tp_dealloc@ */
811 0, /* @tp_print@ */
812 0, /* @tp_getattr@ */
813 0, /* @tp_setattr@ */
814 0, /* @tp_compare@ */
815 0, /* @tp_repr@ */
816 0, /* @tp_as_number@ */
817 0, /* @tp_as_sequence@ */
818 0, /* @tp_as_mapping@ */
819 0, /* @tp_hash@ */
820 0, /* @tp_call@ */
821 0, /* @tp_str@ */
822 0, /* @tp_getattro@ */
823 0, /* @tp_setattro@ */
824 0, /* @tp_as_buffer@ */
825 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
826 Py_TPFLAGS_BASETYPE,
827
828 /* @tp_doc@ */
06cd26e8 829"BinPolyField(P): binary fields with polynomial basis representation.",
d7ab1bab 830
831 0, /* @tp_traverse@ */
832 0, /* @tp_clear@ */
833 field_pyrichcompare, /* @tp_richcompare@ */
834 0, /* @tp_weaklistoffset@ */
835 0, /* @tp_iter@ */
963a6148 836 0, /* @tp_iternext@ */
d7ab1bab 837 0, /* @tp_methods@ */
838 0, /* @tp_members@ */
839 binpolyfield_pygetset, /* @tp_getset@ */
840 0, /* @tp_base@ */
841 0, /* @tp_dict@ */
842 0, /* @tp_descr_get@ */
843 0, /* @tp_descr_set@ */
844 0, /* @tp_dictoffset@ */
845 0, /* @tp_init@ */
846 PyType_GenericAlloc, /* @tp_alloc@ */
847 binpolyfield_pynew, /* @tp_new@ */
3aa33042 848 0, /* @tp_free@ */
d7ab1bab 849 0 /* @tp_is_gc@ */
850};
851
852static PyObject *binnormfield_pynew(PyTypeObject *ty,
853 PyObject *arg, PyObject *kw)
854{
855 mp *xx = 0, *yy = 0;
856 field *f;
827f89d7 857 static const char *const kwlist[] = { "p", "beta", 0 };
d7ab1bab 858
859 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:binnormfield",
827f89d7 860 KWLIST, convgf, &xx, convgf, &yy))
d7ab1bab 861 goto end;
862 if ((f = field_binnorm(xx, yy)) == 0) VALERR("bad args for binnormfield");
863 MP_DROP(xx); MP_DROP(yy);
864 return (field_dopywrap(ty, f));
865end:
866 mp_drop(xx); mp_drop(yy);
867 return (0);
868}
869
870static PyObject *bnfget_beta(PyObject *me, void *hunoz)
871{
872 fctx_binnorm *fc = (fctx_binnorm *)FIELD_F(me);
873 return (gf_pywrap(MP_COPY(fc->ntop.r[fc->ntop.n - 1])));
874}
875
876static PyGetSetDef binnormfield_pygetset[] = {
56e8606c 877#define GETSETNAME(op, name) bf##op##_##name
b2687a0a 878 GET (p, "F.p -> field polynomial")
d7ab1bab 879#undef GETSETNAME
880#define GETSETNAME(op, name) bnf##op##_##name
b2687a0a 881 GET (beta, "F.beta -> conversion factor")
d7ab1bab 882#undef GETSETNAME
883 { 0 }
884};
885
886static PyTypeObject binnormfield_pytype_skel = {
6d4db0bf 887 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 888 "BinNormField", /* @tp_name@ */
d7ab1bab 889 sizeof(field_pyobj), /* @tp_basicsize@ */
890 0, /* @tp_itemsize@ */
891
892 field_pydealloc, /* @tp_dealloc@ */
893 0, /* @tp_print@ */
894 0, /* @tp_getattr@ */
895 0, /* @tp_setattr@ */
896 0, /* @tp_compare@ */
897 0, /* @tp_repr@ */
898 0, /* @tp_as_number@ */
899 0, /* @tp_as_sequence@ */
900 0, /* @tp_as_mapping@ */
901 0, /* @tp_hash@ */
902 0, /* @tp_call@ */
903 0, /* @tp_str@ */
904 0, /* @tp_getattro@ */
905 0, /* @tp_setattro@ */
906 0, /* @tp_as_buffer@ */
907 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
908 Py_TPFLAGS_BASETYPE,
909
910 /* @tp_doc@ */
06cd26e8 911"BinNormField(P, BETA): binary fields with normal basis representation.",
d7ab1bab 912
913 0, /* @tp_traverse@ */
914 0, /* @tp_clear@ */
915 field_pyrichcompare, /* @tp_richcompare@ */
916 0, /* @tp_weaklistoffset@ */
917 0, /* @tp_iter@ */
963a6148 918 0, /* @tp_iternext@ */
d7ab1bab 919 0, /* @tp_methods@ */
920 0, /* @tp_members@ */
921 binnormfield_pygetset, /* @tp_getset@ */
922 0, /* @tp_base@ */
923 0, /* @tp_dict@ */
924 0, /* @tp_descr_get@ */
925 0, /* @tp_descr_set@ */
926 0, /* @tp_dictoffset@ */
927 0, /* @tp_init@ */
928 PyType_GenericAlloc, /* @tp_alloc@ */
929 binnormfield_pynew, /* @tp_new@ */
3aa33042 930 0, /* @tp_free@ */
d7ab1bab 931 0 /* @tp_is_gc@ */
932};
933
934/*----- Setup -------------------------------------------------------------*/
935
936static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
937{
938 field *f;
939 char *p;
940 PyObject *rc = 0;
941 qd_parse qd;
942
943 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
944 goto end;
945 qd.p = p;
946 qd.e = 0;
947 if ((f = field_parse(&qd)) == 0)
80f7cd89 948 VALERR(qd.e);
d7ab1bab 949 rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
950end:
951 return (rc);
952}
953
954static PyMethodDef methods[] = {
955#define METHNAME(func) meth_##func
b2687a0a 956 METH (_Field_parse, "parse(STR) -> F, REST")
d7ab1bab 957#undef METHNAME
958 { 0 }
959};
960
961void field_pyinit(void)
962{
963 INITTYPE(fe, root);
964 INITTYPE(field, type);
965 INITTYPE(primefield, field);
966 INITTYPE(niceprimefield, primefield);
967 INITTYPE(binfield, field);
968 INITTYPE(binpolyfield, binfield);
969 INITTYPE(binnormfield, binfield);
970 addmethods(methods);
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);
982}
983
984/*----- That's all, folks -------------------------------------------------*/