Chris@16
|
1 /* boost random/random_device.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000
|
Chris@16
|
4 * Copyright Steven Watanabe 2010-2011
|
Chris@16
|
5 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 *
|
Chris@16
|
9 * $Id: random_device.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
|
Chris@16
|
10 *
|
Chris@16
|
11 * Revision history
|
Chris@16
|
12 * 2000-02-18 Portability fixes (thanks to Beman Dawes)
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 // See http://www.boost.org/libs/random for documentation.
|
Chris@16
|
16
|
Chris@16
|
17
|
Chris@16
|
18 #ifndef BOOST_RANDOM_RANDOM_DEVICE_HPP
|
Chris@16
|
19 #define BOOST_RANDOM_RANDOM_DEVICE_HPP
|
Chris@16
|
20
|
Chris@16
|
21 #include <string>
|
Chris@16
|
22 #include <boost/config.hpp>
|
Chris@16
|
23 #include <boost/noncopyable.hpp>
|
Chris@16
|
24 #include <boost/random/detail/auto_link.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost {
|
Chris@16
|
27 namespace random {
|
Chris@16
|
28
|
Chris@16
|
29 /**
|
Chris@16
|
30 * Class \random_device models a \nondeterministic_random_number_generator.
|
Chris@16
|
31 * It uses one or more implementation-defined stochastic processes to
|
Chris@16
|
32 * generate a sequence of uniformly distributed non-deterministic random
|
Chris@16
|
33 * numbers. For those environments where a non-deterministic random number
|
Chris@16
|
34 * generator is not available, class random_device must not be implemented. See
|
Chris@16
|
35 *
|
Chris@16
|
36 * @blockquote
|
Chris@16
|
37 * "Randomness Recommendations for Security", D. Eastlake, S. Crocker,
|
Chris@16
|
38 * J. Schiller, Network Working Group, RFC 1750, December 1994
|
Chris@16
|
39 * @endblockquote
|
Chris@16
|
40 *
|
Chris@16
|
41 * for further discussions.
|
Chris@16
|
42 *
|
Chris@16
|
43 * @xmlnote
|
Chris@16
|
44 * Some operating systems abstract the computer hardware enough
|
Chris@16
|
45 * to make it difficult to non-intrusively monitor stochastic processes.
|
Chris@16
|
46 * However, several do provide a special device for exactly this purpose.
|
Chris@16
|
47 * It seems to be impossible to emulate the functionality using Standard
|
Chris@16
|
48 * C++ only, so users should be aware that this class may not be available
|
Chris@16
|
49 * on all platforms.
|
Chris@16
|
50 * @endxmlnote
|
Chris@16
|
51 *
|
Chris@16
|
52 * <b>Implementation Note for Linux</b>
|
Chris@16
|
53 *
|
Chris@16
|
54 * On the Linux operating system, token is interpreted as a filesystem
|
Chris@16
|
55 * path. It is assumed that this path denotes an operating system
|
Chris@16
|
56 * pseudo-device which generates a stream of non-deterministic random
|
Chris@16
|
57 * numbers. The pseudo-device should never signal an error or end-of-file.
|
Chris@16
|
58 * Otherwise, @c std::ios_base::failure is thrown. By default,
|
Chris@16
|
59 * \random_device uses the /dev/urandom pseudo-device to retrieve
|
Chris@16
|
60 * the random numbers. Another option would be to specify the /dev/random
|
Chris@16
|
61 * pseudo-device, which blocks on reads if the entropy pool has no more
|
Chris@16
|
62 * random bits available.
|
Chris@16
|
63 *
|
Chris@16
|
64 * <b>Implementation Note for Windows</b>
|
Chris@16
|
65 *
|
Chris@16
|
66 * On the Windows operating system, token is interpreted as the name
|
Chris@16
|
67 * of a cryptographic service provider. By default \random_device uses
|
Chris@16
|
68 * MS_DEF_PROV.
|
Chris@16
|
69 *
|
Chris@16
|
70 * <b>Performance</b>
|
Chris@16
|
71 *
|
Chris@16
|
72 * The test program <a href="\boost/libs/random/performance/nondet_random_speed.cpp">
|
Chris@16
|
73 * nondet_random_speed.cpp</a> measures the execution times of the
|
Chris@16
|
74 * random_device.hpp implementation of the above algorithms in a tight
|
Chris@16
|
75 * loop. The performance has been evaluated on an
|
Chris@16
|
76 * Intel(R) Core(TM) i7 CPU Q 840 \@ 1.87GHz, 1867 Mhz with
|
Chris@16
|
77 * Visual C++ 2010, Microsoft Windows 7 Professional and with gcc 4.4.5,
|
Chris@16
|
78 * Ubuntu Linux 2.6.35-25-generic.
|
Chris@16
|
79 *
|
Chris@16
|
80 * <table cols="2">
|
Chris@16
|
81 * <tr><th>Platform</th><th>time per invocation [microseconds]</th></tr>
|
Chris@16
|
82 * <tr><td> Windows </td><td>2.9</td></tr>
|
Chris@16
|
83 * <tr><td> Linux </td><td>1.7</td></tr>
|
Chris@16
|
84 * </table>
|
Chris@16
|
85 *
|
Chris@16
|
86 * The measurement error is estimated at +/- 1 usec.
|
Chris@16
|
87 */
|
Chris@16
|
88 class random_device : private noncopyable
|
Chris@16
|
89 {
|
Chris@16
|
90 public:
|
Chris@16
|
91 typedef unsigned int result_type;
|
Chris@16
|
92 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
Chris@16
|
93
|
Chris@16
|
94 /** Returns the smallest value that the \random_device can produce. */
|
Chris@16
|
95 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
|
Chris@16
|
96 /** Returns the largest value that the \random_device can produce. */
|
Chris@16
|
97 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return ~0u; }
|
Chris@16
|
98
|
Chris@16
|
99 /** Constructs a @c random_device, optionally using the default device. */
|
Chris@16
|
100 BOOST_RANDOM_DECL random_device();
|
Chris@16
|
101 /**
|
Chris@16
|
102 * Constructs a @c random_device, optionally using the given token as an
|
Chris@16
|
103 * access specification (for example, a URL) to some implementation-defined
|
Chris@16
|
104 * service for monitoring a stochastic process.
|
Chris@16
|
105 */
|
Chris@16
|
106 BOOST_RANDOM_DECL explicit random_device(const std::string& token);
|
Chris@16
|
107
|
Chris@16
|
108 BOOST_RANDOM_DECL ~random_device();
|
Chris@16
|
109
|
Chris@16
|
110 /**
|
Chris@16
|
111 * Returns: An entropy estimate for the random numbers returned by
|
Chris@16
|
112 * operator(), in the range min() to log2( max()+1). A deterministic
|
Chris@16
|
113 * random number generator (e.g. a pseudo-random number engine)
|
Chris@16
|
114 * has entropy 0.
|
Chris@16
|
115 *
|
Chris@16
|
116 * Throws: Nothing.
|
Chris@16
|
117 */
|
Chris@16
|
118 BOOST_RANDOM_DECL double entropy() const;
|
Chris@16
|
119 /** Returns a random value in the range [min, max]. */
|
Chris@16
|
120 BOOST_RANDOM_DECL unsigned int operator()();
|
Chris@16
|
121
|
Chris@16
|
122 /** Fills a range with random 32-bit values. */
|
Chris@16
|
123 template<class Iter>
|
Chris@16
|
124 void generate(Iter begin, Iter end)
|
Chris@16
|
125 {
|
Chris@16
|
126 for(; begin != end; ++begin) {
|
Chris@16
|
127 *begin = (*this)();
|
Chris@16
|
128 }
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 private:
|
Chris@16
|
132 class impl;
|
Chris@16
|
133 impl * pimpl;
|
Chris@16
|
134 };
|
Chris@16
|
135
|
Chris@16
|
136 } // namespace random
|
Chris@16
|
137
|
Chris@16
|
138 using random::random_device;
|
Chris@16
|
139
|
Chris@16
|
140 } // namespace boost
|
Chris@16
|
141
|
Chris@16
|
142 #endif /* BOOST_RANDOM_RANDOM_DEVICE_HPP */
|