randen_traits.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Y_ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_
  15. #define Y_ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_
  16. // HERMETIC NOTE: The randen_hwaes target must not introduce duplicate
  17. // symbols from arbitrary system and other headers, since it may be built
  18. // with different flags from other targets, using different levels of
  19. // optimization, potentially introducing ODR violations.
  20. #include <cstddef>
  21. #include "y_absl/base/config.h"
  22. namespace y_absl {
  23. Y_ABSL_NAMESPACE_BEGIN
  24. namespace random_internal {
  25. // RANDen = RANDom generator or beetroots in Swiss High German.
  26. // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
  27. // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
  28. //
  29. // High-level summary:
  30. // 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is
  31. // a sponge-like random generator that requires a cryptographic permutation.
  32. // It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by
  33. // achieving backtracking resistance with only one Permute() per buffer.
  34. //
  35. // 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round
  36. // Function" constructs up to 1024-bit permutations using an improved
  37. // Generalized Feistel network with 2-round AES-128 functions. This Feistel
  38. // block shuffle achieves diffusion faster and is less vulnerable to
  39. // sliced-biclique attacks than the Type-2 cyclic shuffle.
  40. //
  41. // 3) "Improving the Generalized Feistel" and "New criterion for diffusion
  42. // property" extends the same kind of improved Feistel block shuffle to 16
  43. // branches, which enables a 2048-bit permutation.
  44. //
  45. // Combine these three ideas and also change Simpira's subround keys from
  46. // structured/low-entropy counters to digits of Pi (or other random source).
  47. // RandenTraits contains the basic algorithm traits, such as the size of the
  48. // state, seed, sponge, etc.
  49. struct RandenTraits {
  50. // Size of the entire sponge / state for the randen PRNG.
  51. static constexpr size_t kStateBytes = 256; // 2048-bit
  52. // Size of the 'inner' (inaccessible) part of the sponge. Larger values would
  53. // require more frequent calls to RandenGenerate.
  54. static constexpr size_t kCapacityBytes = 16; // 128-bit
  55. // Size of the default seed consumed by the sponge.
  56. static constexpr size_t kSeedBytes = kStateBytes - kCapacityBytes;
  57. // Assuming 128-bit blocks, the number of blocks in the state.
  58. // Largest size for which security proofs are known.
  59. static constexpr size_t kFeistelBlocks = 16;
  60. // Ensures SPRP security and two full subblock diffusions.
  61. // Must be > 4 * log2(kFeistelBlocks).
  62. static constexpr size_t kFeistelRounds = 16 + 1;
  63. // Size of the key. A 128-bit key block is used for every-other
  64. // feistel block (Type-2 generalized Feistel network) in each round.
  65. static constexpr size_t kKeyBytes = 16 * kFeistelRounds * kFeistelBlocks / 2;
  66. };
  67. // Randen key arrays. In randen_round_keys.cc
  68. extern const unsigned char kRandenRoundKeys[RandenTraits::kKeyBytes];
  69. extern const unsigned char kRandenRoundKeysBE[RandenTraits::kKeyBytes];
  70. } // namespace random_internal
  71. Y_ABSL_NAMESPACE_END
  72. } // namespace y_absl
  73. #endif // Y_ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_