Generate random primes method.

CVS patchset: 6807
CVS date: 2003/05/02 21:05:46
This commit is contained in:
jbj 2003-05-02 21:05:46 +00:00
parent 69eaee5148
commit 1b00e0031d
10 changed files with 108 additions and 45 deletions

View File

@ -3,7 +3,6 @@
+partial
+forcehints
-warnunixlib
-warnposix
+unixlib
@ -13,7 +12,7 @@
+strict # lclint level
# --- in progress
#+bounds
+likelybounds
+slovak-fcns
-bufferoverflowhigh
@ -33,17 +32,18 @@
-modunconnomods
-noeffect
-noeffectuncon
-nullassign
-nullderef
-nullpass
-nullpass # 40
-nullret
-nullstate
-paramuse # 14
-predboolothers
-protoparammatch
-redecl
-redef
-redecl # 23
-retalias
-retvalint
-retvalother
-shiftimplementation
-sizeoftype
-type
-usereleased

View File

@ -1,12 +1,23 @@
import rpm
rpm.rng().Debug(-1);
rpm.rng().Debug(1);
#rpm.mpw().Debug(-1);
# Generate random intergers
r = rpm.rng()
for i in range(10):
for i in range(100):
print r.next()
# Generate random numbers with lots of bits
m = rpm.mpw("800000000000000000000000000000000000000000000000000000000000000000000000")
for i in range(10):
print r.next(m)
for i in range(100):
r.next(m)
# Generate 160 bit random primes
bits = 160
trials = -1
a = r.prime(bits, trials)
for i in range(100):
b = r.prime(bits, trials)
print a.gcd(a,b)
a = b

View File

@ -28,7 +28,7 @@ static const char initialiser_name[] = "rpm.bc";
static void
rpmbc_dealloc(rpmbcObject * s)
/*@modifies s @*/
/*@*/
{
if (_bc_debug < -1)
fprintf(stderr, "*** rpmbc_dealloc(%p)\n", s);
@ -38,8 +38,7 @@ fprintf(stderr, "*** rpmbc_dealloc(%p)\n", s);
static int
rpmbc_print(rpmbcObject * s, FILE * fp, /*@unused@*/ int flags)
/*@globals fileSystem @*/
/*@modifies fp, fileSystem @*/
/*@*/
{
if (_bc_debug < -1)
fprintf(stderr, "*** rpmbc_print(%p)\n", s);
@ -49,7 +48,7 @@ fprintf(stderr, "*** rpmbc_print(%p)\n", s);
/** \ingroup py_c
*/
static int rpmbc_init(rpmbcObject * z, PyObject *args, PyObject *kwds)
/*@modifies z @*/
/*@*/
{
PyObject * o = NULL;
@ -64,7 +63,7 @@ fprintf(stderr, "*** rpmbc_init(%p[%s],%p[%s],%p[%s])\n", z, lbl(z), args, lbl(a
/** \ingroup py_c
*/
static void rpmbc_free(/*@only@*/ rpmbcObject * s)
/*@modifies s @*/
/*@*/
{
if (_bc_debug)
fprintf(stderr, "*** rpmbc_free(%p[%s])\n", s, lbl(s));

View File

@ -125,7 +125,7 @@ rpmds_iternext(rpmdsObject * s)
/* Reset loop indices on 1st entry. */
if (!s->active) {
rpmdsInit(s->ds);
s->ds = rpmdsInit(s->ds);
s->active = 1;
}
@ -268,7 +268,7 @@ rpmds_print(rpmdsObject * s, FILE * fp, /*@unused@*/ int flags)
if (!(s && s->ds))
return -1;
rpmdsInit(s->ds);
s->ds = rpmdsInit(s->ds);
while (rpmdsNext(s->ds) >= 0)
fprintf(fp, "%s\n", rpmdsDNEVR(s->ds));
return 0;

View File

@ -115,7 +115,7 @@ rpmfd_Fopen(/*@unused@*/ PyObject * s, PyObject * args)
node->note = xstrdup (path);
if (!node->fd) {
PyErr_SetFromErrno(pyrpmError);
(void) PyErr_SetFromErrno(pyrpmError);
node = _free (node);
return NULL;
}
@ -201,7 +201,7 @@ fprintf(stderr, "*** rpmfd_init(%p,%p,%p)\n", s, args, kwds);
s->fd = Fopen(path, mode);
if (s->fd == NULL) {
PyErr_SetFromErrno(pyrpmError);
(void) PyErr_SetFromErrno(pyrpmError);
return -1;
}

View File

@ -222,7 +222,7 @@ rpmfi_iternext(rpmfiObject * s)
/* Reset loop indices on 1st entry. */
if (!s->active) {
rpmfiInit(s->fi, 0);
s->fi = rpmfiInit(s->fi, 0);
s->active = 1;
}
@ -410,7 +410,7 @@ rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
if (!(s && s->fi))
return -1;
rpmfiInit(s->fi, 0);
s->fi = rpmfiInit(s->fi, 0);
while (rpmfiNext(s->fi) >= 0)
fprintf(fp, "%s\n", rpmfiFN(s->fi));
return 0;

View File

@ -299,7 +299,7 @@ static PyMemberDef rpmfts_members[] = {
"Option bit(s): FTS_{COMFOLLOW|LOGICAL|NOCHDIR|NOSTAT|PHYSICAL|SEEDOT|XDEV}"},
{"ignore", T_INT, offsetof(rpmftsObject, ignore), 0,
"Ignore bit(s): (1 << info) with info one of FTS_{D|DC|DEFAULT|DNR|DOT|DP|ERR|F|INIT|NS|NSOK|SL|SLNONE|W}"},
{NULL, 0, 0, 0}
{NULL, 0, 0, 0, NULL}
};
static PyObject * rpmfts_getattro(rpmftsObject * s, PyObject * n)
@ -341,7 +341,7 @@ rpmfts_iternext(rpmftsObject * s)
/* ---------- */
static void rpmfts_free(/*@only@*/ PyObject * s)
/*@modifies s @*/
/*@*/
{
_PyObject_GC_Del(s);
}
@ -375,7 +375,7 @@ rpmfts_debug(__FUNCTION__, s);
}
static int rpmfts_init(rpmftsObject * s, PyObject *args, PyObject *kwds)
/*@*/
/*@modifies s @*/
{
char * root = NULL;
int options = -1;

View File

@ -348,7 +348,9 @@ static void myndivmod(mpw* result, size_t xsize, const mpw* xdata, size_t ysize,
{
q = mppndiv(result[0], result[1], msw);
/*@-evalorder@*/
*workspace = mpsetmul(ysize, workspace+1, ydata, q);
/*@=evalorder@*/
while (mplt(ysize+1, result, workspace))
{
@ -848,7 +850,7 @@ fprintf(stderr, "*** mpw_str(%p): \"%s\"\n", a, PyString_AS_STRING(so));
/** \ingroup py_c
*/
static int mpw_init(mpwObject * s, PyObject *args, PyObject *kwds)
/*@modifies z @*/
/*@modifies s @*/
{
PyObject * o = NULL;
size_t words = 0;
@ -956,7 +958,7 @@ mpwObject * mpw_New(void)
*/
static mpwObject *
mpw_i2mpw(PyObject * o)
/*@*/
/*@modifies o @*/
{
if (is_mpw(o)) {
Py_INCREF(o);
@ -1076,7 +1078,6 @@ fprintf(stderr, " b %p[%d]:\t", m->n.data, m->n.size), mpprintln(stderr, m->n
size_t zsize;
mpw* zdata;
size_t znorm;
mpw* wksp;
if (anorm < asize) {
asize -= anorm;
@ -1107,7 +1108,6 @@ fprintf(stderr, " b %p[%d]:\t", m->n.data, m->n.size), mpprintln(stderr, m->n
size_t bnorm = mpsize(bsize, bdata);
size_t zsize = x->n.size;
mpw* zdata = alloca(zsize * sizeof(*zdata));
mpw* wksp;
if (bnorm < bsize) {
bsize -= bnorm;
@ -1829,7 +1829,7 @@ fprintf(stderr, "*** mpw_inplace_subtract(%p,%p):\t", a, b), mpprintln(stderr, a
static PyObject *
mpw_inplace_multiply(mpwObject * a, mpwObject * b)
/*@*/
/*@modifies a @*/
{
size_t zsize = a->n.size + b->n.size;
mpw* zdata = alloca(zsize * sizeof(*zdata));
@ -1851,7 +1851,7 @@ fprintf(stderr, "*** mpw_inplace_multiply(%p,%p):\t", a, b), mpprintln(stderr, a
static PyObject *
mpw_inplace_divide(mpwObject * a, mpwObject * b)
/*@*/
/*@modifies a @*/
{
size_t asize = a->n.size;
mpw* adata = a->n.data;
@ -1896,7 +1896,7 @@ fprintf(stderr, "*** mpw_inplace_divide(%p,%p):\t", a, b), mpprintln(stderr, a->
static PyObject *
mpw_inplace_remainder(mpwObject * a, mpwObject * b)
/*@*/
/*@modifies a @*/
{
size_t bsize = b->n.size;
mpw* bdata = b->n.data;
@ -1976,7 +1976,7 @@ fprintf(stderr, "*** mpw_inplace_rshift(%p,%p):\t", a, b), mpprintln(stderr, a->
static PyObject *
mpw_inplace_and(mpwObject * a, mpwObject * b)
/*@*/
/*@modifies a @*/
{
if (a->n.size <= b->n.size)
mpand(a->n.size, a->n.data, b->n.data + (b->n.size - a->n.size));
@ -1994,6 +1994,7 @@ fprintf(stderr, "*** mpw_inplace_and(%p,%p):\t", a, b), mpprintln(stderr, a->n.s
static PyObject *
mpw_inplace_xor(mpwObject * a, mpwObject * b)
/*@modifies a @*/
{
if (a->n.size <= b->n.size)
mpxor(a->n.size, a->n.data, b->n.data + (b->n.size - a->n.size));
@ -2011,7 +2012,7 @@ fprintf(stderr, "*** mpw_inplace_xor(%p,%p):\t", a, b), mpprintln(stderr, a->n.s
static PyObject *
mpw_inplace_or(mpwObject * a, mpwObject * b)
/*@*/
/*@modifies a @*/
{
if (a->n.size <= b->n.size)
mpor(a->n.size, a->n.data, b->n.data + (b->n.size - a->n.size));

View File

@ -11,6 +11,7 @@
#endif
#include "rpmrng-py.h"
#include "mpprime.h"
#include "rpmdebug-py.c"
@ -31,7 +32,9 @@ rng_dealloc(rngObject * s)
if (_rng_debug < -1)
fprintf(stderr, "*** rng_dealloc(%p)\n", s);
/*@-modobserver@*/
randomGeneratorContextFree(&s->rngc);
/*@=modobserver@*/
mpbfree(&s->b);
PyObject_Del(s);
}
@ -39,7 +42,7 @@ fprintf(stderr, "*** rng_dealloc(%p)\n", s);
static int
rng_print(rngObject * s, FILE * fp, /*@unused@*/ int flags)
/*@globals fileSystem @*/
/*@modifies fp, fileSystem @*/
/*@modifies fileSystem @*/
{
if (_rng_debug < -1)
fprintf(stderr, "*** rng_print(%p)\n", s);
@ -65,8 +68,10 @@ static int rng_init(rngObject * s, PyObject *args, PyObject *kwds)
if (rng == NULL)
rng = randomGeneratorDefault();
/*@-modobserver@*/
if (randomGeneratorContextInit(&s->rngc, rng) != 0)
return -1;
/*@=modobserver@*/
mpbzero(&s->b);
if (_rng_debug)
@ -82,7 +87,9 @@ static void rng_free(/*@only@*/ rngObject * s)
{
if (_rng_debug)
fprintf(stderr, "*** rng_free(%p[%s])\n", s, lbl(s));
/*@-modobserver@*/
randomGeneratorContextFree(&s->rngc);
/*@=modobserver@*/
mpbfree(&s->b);
PyObject_Del(s);
}
@ -140,9 +147,19 @@ fprintf(stderr, "*** rng_Debug(%p)\n", s);
*/
static PyObject *
rng_Seed(rngObject * s, PyObject * args)
/*@*/
/*@globals _Py_NoneStruct @*/
/*@modifies _Py_NoneStruct @*/
{
if (!PyArg_ParseTuple(args, ":Seed")) return NULL;
PyObject * o;
randomGeneratorContext* rc = &s->rngc;
mpwObject *z;
if (!PyArg_ParseTuple(args, "O:Seed", &o)) return NULL;
if (!is_mpw(o) || (z = (mpwObject*)o)->n.size > 0)
return NULL;
rc->rng->seed(rc->param, (byte*) z->n.data, z->n.size);
if (_rng_debug < 0)
fprintf(stderr, "*** rng_Seed(%p)\n", s);
@ -157,15 +174,15 @@ static PyObject *
rng_Next(rngObject * s, PyObject * args)
/*@*/
{
PyObject * bo = NULL;
PyObject * o = NULL;
randomGeneratorContext* rc = &s->rngc;
mpbarrett* b = &s->b;
mpwObject *z;
if (!PyArg_ParseTuple(args, "|O:Next", &bo)) return NULL;
if (!PyArg_ParseTuple(args, "|O:Next", &o)) return NULL;
if (bo) {
if (is_mpw(bo) && (z = (mpwObject*)bo)->n.size > 0) {
if (o) {
if (is_mpw(o) && (z = (mpwObject*)o)->n.size > 0) {
b = alloca(sizeof(*b));
mpbzero(b);
/* XXX z probably needs normalization here. */
@ -191,6 +208,38 @@ fprintf(stderr, "*** rng_Next(%p) %p[%d]\t", s, z->n.data, z->n.size), mpprintln
return (PyObject *)z;
}
/** \ingroup py_c
*/
static PyObject *
rng_Prime(rngObject * s, PyObject * args)
/*@*/
{
randomGeneratorContext* rc = &s->rngc;
unsigned pbits = 160;
int trials = -1;
size_t psize;
mpwObject *z;
if (!PyArg_ParseTuple(args, "|ii:Prime", &pbits, &trials)) return NULL;
psize = MP_BITS_TO_WORDS(pbits + MP_WBITS - 1);
if((z = mpw_New()) != NULL) {
mpbarrett* b = alloca(sizeof(*b));
mpw *temp = alloca((8*psize+2) * sizeof(*temp));
mpbzero(b);
if (trials <= 2)
trials = mpptrials(pbits);
mpprnd_w(b, rc, pbits, trials, (const mpnumber*) 0, temp);
mpnset(&z->n, b->size, b->modl);
if (_rng_debug)
fprintf(stderr, "*** rng_Prime(%p) %p[%d]\t", s, z->n.data, z->n.size), mpprintln(stderr, z->n.size, z->n.data);
}
return (PyObject *)z;
}
/*@-fullinitblock@*/
/*@unchecked@*/ /*@observer@*/
static struct PyMethodDef rng_methods[] = {
@ -200,6 +249,8 @@ static struct PyMethodDef rng_methods[] = {
NULL},
{"next", (PyCFunction)rng_Next, METH_VARARGS,
NULL},
{"prime", (PyCFunction)rng_Prime, METH_VARARGS,
NULL},
{NULL, NULL} /* sentinel */
};
/*@=fullinitblock@*/

View File

@ -467,7 +467,8 @@ fprintf(stderr, "*** rpmts_Check(%p) ts %p cb %p\n", s, s->ts, cbInfo.cb);
*/
static PyObject *
rpmts_Order(rpmtsObject * s, PyObject * args)
/*@modifies s @*/
/*@globals rpmGlobalMacroContext @*/
/*@modifies s, rpmGlobalMacroContext @*/
{
int rc;
@ -733,8 +734,8 @@ fprintf(stderr, "*** rpmts_VerifyDB(%p) ts %p\n", s, s->ts);
*/
static PyObject *
rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args)
/*@globals rpmGlobalMacroContext, _Py_NoneStruct, fileSystem @*/
/*@modifies s, rpmGlobalMacroContext, _Py_NoneStruct, fileSystem @*/
/*@globals rpmGlobalMacroContext, fileSystem @*/
/*@modifies s, rpmGlobalMacroContext, fileSystem @*/
{
PyObject * result = NULL;
Header h;
@ -1351,7 +1352,7 @@ static void rpmts_dealloc(/*@only@*/ rpmtsObject * s)
if (_rpmts_debug)
fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, s->ts->rdb);
rpmtsFree(s->ts);
s->ts = rpmtsFree(s->ts);
if (s->scriptFd) Fclose(s->scriptFd);
/* this will free the keyList, and decrement the ref count of all
@ -1425,7 +1426,7 @@ static void rpmts_free(/*@only@*/ rpmtsObject * s)
{
if (_rpmts_debug)
fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, s->ts->rdb);
rpmtsFree(s->ts);
s->ts = rpmtsFree(s->ts);
if (s->scriptFd)
Fclose(s->scriptFd);