unsigned ql, pl;
unsigned steps = 0;
dsa_seed ds;
- char *k;
- Py_ssize_t ksz;
+ struct bin k;
struct excinfo exc = EXCINFO_INIT;
pypgev evt = { { 0 } };
static const char *const kwlist[] =
PyObject *rc = 0;
evt.exc = &exc;
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&s#|O&O&:gendsa", KWLIST,
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&|O&O&:gendsa", KWLIST,
convuint, &pl, convuint, &ql,
- &k, &ksz, convpgev, &evt,
- convuint, &steps))
+ convbin, &k,
+ convpgev, &evt, convuint, &steps))
goto end;
- if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.ev.proc, evt.ev.ctx))
+ if (dsa_gen(&dp, ql, pl, steps, k.p, k.sz, &ds, evt.ev.proc, evt.ev.ctx))
PGENERR(&exc);
rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
return (rc);
}
-static int npgroups = -1, nbingroups = -1;
-
-static PyObject *namedgroups(const pentry *pp, int *ne)
-{
- int i, j;
- const char *p;
- PyObject *d, *c;
-
- d = PyDict_New();
- for (i = 0; pp[i].name; i++) {
- p = pp[i].name;
- for (j = 0; j < i; j++) {
- if (pp[i].data == pp[j].data) {
- c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name);
- Py_INCREF(c);
- goto found;
- }
- }
- c = PyInt_FromLong(i);
- found:
- PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
- Py_DECREF(c);
- }
- *ne = i;
- return (d);
-}
-
-static PyObject *meth__groupn(PyObject *me, PyObject *arg,
- PyTypeObject *ty, const pentry *pp, int ne)
-{
- int i;
- gprime_param gp;
- PyObject *rc = 0;
-
- if (!PyArg_ParseTuple(arg, "i:_groupn", &i)) goto end;
- if (i < 0 || i >= ne) VALERR("group index out of range");
- dh_infofromdata(&gp, pp[i].data);
- rc = fginfo_pywrap(&gp, ty);
-end:
- return (rc);
-}
-
-static PyObject *dimeth__groupn(PyObject *me, PyObject *arg)
- { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); }
-
-static PyObject *bimeth__groupn(PyObject *me, PyObject *arg)
- { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); }
-
static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
int (*parse)(qd_parse *, gprime_param *))
{
static const PyMethodDef dhinfo_pymethods[] = {
#define METHNAME(name) dimeth_##name
SMTH (parse, "parse(STR) -> D, REST")
- SMTH (_groupn, 0)
KWSMTH(generate,
"generate(PBITS, [qbits = 0], [event = pgen_nullev],\n"
" [rng = rand], [nsteps = 0]) -> D")
static const PyMethodDef bindhinfo_pymethods[] = {
#define METHNAME(name) bimeth_##name
SMTH (parse, "parse(STR) -> D, REST")
- SMTH (_groupn, 0)
#undef METHNAME
{ 0 }
};
static const PyTypeObject fginfo_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"FGInfo", /* @tp_name@ */
sizeof(fginfo_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
};
static const PyTypeObject dhinfo_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"DHInfo", /* @tp_name@ */
sizeof(fginfo_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
};
static const PyTypeObject bindhinfo_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"BinDHInfo", /* @tp_name@ */
sizeof(fginfo_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
ec p = EC_INIT;
mp *y = 0;
ge *xx = 0;
+ size_t n;
mptext_stringctx sc;
g = GROUP_G(ty);
if (G_FROMINT(g, xx, y))
TYERR("can't convert from integer");
MP_DROP(y);
- } else if (PyString_Check(x)) {
- sc.buf = PyString_AS_STRING(x);
- sc.lim = sc.buf + PyString_GET_SIZE(x);
+ } else if (TEXT_CHECK(x)) {
+ TEXT_PTRLEN(x, sc.buf, n); sc.lim = sc.buf + n;
if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim)
VALERR("malformed group element string");
} else
mp *nn;
ge *z;
- if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0)
+ if (m != Py_None || !GE_PYCHECK(x)) RETURN_NOTIMPL;
+ if (FE_PYCHECK(n) && FE_F(n)->ops->ty == FTY_PRIME)
+ nn = F_OUT(FE_F(n), MP_NEW, FE_X(n));
+ else if ((nn = implicitmp(n)) == 0)
RETURN_NOTIMPL;
z = G_CREATE(GE_G(x));
G_EXP(GE_G(x), z, GE_X(x), nn);
PyObject *rc;
group_writedstr(GE_G(me), GE_X(me), &d);
- rc = PyString_FromStringAndSize(d.buf, d.len);
+ rc = TEXT_FROMSTRLEN(d.buf, d.len);
DDESTROY(&d);
return (rc);
}
+#ifdef PY2
static PyObject *ge_pylong(PyObject *me)
{
mp *x = 0;
mp_drop(x);
return (rc);
}
+#endif
static PyObject *ge_pyint(PyObject *me)
{
n = GE_G(me)->noctets + 4;
rc = bytestring_pywrap(0, n);
- buf_init(&b, PyString_AS_STRING(rc), n);
+ buf_init(&b, BIN_PTR(rc), n);
G_TOBUF(GE_G(me), &b, GE_X(me));
assert(BOK(&b));
- _PyString_Resize(&rc, BLEN(&b));
+ BIN_SETLEN(rc, BLEN(&b));
return (rc);
}
n = GE_G(me)->noctets;
rc = bytestring_pywrap(0, n);
- buf_init(&b, PyString_AS_STRING(rc), n);
+ buf_init(&b, BIN_PTR(rc), n);
G_TORAW(GE_G(me), &b, GE_X(me));
assert(BOK(&b));
- _PyString_Resize(&rc, BLEN(&b));
+ BIN_SETLEN(rc, BLEN(&b));
return (rc);
}
-static PyObject *gmexp_exp(PyObject *me, void *pp, int n)
+static PyObject *gmexp_exp(PyObject *me, void *pp, size_t n)
{
ge *z = G_CREATE(GROUP_G(me));
G_MEXP(GROUP_G(me), z, pp, n);
static PyObject *gemeth_frombuf(PyObject *me, PyObject *arg)
{
buf b;
- char *p;
- Py_ssize_t n;
+ struct bin in;
group *g;
ge *x = 0;
- if (!PyArg_ParseTuple(arg, "s#:frombuf", &p, &n)) return (0);
+ if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) return (0);
g = GROUP_G(me);
- buf_init(&b, p, n);
+ buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
x = G_CREATE(g);
if (G_FROMBUF(g, &b, x)) VALERR("invalid data");
return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
static PyObject *gemeth_fromraw(PyObject *me, PyObject *arg)
{
buf b;
- char *p;
- Py_ssize_t n;
+ struct bin in;
group *g;
ge *x = 0;
- if (!PyArg_ParseTuple(arg, "s#:fromraw", &p, &n)) return (0);
+ if (!PyArg_ParseTuple(arg, "O&:fromraw", convbin, &in)) return (0);
g = GROUP_G(me);
- buf_init(&b, p, n);
+ buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
x = G_CREATE(g);
if (G_FROMRAW(g, &b, x)) VALERR("invalid data");
return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
G_COPY(g, x, g->g); return (ge_pywrap(me, x));
}
-static long ge_pyhash(PyObject *me)
+static Py_hash_t ge_pyhash(PyObject *me)
{
buf b;
size_t sz = GE_G(me)->noctets + 4;
0, /* @nb_add@ */
0, /* @nb_subtract@ */
ge_pymul, /* @nb_multiply@ */
+#ifdef PY2
ge_pydiv, /* @nb_divide@ */
+#endif
0, /* @nb_remainder@ */
0, /* @nb_divmod@ */
ge_pyexp, /* @nb_power@ */
0, /* @nb_and@ */
0, /* @nb_xor@ */
0, /* @nb_or@ */
+#ifdef PY2
0, /* @nb_coerce@ */
+#endif
ge_pyint, /* @nb_int@ */
- ge_pylong, /* @nb_long@ */
+ PY23(ge_pylong, 0), /* @nb_long@ */
0 /* meaningless */, /* @nb_float@ */
+#ifdef PY2
0, /* @nb_oct@ */
0, /* @nb_hex@ */
+#endif
0, /* @nb_inplace_add@ */
0, /* @nb_inplace_subtract@ */
0, /* @nb_inplace_multiply@ */
+#ifdef PY2
0, /* @nb_inplace_divide@ */
+#endif
0, /* @nb_inplace_remainder@ */
0, /* @nb_inplace_power@ */
0, /* @nb_inplace_lshift@ */
};
static const PyTypeObject ge_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"GE", /* @tp_name@ */
sizeof(ge_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
};
static const PyTypeObject group_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"Group", /* @tp_name@ */
sizeof(group_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
}
static const PyTypeObject primegroup_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"PrimeGroup", /* @tp_name@ */
sizeof(group_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
}
static const PyTypeObject bingroup_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"BinGroup", /* @tp_name@ */
sizeof(group_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
}
static const PyTypeObject ecgroup_pytype_skel = {
- PyObject_HEAD_INIT(0) 0, /* Header */
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
"ECGroup", /* @tp_name@ */
sizeof(group_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
INITTYPE(ecgroup, group);
}
+static const char *grp_namefn(const void *p)
+ { const pentry *pt = p; return (pt->name); }
+
+static int grp_ixfn(const pentry *tab, const pentry *pt)
+{
+ int i;
+
+ for (i = 0; tab[i].name; i++)
+ if (tab[i].data == pt->data) return (i);
+ return (-1);
+}
+static int pgrp_ixfn(const void *p) { return (grp_ixfn(ptab, p)); }
+static int bgrp_ixfn(const void *p) { return (grp_ixfn(bintab, p)); }
+
+static PyObject *grp_valfn(const pentry *tab, PyTypeObject *ty, int i)
+{
+ gprime_param gp;
+
+ dh_infofromdata(&gp, tab[i].data);
+ return (fginfo_pywrap(&gp, ty));
+}
+static PyObject *pgrp_valfn(int i)
+ { return (grp_valfn(ptab, dhinfo_pytype, i)); }
+static PyObject *bgrp_valfn(int i)
+ { return (grp_valfn(bintab, bindhinfo_pytype, i)); }
+
void group_pyinsert(PyObject *mod)
{
INSERT("FGInfo", fginfo_pytype);
INSERT("PrimeGroup", primegroup_pytype);
INSERT("BinGroup", bingroup_pytype);
INSERT("ECGroup", ecgroup_pytype);
- INSERT("_pgroups", namedgroups(ptab, &npgroups));
- INSERT("_bingroups", namedgroups(bintab, &nbingroups));
+ INSERT("primegroups", make_grouptab(ptab, sizeof(*ptab),
+ grp_namefn, pgrp_ixfn, pgrp_valfn));
+ INSERT("bingroups", make_grouptab(bintab, sizeof(*bintab),
+ grp_namefn, bgrp_ixfn, bgrp_valfn));
}
/*----- That's all, folks -------------------------------------------------*/