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 #include "components/rappor/rappor_metric.h" | 5 #include "components/rappor/rappor_metric.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "components/rappor/reports.h" |
9 | 10 |
10 namespace rappor { | 11 namespace rappor { |
11 | 12 |
12 RapporMetric::RapporMetric(const std::string& metric_name, | 13 RapporMetric::RapporMetric(const std::string& metric_name, |
13 const RapporParameters& parameters, | 14 const RapporParameters& parameters, |
14 int32_t cohort_seed) | 15 int32_t cohort_seed) |
15 : metric_name_(metric_name), | 16 : metric_name_(metric_name), |
16 parameters_(parameters), | 17 parameters_(parameters), |
17 sample_count_(0), | 18 sample_count_(0), |
18 bloom_filter_(parameters.bloom_filter_size_bytes, | 19 bloom_filter_(parameters.bloom_filter_size_bytes, |
(...skipping 12 matching lines...) Expand all Loading... |
31 void RapporMetric::AddSample(const std::string& str) { | 32 void RapporMetric::AddSample(const std::string& str) { |
32 ++sample_count_; | 33 ++sample_count_; |
33 // Replace the previous sample with a 1 in sample_count_ chance so that each | 34 // Replace the previous sample with a 1 in sample_count_ chance so that each |
34 // sample has equal probability of being reported. | 35 // sample has equal probability of being reported. |
35 if (base::RandGenerator(sample_count_) == 0) { | 36 if (base::RandGenerator(sample_count_) == 0) { |
36 bloom_filter_.SetString(str); | 37 bloom_filter_.SetString(str); |
37 } | 38 } |
38 } | 39 } |
39 | 40 |
40 ByteVector RapporMetric::GetReport(const std::string& secret) const { | 41 ByteVector RapporMetric::GetReport(const std::string& secret) const { |
41 // Generate a deterministically random mask of fake data using the | 42 return internal::GenerateReport(secret, parameters(), bytes()); |
42 // client's secret key + real data as a seed. The inclusion of the secret | |
43 // in the seed avoids correlations between real and fake data. | |
44 // The seed isn't a human-readable string. | |
45 const std::string personalization_string = metric_name_ + | |
46 std::string(bytes().begin(), bytes().end()); | |
47 HmacByteVectorGenerator hmac_generator(bytes().size(), secret, | |
48 personalization_string); | |
49 const ByteVector fake_mask = | |
50 hmac_generator.GetWeightedRandomByteVector(parameters().fake_prob); | |
51 ByteVector fake_bits = | |
52 hmac_generator.GetWeightedRandomByteVector(parameters().fake_one_prob); | |
53 | |
54 // Redact most of the real data by replacing it with the fake data, hiding | |
55 // and limiting the amount of information an individual client reports on. | |
56 const ByteVector* fake_and_redacted_bits = | |
57 ByteVectorMerge(fake_mask, bytes(), &fake_bits); | |
58 | |
59 // Generate biased coin flips for each bit. | |
60 ByteVectorGenerator coin_generator(bytes().size()); | |
61 const ByteVector zero_coins = | |
62 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); | |
63 ByteVector one_coins = | |
64 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); | |
65 | |
66 // Create a randomized response report on the fake and redacted data, sending | |
67 // the outcome of flipping a zero coin for the zero bits in that data, and of | |
68 // flipping a one coin for the one bits in that data, as the final report. | |
69 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); | |
70 } | 43 } |
71 | 44 |
72 void RapporMetric::SetBytesForTesting(const ByteVector& bytes) { | 45 void RapporMetric::SetBytesForTesting(const ByteVector& bytes) { |
73 bloom_filter_.SetBytesForTesting(bytes); | 46 bloom_filter_.SetBytesForTesting(bytes); |
74 } | 47 } |
75 | 48 |
76 } // namespace rappor | 49 } // namespace rappor |
OLD | NEW |