| 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 #include "components/variations/entropy_provider.h" | 5 #include "components/variations/entropy_provider.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <numeric> | 9 #include <numeric> |
| 10 | 10 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 // An TrialEntropyGenerator that uses the SHA1EntropyProvider with the high | 74 // An TrialEntropyGenerator that uses the SHA1EntropyProvider with the high |
| 75 // entropy source (random GUID with 128 bits of entropy + 13 additional bits of | 75 // entropy source (random GUID with 128 bits of entropy + 13 additional bits of |
| 76 // entropy corresponding to a low entropy source). | 76 // entropy corresponding to a low entropy source). |
| 77 class SHA1EntropyGenerator : public TrialEntropyGenerator { | 77 class SHA1EntropyGenerator : public TrialEntropyGenerator { |
| 78 public: | 78 public: |
| 79 explicit SHA1EntropyGenerator(const std::string& trial_name) | 79 explicit SHA1EntropyGenerator(const std::string& trial_name) |
| 80 : trial_name_(trial_name) { | 80 : trial_name_(trial_name) { |
| 81 } | 81 } |
| 82 | 82 |
| 83 virtual ~SHA1EntropyGenerator() { | 83 ~SHA1EntropyGenerator() override {} |
| 84 } | |
| 85 | 84 |
| 86 virtual double GenerateEntropyValue() const override { | 85 double GenerateEntropyValue() const override { |
| 87 // Use a random GUID + 13 additional bits of entropy to match how the | 86 // Use a random GUID + 13 additional bits of entropy to match how the |
| 88 // SHA1EntropyProvider is used in metrics_service.cc. | 87 // SHA1EntropyProvider is used in metrics_service.cc. |
| 89 const int low_entropy_source = | 88 const int low_entropy_source = |
| 90 static_cast<uint16>(base::RandInt(0, kMaxLowEntropySize - 1)); | 89 static_cast<uint16>(base::RandInt(0, kMaxLowEntropySize - 1)); |
| 91 const std::string high_entropy_source = | 90 const std::string high_entropy_source = |
| 92 base::GenerateGUID() + base::IntToString(low_entropy_source); | 91 base::GenerateGUID() + base::IntToString(low_entropy_source); |
| 93 return GenerateSHA1Entropy(high_entropy_source, trial_name_); | 92 return GenerateSHA1Entropy(high_entropy_source, trial_name_); |
| 94 } | 93 } |
| 95 | 94 |
| 96 private: | 95 private: |
| 97 std::string trial_name_; | 96 std::string trial_name_; |
| 98 | 97 |
| 99 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyGenerator); | 98 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyGenerator); |
| 100 }; | 99 }; |
| 101 | 100 |
| 102 // An TrialEntropyGenerator that uses the permuted entropy provider algorithm, | 101 // An TrialEntropyGenerator that uses the permuted entropy provider algorithm, |
| 103 // using 13-bit low entropy source values. | 102 // using 13-bit low entropy source values. |
| 104 class PermutedEntropyGenerator : public TrialEntropyGenerator { | 103 class PermutedEntropyGenerator : public TrialEntropyGenerator { |
| 105 public: | 104 public: |
| 106 explicit PermutedEntropyGenerator(const std::string& trial_name) | 105 explicit PermutedEntropyGenerator(const std::string& trial_name) |
| 107 : mapping_(kMaxLowEntropySize) { | 106 : mapping_(kMaxLowEntropySize) { |
| 108 // Note: Given a trial name, the computed mapping will be the same. | 107 // Note: Given a trial name, the computed mapping will be the same. |
| 109 // As a performance optimization, pre-compute the mapping once per trial | 108 // As a performance optimization, pre-compute the mapping once per trial |
| 110 // name and index into it for each entropy value. | 109 // name and index into it for each entropy value. |
| 111 const uint32 randomization_seed = HashName(trial_name); | 110 const uint32 randomization_seed = HashName(trial_name); |
| 112 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, | 111 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, |
| 113 &mapping_); | 112 &mapping_); |
| 114 } | 113 } |
| 115 | 114 |
| 116 virtual ~PermutedEntropyGenerator() { | 115 ~PermutedEntropyGenerator() override {} |
| 117 } | |
| 118 | 116 |
| 119 virtual double GenerateEntropyValue() const override { | 117 double GenerateEntropyValue() const override { |
| 120 const int low_entropy_source = | 118 const int low_entropy_source = |
| 121 static_cast<uint16>(base::RandInt(0, kMaxLowEntropySize - 1)); | 119 static_cast<uint16>(base::RandInt(0, kMaxLowEntropySize - 1)); |
| 122 return mapping_[low_entropy_source] / | 120 return mapping_[low_entropy_source] / |
| 123 static_cast<double>(kMaxLowEntropySize); | 121 static_cast<double>(kMaxLowEntropySize); |
| 124 } | 122 } |
| 125 | 123 |
| 126 private: | 124 private: |
| 127 std::vector<uint16> mapping_; | 125 std::vector<uint16> mapping_; |
| 128 | 126 |
| 129 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyGenerator); | 127 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyGenerator); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 ++count; | 358 ++count; |
| 361 } | 359 } |
| 362 | 360 |
| 363 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << | 361 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 364 kExpectedAverage << ", average ended at " << cumulative_average << | 362 kExpectedAverage << ", average ended at " << cumulative_average << |
| 365 ", for trial " << kTestTrialNames[i]; | 363 ", for trial " << kTestTrialNames[i]; |
| 366 } | 364 } |
| 367 } | 365 } |
| 368 | 366 |
| 369 } // namespace metrics | 367 } // namespace metrics |
| OLD | NEW |