35 namespace PrivateRecommendations {
48 paillierCryptoProvider(paillierPublicKey),
49 dgkCryptoProvider(dgkPublicKey),
50 userCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".userCount")),
51 itemCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".itemCount")),
52 denselyRatedItemCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".denselyRatedItemCount")),
53 ratingBitSize(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".t")),
54 digitsToPreserve(Utils::Config::GetInstance().GetParameter<unsigned int>(configurationPath +
".digitsToPreserve")),
56 similarityTreshold(BigInteger(Utils::Config::GetInstance().GetParameter<double>(configurationPath +
".similarityTreshold"), 2 * digitsToPreserve)),
57 ratingsFilePath(Utils::Config::GetInstance().GetParameter<std::string>(configurationPath +
".ratingsFilePath")),
58 secureComparisonServer(std::make_shared<
SecureComparisonServer>(paillierCryptoProvider, dgkCryptoProvider, similarityTreshold, configurationPath)),
59 secureMultiplicationServer(std::make_shared<
SecureMultiplicationServer<
Paillier>>(paillierCryptoProvider, Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".l"), configurationPath)) {
73 if (!ratingsFile.is_open()) {
74 throw std::runtime_error(
"Can't open the ratings file.");
78 while (std::getline(ratingsFile, line)) {
79 std::istringstream lineStream(line);
82 std::deque<unsigned long> userRatings;
83 double squaredSum = 0.0;
90 squaredSum +=
static_cast<double>(rating * rating);
92 userRatings.emplace_back(rating);
95 double denominator = std::sqrt(squaredSum);
97 double normalizedValue =
static_cast<double>(userRatings[item]) / denominator;
105 unsigned long rating;
106 while (lineStream >> rating) {
112 #if 0//auto-generate ratings
113 for (
size_t user = 0; user < this->
userCount; ++user) {
115 std::deque<unsigned long> userRatings;
116 double squaredSum = 0.0;
121 random = RandomProvider::GetInstance().GetRandomInteger(
ratingBitSize);
124 unsigned long randomValue = random.ToUnsignedLong();
126 squaredSum +=
static_cast<double>(randomValue * randomValue);
128 userRatings.emplace_back(randomValue);
131 double denominator = std::sqrt(squaredSum);
133 double normalizedValue =
static_cast<double>(userRatings[item]) / denominator;
140 for (
size_t item = this->denselyRatedItemCount; item < this->
itemCount; ++item) {
155 #ifdef FIRST_USER_ONLY
156 for (
size_t i = 0; i < 1; ++i) {
158 for (
size_t i = 0; i < this->
userCount; ++i) {
164 for (
size_t j = 0; j < this->
userCount; ++j) {
168 userSimilarityValues.emplace_back(this->
secureMultiplicationServer->Multiply(this->normalizedScaledRatings[i][0], this->normalizedScaledRatings[j][0]));
181 std::cout <<
"Similarity processing: " << similarityTimer.
ToString();
186 for (EncryptedUserData::const_iterator similarity = userSimilarityValues.begin(); similarity != userSimilarityValues.end(); ++similarity) {
195 std::cout <<
" Gamma processing (" << userSimilarityValues.size() <<
" values): " << gammaTimer.
ToString() <<
" for user " << i << std::endl;
211 #ifdef FIRST_USER_ONLY
212 for (
size_t user = 0; user < 1; ++user) {
214 for (
size_t user = 0; user < this->
userCount; ++user) {
231 userURSum.emplace_back(this->
secureMultiplicationServer->Multiply(this->gammaValues[user == 0 ? 0 : user - 1], this->sparseRatings[user == 0 ? 1 : 0][item]));
235 for (
size_t i = (user == 0 ? 2 : 1); i < this->
userCount; ++i) {
240 gammaIndex = user * this->userCount + i - (user + 1) * (user + 2) / 2;
242 gammaIndex = i * this->userCount + user - (i + 1) * (i + 2) / 2;
244 userLValue = userLValue + this->
gammaValues[gammaIndex];
253 this->
LValues.emplace_back(userLValue);
262 std::cout <<
"Generated recommendations for user " << user <<
" in " << recommendationsTimer.
ToString() << std::endl;
271 return this->
LValues.at(userId);
PaillierCiphertext Ciphertext
Provide public access to the T_Ciphertext type.
size_t ratingBitSize
The size of the user ratings (in bits)
std::string ratingsFilePath
The path to the file containing precomputed ratings.
void ComputeUserRecommendations()
Computes , and for each user.
size_t denselyRatedItemCount
Number of densely rated items.
std::deque< Paillier::Ciphertext > EncryptedUserData
Encrypted user data.
Definition of class PrivacyServiceProvider.
size_t itemCount
Number of ratings per user.
The public key container structure for the Dgk cryptosystem.
virtual T_Ciphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
EncryptedUserData LValues
Vector of values, where L is the number of users similar to a given user.
void SetPrivacyServiceProvider(const std::shared_ptr< const PrivacyServiceProvider > &privacyServiceProvider)
Sets a reference to the Privacy Service Provider.
Paillier paillierCryptoProvider
Paillier crypto provider.
EncryptedUserDataContainer sparseRatings
static const std::string configurationPath
Service Provider configuration path.
EncryptedUserDataContainer normalizedScaledRatings
unsigned int digitsToPreserve
Number of digits preserved from the normalized user ratings.
Utilitary class providing algorithm timing functionality.
std::string ToString() const
Returns the elapsed user process time as a formatted string (HH::MM::SS.mmm)
const Paillier::Ciphertext & GetEncryptedL(size_t userId) const
Returns the value for the specified user.
size_t userCount
Number of users who need recommendations.
Secure Comparison Server.
EncryptedUserDataContainer URSumContainer
Contains the vectors for each user.
const std::shared_ptr< SecureMultiplicationServer< Paillier > > & GetSecureMultiplicationServer() const
Getter for this->secureMultiplicationServer.
Implementation of the public-key Paillier Cryptosystem.
std::shared_ptr< SecureMultiplicationServer< Paillier > > secureMultiplicationServer
A reference to the SecureMultiplicationServer.
const std::shared_ptr< SecureComparisonServer > secureComparisonServer
A reference to the SecureComparisonServer.
void ComputeSimilarityValues()
Computes the similarity values between each pair of users for the first items.
ServiceProvider(const PaillierPublicKey &paillierPublicKey, const DgkPublicKey &dgkPublicKey)
Constructor.
void GenerateDummyDatabase()
Generates a dummy database for the users.
Secure Multiplication Server.
const std::shared_ptr< SecureComparisonServer > & GetSecureComparisonServer() const
Getter for this->secureComparisonServer.
const EncryptedUserData & GetEncryptedURSum(size_t userId) const
Returns the vector for the specified user.
Definition of class ServiceProvider.
EncryptedUserData gammaValues
The public key container structure for the Paillier cryptosystem.
std::weak_ptr< const PrivacyServiceProvider > privacyServiceProvider
A reference to the PrivacyServiceProvider.