Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Unified Diff: components/rappor/rappor_metric.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cohorts Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/rappor/rappor_metric.cc
diff --git a/components/rappor/rappor_metric.cc b/components/rappor/rappor_metric.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3774c54987d4f7def2a6727acdf6e8a2afeaa18f
--- /dev/null
+++ b/components/rappor/rappor_metric.cc
@@ -0,0 +1,61 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/rappor/rappor_metric.h"
+
+#include "base/logging.h"
+
+namespace rappor {
+
+RapporMetric::RapporMetric(const RapporParameters& parameters,
+ int32_t cohort)
+ : parameters_(parameters),
+ bloom_(parameters.bloom_filter_size_bytes,
+ parameters.bloom_filter_hash_function_count,
+ cohort * parameters.bloom_filter_hash_function_count) {
+ DCHECK_GE(cohort, 0);
Alexei Svitkine (slow) 2014/01/24 22:09:03 Bad indentation.
+}
+
+RapporMetric::~RapporMetric() {}
+
+void RapporMetric::AddSamples(const std::vector<std::string>& strings) {
+ bloom_.AddStrings(strings);
+}
+
+void RapporMetric::AddSample(const std::string& str) { bloom_.AddString(str); }
+
+ByteVector RapporMetric::GetReport(const std::string& secret) const {
+ // Start with the real bloom filter data.
+ const ByteVector real_bits(bytes());
+
+ // Generate a deterministically random mask of fake data based on the
+ // client's secret key.
+ HmacByteVectorGenerator hmac_generator(real_bits.size(),
+ secret + parameters()->rappor_name);
+ const ByteVector fake_mask =
+ hmac_generator.GetWeightedRandomByteVector(parameters()->fake_prob);
+ ByteVector fake_ones =
+ hmac_generator.GetWeightedRandomByteVector(parameters()->fake_one_prob);
+
+ // Redact most of the real data by replacing it with the fake data, hiding
+ // and limiting the amount of information an individual client reports on.
+ const ByteVector* redacted_bits =
+ ByteVectorMerge(fake_mask, real_bits, &fake_ones);
+
+ // Generate biased coin flips for each bit.
+ ByteVectorGenerator coin_generator(real_bits.size());
+ const ByteVector zero_coins =
+ coin_generator.GetWeightedRandomByteVector(parameters()->zero_coin_prob);
+ ByteVector one_coins =
+ coin_generator.GetWeightedRandomByteVector(parameters()->one_coin_prob);
+
+ // Use the redacted data to select which coin type is used for each bit in
+ // the final report.
+ const ByteVector* output =
+ ByteVectorMerge(*redacted_bits, zero_coins, &one_coins);
+
+ return *output;
+}
+
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698