1 #include <linux/version.h>
2 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
5 This is a maximally equidistributed combined Tausworthe generator
6 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
8 x_n = (s1_n ^ s2_n ^ s3_n)
10 s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
11 s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
12 s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
14 The period of this generator is about 2^88.
16 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
17 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
19 This is available on the net from L'Ecuyer's home page,
21 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
22 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
24 There is an erratum in the paper "Tables of Maximally
25 Equidistributed Combined LFSR Generators", Mathematics of
26 Computation, 68, 225 (1999), 261--269:
27 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
29 ... the k_j most significant bits of z_j must be non-
30 zero, for each j. (Note: this restriction also applies to the
31 computer code given in [4], but was mistakenly not mentioned in
34 This affects the seeding procedure by imposing the requirement
35 s1 > 1, s2 > 7, s3 > 15.
39 #include <linux/types.h>
40 #include <linux/module.h>
41 #include <linux/jiffies.h>
42 #include <linux/random.h>
43 #include <linux/smp.h>
51 static struct rnd_state net_rand_state[NR_CPUS];
53 static u32 __random32(struct rnd_state *state)
55 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
57 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
58 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
59 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
61 return (state->s1 ^ state->s2 ^ state->s3);
64 static void __set_random32(struct rnd_state *state, unsigned long s)
67 s = 1; /* default seed is 1 */
69 #define LCG(n) (69069 * n)
71 state->s2 = LCG(state->s1);
72 state->s3 = LCG(state->s2);
84 * random32 - pseudo random number generator
86 * A 32 bit pseudo-random number is generated using a fast
87 * algorithm suitable for simulation. This algorithm is NOT
88 * considered safe for cryptographic use.
92 return __random32(&net_rand_state[smp_processor_id()]);
96 * srandom32 - add entropy to pseudo random number generator
99 * Add some additional seeding to the random32() pool.
100 * Note: this pool is per cpu so it only affects current CPU.
102 void srandom32(u32 entropy)
104 struct rnd_state *state = &net_rand_state[smp_processor_id()];
105 __set_random32(state, state->s1 ^ entropy);
108 static int __init random32_reseed(void);
111 * Generate some initially weak seeding values to allow
112 * to start the random32() engine.
114 int __init random32_init(void)
118 for (i = 0; i < NR_CPUS; i++) {
119 struct rnd_state *state = &net_rand_state[i];
120 __set_random32(state, i + jiffies);
127 * Generate better values after random number generator
128 * is fully initalized.
130 static int __init random32_reseed(void)
135 for (i = 0; i < NR_CPUS; i++) {
136 struct rnd_state *state = &net_rand_state[i];
138 get_random_bytes(&seed, sizeof(seed));
139 __set_random32(state, seed);
144 #endif /* kernel < 2.6.19 */