Support for generating random large integers.
authormdw <mdw>
Fri, 10 Dec 1999 23:23:05 +0000 (23:23 +0000)
committermdw <mdw>
Fri, 10 Dec 1999 23:23:05 +0000 (23:23 +0000)
mprand.c [new file with mode: 0644]
mprand.h [new file with mode: 0644]

diff --git a/mprand.c b/mprand.c
new file mode 100644 (file)
index 0000000..41c2b39
--- /dev/null
+++ b/mprand.c
@@ -0,0 +1,91 @@
+/* -*-c-*-
+ *
+ * $Id: mprand.c,v 1.1 1999/12/10 23:23:05 mdw Exp $
+ *
+ * Generate a random multiprecision integer
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: mprand.c,v $
+ * Revision 1.1  1999/12/10 23:23:05  mdw
+ * Support for generating random large integers.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/alloc.h>
+
+#include "grand.h"
+#include "mp.h"
+#include "mprand.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mprand@ --- *
+ *
+ * Arguments:  @mp *d@ = destination integer
+ *             @unsigned b@ = number of bits
+ *             @grand *r@ = pointer to random number source
+ *             @mpw or@ = mask to OR with low-order bits
+ *
+ * Returns:    A random integer with the requested number of bits.
+ *
+ * Use:                Constructs an arbitrarily large pseudorandom integer.
+ *             Assuming that the generator @r@ is good, the result is
+ *             uniformly distributed in the interval %$[2^{b - 1}, 2^b)$%.
+ *             The result is then ORred with the given @or@ value.  This
+ *             will often be 1, to make the result odd.
+ */
+
+mp *mprand(mp *d, unsigned b, grand *r, mpw or)
+{
+  size_t sz = (b + 7) / 8;
+  octet *v = xmalloc(sz);
+  unsigned m;
+
+  /* --- Fill buffer with random data --- */
+
+  r->ops->fill(r, v, sz);
+
+  /* --- Force into the correct range --- *
+   *
+   * This is slightly tricky.  Oh, well.
+   */
+
+  b = (b - 1) % 8;
+  m = (1 << b);
+  v[0] = (v[0] & (m - 1)) | m;
+
+  /* --- Mask, load and return --- */
+
+  d = mp_loadb(d, v, sz);
+  d->v[0] |= or;
+  free(v);
+  return (d);
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/mprand.h b/mprand.h
new file mode 100644 (file)
index 0000000..2ecfa15
--- /dev/null
+++ b/mprand.h
@@ -0,0 +1,81 @@
+/* -*-c-*-
+ *
+ * $Id: mprand.h,v 1.1 1999/12/10 23:23:05 mdw Exp $
+ *
+ * Generate a random multiprecision integer
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: mprand.h,v $
+ * Revision 1.1  1999/12/10 23:23:05  mdw
+ * Support for generating random large integers.
+ *
+ */
+
+#ifndef CATACOMB_MPRAND_H
+#define CATACOMB_MPRAND_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#ifndef CATACOMB_GRAND_H
+#  include "grand.h"
+#endif
+
+#ifndef CATACOMB_MP_H
+#  include "mp.h"
+#endif
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @mprand@ --- *
+ *
+ * Arguments:  @mp *d@ = destination integer
+ *             @unsigned b@ = number of bits
+ *             @grand *r@ = pointer to random number source
+ *             @mpw or@ = mask to OR with low-order bits
+ *
+ * Returns:    A random integer with the requested number of bits
+ *
+ * Use:                Constructs an arbitrarily large pseudorandom integer.
+ *             Assuming that the generator @r@ is good, the result is
+ *             uniformly distributed in the interval %$[2^{b - 1}, 2^b)$%.
+ *             The result is then ORred with the given @or@ value.  This
+ *             will often be 1, to make the result odd.
+ */
+
+extern mp *mprand(mp */*d*/, unsigned /*b*/, grand */*r*/, mpw /*or*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif