blob: fa7a1e25610ccaff49efa8a1bd517684bc6177cc [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>
12#include <string>
13#include <vector>
14
[email protected]20f999b52012-08-24 22:32:5915#include "base/compiler_specific.h"
avi5dd91f82015-12-25 22:30:4616#include "base/macros.h"
[email protected]20f999b52012-08-24 22:32:5917#include "base/metrics/field_trial.h"
18#include "third_party/mt19937ar/mt19937ar.h"
19
20namespace metrics {
21
22// Internals of entropy_provider.cc exposed for testing.
23namespace 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().
avi5dd91f82015-12-25 22:30:4627struct SeededRandGenerator : std::unary_function<uint32_t, uint32_t> {
28 explicit SeededRandGenerator(uint32_t seed);
[email protected]20f999b52012-08-24 22:32:5929 ~SeededRandGenerator();
30
31 // Returns a random number in range [0, range).
avi5dd91f82015-12-25 22:30:4632 uint32_t operator()(uint32_t range);
[email protected]20f999b52012-08-24 22:32:5933
34 MersenneTwister mersenne_twister_;
35};
36
[email protected]20f999b52012-08-24 22:32:5937// Fills |mapping| to create a bijection of values in the range of
[email protected]6fded222013-04-11 20:59:5038// [0, |mapping.size()|), permuted based on |randomization_seed|.
avi5dd91f82015-12-25 22:30:4639void PermuteMappingUsingRandomizationSeed(uint32_t randomization_seed,
40 std::vector<uint16_t>* mapping);
[email protected]20f999b52012-08-24 22:32:5941
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.
48class 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);
dcheng00ea022b2014-10-21 11:24:5654 ~SHA1EntropyProvider() override;
[email protected]20f999b52012-08-24 22:32:5955
56 // base::FieldTrial::EntropyProvider implementation:
dcheng00ea022b2014-10-21 11:24:5657 double GetEntropyForTrial(const std::string& trial_name,
avi5dd91f82015-12-25 22:30:4658 uint32_t randomization_seed) const override;
[email protected]20f999b52012-08-24 22:32:5959
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.
71class 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).
avi5dd91f82015-12-25 22:30:4675 PermutedEntropyProvider(uint16_t low_entropy_source,
[email protected]20f999b52012-08-24 22:32:5976 size_t low_entropy_source_max);
dcheng00ea022b2014-10-21 11:24:5677 ~PermutedEntropyProvider() override;
[email protected]20f999b52012-08-24 22:32:5978
79 // base::FieldTrial::EntropyProvider implementation:
dcheng00ea022b2014-10-21 11:24:5680 double GetEntropyForTrial(const std::string& trial_name,
avi5dd91f82015-12-25 22:30:4681 uint32_t randomization_seed) const override;
[email protected]20f999b52012-08-24 22:32:5982
[email protected]fb6670a2013-07-31 13:31:3583 protected:
84 // Performs the permutation algorithm and returns the permuted value that
85 // corresponds to |low_entropy_source_|.
avi5dd91f82015-12-25 22:30:4686 virtual uint16_t GetPermutedValue(uint32_t randomization_seed) const;
[email protected]fb6670a2013-07-31 13:31:3587
[email protected]20f999b52012-08-24 22:32:5988 private:
avi5dd91f82015-12-25 22:30:4689 uint16_t low_entropy_source_;
[email protected]20f999b52012-08-24 22:32:5990 size_t low_entropy_source_max_;
91
92 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyProvider);
93};
94
[email protected]20f999b52012-08-24 22:32:5995} // namespace metrics
96
[email protected]50ae9f12013-08-29 18:03:2297#endif // COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_