OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Change for readability |
| 6 |
5 #include "components/rappor/rappor_metric.h" | 7 #include "components/rappor/rappor_metric.h" |
6 | 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 | 10 |
9 namespace rappor { | 11 namespace rappor { |
10 | 12 |
11 RapporMetric::RapporMetric(const std::string& metric_name, | 13 RapporMetric::RapporMetric(const std::string& metric_name, |
12 const RapporParameters& parameters, | 14 const RapporParameters& parameters, |
13 int32_t cohort) | 15 int32_t cohort) |
14 : metric_name_(metric_name), | 16 : metric_name_(metric_name), |
15 parameters_(parameters), | 17 parameters_(parameters), |
16 bloom_filter_(parameters.bloom_filter_size_bytes, | 18 bloom_filter_(parameters.bloom_filter_size_bytes, |
17 parameters.bloom_filter_hash_function_count, | 19 parameters.bloom_filter_hash_function_count, |
18 cohort * parameters.bloom_filter_hash_function_count) { | 20 cohort * parameters.bloom_filter_hash_function_count) { |
19 DCHECK_GE(cohort, 0); | 21 DCHECK_GE(cohort, 0); |
20 } | 22 } |
21 | 23 |
22 RapporMetric::~RapporMetric() {} | 24 RapporMetric::~RapporMetric() {} |
23 | 25 |
24 void RapporMetric::AddSample(const std::string& str) { | 26 void RapporMetric::AddSample(const std::string& str) { |
25 bloom_filter_.AddString(str); | 27 bloom_filter_.AddString(str); |
26 } | 28 } |
27 | 29 |
28 ByteVector RapporMetric::GetReport(const std::string& secret) const { | 30 ByteVector RapporMetric::GetReport(const std::string& secret) const { |
29 // Generate a deterministically random mask of fake data using the | 31 // Generate a deterministically random mask of fake data using the |
30 // client's secret key + real data as a seed. The inclusion of the secret | 32 // client's secret key + real data as a seed. The inclusion of the secret |
31 // in the seed avoids correlations between real and fake data. | 33 // in the seed avoids correlations between real and fake data. |
32 // The seed isn't a human-readable string. | 34 // The seed isn't a human-readable string. |
33 std::string personalization_string = metric_name_ + | 35 const std::string personalization_string = metric_name_ + |
34 std::string(bytes().begin(), bytes().end()); | 36 std::string(bytes().begin(), bytes().end()); |
35 HmacByteVectorGenerator hmac_generator(bytes().size(), secret, | 37 HmacByteVectorGenerator hmac_generator(bytes().size(), secret, |
36 personalization_string); | 38 personalization_string); |
37 const ByteVector fake_mask = | 39 const ByteVector fake_mask = |
38 hmac_generator.GetWeightedRandomByteVector(parameters().fake_prob); | 40 hmac_generator.GetWeightedRandomByteVector(parameters().fake_prob); |
39 ByteVector fake_bits = | 41 ByteVector fake_bits = |
40 hmac_generator.GetWeightedRandomByteVector(parameters().fake_one_prob); | 42 hmac_generator.GetWeightedRandomByteVector(parameters().fake_one_prob); |
41 | 43 |
42 // Redact most of the real data by replacing it with the fake data, hiding | 44 // Redact most of the real data by replacing it with the fake data, hiding |
43 // and limiting the amount of information an individual client reports on. | 45 // and limiting the amount of information an individual client reports on. |
44 const ByteVector* fake_and_redacted_bits = | 46 const ByteVector* fake_and_redacted_bits = |
45 ByteVectorMerge(fake_mask, bytes(), &fake_bits); | 47 ByteVectorMerge(fake_mask, bytes(), &fake_bits); |
46 | 48 |
47 // Generate biased coin flips for each bit. | 49 // Generate biased coin flips for each bit. |
48 ByteVectorGenerator coin_generator(bytes().size()); | 50 ByteVectorGenerator coin_generator(bytes().size()); |
49 const ByteVector zero_coins = | 51 const ByteVector zero_coins = |
50 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); | 52 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); |
51 ByteVector one_coins = | 53 ByteVector one_coins = |
52 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); | 54 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); |
53 | 55 |
54 // Create a randomized response report on the fake and redacted data, sending | 56 // Create a randomized response report on the fake and redacted data, sending |
55 // the outcome of flipping a zero coin for the zero bits in that data, and of | 57 // the outcome of flipping a zero coin for the zero bits in that data, and of |
56 // flipping a one coin for the one bits in that data, as the final report. | 58 // flipping a one coin for the one bits in that data, as the final report. |
57 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); | 59 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); |
58 } | 60 } |
59 | 61 |
60 } // namespace rappor | 62 } // namespace rappor |
OLD | NEW |