diff options
author | Paul R. Eggert <eggert@cs.ucla.edu> | 2010-07-23 15:07:27 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2010-07-23 15:08:36 -0700 |
commit | df906d2e75d6822c88999b8cc537166371f2da6e (patch) | |
tree | 74be2ee6513ee1ee98ffe683cfa3d2b134fc9dd7 /gl/lib/rand-isaac.h | |
parent | 47076e3c7c22fc7557f388ad3d47228b922da71e (diff) | |
download | coreutils-df906d2e75d6822c88999b8cc537166371f2da6e.tar.xz |
randread: run 2x faster on 64-bit hosts, don't assume no padding bits
* gl/lib/rand-isaac.c: Remove the I/O; this belongs elsewhere.
Add support for ISAAC64. Port to hosts with padding bits.
Add self to author list. Include <limits.h>, for CHAR_BIT.
Don't include string.h, sys/time.h, unistd.h.
(min, just): New functions.
(IF32): New macros.
(ind, ISAAC_STEP, isaac_refill, mix, isaac_init, isaac_seed):
Add support for ISAAC64. Port to hosts with padding bits.
(ind): Now an inline function rather than a macro; no need for it
to be a macro with modern compilers.
(ISAAC_STEP): Renamed from isaac_step, since it's not function-like.
Don't bother to pass args that are always the same. All uses changed.
(ISAAC_STEP, ISAAC_SEED): Move to inside the only function body
that can use it.
(ISAAC_MIX): Renamed from isaac_mix, since it's now a macro and is
no longer function-like. Don't bother saving and restoring state;
no longer needed now that we're not a function. All uses changed.
(isaac_seed_start, isaac_seed_data, isaac_seed_finish): Remove.
(isaac_seed): Take just the one arg; the caller now sets s->m.
* gl/lib/rand-isaac.h: Use _GL_RAND_ISAAC_H to protect, instead
of RAND_ISAAC_H. Try out " #" rather than "# " for indenting.
(ISAAC_BITS_LOG, ISAAC_BITS): New macros.
(ISAAC_WORDS_LOG): Renamed from ISAAC_LOG.
(isaac_word): New type. All uses of uint32_t changed to isaac_word,
to support ISAAC64.
(struct isaac_state): Rename member MM to M, and make it public.
(isaac_seed, isaac_refill): Adjust to new API.
* gl/lib/randread.c: Include sys/time.h.
(get_nonce): New function, containing the nonce stuff that used
to be in rand-isaac.c but better belongs here.
(randread_new): Use it.
* gl/modules/randread (Depends-on): Add inline.
* gl/modules/randread-tests: New file.
* gl/tests/test-rand-isaac.c: New file.
Diffstat (limited to 'gl/lib/rand-isaac.h')
-rw-r--r-- | gl/lib/rand-isaac.h | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/gl/lib/rand-isaac.h b/gl/lib/rand-isaac.h index 052dc9f3a..6e5af0676 100644 --- a/gl/lib/rand-isaac.h +++ b/gl/lib/rand-isaac.h @@ -1,4 +1,4 @@ -/* Bob Jenkins's cryptographic random number generator, ISAAC. +/* Bob Jenkins's cryptographic random number generators, ISAAC and ISAAC64. Copyright (C) 1999-2005, 2009-2010 Free Software Foundation, Inc. Copyright (C) 1997, 1998, 1999 Colin Plumb. @@ -16,29 +16,50 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. - Written by Colin Plumb. */ + Written by Colin Plumb and Paul Eggert. */ -#ifndef RAND_ISAAC_H -# define RAND_ISAAC_H +#ifndef _GL_RAND_ISAAC_H +#define _GL_RAND_ISAAC_H -# include <stddef.h> -# include <stdint.h> +#include <stddef.h> +#include <stdint.h> -/* Size of the state tables to use. ISAAC_LOG should be at least 3, +/* Log base 2 of the number of useful bits in an ISAAC word. It must + be either 5 or 6. By default, this uses a value that should be + faster for this architecture. */ +#ifndef ISAAC_BITS_LOG + #if SIZE_MAX >> 31 >> 31 < 3 /* SIZE_MAX < 2**64 - 1 */ + #define ISAAC_BITS_LOG 5 + #else + #define ISAAC_BITS_LOG 6 + #endif +#endif + +/* The number of bits in an ISAAC word. */ +#define ISAAC_BITS (1 << ISAAC_BITS_LOG) + +#if ISAAC_BITS == 32 + typedef uint_least32_t isaac_word; +#else + typedef uint_least64_t isaac_word; +#endif + +/* Size of the state tables to use. ISAAC_WORDS_LOG should be at least 3, and smaller values give less security. */ -# define ISAAC_LOG 8 -# define ISAAC_WORDS (1 << ISAAC_LOG) -# define ISAAC_BYTES (ISAAC_WORDS * sizeof (uint32_t)) +#define ISAAC_WORDS_LOG 8 +#define ISAAC_WORDS (1 << ISAAC_WORDS_LOG) +#define ISAAC_BYTES (ISAAC_WORDS * sizeof (isaac_word)) -/* RNG state variables. The members of this structure are private. */ +/* State variables for the random number generator. The M member + should be seeded with nonce data before calling isaac_seed. The + other members are private. */ struct isaac_state { - uint32_t mm[ISAAC_WORDS]; /* Main state array */ - uint32_t iv[8]; /* Seeding initial vector */ - uint32_t a, b, c; /* Extra index variables */ + isaac_word m[ISAAC_WORDS]; /* Main state array */ + isaac_word a, b, c; /* Extra variables */ }; -void isaac_seed (struct isaac_state *, int, size_t); -void isaac_refill (struct isaac_state *, uint32_t[ISAAC_WORDS]); +void isaac_seed (struct isaac_state *); +void isaac_refill (struct isaac_state *, isaac_word[ISAAC_WORDS]); #endif |