OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/variations/entropy_provider.h" | 5 #include "components/variations/entropy_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 return value % range; | 44 return value % range; |
45 } | 45 } |
46 | 46 |
47 void PermuteMappingUsingRandomizationSeed(uint32 randomization_seed, | 47 void PermuteMappingUsingRandomizationSeed(uint32 randomization_seed, |
48 std::vector<uint16>* mapping) { | 48 std::vector<uint16>* mapping) { |
49 for (size_t i = 0; i < mapping->size(); ++i) | 49 for (size_t i = 0; i < mapping->size(); ++i) |
50 (*mapping)[i] = static_cast<uint16>(i); | 50 (*mapping)[i] = static_cast<uint16>(i); |
51 | 51 |
52 SeededRandGenerator generator(randomization_seed); | 52 SeededRandGenerator generator(randomization_seed); |
53 std::random_shuffle(mapping->begin(), mapping->end(), generator); | 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 } |
54 } | 68 } |
55 | 69 |
56 } // namespace internal | 70 } // namespace internal |
57 | 71 |
58 SHA1EntropyProvider::SHA1EntropyProvider(const std::string& entropy_source) | 72 SHA1EntropyProvider::SHA1EntropyProvider(const std::string& entropy_source) |
59 : entropy_source_(entropy_source) { | 73 : entropy_source_(entropy_source) { |
60 } | 74 } |
61 | 75 |
62 SHA1EntropyProvider::~SHA1EntropyProvider() { | 76 SHA1EntropyProvider::~SHA1EntropyProvider() { |
63 } | 77 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 124 } |
111 | 125 |
112 uint16 PermutedEntropyProvider::GetPermutedValue( | 126 uint16 PermutedEntropyProvider::GetPermutedValue( |
113 uint32 randomization_seed) const { | 127 uint32 randomization_seed) const { |
114 std::vector<uint16> mapping(low_entropy_source_max_); | 128 std::vector<uint16> mapping(low_entropy_source_max_); |
115 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping); | 129 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping); |
116 return mapping[low_entropy_source_]; | 130 return mapping[low_entropy_source_]; |
117 } | 131 } |
118 | 132 |
119 } // namespace metrics | 133 } // namespace metrics |
OLD | NEW |