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_SALTED_SEED_SEQ_H_ 16 #define ABSL_RANDOM_INTERNAL_SALTED_SEED_SEQ_H_ 17 18 #include <cstdint> 19 #include <cstdlib> 20 #include <initializer_list> 21 #include <iterator> 22 #include <memory> 23 #include <type_traits> 24 #include <utility> 25 26 #include "absl/container/inlined_vector.h" 27 #include "absl/meta/type_traits.h" 28 #include "absl/random/internal/seed_material.h" 29 #include "absl/types/optional.h" 30 #include "absl/types/span.h" 31 32 namespace absl { 33 ABSL_NAMESPACE_BEGIN 34 namespace random_internal { 35 36 // This class conforms to the C++ Standard "Seed Sequence" concept 37 // [rand.req.seedseq]. 38 // 39 // A `SaltedSeedSeq` is meant to wrap an existing seed sequence and modify 40 // generated sequence by mixing with extra entropy. This entropy may be 41 // build-dependent or process-dependent. The implementation may change to be 42 // have either or both kinds of entropy. If salt is not available sequence is 43 // not modified. 44 template <typename SSeq> 45 class SaltedSeedSeq { 46 public: 47 using inner_sequence_type = SSeq; 48 using result_type = typename SSeq::result_type; 49 SaltedSeedSeq()50 SaltedSeedSeq() : seq_(absl::make_unique<SSeq>()) {} 51 52 template <typename Iterator> SaltedSeedSeq(Iterator begin,Iterator end)53 SaltedSeedSeq(Iterator begin, Iterator end) 54 : seq_(absl::make_unique<SSeq>(begin, end)) {} 55 56 template <typename T> SaltedSeedSeq(std::initializer_list<T> il)57 SaltedSeedSeq(std::initializer_list<T> il) 58 : SaltedSeedSeq(il.begin(), il.end()) {} 59 60 SaltedSeedSeq(const SaltedSeedSeq&) = delete; 61 SaltedSeedSeq& operator=(const SaltedSeedSeq&) = delete; 62 63 SaltedSeedSeq(SaltedSeedSeq&&) = default; 64 SaltedSeedSeq& operator=(SaltedSeedSeq&&) = default; 65 66 template <typename RandomAccessIterator> generate(RandomAccessIterator begin,RandomAccessIterator end)67 void generate(RandomAccessIterator begin, RandomAccessIterator end) { 68 // The common case is that generate is called with ContiguousIterators 69 // to uint arrays. Such contiguous memory regions may be optimized, 70 // which we detect here. 71 using tag = absl::conditional_t< 72 (std::is_pointer<RandomAccessIterator>::value && 73 std::is_same<absl::decay_t<decltype(*begin)>, uint32_t>::value), 74 ContiguousAndUint32Tag, DefaultTag>; 75 if (begin != end) { 76 generate_impl(begin, end, tag{}); 77 } 78 } 79 80 template <typename OutIterator> param(OutIterator out)81 void param(OutIterator out) const { 82 seq_->param(out); 83 } 84 size()85 size_t size() const { return seq_->size(); } 86 87 private: 88 struct ContiguousAndUint32Tag {}; 89 struct DefaultTag {}; 90 91 // Generate which requires the iterators are contiguous pointers to uint32_t. generate_impl(uint32_t * begin,uint32_t * end,ContiguousAndUint32Tag)92 void generate_impl(uint32_t* begin, uint32_t* end, ContiguousAndUint32Tag) { 93 generate_contiguous(absl::MakeSpan(begin, end)); 94 } 95 96 // The uncommon case for generate is that it is called with iterators over 97 // some other buffer type which is assignable from a 32-bit value. In this 98 // case we allocate a temporary 32-bit buffer and then copy-assign back 99 // to the initial inputs. 100 template <typename RandomAccessIterator> generate_impl(RandomAccessIterator begin,RandomAccessIterator end,DefaultTag)101 void generate_impl(RandomAccessIterator begin, RandomAccessIterator end, 102 DefaultTag) { 103 return generate_and_copy(std::distance(begin, end), begin); 104 } 105 106 // Fills the initial seed buffer the underlying SSeq::generate() call, 107 // mixing in the salt material. generate_contiguous(absl::Span<uint32_t> buffer)108 void generate_contiguous(absl::Span<uint32_t> buffer) { 109 seq_->generate(buffer.begin(), buffer.end()); 110 const uint32_t salt = absl::random_internal::GetSaltMaterial().value_or(0); 111 MixIntoSeedMaterial(absl::MakeConstSpan(&salt, 1), buffer); 112 } 113 114 // Allocates a seed buffer of `n` elements, generates the seed, then 115 // copies the result into the `out` iterator. 116 template <typename Iterator> generate_and_copy(size_t n,Iterator out)117 void generate_and_copy(size_t n, Iterator out) { 118 // Allocate a temporary buffer, generate, and then copy. 119 absl::InlinedVector<uint32_t, 8> data(n, 0); 120 generate_contiguous(absl::MakeSpan(data.data(), data.size())); 121 std::copy(data.begin(), data.end(), out); 122 } 123 124 // Because [rand.req.seedseq] is not required to be copy-constructible, 125 // copy-assignable nor movable, we wrap it with unique pointer to be able 126 // to move SaltedSeedSeq. 127 std::unique_ptr<SSeq> seq_; 128 }; 129 130 // is_salted_seed_seq indicates whether the type is a SaltedSeedSeq. 131 template <typename T, typename = void> 132 struct is_salted_seed_seq : public std::false_type {}; 133 134 template <typename T> 135 struct is_salted_seed_seq< 136 T, typename std::enable_if<std::is_same< 137 T, SaltedSeedSeq<typename T::inner_sequence_type>>::value>::type> 138 : public std::true_type {}; 139 140 // MakeSaltedSeedSeq returns a salted variant of the seed sequence. 141 // When provided with an existing SaltedSeedSeq, returns the input parameter, 142 // otherwise constructs a new SaltedSeedSeq which embodies the original 143 // non-salted seed parameters. 144 template < 145 typename SSeq, // 146 typename EnableIf = absl::enable_if_t<is_salted_seed_seq<SSeq>::value>> 147 SSeq MakeSaltedSeedSeq(SSeq&& seq) { 148 return SSeq(std::forward<SSeq>(seq)); 149 } 150 151 template < 152 typename SSeq, // 153 typename EnableIf = absl::enable_if_t<!is_salted_seed_seq<SSeq>::value>> 154 SaltedSeedSeq<typename std::decay<SSeq>::type> MakeSaltedSeedSeq(SSeq&& seq) { 155 using sseq_type = typename std::decay<SSeq>::type; 156 using result_type = typename sseq_type::result_type; 157 158 absl::InlinedVector<result_type, 8> data; 159 seq.param(std::back_inserter(data)); 160 return SaltedSeedSeq<sseq_type>(data.begin(), data.end()); 161 } 162 163 } // namespace random_internal 164 ABSL_NAMESPACE_END 165 } // namespace absl 166 167 #endif // ABSL_RANDOM_INTERNAL_SALTED_SEED_SEQ_H_ 168