blob: 88d549416d59af3486e54c0720d7927962cf74da [file] [log] [blame]
[email protected]20f999b52012-08-24 22:32:591// 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]50ae9f12013-08-29 18:03:225#ifndef COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_
6#define COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_
[email protected]20f999b52012-08-24 22:32:597
avi5dd91f82015-12-25 22:30:468#include <stddef.h>
9#include <stdint.h>
10
[email protected]20f999b52012-08-24 22:32:5911#include <functional>
Alexei Svitkine72516642018-02-28 03:08:5212#include <random>
[email protected]20f999b52012-08-24 22:32:5913#include <string>
14#include <vector>
15
[email protected]20f999b52012-08-24 22:32:5916#include "base/compiler_specific.h"
avi5dd91f82015-12-25 22:30:4617#include "base/macros.h"
[email protected]20f999b52012-08-24 22:32:5918#include "base/metrics/field_trial.h"
[email protected]20f999b52012-08-24 22:32:5919
Alexei Svitkine9de32cb2018-02-06 20:21:2120namespace variations {
[email protected]20f999b52012-08-24 22:32:5921
jwdc6e07e22016-11-21 16:36:5422// 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]20f999b52012-08-24 22:32:5926class 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);
dcheng00ea022b2014-10-21 11:24:5632 ~SHA1EntropyProvider() override;
[email protected]20f999b52012-08-24 22:32:5933
34 // base::FieldTrial::EntropyProvider implementation:
dcheng00ea022b2014-10-21 11:24:5635 double GetEntropyForTrial(const std::string& trial_name,
avi5dd91f82015-12-25 22:30:4636 uint32_t randomization_seed) const override;
[email protected]20f999b52012-08-24 22:32:5937
38 private:
Paul Miller7c0efea2018-11-13 23:49:0039 const std::string entropy_source_;
[email protected]20f999b52012-08-24 22:32:5940
41 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider);
42};
43
Paul Miller7c0efea2018-11-13 23:49:0044// 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
50class 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 Svitkine9de32cb2018-02-06 20:21:2168} // namespace variations
[email protected]20f999b52012-08-24 22:32:5969
[email protected]50ae9f12013-08-29 18:03:2270#endif // COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_