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

Unified Diff: components/rappor/rappor_reporter.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years 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_reporter.cc
diff --git a/components/rappor/rappor_reporter.cc b/components/rappor/rappor_reporter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..02d85297d38dc57d8e8dd7bb618d37dd32552929
--- /dev/null
+++ b/components/rappor/rappor_reporter.cc
@@ -0,0 +1,144 @@
+// Copyright (c) 2013 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 <assert.h>
+#include <limits.h>
+#include <math.h>
+
+#include <bitset>
+#include <map>
+#include <string>
+#include <vector>
+
+#include "base/logging.h"
+#include "base/strings/string_number_conversions.h"
+#include "components/rappor/bytevector.h"
+#include "components/rappor/rappor.h"
+#include "components/rappor/rappor_reporter.h"
+#include "crypto/hmac.h"
+#include "crypto/random.h"
+
+#define HMAC_SEED 10
+
+namespace rappor {
+
+namespace {
+
+// A utility object for generating random binary data with different
+// likelihood of bits being true.
+class ByteVectorGenerator {
+ public:
+ explicit ByteVectorGenerator(size_t byte_count);
+ ByteVector GetRandomByteVector();
+ ByteVector GetWeightedRandomByteVector(int probIndex);
+
+ protected:
+ virtual uint8_t RandByte();
+
+ private:
+ size_t byte_count_;
+};
+
+ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
+ : byte_count_(byte_count) {}
+
+uint8_t ByteVectorGenerator::RandByte() {
+ uint8_t randomBits;
+ crypto::RandBytes(&randomBits, sizeof(uint8_t));
+ return randomBits;
+}
+
+ByteVector ByteVectorGenerator::GetRandomByteVector() {
jwd 2013/12/17 23:00:25 Mention that it's expected 50% 1s or something lik
Steven Holte 2013/12/18 02:06:12 Added comments about RandByte and RandBytes being
+ ByteVector bytes(byte_count_);
+ for (size_t i = 0; i < byte_count_; i++) {
+ bytes[i] = RandByte();
+ }
+ return bytes;
+}
+
+ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(int probIndex) {
+ switch (probIndex) {
+ // 87.5% ones
+ case -8:
jwd 2013/12/17 23:00:25 What determines these constants? As in, why -8,...
Steven Holte 2013/12/18 02:06:12 Changed these to Probability enum values.
+ return GetRandomByteVector().Or(GetRandomByteVector()).Or(
+ GetRandomByteVector());
+ // 75% ones
+ case -4:
+ return GetRandomByteVector().Or(GetRandomByteVector());
+ // 50% ones
+ case 2:
+ return GetRandomByteVector();
+ // 25% ones
+ case 4:
+ return GetRandomByteVector().And(GetRandomByteVector());
+ // 12.5% ones
+ case 8:
+ return GetRandomByteVector().And(GetRandomByteVector()).And(
+ GetRandomByteVector());
+ default:
+ // Invalid probability index "probIndex" for coin flips
+ abort();
+ }
+}
+
+// A ByteVectorGenerator that uses a psuedo-random function to generate
+// deterministicly random bits.
+class HmacByteVectorGenerator : public ByteVectorGenerator {
+ public:
+ HmacByteVectorGenerator(size_t byte_count, const std::string& secret);
+
+ protected:
+ virtual uint8_t RandByte();
+
+ private:
+ crypto::HMAC hmac_;
+ uint64_t hmac_state_;
+};
+
+HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count,
+ const std::string& secret)
+ : ByteVectorGenerator(byte_count), hmac_(crypto::HMAC::SHA256) {
+ if (!hmac_.Init(secret)) {
+ abort();
+ };
+ assert(hmac_.DigestLength() > sizeof(uint64_t));
+ hmac_state_ = uint64_t(HMAC_SEED);
+}
+
+uint8_t HmacByteVectorGenerator::RandByte() {
+ uint8_t randomBits;
+ std::string state = base::Uint64ToString(hmac_state_);
+ if (!hmac_.Sign(state, &randomBits, sizeof(uint8_t))) {
+ abort();
+ }
+ ++hmac_state_;
+ return randomBits;
+}
+
+} // namespace
+
+RapporReporter::RapporReporter(const std::string& secret) : secret_(secret) {}
+
+std::vector<uint8_t> RapporReporter::GetReport(const Rappor& rappor) {
+ const ByteVector realbits(rappor.GetBytes());
+ size_t size = realbits.size();
+ HmacByteVectorGenerator hmacGenerator(size, secret_ + rappor.rappor_name());
+ const ByteVector fakebits =
+ hmacGenerator.GetWeightedRandomByteVector(rappor.fake_prob_index());
+ const ByteVector fakeones =
+ hmacGenerator.GetWeightedRandomByteVector(rappor.fake_one_prob_index());
+ const ByteVector onebits = (realbits.And(fakebits.Not())).Or(
+ fakeones.And(fakebits));
+
+ ByteVectorGenerator coinGenerator(size);
+ const ByteVector zero_coins = coinGenerator.GetWeightedRandomByteVector(
+ rappor.zero_honesty_prob_index());
+ const ByteVector one_coins = coinGenerator.GetWeightedRandomByteVector(
+ rappor.one_honesty_prob_index());
+ return (zero_coins.And(onebits.Not())).Or(one_coins.And(onebits));
+
+ return std::vector<uint8_t>();
+}
+
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698