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