[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> |
Alexei Svitkine | 7251664 | 2018-02-28 03:08:52 | [diff] [blame] | 12 | #include <random> |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 13 | #include <string> |
14 | #include <vector> | ||||
15 | |||||
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 16 | #include "base/compiler_specific.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 17 | #include "base/macros.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 18 | #include "base/metrics/field_trial.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 19 | |
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 20 | namespace variations { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 21 | |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 22 | // SHA1EntropyProvider is an entropy provider suitable for high entropy sources. |
23 | // It works by taking the first 64 bits of the SHA1 hash of the entropy source | ||||
24 | // concatenated with the trial name, or randomization seed and using that for | ||||
25 | // the final entropy value. | ||||
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 26 | class SHA1EntropyProvider : public base::FieldTrial::EntropyProvider { |
27 | public: | ||||
28 | // Creates a SHA1EntropyProvider with the given |entropy_source|, which | ||||
29 | // should contain a large amount of entropy - for example, a textual | ||||
30 | // representation of a persistent randomly-generated 128-bit value. | ||||
31 | explicit SHA1EntropyProvider(const std::string& entropy_source); | ||||
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 32 | ~SHA1EntropyProvider() override; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 33 | |
34 | // base::FieldTrial::EntropyProvider implementation: | ||||
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 35 | double GetEntropyForTrial(const std::string& trial_name, |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 36 | uint32_t randomization_seed) const override; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 37 | |
38 | private: | ||||
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 39 | const std::string entropy_source_; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 40 | |
41 | DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider); | ||||
42 | }; | ||||
43 | |||||
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 44 | // NormalizedMurmurHashEntropyProvider is an entropy provider suitable for low |
45 | // entropy sources (below 16 bits). It uses MurmurHash3_32 to hash the study | ||||
46 | // name along with all possible low entropy sources. It finds the index where | ||||
47 | // the actual low entropy source's hash would fall in the sorted list of all | ||||
48 | // those hashes, and uses that as the final value. For more info, see: | ||||
49 | // https://docs.google.com/document/d/1cPF5PruriWNP2Z5gSkq4MBTm0wSZqLyIJkUO9ekibeo | ||||
50 | class NormalizedMurmurHashEntropyProvider | ||||
51 | : public base::FieldTrial::EntropyProvider { | ||||
52 | public: | ||||
53 | NormalizedMurmurHashEntropyProvider(uint16_t low_entropy_source, | ||||
54 | size_t low_entropy_source_max); | ||||
55 | ~NormalizedMurmurHashEntropyProvider() override; | ||||
56 | |||||
57 | // base::FieldTrial::EntropyProvider: | ||||
58 | double GetEntropyForTrial(const std::string& trial_name, | ||||
59 | uint32_t randomization_seed) const override; | ||||
60 | |||||
61 | private: | ||||
62 | const uint16_t low_entropy_source_; | ||||
63 | const size_t low_entropy_source_max_; | ||||
64 | |||||
65 | DISALLOW_COPY_AND_ASSIGN(NormalizedMurmurHashEntropyProvider); | ||||
66 | }; | ||||
67 | |||||
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 68 | } // namespace variations |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 69 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 70 | #endif // COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_ |