isaac.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* CC0 (Public domain) - see LICENSE file for details */
  2. #if !defined(_isaac_H)
  3. # define _isaac_H (1)
  4. # include <stdint.h>
  5. typedef struct isaac_ctx isaac_ctx;
  6. /*This value may be lowered to reduce memory usage on embedded platforms, at
  7. the cost of reducing security and increasing bias.
  8. Quoting Bob Jenkins: "The current best guess is that bias is detectable after
  9. 2**37 values for [ISAAC_SZ_LOG]=3, 2**45 for 4, 2**53 for 5, 2**61 for 6,
  10. 2**69 for 7, and 2**77 values for [ISAAC_SZ_LOG]=8."*/
  11. #define ISAAC_SZ_LOG (8)
  12. #define ISAAC_SZ (1<<ISAAC_SZ_LOG)
  13. #define ISAAC_SEED_SZ_MAX (ISAAC_SZ<<2)
  14. /*ISAAC is the most advanced of a series of pseudo-random number generators
  15. designed by Robert J. Jenkins Jr. in 1996.
  16. http://www.burtleburtle.net/bob/rand/isaac.html
  17. To quote:
  18. No efficient method is known for deducing their internal states.
  19. ISAAC requires an amortized 18.75 instructions to produce a 32-bit value.
  20. There are no cycles in ISAAC shorter than 2**40 values.
  21. The expected cycle length is 2**8295 values.*/
  22. struct isaac_ctx{
  23. unsigned n;
  24. uint32_t r[ISAAC_SZ];
  25. uint32_t m[ISAAC_SZ];
  26. uint32_t a;
  27. uint32_t b;
  28. uint32_t c;
  29. };
  30. /**
  31. * isaac_init - Initialize an instance of the ISAAC random number generator.
  32. * @_ctx: The instance to initialize.
  33. * @_seed: The specified seed bytes.
  34. * This may be NULL if _nseed is less than or equal to zero.
  35. * @_nseed: The number of bytes to use for the seed.
  36. * If this is greater than ISAAC_SEED_SZ_MAX, the extra bytes are
  37. * ignored.
  38. */
  39. void isaac_init(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed);
  40. /**
  41. * isaac_reseed - Mix a new batch of entropy into the current state.
  42. * To reset ISAAC to a known state, call isaac_init() again instead.
  43. * @_ctx: The instance to reseed.
  44. * @_seed: The specified seed bytes.
  45. * This may be NULL if _nseed is zero.
  46. * @_nseed: The number of bytes to use for the seed.
  47. * If this is greater than ISAAC_SEED_SZ_MAX, the extra bytes are
  48. * ignored.
  49. */
  50. void isaac_reseed(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed);
  51. /**
  52. * isaac_next_uint32 - Return the next random 32-bit value.
  53. * @_ctx: The ISAAC instance to generate the value with.
  54. */
  55. uint32_t isaac_next_uint32(isaac_ctx *_ctx);
  56. /**
  57. * isaac_next_uint - Uniform random integer less than the given value.
  58. * @_ctx: The ISAAC instance to generate the value with.
  59. * @_n: The upper bound on the range of numbers returned (not inclusive).
  60. * This must be greater than zero and less than 2**32.
  61. * To return integers in the full range 0...2**32-1, use
  62. * isaac_next_uint32() instead.
  63. * Return: An integer uniformly distributed between 0 and _n-1 (inclusive).
  64. */
  65. uint32_t isaac_next_uint(isaac_ctx *_ctx,uint32_t _n);
  66. /**
  67. * isaac_next_float - Uniform random float in the range [0,1).
  68. * @_ctx: The ISAAC instance to generate the value with.
  69. * Returns a high-quality float uniformly distributed between 0 (inclusive)
  70. * and 1 (exclusive).
  71. * All of the float's mantissa bits are random, e.g., the least significant bit
  72. * may still be non-zero even if the value is less than 0.5, and any
  73. * representable float in the range [0,1) has a chance to be returned, though
  74. * values very close to zero become increasingly unlikely.
  75. * To generate cheaper float values that do not have these properties, use
  76. * ldexpf((float)isaac_next_uint32(_ctx),-32);
  77. */
  78. float isaac_next_float(isaac_ctx *_ctx);
  79. /**
  80. * isaac_next_signed_float - Uniform random float in the range (-1,1).
  81. * @_ctx: The ISAAC instance to generate the value with.
  82. * Returns a high-quality float uniformly distributed between -1 and 1
  83. * (exclusive).
  84. * All of the float's mantissa bits are random, e.g., the least significant bit
  85. * may still be non-zero even if the magnitude is less than 0.5, and any
  86. * representable float in the range (-1,1) has a chance to be returned, though
  87. * values very close to zero become increasingly unlikely.
  88. * To generate cheaper float values that do not have these properties, use
  89. * ldexpf((float)isaac_next_uint32(_ctx),-31)-1;
  90. * though this returns values in the range [-1,1).
  91. */
  92. float isaac_next_signed_float(isaac_ctx *_ctx);
  93. /**
  94. * isaac_next_double - Uniform random double in the range [0,1).
  95. * @_ctx: The ISAAC instance to generate the value with.
  96. * Returns a high-quality double uniformly distributed between 0 (inclusive)
  97. * and 1 (exclusive).
  98. * All of the double's mantissa bits are random, e.g., the least significant
  99. * bit may still be non-zero even if the value is less than 0.5, and any
  100. * representable double in the range [0,1) has a chance to be returned, though
  101. * values very close to zero become increasingly unlikely.
  102. * To generate cheaper double values that do not have these properties, use
  103. * ldexp((double)isaac_next_uint32(_ctx),-32);
  104. */
  105. double isaac_next_double(isaac_ctx *_ctx);
  106. /**
  107. * isaac_next_signed_double - Uniform random double in the range (-1,1).
  108. * @_ctx: The ISAAC instance to generate the value with.
  109. * Returns a high-quality double uniformly distributed between -1 and 1
  110. * (exclusive).
  111. * All of the double's mantissa bits are random, e.g., the least significant
  112. * bit may still be non-zero even if the value is less than 0.5, and any
  113. * representable double in the range (-1,1) has a chance to be returned,
  114. * though values very close to zero become increasingly unlikely.
  115. * To generate cheaper double values that do not have these properties, use
  116. * ldexp((double)isaac_next_uint32(_ctx),-31)-1;
  117. * though this returns values in the range [-1,1).
  118. */
  119. double isaac_next_signed_double(isaac_ctx *_ctx);
  120. #endif