| 1 |
/* rng_threefish |
| 2 |
* |
| 3 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <dieharder/libdieharder.h> |
| 21 |
#include "skein.h" |
| 22 |
|
| 23 |
// State blocks = 1 for weakest form |
| 24 |
#define STATE_BLOCKS (1) |
| 25 |
#define BLOCKS_SIZE (64 * STATE_BLOCKS) |
| 26 |
|
| 27 |
#define Threefish_512_Process_Blocks(a,b,c,d) Threefish_512_Process_Blocks64(a,b,c,d) |
| 28 |
|
| 29 |
/* |
| 30 |
* This is a wrapping of the Threefish algorithm as a generator |
| 31 |
*/ |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
Threefish_512_Ctxt_t ctx; |
| 35 |
unsigned char block[BLOCKS_SIZE]; |
| 36 |
short int pos; |
| 37 |
} Threefish_state_t; |
| 38 |
|
| 39 |
unsigned long int threefish_get (void *vstate); |
| 40 |
double threefish_get_double (void *vstate); |
| 41 |
void threefish_set (void *vstate, unsigned long int s); |
| 42 |
|
| 43 |
#ifndef BADRNG |
| 44 |
unsigned long int threefish_get_long (void *vstate) { |
| 45 |
Threefish_state_t *state = vstate; |
| 46 |
unsigned long int ret; |
| 47 |
|
| 48 |
if (state->pos + sizeof(unsigned long int) > BLOCKS_SIZE) { |
| 49 |
Threefish_512_Process_Blocks(&(state->ctx), state->block, state->block, STATE_BLOCKS); |
| 50 |
state->pos = 0; |
| 51 |
} |
| 52 |
|
| 53 |
ret = *((unsigned long int *) (state->block + state->pos)); |
| 54 |
state->pos += sizeof(unsigned long int); |
| 55 |
|
| 56 |
return(ret); |
| 57 |
} |
| 58 |
|
| 59 |
unsigned long int threefish_get (void *vstate) { |
| 60 |
Threefish_state_t *state = vstate; |
| 61 |
unsigned int ret; |
| 62 |
|
| 63 |
if (state->pos + sizeof(ret) > BLOCKS_SIZE) { |
| 64 |
Threefish_512_Process_Blocks(&(state->ctx), state->block, state->block, STATE_BLOCKS); |
| 65 |
state->pos = 0; |
| 66 |
} |
| 67 |
|
| 68 |
ret = *((unsigned int *) (state->block + state->pos)); |
| 69 |
state->pos += sizeof(ret); |
| 70 |
|
| 71 |
// ret &= 0x7fffffff; |
| 72 |
return(ret); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
double threefish_get_double (void *vstate) { |
| 77 |
// return threefish_get_long(vstate) / (double) ULONG_MAX; |
| 78 |
return (double) threefish_get(vstate) / (double) (UINT_MAX >> 0); |
| 79 |
} |
| 80 |
|
| 81 |
void threefish_set (void *vstate, unsigned long int s) { |
| 82 |
Threefish_state_t *state = vstate; |
| 83 |
int i; |
| 84 |
|
| 85 |
memset(state, 0, sizeof(*state)); |
| 86 |
for (i = 0; i < 16; i++) ((unsigned char *) state->ctx.T)[i] = 112 + i; |
| 87 |
for (i = 0; i < 64; i++) ((unsigned char *) state->ctx.Key)[i] = 64 + i; |
| 88 |
*((unsigned long int *) state->block) = s; |
| 89 |
Threefish_512_Process_Blocks(&(state->ctx), state->block, state->block, 1); |
| 90 |
|
| 91 |
for (i = 1; i < STATE_BLOCKS; i++) { |
| 92 |
state->ctx.T[0] = i; |
| 93 |
Threefish_512_Process_Blocks(&(state->ctx), state->block, state->block+(i*64), 1); |
| 94 |
} |
| 95 |
state->ctx.T[0] = 112; |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
static const gsl_rng_type threefish_type = { |
| 101 |
"Threefish_OFB", /* name */ |
| 102 |
UINT_MAX>>0, // UINT_MAX, /* RAND_MAX */ |
| 103 |
0, /* RAND_MIN */ |
| 104 |
sizeof (Threefish_state_t), |
| 105 |
&threefish_set, |
| 106 |
&threefish_get, |
| 107 |
&threefish_get_double}; |
| 108 |
|
| 109 |
const gsl_rng_type *gsl_rng_threefish = &threefish_type; |
| 110 |
#endif |
| 111 |
|