abseil-cpp/absl/random/internal/seed_material.h
Go to the documentation of this file.
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 
15 #ifndef ABSL_RANDOM_INTERNAL_SEED_MATERIAL_H_
16 #define ABSL_RANDOM_INTERNAL_SEED_MATERIAL_H_
17 
18 #include <cassert>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <string>
22 #include <vector>
23 
24 #include "absl/base/attributes.h"
25 #include "absl/random/internal/fast_uniform_bits.h"
26 #include "absl/types/optional.h"
27 #include "absl/types/span.h"
28 
29 namespace absl {
31 namespace random_internal {
32 
33 // Returns the number of 32-bit blocks needed to contain the given number of
34 // bits.
35 constexpr size_t SeedBitsToBlocks(size_t seed_size) {
36  return (seed_size + 31) / 32;
37 }
38 
39 // Amount of entropy (measured in bits) used to instantiate a Seed Sequence,
40 // with which to create a URBG.
41 constexpr size_t kEntropyBitsNeeded = 256;
42 
43 // Amount of entropy (measured in 32-bit blocks) used to instantiate a Seed
44 // Sequence, with which to create a URBG.
45 constexpr size_t kEntropyBlocksNeeded =
47 
48 static_assert(kEntropyBlocksNeeded > 0,
49  "Entropy used to seed URBGs must be nonzero.");
50 
51 // Attempts to fill a span of uint32_t-values using an OS-provided source of
52 // true entropy (eg. /dev/urandom) into an array of uint32_t blocks of data. The
53 // resulting array may be used to initialize an instance of a class conforming
54 // to the C++ Standard "Seed Sequence" concept [rand.req.seedseq].
55 //
56 // If values.data() == nullptr, the behavior is undefined.
59 
60 // Attempts to fill a span of uint32_t-values using variates generated by an
61 // existing instance of a class conforming to the C++ Standard "Uniform Random
62 // Bit Generator" concept [rand.req.urng]. The resulting data may be used to
63 // initialize an instance of a class conforming to the C++ Standard
64 // "Seed Sequence" concept [rand.req.seedseq].
65 //
66 // If urbg == nullptr or values.data() == nullptr, the behavior is undefined.
67 template <typename URBG>
69  URBG* urbg, absl::Span<uint32_t> values) {
71 
72  assert(urbg != nullptr && values.data() != nullptr);
73  if (urbg == nullptr || values.data() == nullptr) {
74  return false;
75  }
76 
77  for (uint32_t& seed_value : values) {
78  seed_value = distr(*urbg);
79  }
80  return true;
81 }
82 
83 // Mixes given sequence of values with into given sequence of seed material.
84 // Time complexity of this function is O(sequence.size() *
85 // seed_material.size()).
86 //
87 // Algorithm is based on code available at
88 // https://gist.github.com/imneme/540829265469e673d045
89 // by Melissa O'Neill.
91  absl::Span<uint32_t> seed_material);
92 
93 // Returns salt value.
94 //
95 // Salt is obtained only once and stored in static variable.
96 //
97 // May return empty value if optaining the salt was not possible.
99 
100 } // namespace random_internal
102 } // namespace absl
103 
104 #endif // ABSL_RANDOM_INTERNAL_SEED_MATERIAL_H_
absl::random_internal::SeedBitsToBlocks
constexpr size_t SeedBitsToBlocks(size_t seed_size)
Definition: abseil-cpp/absl/random/internal/seed_material.h:35
absl::Span
Definition: abseil-cpp/absl/types/span.h:152
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
ABSL_MUST_USE_RESULT
#define ABSL_MUST_USE_RESULT
Definition: abseil-cpp/absl/base/attributes.h:441
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::random_internal::ReadSeedMaterialFromOSEntropy
bool ReadSeedMaterialFromOSEntropy(absl::Span< uint32_t > values)
Definition: abseil-cpp/absl/random/internal/seed_material.cc:205
absl::optional< uint32_t >
absl::random_internal::kEntropyBlocksNeeded
constexpr size_t kEntropyBlocksNeeded
Definition: abseil-cpp/absl/random/internal/seed_material.h:45
absl::random_internal::MixIntoSeedMaterial
void MixIntoSeedMaterial(absl::Span< const uint32_t > sequence, absl::Span< uint32_t > seed_material)
Definition: abseil-cpp/absl/random/internal/seed_material.cc:216
values
std::array< int64_t, Size > values
Definition: abseil-cpp/absl/container/btree_benchmark.cc:608
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::random_internal::GetSaltMaterial
absl::optional< uint32_t > GetSaltMaterial()
Definition: abseil-cpp/absl/random/internal/seed_material.cc:248
absl::random_internal::ReadSeedMaterialFromURBG
ABSL_MUST_USE_RESULT bool ReadSeedMaterialFromURBG(URBG *urbg, absl::Span< uint32_t > values)
Definition: abseil-cpp/absl/random/internal/seed_material.h:68
absl::random_internal::FastUniformBits
Definition: abseil-cpp/absl/random/internal/fast_uniform_bits.h:89
absl::random_internal::kEntropyBitsNeeded
constexpr size_t kEntropyBitsNeeded
Definition: abseil-cpp/absl/random/internal/seed_material.h:41


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:10