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