X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/c461c9b380b56a41399e5f540e4bb97cfd9ceffe..8afbf782f42e549cacf25cef947463853f92f468:/group.c diff --git a/group.c b/group.c index ac10866..e00e3fb 100644 --- a/group.c +++ b/group.c @@ -92,18 +92,20 @@ static PyObject *meth__DHInfo_generate(PyObject *me, unsigned ql = 0, pl; unsigned steps = 0; grand *r = &rand_global; - pgev evt = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev evt = { { 0 } }; char *kwlist[] = { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 }; PyObject *rc = 0; + evt.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist, &me, convuint, &pl, convuint, &ql, convpgev, &evt, convgrand, &r, convuint, &steps)) goto end; - if (dh_gen(&dp, ql, pl, steps, r, evt.proc, evt.ctx)) - PGENERR; + if (dh_gen(&dp, ql, pl, steps, r, evt.ev.proc, evt.ev.ctx)) + PGENERR(&exc); rc = fginfo_pywrap(&dp, dhinfo_pytype); end: droppgev(&evt); @@ -117,7 +119,8 @@ static PyObject *meth__DHInfo_genlimlee(PyObject *me, unsigned ql, pl; unsigned steps = 0; grand *r = &rand_global; - pgev oe = { 0 }, ie = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev oe = { { 0 } }, ie = { { 0 } }; int subgroupp = 1; unsigned f = 0; char *kwlist[] = { "class", "pbits", "qbits", "event", "ievent", @@ -126,6 +129,7 @@ static PyObject *meth__DHInfo_genlimlee(PyObject *me, mp **v = 0; PyObject *rc = 0, *vec = 0; + oe.exc = ie.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&O&O&:genlimlee", kwlist, &me, convuint, &pl, convuint, &ql, @@ -135,8 +139,8 @@ static PyObject *meth__DHInfo_genlimlee(PyObject *me, goto end; if (subgroupp) f |= DH_SUBGROUP; if (dh_limlee(&dp, ql, pl, f, steps, r, - oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v)) - PGENERR; + oe.ev.proc, oe.ev.ctx, ie.ev.proc, ie.ev.ctx, &nf, &v)) + PGENERR(&exc); vec = PyList_New(nf); for (i = 0; i < nf; i++) PyList_SetItem(vec, i, mp_pywrap(v[i])); @@ -154,19 +158,21 @@ static PyObject *meth__DHInfo_genkcdsa(PyObject *me, unsigned ql, pl; unsigned steps = 0; grand *r = &rand_global; - pgev evt = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev evt = { { 0 } }; char *kwlist[] = { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 }; mp *v = MP_NEW; PyObject *rc = 0; + evt.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", kwlist, &me, convuint, &pl, convuint, &ql, convpgev, &evt, convgrand, &r, convuint, &steps)) goto end; - if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.proc, evt.ctx)) - PGENERR; + if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.ev.proc, evt.ev.ctx)) + PGENERR(&exc); mp_div(&v, 0, dp.p, dp.q); v = mp_lsr(v, v, 1); rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), @@ -184,19 +190,21 @@ static PyObject *meth__DHInfo_gendsa(PyObject *me, unsigned steps = 0; dsa_seed ds; char *k; - int ksz; - pgev evt = { 0 }; + Py_ssize_t ksz; + struct excinfo exc = EXCINFO_INIT; + pypgev evt = { { 0 } }; char *kwlist[] = { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 }; PyObject *rc = 0; + evt.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", kwlist, &me, convuint, &pl, convuint, &ql, &k, &ksz, convpgev, &evt, convuint, &steps)) goto end; - if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.proc, evt.ctx)) - PGENERR; + if (dsa_gen(&dp, ql, pl, steps, k, ksz, &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); xfree(ds.p); @@ -264,7 +272,7 @@ static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty, if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; qd.p = p; qd.e = 0; - if (parse(&qd, &gp)) SYNERR(qd.e); + if (parse(&qd, &gp)) VALERR(qd.e); rc = fginfo_pywrap(&gp, ty); end: return (rc); @@ -517,7 +525,7 @@ static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) sc.buf = PyString_AS_STRING(x); sc.lim = sc.buf + PyString_GET_SIZE(x); if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim) - SYNERR("malformed group element string"); + VALERR("malformed group element string"); } else TYERR("can't convert to group element"); return (ge_pywrap((PyObject *)ty, xx)); @@ -542,7 +550,7 @@ static PyObject *group_dopywrap(PyTypeObject *ty, group *g) gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc; gobj->ty.ht_type.tp_free = 0; gobj->ty.ht_type.tp_new = ge_pynew; - PyType_Ready(&gobj->ty.ht_type); + typeready(&gobj->ty.ht_type); return ((PyObject *)gobj); } @@ -695,8 +703,8 @@ static PyObject *ge_pyint(PyObject *me) if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0) TYERR("can't convert to integer"); - if (mp_tolong_checked(x, &l)) goto end; - rc = PyInt_FromLong(l); + if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l); + else rc = mp_topylong(x); end: mp_drop(x); return (rc); @@ -717,18 +725,34 @@ end: static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw) { char *kwlist[] = { "curve", 0 }; - PyTypeObject *cty = ecpt_pytype; + PyTypeObject *cty = 0; + PyObject *rc = 0; + group *g; + ec_curve *c; ec p = EC_INIT; if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", kwlist, &cty)) goto end; - if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype)) - TYERR("want subtype of catacomb.ECPt"); - if (G_TOEC(GE_G(me), &p, GE_X(me))) + g = GROUP_G(GE_GOBJ(me)); + if (cty) { + if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype)) + TYERR("want subtype of catacomb.ECPt"); + Py_INCREF((PyObject *)cty); + } else if (strcmp(G_NAME(g), "ec") == 0) { + c = eccurve_copy(((gctx_ec *)g)->ei.c); + cty = (PyTypeObject *)eccurve_pywrap(0, c); + } else { + cty = ecpt_pytype; + Py_INCREF((PyObject *)cty); + } + if (G_TOEC(GE_G(me), &p, GE_X(me))) { + Py_DECREF((PyObject *)cty); TYERR("can't convert to ec point"); - return (ecpt_pywrapout(cty, &p)); + } + rc = ecpt_pywrapout(cty, &p); + Py_DECREF((PyObject *)cty); end: - return (0); + return (rc); } static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg) @@ -815,7 +839,7 @@ static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg) { buf b; char *p; - int n; + Py_ssize_t n; group *g; ge *x = 0; @@ -836,7 +860,7 @@ static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg) { buf b; char *p; - int n; + Py_ssize_t n; group *g; ge *x = 0; @@ -857,7 +881,7 @@ static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg) { mptext_stringctx sc; char *p; - int n; + Py_ssize_t n; group *g; ge *x = 0; @@ -868,9 +892,9 @@ static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg) g = GROUP_G(me); x = G_CREATE(g); if (G_READ(g, x, &mptext_stringops, &sc)) - SYNERR("bad group element string"); + VALERR("bad group element string"); return (Py_BuildValue("(Ns#)", ge_pywrap(me, x), - sc.buf, (int)(sc.lim - sc.buf))); + sc.buf, (Py_ssize_t)(sc.lim - sc.buf))); end: if (x) G_DESTROY(g, x); return (0); @@ -887,7 +911,7 @@ static PyObject *meth__Group_parse(PyObject *me, PyObject *arg) qd.p = p; qd.e = 0; if ((g = group_parse(&qd)) == 0) - SYNERR(qd.e); + VALERR(qd.e); return (group_pywrap(g)); end: return (0); @@ -914,6 +938,20 @@ static PyObject *gget_g(PyObject *me, void *hunoz) G_COPY(g, x, g->g); return (ge_pywrap(me, x)); } +static long ge_pyhash(PyObject *me) +{ + buf b; + size_t sz = GE_G(me)->noctets + 4; + uint32 h = 0xf672c776 + GE_G(me)->ops->ty; + octet *p = xmalloc(sz); + buf_init(&b, p, sz); + G_TOBUF(GE_G(me), &b, GE_X(me)); + assert(BOK(&b)); + h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b)); + xfree(p); + return (h % LONG_MAX); +} + static PyObject *gget_r(PyObject *me, void *hunoz) { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); } @@ -934,7 +972,7 @@ static PyMethodDef ge_pymethods[] = { METH (check, "X.check() -> check X really belongs to its group") METH (toint, "X.toint() -> X converted to an integer") KWMETH(toec, "\ -X.toec(curve = ecpt) -> X converted to elliptic curve point") +X.toec([curve = ECPt]) -> X converted to elliptic curve point") METH (tobuf, "X.tobuf() -> X in buffer representation") METH (toraw, "X.toraw() -> X in raw representation") #undef METHNAME @@ -999,7 +1037,7 @@ static PyTypeObject ge_pytype_skel = { &ge_pynumber, /* @tp_as_number@ */ 0, /* @tp_as_sequence@ */ 0, /* @tp_as_mapping@ */ - 0, /* @tp_hash@ */ + ge_pyhash, /* @tp_hash@ */ 0, /* @tp_call@ */ ge_pystr, /* @tp_str@ */ 0, /* @tp_getattro@ */ @@ -1049,7 +1087,7 @@ static PyMethodDef group_pymethods[] = { #define METHNAME(name) gmeth_##name METH (mexp, "\ G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...") - KWMETH(checkgroup, "G.checkgroup(rand = random): check group is good") + KWMETH(checkgroup, "G.checkgroup([rng = rand]): check group is good") #undef METHNAME { 0 } }; @@ -1108,7 +1146,7 @@ static PyObject *pgget_info(PyObject *me, void *hunoz) gctx_prime *gg = (gctx_prime *)GROUP_G(me); dp.p = MP_COPY(gg->mm.m); dp.q = MP_COPY(gg->g.r); - dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen); + dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen.x); return (fginfo_pywrap(&dp, dhinfo_pytype)); } @@ -1185,7 +1223,7 @@ static PyObject *bgget_info(PyObject *me, void *hunoz) gctx_bin *gg = (gctx_bin *)GROUP_G(me); dp.p = MP_COPY(gg->r.p); dp.q = MP_COPY(gg->g.r); - dp.g = MP_COPY(gg->gen); + dp.g = MP_COPY(gg->gen.x); return (fginfo_pywrap(&dp, bindhinfo_pytype)); } @@ -1347,16 +1385,16 @@ static PyMethodDef methods[] = { METH (_DHInfo__groupn, 0) METH (_BinDHInfo__groupn, 0) KWMETH(_DHInfo_generate, "\ -generate(PBITS, [qbits = 0, event = pgen_nullev,\n\ - rng = rand, nsteps = 0]) -> D") +generate(PBITS, [qbits = 0], [event = pgen_nullev],\n\ + [rng = rand], [nsteps = 0]) -> D") KWMETH(_DHInfo_genlimlee, "\ -genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\ - rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])") +genlimlee(PBITS, QBITS, [event = pgen_nullev], [ievent = pgen_nullev],\n\ + [rng = rand], [nsteps = 0], [subgroupp = True]) -> (D, [Q, ...])") KWMETH(_DHInfo_gendsa, "\ -gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\ +gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n\ -> (D, SEED, COUNT)") KWMETH(_DHInfo_genkcdsa, "\ -gendsa(PBITS, QBITS, [event = pgen_nullev, rng = rand, nsteps = 0])\n\ +gendsa(PBITS, QBITS, [event = pgen_nullev], [rng = rand], [nsteps = 0])\n\ -> (D, V)") #undef METHNAME { 0 }