1*2abb3134SXin Li // Copyright 2015 Google Inc. All rights reserved. 2*2abb3134SXin Li // 3*2abb3134SXin Li // Licensed under the Apache License, Version 2.0 (the "License"); 4*2abb3134SXin Li // you may not use this file except in compliance with the License. 5*2abb3134SXin Li // You may obtain a copy of the License at 6*2abb3134SXin Li // 7*2abb3134SXin Li // http://www.apache.org/licenses/LICENSE-2.0 8*2abb3134SXin Li // 9*2abb3134SXin Li // Unless required by applicable law or agreed to in writing, software 10*2abb3134SXin Li // distributed under the License is distributed on an "AS IS" BASIS, 11*2abb3134SXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*2abb3134SXin Li // See the License for the specific language governing permissions and 13*2abb3134SXin Li // limitations under the License. 14*2abb3134SXin Li 15*2abb3134SXin Li // RAPPOR encoder. 16*2abb3134SXin Li // 17*2abb3134SXin Li // See README.md and encoder_demo.cc for an example. 18*2abb3134SXin Li 19*2abb3134SXin Li #ifndef RAPPOR_H_ 20*2abb3134SXin Li #define RAPPOR_H_ 21*2abb3134SXin Li 22*2abb3134SXin Li #include <string> 23*2abb3134SXin Li 24*2abb3134SXin Li #include "rappor_deps.h" // for dependency injection 25*2abb3134SXin Li 26*2abb3134SXin Li namespace rappor { 27*2abb3134SXin Li 28*2abb3134SXin Li // For debug logging 29*2abb3134SXin Li void log(const char* fmt, ...); 30*2abb3134SXin Li 31*2abb3134SXin Li // RAPPOR encoding parameters. 32*2abb3134SXin Li class Params { 33*2abb3134SXin Li public: Params(int num_bits,int num_hashes,int num_cohorts,float prob_f,float prob_p,float prob_q)34*2abb3134SXin Li Params(int num_bits, int num_hashes, int num_cohorts, 35*2abb3134SXin Li float prob_f, float prob_p, float prob_q) 36*2abb3134SXin Li : num_bits_(num_bits), 37*2abb3134SXin Li num_hashes_(num_hashes), 38*2abb3134SXin Li num_cohorts_(num_cohorts), 39*2abb3134SXin Li prob_f_(prob_f), 40*2abb3134SXin Li prob_p_(prob_p), 41*2abb3134SXin Li prob_q_(prob_q) { 42*2abb3134SXin Li } 43*2abb3134SXin Li 44*2abb3134SXin Li // Accessors num_bits()45*2abb3134SXin Li int num_bits() { return num_bits_; } num_hashes()46*2abb3134SXin Li int num_hashes() { return num_hashes_; } num_cohorts()47*2abb3134SXin Li int num_cohorts() { return num_cohorts_; } prob_f()48*2abb3134SXin Li float prob_f() { return prob_f_; } prob_p()49*2abb3134SXin Li float prob_p() { return prob_p_; } prob_q()50*2abb3134SXin Li float prob_q() { return prob_q_; } 51*2abb3134SXin Li 52*2abb3134SXin Li private: 53*2abb3134SXin Li friend class Encoder; 54*2abb3134SXin Li 55*2abb3134SXin Li // k: size of bloom filter, PRR, and IRR. 0 < k <= 32. 56*2abb3134SXin Li int num_bits_; 57*2abb3134SXin Li 58*2abb3134SXin Li // number of bits set in the Bloom filter ("h") 59*2abb3134SXin Li int num_hashes_; 60*2abb3134SXin Li 61*2abb3134SXin Li // Total number of cohorts ("m"). Note that the cohort assignment is what 62*2abb3134SXin Li // is used in the client, not m. We include it here for documentation (it 63*2abb3134SXin Li // can be unset, unlike the other params.) 64*2abb3134SXin Li int num_cohorts_; 65*2abb3134SXin Li 66*2abb3134SXin Li float prob_f_; // noise probability for PRR, quantized to 1/128 67*2abb3134SXin Li 68*2abb3134SXin Li float prob_p_; // noise probability for IRR, quantized to 1/128 69*2abb3134SXin Li float prob_q_; // noise probability for IRR, quantized to 1/128 70*2abb3134SXin Li }; 71*2abb3134SXin Li 72*2abb3134SXin Li // Encoder: take client values and transform them with the RAPPOR privacy 73*2abb3134SXin Li // algorithm. 74*2abb3134SXin Li class Encoder { 75*2abb3134SXin Li public: 76*2abb3134SXin Li // Note that invalid parameters cause runtime assertions in the constructor. 77*2abb3134SXin Li // Encoders are intended to be created at application startup with constant 78*2abb3134SXin Li // arguments, so errors should be caught early. 79*2abb3134SXin Li 80*2abb3134SXin Li // encoder_id: A unique ID for this encoder -- typically the name of the 81*2abb3134SXin Li // metric being encoded, so that different metrics have different PRR 82*2abb3134SXin Li // mappings. 83*2abb3134SXin Li // params: RAPPOR encoding parameters, which affect privacy and decoding. 84*2abb3134SXin Li // (held by reference; it must outlive the Encoder) 85*2abb3134SXin Li // deps: application-supplied dependencies. 86*2abb3134SXin Li // (held by reference; it must outlive the Encoder) 87*2abb3134SXin Li Encoder(const std::string& encoder_id, const Params& params, 88*2abb3134SXin Li const Deps& deps); 89*2abb3134SXin Li 90*2abb3134SXin Li // Encode raw bits (represented as an integer), setting output parameter 91*2abb3134SXin Li // irr_out. Only valid when the return value is 'true' (success). 92*2abb3134SXin Li bool EncodeBits(const Bits bits, Bits* irr_out) const; 93*2abb3134SXin Li 94*2abb3134SXin Li // Encode a string, setting output parameter irr_out. Only valid when the 95*2abb3134SXin Li // return value is 'true' (success). 96*2abb3134SXin Li bool EncodeString(const std::string& value, Bits* irr_out) const; 97*2abb3134SXin Li // For use with HmacDrbg hash function and any num_bits divisible by 8. 98*2abb3134SXin Li bool EncodeString(const std::string& value, 99*2abb3134SXin Li std::vector<uint8_t>* irr_out) const; 100*2abb3134SXin Li 101*2abb3134SXin Li // For testing/simulation use only. 102*2abb3134SXin Li bool _EncodeBitsInternal(const Bits bits, Bits* prr_out, Bits* irr_out) 103*2abb3134SXin Li const; 104*2abb3134SXin Li bool _EncodeStringInternal(const std::string& value, Bits* bloom_out, 105*2abb3134SXin Li Bits* prr_out, Bits* irr_out) const; 106*2abb3134SXin Li 107*2abb3134SXin Li // Accessor for the assigned cohort. cohort()108*2abb3134SXin Li uint32_t cohort() { return cohort_; } 109*2abb3134SXin Li // Set a cohort manually, if previously generated. 110*2abb3134SXin Li void set_cohort(uint32_t cohort); 111*2abb3134SXin Li 112*2abb3134SXin Li private: 113*2abb3134SXin Li bool MakeBloomFilter(const std::string& value, Bits* bloom_out) const; 114*2abb3134SXin Li bool MakeBloomFilter(const std::string& value, 115*2abb3134SXin Li std::vector<uint8_t>* bloom_out) const; 116*2abb3134SXin Li bool GetPrrMasks(const Bits bits, Bits* uniform, Bits* f_mask) const; 117*2abb3134SXin Li 118*2abb3134SXin Li // static helper function for initialization 119*2abb3134SXin Li static uint32_t AssignCohort(const Deps& deps, int num_cohorts); 120*2abb3134SXin Li 121*2abb3134SXin Li const std::string encoder_id_; 122*2abb3134SXin Li const Params& params_; 123*2abb3134SXin Li const Deps& deps_; 124*2abb3134SXin Li uint32_t cohort_; 125*2abb3134SXin Li std::string cohort_str_; 126*2abb3134SXin Li }; 127*2abb3134SXin Li 128*2abb3134SXin Li } // namespace rappor 129*2abb3134SXin Li 130*2abb3134SXin Li #endif // RAPPOR_H_ 131