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