[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 5 | #ifndef COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_ |
| 6 | #define COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_ |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 7 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 11 | #include <functional> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 16 | #include "base/macros.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 17 | #include "base/metrics/field_trial.h" |
| 18 | #include "third_party/mt19937ar/mt19937ar.h" |
| 19 | |
| 20 | namespace metrics { |
| 21 | |
| 22 | // Internals of entropy_provider.cc exposed for testing. |
| 23 | namespace internal { |
| 24 | |
| 25 | // A functor that generates random numbers based on a seed, using the Mersenne |
| 26 | // Twister algorithm. Suitable for use with std::random_shuffle(). |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 27 | struct SeededRandGenerator : std::unary_function<uint32_t, uint32_t> { |
| 28 | explicit SeededRandGenerator(uint32_t seed); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 29 | ~SeededRandGenerator(); |
| 30 | |
| 31 | // Returns a random number in range [0, range). |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 32 | uint32_t operator()(uint32_t range); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 33 | |
| 34 | MersenneTwister mersenne_twister_; |
| 35 | }; |
| 36 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 37 | // Fills |mapping| to create a bijection of values in the range of |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 38 | // [0, |mapping.size()|), permuted based on |randomization_seed|. |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 39 | void PermuteMappingUsingRandomizationSeed(uint32_t randomization_seed, |
| 40 | std::vector<uint16_t>* mapping); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 41 | |
| 42 | } // namespace internal |
| 43 | |
| 44 | // SHA1EntropyProvider is an entropy provider suitable for high entropy |
| 45 | // sources. It works by taking the first 64 bits of the SHA1 hash of the |
| 46 | // entropy source concatenated with the trial name and using that for the |
| 47 | // final entropy value. |
| 48 | class SHA1EntropyProvider : public base::FieldTrial::EntropyProvider { |
| 49 | public: |
| 50 | // Creates a SHA1EntropyProvider with the given |entropy_source|, which |
| 51 | // should contain a large amount of entropy - for example, a textual |
| 52 | // representation of a persistent randomly-generated 128-bit value. |
| 53 | explicit SHA1EntropyProvider(const std::string& entropy_source); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 54 | ~SHA1EntropyProvider() override; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 55 | |
| 56 | // base::FieldTrial::EntropyProvider implementation: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 57 | double GetEntropyForTrial(const std::string& trial_name, |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 58 | uint32_t randomization_seed) const override; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | std::string entropy_source_; |
| 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider); |
| 64 | }; |
| 65 | |
| 66 | // PermutedEntropyProvider is an entropy provider suitable for low entropy |
| 67 | // sources (below 16 bits). It uses the field trial name to generate a |
| 68 | // permutation of a mapping array from an initial entropy value to a new value. |
| 69 | // Note: This provider's performance is O(2^n), where n is the number of bits |
| 70 | // in the entropy source. |
| 71 | class PermutedEntropyProvider : public base::FieldTrial::EntropyProvider { |
| 72 | public: |
| 73 | // Creates a PermutedEntropyProvider with the given |low_entropy_source|, |
| 74 | // which should have a value in the range of [0, low_entropy_source_max). |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 75 | PermutedEntropyProvider(uint16_t low_entropy_source, |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 76 | size_t low_entropy_source_max); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 77 | ~PermutedEntropyProvider() override; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 78 | |
| 79 | // base::FieldTrial::EntropyProvider implementation: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 80 | double GetEntropyForTrial(const std::string& trial_name, |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 81 | uint32_t randomization_seed) const override; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 82 | |
[email protected] | fb6670a | 2013-07-31 13:31:35 | [diff] [blame] | 83 | protected: |
| 84 | // Performs the permutation algorithm and returns the permuted value that |
| 85 | // corresponds to |low_entropy_source_|. |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 86 | virtual uint16_t GetPermutedValue(uint32_t randomization_seed) const; |
[email protected] | fb6670a | 2013-07-31 13:31:35 | [diff] [blame] | 87 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 88 | private: |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 89 | uint16_t low_entropy_source_; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 90 | size_t low_entropy_source_max_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(PermutedEntropyProvider); |
| 93 | }; |
| 94 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 95 | } // namespace metrics |
| 96 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 97 | #endif // COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_ |