SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
private_recommendations/client.cpp
Go to the documentation of this file.
1 /*
2 SeComLib
3 Copyright 2012-2013 TU Delft, Information Security & Privacy Lab (http://isplab.tudelft.nl/)
4 
5 Contributors:
6 Inald Lagendijk (R.L.Lagendijk@TUDelft.nl)
7 Mihai Todor (todormihai@gmail.com)
8 Thijs Veugen (P.J.M.Veugen@tudelft.nl)
9 Zekeriya Erkin (z.erkin@tudelft.nl)
10 
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14 
15 http://www.apache.org/licenses/LICENSE-2.0
16 
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
30 #include "client.h"
31 
32 namespace SeComLib {
33 namespace PrivateRecommendations {
37  const std::string Client::configurationPath("PrivateRecommendations");
38 
46  Client::Client (const std::shared_ptr<ServiceProvider> &serviceProvider, const std::shared_ptr<PrivacyServiceProvider> &privacyServiceProvider, const PaillierPublicKey &publicKey) :
47  serviceProvider(serviceProvider),
48  privacyServiceProvider(privacyServiceProvider),
49  paillierCryptoProvider(publicKey),
50  blindingFactorCache(paillierCryptoProvider, BlindingFactorCacheParameters(configurationPath + ".BlindingFactorCache", Utils::Config::GetInstance().GetParameter<size_t>(configurationPath + ".l"))),
51  userCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath + ".userCount")) {
52  }
53 
58  #ifdef FIRST_USER_ONLY
59  for (size_t user = 0; user < 1; ++user) {
60  #else
61  for (size_t user = 0; user < this->userCount; ++user) {
62  #endif
63  //measure the time it takes to decrypt the recommendations for each user
64  Utils::CpuTimer recommendationsProcessingTimer;
65 
66  /*
67  std::cout << "User " << user << ":" << std::endl << std::endl;
68  */
69 
71  Paillier::Ciphertext encryptedL = this->serviceProvider->GetEncryptedL(user);
72  ServiceProvider::EncryptedUserData encryptedURSum = this->serviceProvider->GetEncryptedURSum(user);
73 
74  const BlindingFactorContainer &LblindingFactor = this->blindingFactorCache.Pop();
75 
77  unsigned long L = (this->privacyServiceProvider->SecureDecryption(encryptedL + LblindingFactor.encryptedR) - LblindingFactor.r).ToUnsignedLong();
78 
79  /*
80  std::cout << "L:" << L << std::endl;
81  */
82  if (L == 0) {
83  std::cout << "No similar users found." << std::endl;
84  continue;
85  }
86 
87  std::vector<unsigned long> URSum;
88  for (ServiceProvider::EncryptedUserData::iterator encryptedURSumIterator = encryptedURSum.begin(); encryptedURSumIterator != encryptedURSum.end(); ++encryptedURSumIterator) {
89  const BlindingFactorContainer &URSumblindingFactor = this->blindingFactorCache.Pop();
90 
92  URSum.push_back((this->privacyServiceProvider->SecureDecryption(*encryptedURSumIterator + URSumblindingFactor.encryptedR) - URSumblindingFactor.r).ToUnsignedLong());
93  }
94 
95  /*
96  std::cout << "UR_sum:" << std::endl;
97  for (std::vector<unsigned long>::const_iterator URSumIterator = URSum.begin(); URSumIterator != URSum.end(); ++URSumIterator) {
98  std::cout << *URSumIterator << std::endl;
99  }
100 
101  std::cout << "Recommendations:" << std::endl;
102  for (std::vector<unsigned long>::const_iterator URSumIterator = URSum.begin(); URSumIterator != URSum.end(); ++URSumIterator) {
103  std::cout << static_cast<double>(*URSumIterator) / static_cast<double>(L) << std::endl;
104  }
105  */
106 
107  std::cout << std::endl;
108 
109  std::cout << "Processed recommendations for user " << user << " in " << recommendationsProcessingTimer.ToString() << std::endl;
110  }
111  }
112 
113 }//namespace PrivateRecommendations
114 }//namespace SeComLib
Definition of class Client.
Client(const std::shared_ptr< ServiceProvider > &serviceProvider, const std::shared_ptr< PrivacyServiceProvider > &privacyServiceProvider, const PaillierPublicKey &publicKey)
Constructor.
std::deque< Paillier::Ciphertext > EncryptedUserData
Encrypted user data.
Utilitary class providing algorithm timing functionality.
Definition: cpu_timer.h:47
size_t userCount
Number of users who need recommendations.
std::string ToString() const
Returns the elapsed user process time as a formatted string (HH::MM::SS.mmm)
Definition: cpu_timer.cpp:63
const std::shared_ptr< ServiceProvider > serviceProvider
A reference to the ServiceProvider.
static const std::string configurationPath
Service Provider configuration path.
RandomizerCache< BlindingFactorContainer > blindingFactorCache
Blinding factor cache instance.
void ComputeRecommendations()
Interact with the server(s) to extract the recommendations for every user.
const std::shared_ptr< PrivacyServiceProvider > privacyServiceProvider
A reference to the PrivacyServiceProvider.
Blinding factor cache parameter container struct.
The public key container structure for the Paillier cryptosystem.
Definition: paillier.h:49