aboutsummaryrefslogtreecommitdiffhomepage
path: root/absl/random/internal/nonsecure_base.h
blob: 8847e74bef6bbc7a370074a6183b6cc659be2f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_
#define ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
#include <type_traits>
#include <vector>

#include "absl/base/macros.h"
#include "absl/meta/type_traits.h"
#include "absl/random/internal/pool_urbg.h"
#include "absl/random/internal/salted_seed_seq.h"
#include "absl/random/internal/seed_material.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"

namespace absl {
namespace random_internal {

// Each instance of NonsecureURBGBase<URBG> will be seeded by variates produced
// by a thread-unique URBG-instance.
template <typename URBG>
class NonsecureURBGBase {
 public:
  using result_type = typename URBG::result_type;

  // Default constructor
  NonsecureURBGBase() : urbg_(ConstructURBG()) {}

  // Copy disallowed, move allowed.
  NonsecureURBGBase(const NonsecureURBGBase&) = delete;
  NonsecureURBGBase& operator=(const NonsecureURBGBase&) = delete;
  NonsecureURBGBase(NonsecureURBGBase&&) = default;
  NonsecureURBGBase& operator=(NonsecureURBGBase&&) = default;

  // Constructor using a seed
  template <class SSeq, typename = typename absl::enable_if_t<
                            !std::is_same<SSeq, NonsecureURBGBase>::value>>
  explicit NonsecureURBGBase(SSeq&& seq)
      : urbg_(ConstructURBG(std::forward<SSeq>(seq))) {}

  // Note: on MSVC, min() or max() can be interpreted as MIN() or MAX(), so we
  // enclose min() or max() in parens as (min)() and (max)().
  // Additionally, clang-format requires no space before this construction.

  // NonsecureURBGBase::min()
  static constexpr result_type(min)() { return (URBG::min)(); }

  // NonsecureURBGBase::max()
  static constexpr result_type(max)() { return (URBG::max)(); }

  // NonsecureURBGBase::operator()()
  result_type operator()() { return urbg_(); }

  // NonsecureURBGBase::discard()
  void discard(unsigned long long values) {  // NOLINT(runtime/int)
    urbg_.discard(values);
  }

  bool operator==(const NonsecureURBGBase& other) const {
    return urbg_ == other.urbg_;
  }

  bool operator!=(const NonsecureURBGBase& other) const {
    return !(urbg_ == other.urbg_);
  }

 private:
  // Seeder is a custom seed sequence type where generate() fills the provided
  // buffer via the RandenPool entropy source.
  struct Seeder {
    using result_type = uint32_t;

    size_t size() { return 0; }

    template <typename OutIterator>
    void param(OutIterator) const {}

    template <typename RandomAccessIterator>
    void generate(RandomAccessIterator begin, RandomAccessIterator end) {
      if (begin != end) {
        // begin, end must be random access iterators assignable from uint32_t.
        generate_impl(
            std::integral_constant<bool, sizeof(*begin) == sizeof(uint32_t)>{},
            begin, end);
      }
    }

    // Commonly, generate is invoked with a pointer to a buffer which
    // can be cast to a uint32_t.
    template <typename RandomAccessIterator>
    void generate_impl(std::integral_constant<bool, true>,
                       RandomAccessIterator begin, RandomAccessIterator end) {
      auto buffer = absl::MakeSpan(begin, end);
      auto target = absl::MakeSpan(reinterpret_cast<uint32_t*>(buffer.data()),
                                   buffer.size());
      RandenPool<uint32_t>::Fill(target);
    }

    // The non-uint32_t case should be uncommon, and involves an extra copy,
    // filling the uint32_t buffer and then mixing into the output.
    template <typename RandomAccessIterator>
    void generate_impl(std::integral_constant<bool, false>,
                       RandomAccessIterator begin, RandomAccessIterator end) {
      const size_t n = std::distance(begin, end);
      absl::InlinedVector<uint32_t, 8> data(n, 0);
      RandenPool<uint32_t>::Fill(absl::MakeSpan(data.begin(), data.end()));
      std::copy(std::begin(data), std::end(data), begin);
    }
  };

  static URBG ConstructURBG() {
    Seeder seeder;
    return URBG(seeder);
  }

  template <typename SSeq>
  static URBG ConstructURBG(SSeq&& seq) {  // NOLINT(runtime/references)
    auto salted_seq =
        random_internal::MakeSaltedSeedSeq(std::forward<SSeq>(seq));
    return URBG(salted_seq);
  }

  URBG urbg_;
};

}  // namespace random_internal
}  // namespace absl

#endif  // ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_