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