[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 | #include "components/variations/entropy_provider.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 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" |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 15 | #include "components/variations/metrics_util.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 16 | |
| 17 | namespace metrics { |
| 18 | |
| 19 | namespace internal { |
| 20 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 21 | SeededRandGenerator::SeededRandGenerator(uint32_t seed) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 22 | mersenne_twister_.init_genrand(seed); |
| 23 | } |
| 24 | |
| 25 | SeededRandGenerator::~SeededRandGenerator() { |
| 26 | } |
| 27 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 28 | uint32_t SeededRandGenerator::operator()(uint32_t range) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 29 | // Based on base::RandGenerator(). |
| 30 | DCHECK_GT(range, 0u); |
| 31 | |
| 32 | // We must discard random results above this number, as they would |
| 33 | // make the random generator non-uniform (consider e.g. if |
| 34 | // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice |
| 35 | // as likely as a result of 3 or 4). |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 36 | uint32_t max_acceptable_value = |
| 37 | (std::numeric_limits<uint32_t>::max() / range) * range - 1; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 38 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 39 | uint32_t value; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 40 | do { |
| 41 | value = mersenne_twister_.genrand_int32(); |
| 42 | } while (value > max_acceptable_value); |
| 43 | |
| 44 | return value % range; |
| 45 | } |
| 46 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 47 | void PermuteMappingUsingRandomizationSeed(uint32_t randomization_seed, |
| 48 | std::vector<uint16_t>* mapping) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 49 | for (size_t i = 0; i < mapping->size(); ++i) |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 50 | (*mapping)[i] = static_cast<uint16_t>(i); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 51 | |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 52 | SeededRandGenerator generator(randomization_seed); |
[email protected] | 9e49614d | 2014-04-03 14:41:36 | [diff] [blame] | 53 | |
| 54 | // Do a deterministic random shuffle of the mapping using |generator|. |
| 55 | // |
| 56 | // Note: This logic is identical to the following call with libstdc++ and VS: |
| 57 | // |
| 58 | // std::random_shuffle(mapping->begin(), mapping->end(), generator); |
| 59 | // |
| 60 | // However, this is not guaranteed by the spec and some implementations (e.g. |
| 61 | // libc++) use a different algorithm. To ensure results are consistent |
| 62 | // regardless of the compiler toolchain used, use our own version. |
| 63 | for (size_t i = 1; i < mapping->size(); ++i) { |
| 64 | // Pick an element in mapping[:i+1] with which to exchange mapping[i]. |
| 65 | size_t j = generator(i + 1); |
| 66 | std::swap((*mapping)[i], (*mapping)[j]); |
| 67 | } |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } // namespace internal |
| 71 | |
| 72 | SHA1EntropyProvider::SHA1EntropyProvider(const std::string& entropy_source) |
| 73 | : entropy_source_(entropy_source) { |
| 74 | } |
| 75 | |
| 76 | SHA1EntropyProvider::~SHA1EntropyProvider() { |
| 77 | } |
| 78 | |
| 79 | double SHA1EntropyProvider::GetEntropyForTrial( |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 80 | const std::string& trial_name, |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 81 | uint32_t randomization_seed) const { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 82 | // Given enough input entropy, SHA-1 will produce a uniformly random spread |
| 83 | // in its output space. In this case, the input entropy that is used is the |
| 84 | // combination of the original |entropy_source_| and the |trial_name|. |
| 85 | // |
| 86 | // Note: If |entropy_source_| has very low entropy, such as 13 bits or less, |
| 87 | // it has been observed that this method does not result in a uniform |
| 88 | // distribution given the same |trial_name|. When using such a low entropy |
| 89 | // source, PermutedEntropyProvider should be used instead. |
| 90 | std::string input(entropy_source_ + trial_name); |
| 91 | unsigned char sha1_hash[base::kSHA1Length]; |
| 92 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 93 | input.size(), |
| 94 | sha1_hash); |
| 95 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 96 | uint64_t bits; |
mostynb | 470748ce | 2014-12-22 21:14:46 | [diff] [blame] | 97 | static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required"); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 98 | memcpy(&bits, sha1_hash, sizeof(bits)); |
| 99 | bits = base::ByteSwapToLE64(bits); |
| 100 | |
| 101 | return base::BitsToOpenEndedUnitInterval(bits); |
| 102 | } |
| 103 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 104 | PermutedEntropyProvider::PermutedEntropyProvider(uint16_t low_entropy_source, |
| 105 | size_t low_entropy_source_max) |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 106 | : low_entropy_source_(low_entropy_source), |
| 107 | low_entropy_source_max_(low_entropy_source_max) { |
| 108 | DCHECK_LT(low_entropy_source, low_entropy_source_max); |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 109 | DCHECK_LE(low_entropy_source_max, std::numeric_limits<uint16_t>::max()); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | PermutedEntropyProvider::~PermutedEntropyProvider() { |
| 113 | } |
| 114 | |
| 115 | double PermutedEntropyProvider::GetEntropyForTrial( |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 116 | const std::string& trial_name, |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 117 | uint32_t randomization_seed) const { |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 118 | if (randomization_seed == 0) |
| 119 | randomization_seed = HashName(trial_name); |
[email protected] | 9d7c4a8 | 2013-05-07 12:10:49 | [diff] [blame] | 120 | |
[email protected] | fb6670a | 2013-07-31 13:31:35 | [diff] [blame] | 121 | return GetPermutedValue(randomization_seed) / |
| 122 | static_cast<double>(low_entropy_source_max_); |
| 123 | } |
| 124 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame^] | 125 | uint16_t PermutedEntropyProvider::GetPermutedValue( |
| 126 | uint32_t randomization_seed) const { |
| 127 | std::vector<uint16_t> mapping(low_entropy_source_max_); |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 128 | internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping); |
[email protected] | fb6670a | 2013-07-31 13:31:35 | [diff] [blame] | 129 | return mapping[low_entropy_source_]; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 132 | } // namespace metrics |