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/bloom_filter.h" |
9 | 10 |
10 namespace rappor { | 11 namespace rappor { |
11 | 12 |
12 RapporMetric::RapporMetric(const std::string& metric_name, | 13 namespace internal { |
13 const RapporParameters& parameters, | 14 |
14 int32_t cohort_seed) | 15 void SetSampleBits(const RapporParameters& parameters, int32_t cohort_seed, |
15 : metric_name_(metric_name), | 16 const std::string& str, uint64_t flags, Sample* output) { |
16 parameters_(parameters), | |
17 sample_count_(0), | |
18 bloom_filter_(parameters.bloom_filter_size_bytes, | |
19 parameters.bloom_filter_hash_function_count, | |
20 (cohort_seed % parameters.num_cohorts) * | |
21 parameters.bloom_filter_hash_function_count) { | |
22 DCHECK_GE(cohort_seed, 0); | 17 DCHECK_GE(cohort_seed, 0); |
23 DCHECK_LT(cohort_seed, RapporParameters::kMaxCohorts); | 18 DCHECK_LT(cohort_seed, RapporParameters::kMaxCohorts); |
24 // Since cohort_seed is in the range [0, kMaxCohorts), num_cohorts should | 19 // Since cohort_seed is in the range [0, kMaxCohorts), num_cohorts should |
25 // divide kMaxCohorts for each cohort to have equal weight. | 20 // divide kMaxCohorts for each cohort to have equal weight. |
26 DCHECK_EQ(0, RapporParameters::kMaxCohorts % parameters.num_cohorts); | 21 DCHECK_EQ(0, RapporParameters::kMaxCohorts % parameters.num_cohorts); |
| 22 output->resize(parameters.bloom_filter_size_bytes + parameters.flag_bytes); |
| 23 internal::SetBloomBits(parameters.bloom_filter_size_bytes, |
| 24 parameters.bloom_filter_hash_function_count, |
| 25 (cohort_seed % parameters.num_cohorts) * |
| 26 parameters.bloom_filter_hash_function_count, |
| 27 str, |
| 28 output); |
| 29 DCHECK_LE(parameters.flag_bytes, 8); |
| 30 if (parameters.flag_bytes != 8) |
| 31 DCHECK_EQ(0ul, flags >> (parameters.flag_bytes * 8)); |
| 32 for(int i = 0; i < parameters.flag_bytes; i++) { |
| 33 uint8_t flag_byte = (flags >> (i * 8)) % (1 << 8); |
| 34 (*output)[parameters.bloom_filter_size_bytes + i] = flag_byte; |
| 35 } |
| 36 } |
| 37 |
| 38 RapporMetric::RapporMetric(const std::string& metric_name, |
| 39 const RapporParameters& parameters) |
| 40 : metric_name_(metric_name), |
| 41 parameters_(parameters), |
| 42 sample_count_(0), |
| 43 sample_(parameters.bloom_filter_size_bytes + parameters.flag_bytes) { |
27 } | 44 } |
28 | 45 |
29 RapporMetric::~RapporMetric() {} | 46 RapporMetric::~RapporMetric() {} |
30 | 47 |
31 void RapporMetric::AddSample(const std::string& str) { | 48 void RapporMetric::AddSample(const Sample& sample) { |
32 ++sample_count_; | 49 ++sample_count_; |
33 // Replace the previous sample with a 1 in sample_count_ chance so that each | 50 // Replace the previous sample with a 1 in sample_count_ chance so that each |
34 // sample has equal probability of being reported. | 51 // sample has equal probability of being reported. |
35 if (base::RandGenerator(sample_count_) == 0) { | 52 if (base::RandGenerator(sample_count_) == 0) { |
36 bloom_filter_.SetString(str); | 53 sample_ = sample; |
37 } | 54 } |
38 } | 55 } |
39 | 56 |
40 ByteVector RapporMetric::GetReport(const std::string& secret) const { | 57 ByteVector RapporMetric::GetReport(const std::string& secret) const { |
41 // Generate a deterministically random mask of fake data using the | 58 // Generate a deterministically random mask of fake data using the |
42 // client's secret key + real data as a seed. The inclusion of the secret | 59 // 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. | 60 // in the seed avoids correlations between real and fake data. |
44 // The seed isn't a human-readable string. | 61 // The seed isn't a human-readable string. |
45 const std::string personalization_string = metric_name_ + | 62 const std::string personalization_string = metric_name_ + |
46 std::string(bytes().begin(), bytes().end()); | 63 std::string(bytes().begin(), bytes().end()); |
(...skipping 15 matching lines...) Expand all Loading... |
62 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); | 79 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); |
63 ByteVector one_coins = | 80 ByteVector one_coins = |
64 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); | 81 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); |
65 | 82 |
66 // Create a randomized response report on the fake and redacted data, sending | 83 // 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 | 84 // 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. | 85 // 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); | 86 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); |
70 } | 87 } |
71 | 88 |
72 void RapporMetric::SetBytesForTesting(const ByteVector& bytes) { | 89 void RapporMetric::SetSampleForTesting(const Sample& sample) { |
73 bloom_filter_.SetBytesForTesting(bytes); | 90 sample_ = sample; |
74 } | 91 } |
75 | 92 |
| 93 } // namespace internal |
| 94 |
76 } // namespace rappor | 95 } // namespace rappor |
OLD | NEW |