Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/common/metrics/entropy_provider.cc

Issue 10830318: Use a different algorithm with the low entropy source for field trials. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
5 #include "chrome/common/metrics/entropy_provider.h"
6
7 #include <algorithm>
8 #include <limits>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/rand_util.h"
13 #include "base/sha1.h"
14 #include "base/sys_byteorder.h"
15
16 namespace metrics_internal {
17
18 SeededRandGenerator::SeededRandGenerator(uint32 seed) {
19 mersenne_twister_.init_genrand(seed);
20 }
21
22 SeededRandGenerator::~SeededRandGenerator() {
23 }
24
25 uint32 SeededRandGenerator::operator()(uint32 range) {
26 // Based on base::RandGenerator().
27 DCHECK_GT(range, 0u);
28
29 // We must discard random results above this number, as they would
30 // make the random generator non-uniform (consider e.g. if
31 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
32 // as likely as a result of 3 or 4).
33 uint32 max_acceptable_value =
34 (std::numeric_limits<uint32>::max() / range) * range - 1;
35
36 uint32 value;
37 do {
38 value = mersenne_twister_.genrand_int32();
39 } while (value > max_acceptable_value);
40
41 return value % range;
42 }
43
44 uint32 HashName(const std::string& name) {
45 // SHA-1 is designed to produce a uniformly random spread in its output space,
46 // even for nearly-identical inputs.
47 unsigned char sha1_hash[base::kSHA1Length];
48 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()),
49 name.size(),
50 sha1_hash);
51
52 uint32 bits;
53 COMPILE_ASSERT(sizeof(bits) < sizeof(sha1_hash), need_more_data);
54 memcpy(&bits, sha1_hash, sizeof(bits));
55
56 return base::ByteSwapToLE32(bits);
57 }
58
59 void PermuteMappingUsingTrialName(const std::string& trial_name,
60 std::vector<uint16>* mapping) {
61 for (size_t i = 0; i < mapping->size(); ++i)
62 (*mapping)[i] = static_cast<uint16>(i);
63
64 SeededRandGenerator generator(HashName(trial_name));
65 std::random_shuffle(mapping->begin(), mapping->end(), generator);
66 }
67
68 } // namespace metrics_internal
69
70 SHA1EntropyProvider::SHA1EntropyProvider(const std::string& entropy_source)
71 : entropy_source_(entropy_source) {
72 }
73
74 SHA1EntropyProvider::~SHA1EntropyProvider() {
75 }
76
77 double SHA1EntropyProvider::GetEntropyForTrial(
78 const std::string& trial_name) const {
79 // Given enough input entropy, SHA-1 will produce a uniformly random spread
80 // in its output space. In this case, the input entropy that is used is the
81 // combination of the original |entropy_source_| and the |trial_name|.
82 //
83 // Note: If |entropy_source_| has very low entropy, such as 13 bits or less,
84 // it has been observed that this method does not result in a uniform
85 // distribution given the same |trial_name|. When using such a low entropy
86 // source, PermutedEntropyProvider should be used instead.
87 std::string input(entropy_source_ + trial_name);
88 unsigned char sha1_hash[base::kSHA1Length];
89 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
90 input.size(),
91 sha1_hash);
92
93 uint64 bits;
94 COMPILE_ASSERT(sizeof(bits) < sizeof(sha1_hash), need_more_data);
95 memcpy(&bits, sha1_hash, sizeof(bits));
96 bits = base::ByteSwapToLE64(bits);
97
98 return base::BitsToOpenEndedUnitInterval(bits);
99 }
100
101 PermutedEntropyProvider::PermutedEntropyProvider(
102 uint16 low_entropy_source,
103 size_t low_entropy_source_max)
104 : low_entropy_source_(low_entropy_source),
105 low_entropy_source_max_(low_entropy_source_max) {
106 DCHECK_LT(low_entropy_source, low_entropy_source_max);
107 DCHECK_LE(low_entropy_source_max, std::numeric_limits<uint16>::max());
108 }
109
110 PermutedEntropyProvider::~PermutedEntropyProvider() {
111 }
112
113 double PermutedEntropyProvider::GetEntropyForTrial(
114 const std::string& trial_name) const {
115 std::vector<uint16> mapping(low_entropy_source_max_);
116 metrics_internal::PermuteMappingUsingTrialName(trial_name, &mapping);
117
118 return mapping[low_entropy_source_] /
119 static_cast<double>(low_entropy_source_max_);
120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698