33 namespace PrivateRecommendationsDataPacking {
48 Client::Client (
const std::shared_ptr<ServiceProvider> &serviceProvider,
const std::shared_ptr<PrivacyServiceProvider> &privacyServiceProvider,
const PaillierPublicKey &publicKey) :
49 serviceProvider(serviceProvider),
50 privacyServiceProvider(privacyServiceProvider),
51 paillierCryptoProvider(publicKey),
52 userCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".userCount")),
53 itemCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".itemCount")),
54 denselyRatedItemCount(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".denselyRatedItemCount")),
55 ratingBitSize(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".t")),
56 scaledNormalizedRatingBitSize(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".scaledNormalizedRatingBitSize")),
57 digitsToPreserve(Utils::Config::GetInstance().GetParameter<unsigned int>(configurationPath +
".digitsToPreserve")),
58 kappa(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".BlindingFactorCache.kappa")),
59 hatL(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath +
".hatL")),
60 ratingsFilePath(Utils::Config::GetInstance().GetParameter<std::string>(configurationPath +
".ratingsFilePath")),
61 LdecryptionBlindingFactorCache(paillierCryptoProvider,
BlindingFactorCacheParameters(configurationPath +
".BlindingFactorCache", BigInteger(static_cast<unsigned long>(userCount - 1)).GetSize())),
62 URSumDecryptionBlindingFactorCache(paillierCryptoProvider,
BlindingFactorCacheParameters(configurationPath +
".BlindingFactorCache", serviceProvider->GetMaxPackedBuckets() * serviceProvider->GetBucketSize())) {
68 if (!ratingsFile.is_open()) {
69 throw std::runtime_error(
"Can't open the ratings file.");
73 while (std::getline(ratingsFile, line)) {
74 std::istringstream lineStream(line);
77 std::deque<unsigned long> userRatings;
78 double squaredSum = 0.0;
85 squaredSum +=
static_cast<double>(rating * rating);
87 userRatings.emplace_back(rating);
90 double denominator = std::sqrt(squaredSum);
92 double normalizedValue =
static_cast<double>(userRatings[item]) / denominator;
98 std::deque<unsigned long> userPlaintextSparseRatings;
101 unsigned long rating;
102 while (lineStream >> rating) {
103 userPlaintextSparseRatings.emplace_back(rating);
111 #if 0//auto-generate ratings
112 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;
139 std::deque<unsigned long> userPlaintextSparseRatings;
140 for (
size_t item = this->denselyRatedItemCount; item < this->
itemCount; ++item) {
142 userPlaintextSparseRatings.emplace_back(RandomProvider::GetInstance().GetRandomInteger(
ratingBitSize).ToUnsignedLong());
171 #ifdef FIRST_USER_ONLY
172 for (
size_t user = 0; user < 1; ++user) {
174 for (
size_t user = 0; user < this->
userCount; ++user) {
196 std::deque<BigInteger> userEmptyBuckets = this->
emptyBuckets;
198 std::cout <<
"No similar users found." << std::endl;
202 else if (L < this->
hatL) {
207 std::cout <<
"hatL is too small!" << std::endl;
222 std::vector<unsigned long> URSum;
223 size_t bucketCount = 0;
224 for (ServiceProvider::PackedData::iterator encryptedURSumIterator = encryptedURSum.begin(); encryptedURSumIterator != encryptedURSum.end(); ++encryptedURSumIterator) {
231 for (
size_t bucketIndex = 0; bucketIndex < userEmptyBuckets.size() && bucketCount < this->
itemCount - this->
denselyRatedItemCount; ++bucketIndex) {
232 if (bucketIndex < userEmptyBuckets.size() - 1) {
233 URSum.push_back(((packedURSum % userEmptyBuckets[bucketIndex + 1]) / userEmptyBuckets[bucketIndex]).ToUnsignedLong());
236 URSum.push_back((packedURSum / userEmptyBuckets[bucketIndex]).ToUnsignedLong());
254 std::cout <<
"Processed recommendations for user " << user <<
" in " << recommendationsProcessingTimer.
ToString() << std::endl;
263 std::deque<BigInteger> output;
266 unsigned long ceilLogBaseTwoHatL =
static_cast<unsigned long>(std::ceil(std::log(static_cast<double>(L)) / std::log(2.0)));
269 size_t bucketSize = this->
ratingBitSize + ceilLogBaseTwoHatL;
279 for (
size_t i = 0; i < maxPackedBuckets; ++i) {
280 output.emplace_back(one << (static_cast<unsigned long>(i * bucketSize)));
293 BigInteger packedBuckets;
294 size_t bucketCount = 0;
295 for (std::deque<unsigned long>::const_iterator rating = userPlaintextSparseRatings.begin(); rating != userPlaintextSparseRatings.end(); ++rating) {
297 if (0 == bucketCount) {
298 packedBuckets = *rating;
302 packedBuckets += userEmptyBuckets[bucketCount] * (*rating);
306 if (bucketCount == userEmptyBuckets.size() - 1) {
315 if (bucketCount > 0) {
319 return userSparseRatings;
PaillierCiphertext Ciphertext
Provide public access to the T_Ciphertext type.
std::deque< BigInteger > emptyBuckets
Empty buckets , where is the bit size of the ratings.
const ServiceProvider::PackedItems & GetSparseRatings() const
Get .
RandomizerCache< BlindingFactorContainer > URSumDecryptionBlindingFactorCache
decryption blinding factor cache instance
RandomizerCache< BlindingFactorContainer > LdecryptionBlindingFactorCache
decryption blinding factor cache instance
void ComputeRecommendations()
Interact with the server(s) to extract the recommendations for every user.
unsigned int digitsToPreserve
Number of digits preserved from the normalized user ratings.
std::deque< EncryptedUserData > EncryptedUserDataContainer
Container for encrypted user data.
std::deque< Paillier::Ciphertext > EncryptedUserData
Encrypted user data.
virtual T_Ciphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
size_t hatL
The upper bound of most similar users ( )
const std::shared_ptr< ServiceProvider > serviceProvider
A reference to the ServiceProvider.
Stores precomputed random data.
const std::shared_ptr< PrivacyServiceProvider > privacyServiceProvider
A reference to the PrivacyServiceProvider.
Utilitary class providing algorithm timing functionality.
static const std::string configurationPath
Service Provider configuration path.
std::deque< std::deque< unsigned long > > plaintextSparseRatings
std::string ToString() const
Returns the elapsed user process time as a formatted string (HH::MM::SS.mmm)
std::deque< BigInteger > computeEmptyBuckets(const size_t L) const
Computes the empty buckets .
std::deque< PackedData > PackedItems
Packed items.
virtual size_t GetMessageSpaceSize() const
Returns the message space bit size.
size_t kappa
The security parameter for the secure comparison protocol (in bits)
size_t denselyRatedItemCount
Number of densely rated items.
std::string ratingsFilePath
The path to the file containing precomputed ratings.
ServiceProvider::PackedData packUserSparseRatings(const std::deque< unsigned long > &userPlaintextSparseRatings, const std::deque< BigInteger > &userEmptyBuckets) const
Pack the sparse ratings for one user.
std::deque< Paillier::Ciphertext > PackedData
Packed data.
Client(const std::shared_ptr< ServiceProvider > &serviceProvider, const std::shared_ptr< PrivacyServiceProvider > &privacyServiceProvider, const PaillierPublicKey &publicKey)
Constructor.
T_CryptoProvider::Ciphertext encryptedR
ServiceProvider::PackedItems sparseRatings
Definition of class Client.
ServiceProvider::EncryptedUserDataContainer normalizedScaledRatings
size_t itemCount
Number of ratings per user.
const ServiceProvider::EncryptedUserDataContainer & GetNormalizedScaledRatings() const
Get .
size_t userCount
Number of users who need recommendations.
Paillier paillierCryptoProvider
Paillier crypto provider.
Blinding factor cache parameter container struct.
The public key container structure for the Paillier cryptosystem.
size_t ratingBitSize
The size of the user ratings (in bits)