You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

randen.h 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_INTERNAL_RANDEN_H_
  15. #define ABSL_RANDOM_INTERNAL_RANDEN_H_
  16. #include <cstddef>
  17. #include "absl/random/internal/platform.h"
  18. #include "absl/random/internal/randen_hwaes.h"
  19. #include "absl/random/internal/randen_slow.h"
  20. #include "absl/random/internal/randen_traits.h"
  21. namespace absl
  22. {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace random_internal
  25. {
  26. // RANDen = RANDom generator or beetroots in Swiss High German.
  27. // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
  28. // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
  29. //
  30. // Randen implements the basic state manipulation methods.
  31. class Randen
  32. {
  33. public:
  34. static constexpr size_t kStateBytes = RandenTraits::kStateBytes;
  35. static constexpr size_t kCapacityBytes = RandenTraits::kCapacityBytes;
  36. static constexpr size_t kSeedBytes = RandenTraits::kSeedBytes;
  37. ~Randen() = default;
  38. Randen();
  39. // Generate updates the randen sponge. The outer portion of the sponge
  40. // (kCapacityBytes .. kStateBytes) may be consumed as PRNG state.
  41. // REQUIRES: state points to kStateBytes of state.
  42. inline void Generate(void* state) const
  43. {
  44. #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
  45. // HW AES Dispatch.
  46. if (has_crypto_)
  47. {
  48. RandenHwAes::Generate(keys_, state);
  49. }
  50. else
  51. {
  52. RandenSlow::Generate(keys_, state);
  53. }
  54. #elif ABSL_HAVE_ACCELERATED_AES
  55. // HW AES is enabled.
  56. RandenHwAes::Generate(keys_, state);
  57. #else
  58. // HW AES is disabled.
  59. RandenSlow::Generate(keys_, state);
  60. #endif
  61. }
  62. // Absorb incorporates additional seed material into the randen sponge. After
  63. // absorb returns, Generate must be called before the state may be consumed.
  64. // REQUIRES: seed points to kSeedBytes of seed.
  65. // REQUIRES: state points to kStateBytes of state.
  66. inline void Absorb(const void* seed, void* state) const
  67. {
  68. #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
  69. // HW AES Dispatch.
  70. if (has_crypto_)
  71. {
  72. RandenHwAes::Absorb(seed, state);
  73. }
  74. else
  75. {
  76. RandenSlow::Absorb(seed, state);
  77. }
  78. #elif ABSL_HAVE_ACCELERATED_AES
  79. // HW AES is enabled.
  80. RandenHwAes::Absorb(seed, state);
  81. #else
  82. // HW AES is disabled.
  83. RandenSlow::Absorb(seed, state);
  84. #endif
  85. }
  86. private:
  87. const void* keys_;
  88. #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
  89. bool has_crypto_;
  90. #endif
  91. };
  92. } // namespace random_internal
  93. ABSL_NAMESPACE_END
  94. } // namespace absl
  95. #endif // ABSL_RANDOM_INTERNAL_RANDEN_H_