isaac.h 5.5 KB

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