random32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. This is a maximally equidistributed combined Tausworthe generator
  3. based on code from GNU Scientific Library 1.5 (30 Jun 2004)
  4. x_n = (s1_n ^ s2_n ^ s3_n)
  5. s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  6. s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  7. s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  8. The period of this generator is about 2^88.
  9. From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  10. Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  11. This is available on the net from L'Ecuyer's home page,
  12. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  13. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
  14. There is an erratum in the paper "Tables of Maximally
  15. Equidistributed Combined LFSR Generators", Mathematics of
  16. Computation, 68, 225 (1999), 261--269:
  17. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  18. ... the k_j most significant bits of z_j must be non-
  19. zero, for each j. (Note: this restriction also applies to the
  20. computer code given in [4], but was mistakenly not mentioned in
  21. that paper.)
  22. This affects the seeding procedure by imposing the requirement
  23. s1 > 1, s2 > 7, s3 > 15.
  24. */
  25. #include <linux/types.h>
  26. #include <linux/percpu.h>
  27. #include <linux/module.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/random.h>
  30. struct rnd_state {
  31. u32 s1, s2, s3;
  32. };
  33. static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
  34. static u32 __random32(struct rnd_state *state)
  35. {
  36. #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
  37. state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
  38. state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
  39. state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
  40. return (state->s1 ^ state->s2 ^ state->s3);
  41. }
  42. static void __set_random32(struct rnd_state *state, unsigned long s)
  43. {
  44. if (s == 0)
  45. s = 1; /* default seed is 1 */
  46. #define LCG(n) (69069 * n)
  47. state->s1 = LCG(s);
  48. state->s2 = LCG(state->s1);
  49. state->s3 = LCG(state->s2);
  50. /* "warm it up" */
  51. __random32(state);
  52. __random32(state);
  53. __random32(state);
  54. __random32(state);
  55. __random32(state);
  56. __random32(state);
  57. }
  58. /**
  59. * random32 - pseudo random number generator
  60. *
  61. * A 32 bit pseudo-random number is generated using a fast
  62. * algorithm suitable for simulation. This algorithm is NOT
  63. * considered safe for cryptographic use.
  64. */
  65. u32 random32(void)
  66. {
  67. unsigned long r;
  68. struct rnd_state *state = &get_cpu_var(net_rand_state);
  69. r = __random32(state);
  70. put_cpu_var(state);
  71. return r;
  72. }
  73. EXPORT_SYMBOL(random32);
  74. /**
  75. * srandom32 - add entropy to pseudo random number generator
  76. * @seed: seed value
  77. *
  78. * Add some additional seeding to the random32() pool.
  79. * Note: this pool is per cpu so it only affects current CPU.
  80. */
  81. void srandom32(u32 entropy)
  82. {
  83. struct rnd_state *state = &get_cpu_var(net_rand_state);
  84. __set_random32(state, state->s1 ^ entropy);
  85. put_cpu_var(state);
  86. }
  87. EXPORT_SYMBOL(srandom32);
  88. /*
  89. * Generate some initially weak seeding values to allow
  90. * to start the random32() engine.
  91. */
  92. static int __init random32_init(void)
  93. {
  94. int i;
  95. for_each_possible_cpu(i) {
  96. struct rnd_state *state = &per_cpu(net_rand_state,i);
  97. __set_random32(state, i + jiffies);
  98. }
  99. return 0;
  100. }
  101. core_initcall(random32_init);
  102. /*
  103. * Generate better values after random number generator
  104. * is fully initalized.
  105. */
  106. static int __init random32_reseed(void)
  107. {
  108. int i;
  109. unsigned long seed;
  110. for_each_possible_cpu(i) {
  111. struct rnd_state *state = &per_cpu(net_rand_state,i);
  112. get_random_bytes(&seed, sizeof(seed));
  113. __set_random32(state, seed);
  114. }
  115. return 0;
  116. }
  117. late_initcall(random32_reseed);