abseil-cpp/absl/random/internal/nonsecure_base.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_NONSECURE_BASE_H_
16 #define ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_
17 
18 #include <algorithm>
19 #include <cstdint>
20 #include <iterator>
21 #include <type_traits>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/base/macros.h"
26 #include "absl/container/inlined_vector.h"
27 #include "absl/meta/type_traits.h"
28 #include "absl/random/internal/pool_urbg.h"
29 #include "absl/random/internal/salted_seed_seq.h"
30 #include "absl/random/internal/seed_material.h"
31 #include "absl/types/span.h"
32 
33 namespace absl {
35 namespace random_internal {
36 
37 // RandenPoolSeedSeq is a custom seed sequence type where generate() fills the
38 // provided buffer via the RandenPool entropy source.
40  private:
41  struct ContiguousTag {};
42  struct BufferTag {};
43 
44  // Generate random unsigned values directly into the buffer.
45  template <typename Contiguous>
46  void generate_impl(ContiguousTag, Contiguous begin, Contiguous end) {
47  const size_t n = std::distance(begin, end);
48  auto* a = &(*begin);
50  absl::MakeSpan(reinterpret_cast<uint8_t*>(a), sizeof(*a) * n));
51  }
52 
53  // Construct a buffer of size n and fill it with values, then copy
54  // those values into the seed iterators.
55  template <typename RandomAccessIterator>
56  void generate_impl(BufferTag, RandomAccessIterator begin,
57  RandomAccessIterator end) {
58  const size_t n = std::distance(begin, end);
62  }
63 
64  public:
66 
67  size_t size() { return 0; }
68 
69  template <typename OutIterator>
70  void param(OutIterator) const {}
71 
72  template <typename RandomAccessIterator>
73  void generate(RandomAccessIterator begin, RandomAccessIterator end) {
74  // RandomAccessIterator must be assignable from uint32_t
75  if (begin != end) {
77  // ContiguousTag indicates the common case of a known contiguous buffer,
78  // which allows directly filling the buffer. In C++20,
79  // std::contiguous_iterator_tag provides a mechanism for testing this
80  // capability, however until Abseil's support requirements allow us to
81  // assume C++20, limit checks to a few common cases.
82  using TagType = absl::conditional_t<
84  std::is_same<RandomAccessIterator,
87 
88  generate_impl(TagType{}, begin, end);
89  }
90  }
91 };
92 
93 // Each instance of NonsecureURBGBase<URBG> will be seeded by variates produced
94 // by a thread-unique URBG-instance.
95 template <typename URBG, typename Seeder = RandenPoolSeedSeq>
97  public:
98  using result_type = typename URBG::result_type;
99 
100  // Default constructor
102 
103  // Copy disallowed, move allowed.
104  NonsecureURBGBase(const NonsecureURBGBase&) = delete;
108 
109  // Constructor using a seed
110  template <class SSeq, typename = typename absl::enable_if_t<
112  explicit NonsecureURBGBase(SSeq&& seq)
113  : urbg_(ConstructURBG(std::forward<SSeq>(seq))) {}
114 
115  // Note: on MSVC, min() or max() can be interpreted as MIN() or MAX(), so we
116  // enclose min() or max() in parens as (min)() and (max)().
117  // Additionally, clang-format requires no space before this construction.
118 
119  // NonsecureURBGBase::min()
120  static constexpr result_type(min)() { return (URBG::min)(); }
121 
122  // NonsecureURBGBase::max()
123  static constexpr result_type(max)() { return (URBG::max)(); }
124 
125  // NonsecureURBGBase::operator()()
126  result_type operator()() { return urbg_(); }
127 
128  // NonsecureURBGBase::discard()
129  void discard(unsigned long long values) { // NOLINT(runtime/int)
130  urbg_.discard(values);
131  }
132 
133  bool operator==(const NonsecureURBGBase& other) const {
134  return urbg_ == other.urbg_;
135  }
136 
137  bool operator!=(const NonsecureURBGBase& other) const {
138  return !(urbg_ == other.urbg_);
139  }
140 
141  private:
142  static URBG ConstructURBG() {
143  Seeder seeder;
144  return URBG(seeder);
145  }
146 
147  template <typename SSeq>
148  static URBG ConstructURBG(SSeq&& seq) { // NOLINT(runtime/references)
149  auto salted_seq =
150  random_internal::MakeSaltedSeedSeq(std::forward<SSeq>(seq));
151  return URBG(salted_seq);
152  }
153 
154  URBG urbg_;
155 };
156 
157 } // namespace random_internal
159 } // namespace absl
160 
161 #endif // ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_
absl::random_internal::NonsecureURBGBase::NonsecureURBGBase
NonsecureURBGBase()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:101
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
absl::random_internal::NonsecureURBGBase::operator=
NonsecureURBGBase & operator=(const NonsecureURBGBase &)=delete
absl::random_internal::RandenPoolSeedSeq::generate_impl
void generate_impl(ContiguousTag, Contiguous begin, Contiguous end)
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:46
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
absl::random_internal::RandenPoolSeedSeq::result_type
uint32_t result_type
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:65
absl::conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: abseil-cpp/absl/meta/type_traits.h:634
absl::random_internal::NonsecureURBGBase::ConstructURBG
static URBG ConstructURBG(SSeq &&seq)
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:148
absl::random_internal::RandenPool::Fill
static void Fill(absl::Span< result_type > data)
Definition: abseil-cpp/absl/random/internal/pool_urbg.cc:240
absl::random_internal::RandenPoolSeedSeq::size
size_t size()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:67
absl::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: abseil-cpp/absl/meta/type_traits.h:631
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
iterator
const typedef MCPhysReg * iterator
Definition: MCRegisterInfo.h:27
absl::random_internal::RandenPoolSeedSeq::BufferTag
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:42
absl::random_internal::MakeSaltedSeedSeq
SSeq MakeSaltedSeedSeq(SSeq &&seq)
Definition: abseil-cpp/absl/random/internal/salted_seed_seq.h:145
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::random_internal::NonsecureURBGBase::operator!=
bool operator!=(const NonsecureURBGBase &other) const
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:137
absl::random_internal::RandenPoolSeedSeq
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:39
absl::random_internal::RandenPoolSeedSeq::ContiguousTag
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:41
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > >::result_type
typename random_internal::randen_engine< uint64_t > ::result_type result_type
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:98
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
absl::random_internal::NonsecureURBGBase::NonsecureURBGBase
NonsecureURBGBase(SSeq &&seq)
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:112
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
result_type
const typedef int * result_type
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:4325
absl::random_internal::NonsecureURBGBase::operator()
result_type operator()()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:126
absl::random_internal::RandenPoolSeedSeq::generate_impl
void generate_impl(BufferTag, RandomAccessIterator begin, RandomAccessIterator end)
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:56
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
absl::random_internal::NonsecureURBGBase::max
static constexpr result_type() max()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:123
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
min
#define min(a, b)
Definition: qsort.h:83
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::random_internal::NonsecureURBGBase::discard
void discard(unsigned long long values)
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:129
absl::random_internal::NonsecureURBGBase::min
static constexpr result_type() min()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:120
value
const char * value
Definition: hpack_parser_table.cc:165
absl::random_internal::RandenPoolSeedSeq::param
void param(OutIterator) const
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:70
absl::random_internal::RandenPoolSeedSeq::generate
void generate(RandomAccessIterator begin, RandomAccessIterator end)
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:73
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
absl::random_internal::NonsecureURBGBase
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:96
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::NonsecureURBGBase::ConstructURBG
static URBG ConstructURBG()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:142
absl::forward
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:230
absl::InlinedVector
Definition: abseil-cpp/absl/container/inlined_vector.h:69
absl::random_internal::NonsecureURBGBase::operator==
bool operator==(const NonsecureURBGBase &other) const
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:133
absl::random_internal::NonsecureURBGBase::urbg_
URBG urbg_
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:154
absl::MakeSpan
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept
Definition: abseil-cpp/absl/types/span.h:661


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:32